( problemDetails: ProblemDetails<T>, options?: ProblemDetailsResponseOptions, )
| 200 | * ``` |
| 201 | */ |
| 202 | export function createProblemDetailsResponse< |
| 203 | T extends ProblemDetailsExtensions, |
| 204 | >( |
| 205 | problemDetails: ProblemDetails<T>, |
| 206 | options?: ProblemDetailsResponseOptions, |
| 207 | ): Response { |
| 208 | const pd: Record<string, unknown> = { ...problemDetails }; |
| 209 | |
| 210 | if (pd.type === undefined) pd.type = "about:blank"; |
| 211 | |
| 212 | if (pd.status === undefined) pd.status = 500; |
| 213 | |
| 214 | if (!Number.isInteger(pd.status)) { |
| 215 | throw new RangeError( |
| 216 | `Cannot create Problem Details response: status must be a finite integer, received ${ |
| 217 | typeof pd.status === "string" ? `"${pd.status}"` : String(pd.status) |
| 218 | }`, |
| 219 | ); |
| 220 | } |
| 221 | |
| 222 | if (pd.type === "about:blank" && pd.title === undefined) { |
| 223 | const statusText = STATUS_TEXT[pd.status as keyof typeof STATUS_TEXT]; |
| 224 | if (statusText !== undefined) { |
| 225 | pd.title = statusText; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | const body = JSON.stringify(pd); |
| 230 | const status = pd.status as number; |
| 231 | const statusText = options?.statusText; |
| 232 | |
| 233 | if (options?.headers === undefined) { |
| 234 | return new Response(body, { |
| 235 | status, |
| 236 | ...(statusText !== undefined ? { statusText } : {}), |
| 237 | headers: { "Content-Type": PROBLEM_JSON_MEDIA_TYPE }, |
| 238 | }); |
| 239 | } |
| 240 | const headers = new Headers(options.headers); |
| 241 | headers.set("Content-Type", PROBLEM_JSON_MEDIA_TYPE); |
| 242 | return new Response(body, { |
| 243 | status, |
| 244 | ...(statusText !== undefined ? { statusText } : {}), |
| 245 | headers, |
| 246 | }); |
| 247 | } |
| 248 | |
| 249 | function normalizeParsedProblemDetails( |
| 250 | raw: unknown, |
no test coverage detected