import type { Express, Request, Response } from 'express'; /** What the install needs of an application: the prototype its responses share. Fulmine has one too. */ export type Application = Pick; /** A compiled serializer, as fast-json-stringify builds it. */ export type Serializer = (body: any) => string; /** Finds the serializer for a response from something other than the route, an OpenAPI document. */ export type SerializerResolver = (res: Response) => Serializer | null; /** Notified when an overridden `res.json()` could not use the fast path. */ export type OverrideErrorHandler = (error: unknown, req: Request) => void; export type FastJsonOptions = { /** * Also route `res.json()`, and `res.send(object)` which Express implements on * top of it, through the serializer in force. * * Off by default. A route's own schema describes the successful payload, so * under it only `2xx` responses take the fast path: an error body would * otherwise be rewritten into the wrong shape. */ readonly overrideJson?: boolean; /** * Called when an overridden `res.json()` could not use the fast path because * the serializer threw. The response falls back to the stock `res.json()` * either way; this is only so the mismatch is visible. */ readonly onError?: OverrideErrorHandler; /** * Make `res.fastJson()` throw when no schema is known for the response, * instead of quietly falling back to `res.json()`. An overridden `res.json()` * always falls back. */ readonly strict?: boolean; }; /** Where `fastJsonSchema` leaves the serializer of the route, on `res.locals`. */ export declare const kSerializer: unique symbol; /** * Give an application `res.fastJson()`, and with `overrideJson` a `res.json()` * that serializes through the schema in force. Once per app, at setup: the * methods go on `app.response`, so a request pays nothing to have them. * * `fastJsonSchema` chooses the schema per route, `fastJsonOpenApi` per * operation from a document and calls this itself. * * @param {Application} app The application to extend * @param {FastJsonOptions} options The options to use (optional) * * Examples: * ```ts * import express from 'express'; * import { installFastJson, fastJsonSchema } from 'express-fast-json-stringify'; * * const app = express(); * installFastJson(app); * * app.get('/', fastJsonSchema(schema), (req, res) => { * res.fastJson({ firstName: 'Simone', lastName: 'Nigro', age: 40 }); * }); * ``` */ export declare const installFastJson: (app: Application, options?: FastJsonOptions) => void; /** Registers where the document based serializers come from, see fastJsonOpenApi. */ export declare const setResolver: (app: Application, resolver: SerializerResolver) => void; declare global { namespace Express { interface Response { /** * Send JSON response, serialized with the schema in force for the route. * * Examples: * ```ts * res.fastJson({ user: 'Simone Nigro' }); * res.status(200).fastJson({ user: 'Simone Nigro' }); * ``` */ fastJson: (body: any) => Response; } } }