import fs from 'node:fs'; import type { Request, Response } from 'express'; import path from 'node:path'; import morgan from 'morgan'; import bodyparser from 'body-parser'; import Err from '@openaddresses/batch-error'; import { Kind } from '@sinclair/typebox'; import type { Static, TSchema } from '@sinclair/typebox'; import { OpenAPIV3 as Doc } from 'openapi-types' import { Router } from 'express' import type { RequestHandler, ErrorRequestHandler } from 'express' import { Ajv } from 'ajv' import type { ErrorObject, ValidateFunction } from 'ajv' import addFormatsModule from 'ajv-formats' import type { RequestValidation, NormalizedBody, RouteSecurity, SecurityRequirement, SecurityHandlers, SecurityLocals } from './types.js'; import { normalizeBody, matchContentType } from './types.js'; import type { OpenAPIDocumentInput } from './openapi.js' import SchemaAPI from './api.js'; import Docs from './openapi.js'; const ajv = new Ajv({ strict: false, allErrors: true, useDefaults: true, removeAdditional: 'all', coerceTypes: true, discriminator: true }); addFormatsModule.default(ajv); 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; }; type RouteMethod = | Doc.HttpMethods.GET | Doc.HttpMethods.DELETE | Doc.HttpMethods.POST | Doc.HttpMethods.PATCH | Doc.HttpMethods.PUT; const MAX_LOGGED_RESPONSE = 4096; /** * Deep-clone a value following JSON.stringify semantics: toJSON() support * (Date => ISO string), undefined/function/symbol omission, NaN/Infinity * => null, and circular reference detection. Response validation needs the * serialized shape of the payload (Dates must already be strings), and ajv's * removeAdditional/useDefaults mutate the object they validate, so the * caller's object must be protected. A single-pass clone does both without * the cost of JSON.parse(JSON.stringify()) building an intermediate string. */ function jsonClone(value: unknown, seen?: Set): unknown { if (value !== null && typeof value === 'object' && typeof (value as { toJSON?: unknown }).toJSON === 'function') { value = (value as { toJSON: () => unknown }).toJSON(); } if (value === null) return null; switch (typeof value) { case 'string': case 'boolean': return value; case 'number': return Number.isFinite(value) ? value : null; case 'bigint': throw new TypeError('Do not know how to serialize a BigInt'); case 'object': break; default: return undefined; // undefined | function | symbol are omitted, as in JSON.stringify } if (!seen) seen = new Set(); if (seen.has(value as object)) throw new TypeError('Converting circular structure to JSON'); seen.add(value as object); let out: unknown; if (Array.isArray(value)) { const arr = new Array(value.length); for (let i = 0; i < value.length; i++) { const v = jsonClone(value[i], seen); arr[i] = v === undefined ? null : v; } out = arr; } else { const obj: Record = {}; for (const key of Object.keys(value)) { const v = jsonClone((value as Record)[key], seen); if (v !== undefined) obj[key] = v; } out = obj; } seen.delete(value as object); return out; } /** * Final error handler of every route: anything a body parser or handler * throws is returned as a batch-error JSON body rather than Express's HTML * error page. Only errors that are already public (a batch-error, or a 4xx * http-error marked `expose`) keep their status and message */ const errorHandler: ErrorRequestHandler = (err, _req, res, _next) => { // eslint-disable-line @typescript-eslint/no-unused-vars if (err && typeof err === 'object' && !Object.hasOwn(err, 'safe')) { const status = Number(err.status); if (err.expose === true && status >= 400 && status < 500) { return Err.respond(new Err(status, null, String(err.message), false), res); } console.error(err); if (res.headersSent) return res.end(); return res.status(500).json({ status: 500, message: 'Internal Server Error', messages: [] }); } return Err.respond(err, res); }; /** * @class * * @param {Object} router Express Router Object * @param {Object} opts Options Object * @param {object} [opts.logging] disable logging with false, or pass morgan options * @param {string|Function} [opts.logging.format=combined] morgan format name, format string, or format function * @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 */ // eslint-disable-next-line @typescript-eslint/no-empty-object-type export default class Schemas { router: Router; docs: Docs; prefix: string; error: Record; schemas: Map> securityHandlers?: THandlers; parsers: Array; validationError(method: Doc.HttpMethods, path: string) { return new Err(400, null, `Validation Error ${String(method).toUpperCase()} ${path}`); } /** * 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 { const normalized = normalizeBody(body); if (!normalized) return null; const validators = new Map(); for (const ct of Object.keys(normalized)) { const schema = normalized[ct].schema; if (schema === true || schema === undefined) { validators.set(ct, true); } else { validators.set(ct, ajv.compile(schema)); } } return { validators, matcher: normalized, supported: Object.keys(normalized).join(', ') }; } /** * 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 { const raw = (req.headers['content-type'] || '').split(';')[0].trim().toLowerCase(); const required = opts.required !== false; if (!raw && !required) return null; const matched = matchContentType(raw, compiled.matcher); const validator = matched ? compiled.validators.get(matched) : undefined; if (!validator) { return new Err(400, null, `Content-Type ${raw || '(none)'} not supported. Supported: ${compiled.supported}`); } if (validator !== true) { if (!validator(req.body)) { errors.push({ type: 'Body', errors: validator.errors as ErrorObject[] }); } } return null; } constructor( router: Router, opts: { prefix?: string; logging?: boolean | (morgan.Options & { format?: string | morgan.FormatFn; }); limit?: number; error?: Record; openapi?: OpenAPIDocumentInput; securityHandlers?: THandlers; } = {} ) { if (!router) throw new Error('Router Param Required'); this.router = router; this.prefix = opts.prefix || ''; this.securityHandlers = opts.securityHandlers; if (opts.logging !== false) { const { format = 'combined', ...morganOptions } = opts.logging && typeof opts.logging === 'object' ? opts.logging : {}; // morgan's overloads don't accept the string | FormatFn union directly this.router.use(morgan(format as string, morganOptions)); } // Applied per route, after its security handlers, so a request is never parsed before it is authorized this.parsers = [ bodyparser.urlencoded({ extended: true, limit: `${opts.limit || 50}mb` }), bodyparser.json({ limit: `${opts.limit || 50}mb` }), bodyparser.text({ limit: `${opts.limit || 50}mb`, type: ['text/*', 'application/xml', 'application/*+xml'] }), bodyparser.raw({ limit: `${opts.limit || 50}mb`, type: ['application/octet-stream'] }) ]; if (opts.error) { this.error = opts.error; } else { this.error = {}; } this.docs = new Docs(opts.openapi, { prefix: this.prefix, error: this.error }); this.schemas = new Map(); if (this.securityHandlers && this.docs.base.security) { this.checkSecurity(this.docs.base.security as RouteSecurity, 'Document level security'); } } /** * With security enforced every scheme a route names must be documented * and have a handler, so a misspelt scheme fails at startup */ private checkSecurity(security: RouteSecurity, label: string): ReadonlyArray { if (!security) { throw new Error(`${label} must declare security when securityHandlers are set - use [] for a public route`); } const schemes = this.docs.base.components.securitySchemes || {}; for (const requirement of security) { const names = Object.keys(requirement).filter(name => requirement[name] !== undefined); if (!names.length) throw new Error(`${label} has an empty Security Requirement - use [] for a public route`); for (const name of names) { if (!Object.hasOwn(schemes, name)) { throw new Error(`${label} references unknown security scheme: ${name}`); } else if (!this.securityHandlers || !Object.hasOwn(this.securityHandlers, name)) { throw new Error(`${label} references security scheme without a handler: ${name}`); } } } return security; } /** * Find the first Security Requirement the request satisfies and return * its principals keyed by scheme. A 403 is preferred over a 401 when * nothing is satisfied, as the caller was at least identified */ private async authorize( req: Request, res: Response, security: ReadonlyArray, route: { method: string; path: string } ): Promise> { if (!security.length) return {}; const handlers = this.securityHandlers as SecurityHandlers; let failure: { status: number } | undefined; for (const requirement of security) { const auth: Record = {}; let satisfied = true; for (const name of Object.keys(requirement)) { const scopes = requirement[name]; if (scopes === undefined) continue; let error: { status: number } | undefined; try { const principal = await handlers[name](req, [...scopes], route); if (principal === null || principal === undefined || principal === false) { error = new Err(401, null, 'Authentication Required'); } else { auth[name] = principal; } } catch (err) { const status = typeof err === 'object' && err !== null ? Number((err as { status?: unknown }).status) : NaN; if (status !== 401 && status !== 403) throw err; error = err as { status: number }; } if (error) { if (!failure || (error.status === 403 && failure.status !== 403)) failure = error; satisfied = false; break; } } if (satisfied) return auth; } if (failure && failure.status === 401) { const schemes = (this.docs.base.components.securitySchemes || {}) as Record; const challenges = new Set(); for (const requirement of security) { for (const name of Object.keys(requirement)) { const scheme = schemes[name]; if (scheme && scheme.type === 'http' && scheme.scheme) { challenges.add(scheme.scheme.charAt(0).toUpperCase() + scheme.scheme.slice(1).toLowerCase()); } } } if (challenges.size) res.setHeader('WWW-Authenticate', Array.from(challenges).join(', ')); } throw failure; } normalizeValidation( opts: RequestValidation = {} ): RequestValidation { return { deprecated: false, ...opts }; } async api() { await SchemaAPI(this); } /** * 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 */ async load(dirpath: string | URL, config: T, opts: { silent: boolean; } = { silent: false }) { if (dirpath instanceof URL) dirpath = dirpath.pathname; else dirpath = String(dirpath); const routes: Array, config: T) => void>> = []; // Load dynamic routes directory for (const r of fs.readdirSync(dirpath)) { if (!opts.silent) console.log(`ok - loaded routes/${r}`); if (!['.ts', '.js'].includes(path.parse(r).ext)) continue; routes.push((await import(path.resolve(dirpath, r))).default(this, config)); } await Promise.all(routes); this.not_found(); } /** * 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 */ async blueprint(bp_fn: (schema: Schemas, config?: T) => Promise, config?: T, opts: { silent: boolean; } = { silent: false }) { if (!opts.silent) console.log(`ok - loaded blueprint`); await bp_fn(this, config); } /** * 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(res?: TSchema): ValidateFunction | null { if (!res) return null; if (res[Kind] === 'Any' || res[Kind] === 'Unknown') return null; return ajv.compile(res); } /** * 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( method: RouteMethod, path: string, opts: RequestValidation, handler: RequestHandler, Static, Static, Static, SecurityLocals>, allowBody: boolean ): void { const methodUpper = String(method).toUpperCase(); try { const validation = this.normalizeValidation(opts); this.docs.push({ method, path }, validation); this.schemas.set(`${methodUpper} ${path}`, validation); const resValidation = this.compileResponseValidation(validation.res); const paramsValidation = validation.params && ajv.compile(validation.params); const queryValidation = validation.query && ajv.compile(validation.query); let bodyValidation: CompiledBodyValidation | null = null; if (allowBody) { bodyValidation = this.compileBodyValidators(validation.body); } else if (validation.body) { throw new Error(`Body not allowed`); } const security = this.securityHandlers ? this.checkSecurity(validation.security || (this.docs.base.security as RouteSecurity), `${methodUpper} ${path}`) : null; const authorizeRoute = { method: methodUpper, path }; const validated: RequestHandler = (req, res, next) => { if (req.query) { // Ref: https://github.com/cdimascio/express-openapi-validator/issues/969 Object.defineProperty(req, 'query', { writable: true, value: { ...req.query }, }) } const errors: Array = []; if (paramsValidation && !paramsValidation(req.params)) errors.push({ type: 'Params', errors: paramsValidation.errors as ErrorObject[] }); if (queryValidation && !queryValidation(req.query)) errors.push({ type: 'Query', errors: queryValidation.errors as ErrorObject[] }); if (bodyValidation) { const ctErr = this.validateBody(req, bodyValidation, errors, { required: validation.bodyRequired }); if (ctErr) return Err.respond(ctErr, res); } if (errors.length) return Err.respond(this.validationError(method, path), res, errors); if (resValidation) { const json = res.json; res.json = function(obj) { if (res.statusCode === null || res.statusCode === 200) { obj = jsonClone(obj); // Date => String needs to happen before validation if (!resValidation(obj)) { res.status(400); const logged = JSON.stringify(obj); console.error(`Response Validation Error: ${methodUpper} ${path}: ${logged && logged.length > MAX_LOGGED_RESPONSE ? logged.slice(0, MAX_LOGGED_RESPONSE) + '... (truncated)' : logged}`); return json.call(this, { type: 'Response', errors: resValidation.errors as ErrorObject[] }); } } return json.call(this, obj); }; } return (handler as unknown as RequestHandler)(req, res, next); }; const chain: Array = [...this.parsers, validated, errorHandler]; if (security) { const authorizer: RequestHandler = async (req, res, next) => { try { res.locals.auth = await this.authorize(req, res, security, authorizeRoute); } catch (err) { return Err.respond(err, res); } next(); }; chain.unshift(authorizer); } this.router[method as 'get' | 'delete' | 'post' | 'patch' | 'put'](path, ...chain); } catch (err) { const label = methodUpper.charAt(0) + methodUpper.slice(1).toLowerCase(); throw new Error(`${label}: ${path}: ` + String(err), { cause: err }) } } async get( path: string, opts: RequestValidation = {}, handler: RequestHandler, Static, Static, Static, SecurityLocals> ) { this.route(Doc.HttpMethods.GET, path, opts, handler, false); } async delete( path: string, opts: RequestValidation = {}, handler: RequestHandler, Static, Static, Static, SecurityLocals> ) { this.route(Doc.HttpMethods.DELETE, path, opts, handler, false); } async post( path: string, opts: RequestValidation = {}, handler: RequestHandler, Static, Static, Static, SecurityLocals> ) { this.route(Doc.HttpMethods.POST, path, opts, handler, true); } async patch( path: string, opts: RequestValidation = {}, handler: RequestHandler, Static, Static, Static, SecurityLocals> ) { this.route(Doc.HttpMethods.PATCH, path, opts, handler, true); } async put( path: string, opts: RequestValidation = {}, handler: RequestHandler, Static, Static, Static, SecurityLocals> ) { this.route(Doc.HttpMethods.PUT, path, opts, handler, true); } not_found() { this.router.all('*path', (req, res) => { res.status(404).json({ status: 404, message: 'API endpoint does not exist!', messages: [] }); }); } query(method: Doc.HttpMethods, url: string): { deprecated?: boolean; query?: object; body?: object; res?: object; } { if (!this.schemas.has(`${method} ${url}`)) { return {}; } const schema = JSON.parse(JSON.stringify(this.schemas.get(`${method} ${url}`))); return { deprecated: schema.deprecated, query: schema.query, body: schema.body, res: schema.res }; } list(): { [k: string]: { body: boolean; query: boolean; res: boolean; } } { const lite: { [k: string]: { body: boolean; query: boolean; res: boolean; } } = {}; for (const key of this.schemas.keys()) { const schema = this.schemas.get(key); if (!schema) continue; lite[key] = { body: !!schema.body, query: !!schema.query, res: !!schema.res }; } return lite; } }