import type { IncomingMessage, ServerResponse } from 'node:http'; /** The request view a handler receives: the resolved route params, parsed * query, parsed JSON body, and the raw req/res for the rare handler that needs * them (streaming, headers). Everything a handler mutates it does through the * returned `HandlerResult`, not by writing `res` directly — keeping the * error-wrap total. */ export interface RequestContext { method: string; /** The request pathname (no query string). */ path: string; /** Captured `:name` path params. */ params: Record; /** Parsed query string. */ query: URLSearchParams; /** Parsed JSON request body, or `undefined` when there was no body. */ body: unknown; req: IncomingMessage; res: ServerResponse; } /** What a handler returns. `body` is JSON-serialized; omit it (or use 204) for * an empty response. `headers` are merged over the default `content-type`. */ export interface HandlerResult { status: number; body?: unknown; headers?: Record; } /** A route handler — sync or async. A throw is caught and mapped by the router * (map.ts §8); a handler never writes an error response itself. */ export type RouteHandler = (ctx: RequestContext) => HandlerResult | Promise; /** One entry in the route table. `pattern` is a `:name`-style path template. */ export interface RouteDef { method: string; pattern: string; handler: RouteHandler; } /** The flat route table the handler authors (A-7) build up and hand to the * server (A-9). */ export type RouteTable = RouteDef[]; export declare class Router { private readonly routes; /** Register one route. Returns `this` for chaining. */ register(def: RouteDef): this; /** Register a whole table at once. */ registerAll(table: RouteTable): this; /** Match a method + pathname against the table, extracting `:name` params. * Returns null when nothing matches (the caller responds 404). */ match(method: string, path: string): { handler: RouteHandler; params: Record; } | null; /** Resolve the JSON body, dispatch to the matched handler, and write the * response. Every failure path — no route, bad JSON, a handler throw — is * turned into a JSON `ErrorBody` (map.ts §8); this method never throws and * never leaves the daemon crashed. */ handle(req: IncomingMessage, res: ServerResponse): Promise; }