/** * HTTP type contracts. Pure type-only — no runtime, no behavior. * * Per-controller `.routes.gen.ts` files (hand-written today, codegen-emitted * later) compose these primitives into per-route `Request` helpers. */ import type { IApp } from '../../server.ts'; import type { TI18n } from '../i18n/types.ts'; import type { StandardSchemaV1 } from '../validate/types.ts'; import type { FrameworkRequest } from './HttpServer.ts'; /** * Module-augmentation point for app-wide `appInfo` extensions. Users add: * * declare module '@adaptivestone/framework' { * interface AppInfoExtensions { * requestId: string; * sentryTransaction?: SentryTransaction; * } * } * * Per-route precision (e.g. `appInfo.user` from a route's middleware stack) * comes from the per-controller `Request` helper, not from this interface. */ export interface AppInfoExtensions { } /** * `appInfo` shape populated by the framework's built-in middlewares before any * user middleware runs. Mirrors what `HttpServer`'s middleware chain sets per * request (`PrepareAppInfo`, `IpDetector`, `I18n`) plus the slots that the * route-level validation populates (`request`, `query`). * * `i18n` is required — `I18nMiddleware` is part of `HttpServer`'s default * chain (`HttpServer.ts:63`) and runs on every HTTP request before any * controller. If a user removes it, they should augment `BaseAppInfo` to * relax the field. */ export interface BaseAppInfo { app: IApp; ip?: string | undefined; i18n: TI18n; request: Record; query: Record; /** * Validated, coerced path params — populated only when the route declares a * `params:` schema; `{}` otherwise. Raw Express strings stay on `req.params`. */ params: Record; } /** * Default request context handlers see when no per-route `Request` is * imported. Equivalent to today's `FrameworkRequest` plus `AppInfoExtensions`. */ export type BaseRequestContext = FrameworkRequest & { appInfo: BaseAppInfo & AppInfoExtensions; }; /** * Extract the static `provides` shape from a middleware class. Middlewares * declare what they add to `appInfo` via: * * class GetUserByToken extends AbstractMiddleware { * static get provides() { * return {} as { user?: InstanceType }; * } * } * * `ProvidesOf` resolves to `{ user?: InstanceType }`. * Returns `Record` for middlewares without `provides`. */ export type ProvidesOf = T extends { provides: infer P; } ? P : Record; /** * Reduce a tuple of middleware classes (or `[Class, params]` tuples) to the * intersection of their `provides` shapes. Used by per-route `Request` * to layer middleware-contributed `appInfo` fields onto the base context. */ export type UnionAppInfoProvides = MWs extends readonly [infer Head, ...infer Tail extends readonly unknown[]] ? ProvidesOf & UnionAppInfoProvides : Record; type MiddlewareClass = T extends readonly [infer C, unknown] ? C : T; type Member = K extends keyof T ? T[K] : never; /** A widened StandardSchema has no output information (including the base * middleware's nullable defaults). An explicitly non-nullable schema whose * output is `unknown` is still a real singleton output; retaining it avoids * promising the object-shaped base fallback. */ type KnownSchema = [Extract] extends [never] ? never : unknown extends StandardSchemaV1.InferOutput> ? [Extract] extends [never] ? S : never : S; type SchemaMetadata = [KnownSchema] extends [never] ? never : S; type StaticSchemas = { request: SchemaMetadata>; query: SchemaMetadata>; }; type InstanceSchemas = M extends { prototype: infer I; } ? { request: SchemaMetadata>; query: SchemaMetadata>; } : { request: never; query: never; }; type DefiniteSchema = [S] extends [never] ? never : null extends S ? never : undefined extends S ? never : S; type StaticSchemaSlots = StaticSchemas['request'] | StaticSchemas['query']; /** Instance metadata is the deprecated runtime fallback only when neither * static slot contributes a schema. Reading its type never constructs it. */ type MiddlewareSchemas = [ DefiniteSchema['request']> | DefiniteSchema['query']> ] extends [never] ? [StaticSchemaSlots] extends [never] ? InstanceSchemas : StaticSchemas | InstanceSchemas : StaticSchemas; type MiddlewareOutputs = MWs extends readonly [infer Head, ...infer Tail] ? AppendMiddlewareOutput>, Slot, MiddlewareOutputs> : []; /** Distribute over metadata branches before indexing a slot: a `never` slot * means no schema in that branch, and must not disappear from the union. */ type AppendMiddlewareOutput = Metadata extends { [K in Slot]: infer S; } ? [S] extends [never] ? Tail : S extends StandardSchemaV1 ? [StandardSchemaV1.InferOutput, ...Tail] : Tail : Tail; type RequiredKeys = { [K in keyof T]-?: object extends Pick ? never : K; }[keyof T]; type MergedValue = K extends keyof R ? K extends RequiredKeys ? R[K] : K extends keyof L ? L[K] | R[K] : R[K] : K extends keyof L ? L[K] : never; /** Ordered shallow assignment. Optional later keys may be absent, so an * earlier value remains possible; a present undefined still overwrites it. */ type MergeValidationObjects = { [K in RequiredKeys | RequiredKeys]: MergedValue; } & { [K in Exclude | RequiredKeys>]?: MergedValue; }; type MergeValidationOutputs = L extends readonly unknown[] ? never : R extends readonly unknown[] ? never : L extends object ? R extends object ? MergeValidationObjects : never : never; type ReduceValidationOutputs = Parts extends [infer L, infer R, ...infer Tail] ? ReduceValidationOutputs<[MergeValidationOutputs, ...Tail], Fallback> : Parts extends [infer Only] ? Only : Fallback; type ValidatedSlot = ReduceValidationOutputs<[ ...(Slot extends keyof Outputs ? [Outputs[Slot]] : []), ...MiddlewareOutputs ], Fallback>; type WithContentType = [ContentType] extends [ never ] ? T : T extends readonly unknown[] ? never : T extends object ? Omit & { contentType: ContentType; } : never; type ProvidedAppInfo = BaseRequestContext['appInfo'] & UnionAppInfoProvides; /** Handler context after validation: route output first, then middleware * outputs in chain order. Validated slots replace the base record types so * arrays, primitives and nullable schema outputs retain their actual shape. */ export type ValidatedRequest, ContentType extends string = never> = Omit & { appInfo: Omit, 'request' | 'query' | keyof Outputs> & { request: WithContentType['request']>, ContentType>; query: ValidatedSlot['query']>; } & Pick>; }; export {};