import { type Options, type Schema } from 'fast-json-stringify'; import { type Application, type FastJsonOptions } from './install'; /** * The parts of an OpenAPI 3.x or Swagger 2.0 document this package reads. * * It is deliberately a plain document rather than an integration with a * specific library: every popular Express toolchain either consumes or produces * one of these, so `swagger-jsdoc`, `swagger-ui-express`, `tsoa`, * `express-openapi-validator` and a hand written file all work unchanged. */ export type OpenApiDocument = { readonly openapi?: string; readonly swagger?: string; readonly paths?: Readonly>; readonly components?: Readonly>; readonly definitions?: Readonly>; }; export type OpenApiOptions = Omit & FastJsonOptions & { /** Media type to read the schema from. Defaults to `application/json`. */ readonly contentType?: string; }; /** * Translate an Express route pattern into the OpenAPI equivalent: * `/users/:id` becomes `/users/{id}`. Express parameter modifiers — a trailing * `?` or an inline `(regex)` — are dropped, since OpenAPI has no notion of them. */ export declare const toOpenApiPath: (path: string) => string; /** * The schema the document declares for one operation and status, with the * shared schemas attached so `$ref` resolves, ready for `fastJsonSchema`. For * a route whose Express path does not match the document. * * @param {OpenApiDocument} document The OpenAPI 3.x or Swagger 2.0 document * @param {string} path The OpenAPI path, `/users/{id}` * @param {string} method The operation, `get` * @param {number} status The response status, `200` when omitted * @param {string} contentType The media type, `application/json` when omitted * @returns {Schema | undefined} undefined when the document describes no such response * * Examples: * ```ts * app.get('/v2/people/:id', fastJsonSchema(openApiSchema(document, '/users/{id}', 'get')!), handler); * ``` */ export declare const openApiSchema: (document: OpenApiDocument, path: string, method: string, status?: number, contentType?: string) => Schema | undefined; /** * Serialize every documented response of an application from its OpenAPI or * Swagger document, so the contract you already publish is the one used to * serialize. Once per app, at setup: it calls `installFastJson(app)` itself, and no * middleware runs per request. * * The operation is resolved from the matched Express route, and the schema * from the response status code, which means `res.status(201).fastJson()` * serializes with the `201` schema. Routes the document does not describe fall * back to `res.json()` unless `strict` is set. * * @param {Application} app The application * @param {OpenApiDocument} document The OpenAPI 3.x or Swagger 2.0 document * @param {OpenApiOptions} options The options to use (optional) * * Examples: * ```ts * import express from 'express'; * import swaggerJsdoc from 'swagger-jsdoc'; * import { fastJsonOpenApi } from 'express-fast-json-stringify'; * * const app = express(); * const document = swaggerJsdoc({ definition: { openapi: '3.1.0', info: { title: 'API', version: '1.0.0' } }, apis: ['./routes/*.ts'] }); * * fastJsonOpenApi(app, document); * * app.get('/users/:id', (req, res, next) => { * try { * res.fastJson({ id: Number(req.params.id), firstName: 'Simone' }); * } catch (error) { * next(error); * } * }); * ``` */ export declare const fastJsonOpenApi: (app: Application, document: OpenApiDocument, options?: OpenApiOptions) => void;