import type { TSchema } from '@sinclair/typebox'; import type { Request } from 'express'; /** * Per-content-type body entry. May be: * - a TypeBox schema (validated, no example) * - `true` (any body accepted, no validation) * - an object with optional `schema` (TSchema | true), `example`, `examples` * (forwarded to the OpenAPI media type object) */ export type BodyEntryConfig = { schema?: TSchema | true; example?: unknown; examples?: Record; }; export type BodyEntry = TSchema | true | BodyEntryConfig; /** * Body validation can either be: * - a TypeBox schema (treated as `application/json` for backwards compatibility) * - a record mapping content-type strings to a `BodyEntry`. Content-type * keys may include wildcards: `text/*`, `application/*`, or `* / *`. */ export type BodyValidation = TBody | Record; /** * Normalized internal representation: every entry has the expanded * `BodyEntryConfig` shape with `schema` always set (defaulting to `true`). */ export type NormalizedBody = Record> & BodyEntryConfig>; /** * Detect whether a given value is a TypeBox schema. TypeBox attaches a * `[Kind]` symbol to every schema instance which is what we use to * distinguish a schema from a content-type-keyed body record. */ export declare function isTSchema(value: unknown): value is TSchema; /** * Normalize the body option into a `NormalizedBody` map. A bare TypeBox * schema is mapped to `application/json` for backwards compatibility. */ export declare function normalizeBody(body: TSchema | Record | undefined): NormalizedBody | undefined; /** * Match an incoming content-type against a normalized body map, honoring * wildcard entries like `text/*` or `*\/*`. Exact matches always win. */ export declare function matchContentType(incoming: string, normalized: NormalizedBody): string | undefined; /** * An OpenAPI Security Requirement Object - every scheme named must be * satisfied, each with all of its listed scopes */ export type SecurityRequirement = { [scheme: string]: Array | undefined; }; export type SecurityRoute = { method: string; path: string; }; /** * Decides whether a request satisfies one security scheme with the given * scopes. Return the authenticated principal to satisfy the scheme - return * null/undefined/false, or throw a 401/403 error, when it is not satisfied. * Any other thrown error fails the request */ export type SecurityHandler = (req: Request, scopes: Array, route: SecurityRoute) => TPrincipal | null | undefined | false | Promise; export type SecurityHandlers = Record>; export type SecurityPrincipal = THandler extends (...args: never[]) => infer R ? Exclude>, false> : never; type AnyAuth = { [K in keyof THandlers]?: SecurityPrincipal; }; type NoAuth = { [K in keyof THandlers]?: undefined; }; type PresentKeys = { [K in keyof TAlt]-?: undefined extends TAlt[K] ? never : K; }[keyof TAlt]; type AltAuth = { [K in PresentKeys & keyof THandlers]: SecurityPrincipal; } & { [K in Exclude>]?: undefined; }; /** * The type of `res.locals.auth` for a route: the principals of the * alternative that was satisfied, keyed by scheme. A route whose `security` * is written inline gets a union of its alternatives - a route that inherits * the document level security, or passes a widened array, gets every scheme * as optional */ export type SecurityAuth = TSecurity extends ReadonlyArray ? [TAlt] extends [never] ? NoAuth : string extends keyof TAlt ? AnyAuth : TAlt extends unknown ? AltAuth : never : AnyAuth; /** * `res.locals` for a route - left as the Express default when no handlers are * configured so handlers annotated with plain Express types keep compiling */ export type SecurityLocals = [keyof THandlers] extends [never] ? Record : Record & { auth: SecurityAuth; }; export type RouteSecurity = ReadonlyArray | undefined; export type RequestValidation = { private?: boolean; deprecated?: boolean; /** * OpenAPI Security Requirement Objects for this operation. Overrides the * document level `security` array - entries are alternatives (OR) and the * keys must reference `components.securitySchemes`. Enforced when the * Schema is given `securityHandlers` */ security?: TSecurity; name?: string; group?: string; description?: string; params?: TParams; query?: TQuery; body?: BodyValidation; /** Whether the request body is required. Defaults to true when `body` is set. */ bodyRequired?: boolean; res?: TResponse; }; export {};