import type { Server } from 'node:http'; import type { Express, Request } from 'express'; import Base from '../../modules/Base.ts'; import type { IApp } from '../../server.ts'; import type { TI18n } from '../i18n/types.ts'; import { type ErrorHandlerFn, type ErrorHandlerResult, type ErrorLogLevel } from './builtinErrorHandlers.ts'; import { RouteRegistry } from './routing/RouteRegistry.ts'; export interface FrameworkRequest extends Request { appInfo: { app: IApp; ip?: string | undefined; request: Record; query: Record; params: Record; i18n?: TI18n; }; } /** * HTTP server based on Express */ declare class HttpServer extends Base { #private; express: Express; httpServer: Server; routeRegistry: RouteRegistry; constructor(app: IApp); /** * Start accepting connections; resolves once the port is bound. Called by * `startServer` after routes, the 404 page and the error handler are * mounted, so no request reaches a partly built app. */ listen(): Promise; /** Mount the route adapter — single entry to the registry. */ mountAdapter(): void; /** * Register a handler mapping a thrown error class to an HTTP response. * Consumer handlers are checked before the built-ins (`HttpError` mapper, * mongoose validation safety net) in registration order — the first * `instanceof` match whose handler returns non-null wins; return `null` to * pass to the next entry. Typical registration point is the project's * `bootHttp` hook. Returns an unregister function. * * Scope: handlers fire for errors thrown from route handlers, request * validation and middleware (the final sink, `addErrorHandler`, resolves * through the same registry). */ registerErrorHandler(errorClass: abstract new (...args: never[]) => E, handler: ErrorHandlerFn, opts?: { logLevel?: ErrorLogLevel; }): () => void; /** * Resolve a thrown error through the registry: consumer tier first, then * built-ins; first `instanceof` match returning non-null wins. A handler * that itself throws aborts the walk (logged here at `error`; the caller * falls through to its 500) — never a crash loop. Returns null when no entry * produced a response. Called by the route-handler and validation catches * and by the final sink, which receives middleware errors. */ resolveError(err: unknown, req: FrameworkRequest): Promise<(ErrorHandlerResult & { logLevel: ErrorLogLevel; }) | null>; /** * Add handle for 404 error */ add404Page(): void; /** * Add the final error sink. A 4-arg handler is the error sink, so it must * be registered last. Errors from middleware (and from a handler that * already streamed) land here; the registry answers what it recognises, * anything else is a 500. */ addErrorHandler(): void; static get loggerGroup(): string; /** * Stop the HTTP server: refuse new connections and resolve once in-flight * requests have drained, so a caller can await the drain before downstream * resources (mongo, redis) are torn down. */ shutdown(): Promise; } export default HttpServer;