createProblemDetailsResponse
import { createProblemDetailsResponse } from "jsr:@ende/http/unstable-problem-details";function · experimental
function createProblemDetailsResponse<T extends ProblemDetailsExtensions>(problemDetails: ProblemDetails<T>, options?: ProblemDetailsResponseOptions): ResponseCreates a Response with an application/problem+json body from a
ProblemDetails object.
- Sets
Content-Typetoapplication/problem+json, overwriting anyContent-Typeprovided inoptions.headers. - Defaults
typeto"about:blank"if not provided. - Defaults
titleto the standard HTTP status text (from@ende/http/status) whentypeis"about:blank"andtitleis not provided. - Defaults
statusto500if not provided. - Serializes directly to JSON — the flat intersection type already matches the RFC wire format, so no flattening step is needed.
- Forwards
options.statusTextto theResponseonly when defined; when omitted, the constructor's empty-string default is used and the standard HTTP status text is intentionally not synthesized — the JSONtitlefield already carries that information.
problemDetails: ProblemDetails<T>The problem details to serialize into the response body.
options?: ProblemDetailsResponseOptionsAdditional response options such as extra headers.
Returns
A Response with status, application/problem+json content type,
and the serialized problem details as the body.
Example: Basic 404 response
import { createProblemDetailsResponse } from "@ende/http/unstable-problem-details";
import { assertEquals } from "@ende/assert";
const response = createProblemDetailsResponse({
status: 404,
detail: "No user found with ID 42",
});
assertEquals(response.status, 404);
assertEquals(
await response.json(),
{
type: "about:blank",
status: 404,
title: "Not Found",
detail: "No user found with ID 42",
},
);
Example: Custom problem type with extensions
import { createProblemDetailsResponse } from "@ende/http/unstable-problem-details";
import { assertEquals } from "@ende/assert";
const response = createProblemDetailsResponse({
type: "https://example.com/problems/insufficient-credit",
status: 403,
title: "Insufficient credit",
detail: "Your account balance is too low",
instance: "/account/12345/transactions/abc",
balance: 30,
accounts: ["/account/12345", "/account/67890"],
});
assertEquals(response.status, 403);
assertEquals(response.headers.get("Content-Type"), "application/problem+json");