summary#
introduce a mechanism to override the default Context instantiation within the Router to allow for framework-level extensions and typed state augmentation
shortcoming#
the Context type is currently hardcoded inside the createRouter implementation. this prevents downstream frameworks (like cobweb) or other kind of advanced applications from extending the Context with additional props (e.g., an HTTP streaming interface, specific session objects, or typed application state) without duplicating the entire router logic or resorting to unsafe operations with undefined behaviour. while ctx.state exists, itβs effectively just a Map which lacks compile-time safety for specific extensions qwq
proposed solution#
Add an optional contextFactory property to the RouterConfig interface, for example:
export interface RouterConfig {
contextFactory?: <P = Record<string, string>>(
request: Request,
url: URL,
info: Deno.ServeHandlerInfo<Deno.NetAddr>,
params: P,
requestId: string,
) => Context<P>;
}