feat: per-route body limits, custom error rendering, OpenAPI generation
CI / build (push) Has been cancelled
CI / build (push) Has been cancelled
Three capabilities that share the same request/route metadata:
- **Per-route upload limits** — `@Post('/x', { maxBodyBytes, maxParts })`
overrides the server-wide `limits` for one route (falls back to the
global default), mirroring the existing per-route `duplicates`.
- **Custom error rendering** — `createApp({ onError })` takes over how any
error becomes a response (HTML, RFC 7807, content-negotiated), covering
the whole surface: handler/step throws, 404, 405, 413, 400. Returns a
response or undefined to fall back to the default JSON. Also wires the
previously-unused `HttpError` `body` field so `throw new HttpError(409,
'conflict', { code })` renders a structured payload. Default behavior is
unchanged when no hook is set.
- **OpenAPI 3.1** — `app.openapi(info?)` projects the route graph into a
structural spec (paths, methods, path/query/header params, request-body
presence, systematic error responses); opt-in `GET /__openapi__` via
`devOpenapi`. Body/response schemas stay generic (Standard Schema exposes
no JSON Schema) — the full `@Produce` mode is post-beta.
Docs: new error-handling and OpenAPI guides, plus reference updates.
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
19 files changed
+811
-100
No files matched your search
+32
-3
@@ -2,6 +2,26 @@ import { isHttpError, Redirect, ValidationError } from './signals';
|
||||
import type { TransformerFn } from './metadata';
|
||||
import { flattenPath } from './standard-schema';
|
||||
|
||||
/** A ready-to-send HTTP response. */
|
||||
export interface ErrorResponse {
|
||||
status: number;
|
||||
headers: Record<string, string>;
|
||||
body: string;
|
||||
}
|
||||
|
||||
/** The request a {@link ErrorRenderer} is given, to inspect (e.g. `headers.accept`) while rendering. */
|
||||
export interface ErrorRequest {
|
||||
method: string;
|
||||
url: string;
|
||||
headers: Record<string, string | string[] | undefined>;
|
||||
}
|
||||
|
||||
/**
|
||||
* User hook to render errors however you like (HTML, custom JSON, RFC 7807, …).
|
||||
* Return a response to take over, or `undefined`/`void` to fall back to the default JSON.
|
||||
*/
|
||||
export type ErrorRenderer = (error: unknown, req: ErrorRequest) => ErrorResponse | undefined | void;
|
||||
|
||||
/** Default transformer: serialize a handler's return value as a JSON `200` response. */
|
||||
export const JsonTransformer: TransformerFn = (value) => ({
|
||||
status: 200,
|
||||
@@ -9,8 +29,11 @@ export const JsonTransformer: TransformerFn = (value) => ({
|
||||
body: JSON.stringify(value),
|
||||
});
|
||||
|
||||
/** Map a thrown value to a JSON error response (422 for validation, HttpError status, else 500). */
|
||||
export function errorToResponse(error: unknown): { status: number; headers: Record<string, string>; body: string } {
|
||||
/**
|
||||
* Map a thrown value to a JSON error response (422 for validation, `HttpError` status, else 500).
|
||||
* An `HttpError` carrying a `body` renders that payload instead of the default `{ error: message }`.
|
||||
*/
|
||||
export function errorToResponse(error: unknown): ErrorResponse {
|
||||
if (error instanceof ValidationError) {
|
||||
return {
|
||||
status: 422,
|
||||
@@ -26,7 +49,8 @@ export function errorToResponse(error: unknown): { status: number; headers: Reco
|
||||
if (isHttpError(error)) {
|
||||
const headers: Record<string, string> = { 'content-type': 'application/json' };
|
||||
if (error instanceof Redirect) headers.location = error.location;
|
||||
return { status: error.status, headers, body: JSON.stringify({ error: error.message }) };
|
||||
const payload = error.body !== undefined ? error.body : { error: error.message };
|
||||
return { status: error.status, headers, body: JSON.stringify(payload) };
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -35,3 +59,8 @@ export function errorToResponse(error: unknown): { status: number; headers: Reco
|
||||
body: JSON.stringify({ error: 'Internal Server Error' }),
|
||||
};
|
||||
}
|
||||
|
||||
/** Render an error through the optional user {@link ErrorRenderer}, falling back to {@link errorToResponse}. */
|
||||
export function renderError(error: unknown, req: ErrorRequest, onError?: ErrorRenderer): ErrorResponse {
|
||||
return onError?.(error, req) ?? errorToResponse(error);
|
||||
}
|
||||
Reference in new issue
Block a user