import type { Request, Response } from 'express'; import morgan from 'morgan'; import Err from '@openaddresses/batch-error'; import type { Static, TSchema } from '@sinclair/typebox'; import { OpenAPIV3 as Doc } from 'openapi-types'; import { Router } from 'express'; import type { RequestHandler } from 'express'; import type { ErrorObject, ValidateFunction } from 'ajv'; import type { RequestValidation, NormalizedBody } from './types.js'; import type { OpenAPIDocumentInput } from './openapi.js'; import Docs from './openapi.js'; export type ErrorListItem = { type: 'Body' | 'Query' | 'Params'; errors: ErrorObject[]; }; /** * Per-route body validation state, compiled once at route registration: * ajv validators keyed by content-type, the normalized map used for * content-type matching, and the precomputed supported-types string * used in error messages. */ export type CompiledBodyValidation = { validators: Map; matcher: NormalizedBody; supported: string; }; /** * @class * * @param {Object} router Express Router Object * @param {Object} opts Options Object * @param {object} [opts.logging] disable logging with false * @param {number} [opts.limit=50] body size limit in mb * @param {object} [opts.openapi] OpenAPI Base Document * @param {object} [opts.prefix] API string Prefix * @param {object} [opts.error] Error Schemas * * @param {URL} [opts.apidoc] apidoc file location */ export default class Schemas { router: Router; docs: Docs; prefix: string; error: Record; schemas: Map>; validationError(method: Doc.HttpMethods, path: string): Err; /** * Compile body validators keyed by content-type. Returns null when no * body validation is configured. Each validator entry is either `true` * (any body accepted for this content-type) or a compiled ajv validator. * The content-type matcher and supported-types error string are * precomputed here so no per-request allocation is needed. */ compileBodyValidators(body: RequestValidation['body']): CompiledBodyValidation | null; /** * Validate the incoming request body against the compiled validation * state for the route. Pushes a Body error onto `errors` if validation * fails; returns an Err to respond with for an unsupported content-type. * * If `opts.required` is false and the request has no body / no * content-type, validation is skipped. */ validateBody(req: Request, compiled: CompiledBodyValidation, errors: Array, opts?: { required?: boolean; }): Err | null; constructor(router: Router, opts?: { prefix?: string; logging?: boolean | morgan.Options; limit?: number; error?: Record; openapi?: OpenAPIDocumentInput; }); normalizeValidation(opts?: RequestValidation): RequestValidation; api(): Promise; /** * Load routes directory. All .js files in the given directory will be loaded (ES Module) * and the default method run * * @param {String} dirpath Directory to load * @param {object} config Config Object * @param {Object} opts Options Object * @param {boolean} opts.silent Squelch StdOut */ load(dirpath: string | URL, config: T, opts?: { silent: boolean; }): Promise; /** * Load a blueprint into the router * * @param {Object} bp_funcion Blueprint Function to init instances * @param {object} config Config Object * @param {Object} opts Options Object * @param {boolean} opts.silent Squelch StdOut */ blueprint(bp_fn: (schema: Schemas, config?: T) => Promise, config?: T, opts?: { silent: boolean; }): Promise; /** * Compile the response validator for a route. Type.Any and Type.Unknown * impose no constraints, so they are skipped entirely — routes with an * open response schema (or none) bypass response validation and its * clone at request time. */ private compileResponseValidation; /** * Shared route registration: compiles all validators once, then installs * a request handler that validates params/query/body and — only when a * response schema requires it — wraps res.json with response validation. */ private route; get(path: string, opts: RequestValidation | undefined, handler: RequestHandler, Static, Static, Static>): Promise; delete(path: string, opts: RequestValidation | undefined, handler: RequestHandler, Static, Static, Static>): Promise; post(path: string, opts: RequestValidation | undefined, handler: RequestHandler, Static, Static, Static>): Promise; patch(path: string, opts: RequestValidation | undefined, handler: RequestHandler, Static, Static, Static>): Promise; put(path: string, opts: RequestValidation | undefined, handler: RequestHandler, Static, Static, Static>): Promise; not_found(): void; query(method: Doc.HttpMethods, url: string): { deprecated?: boolean; query?: object; body?: object; res?: object; }; list(): { [k: string]: { body: boolean; query: boolean; res: boolean; }; }; }