BREAKING CHANGE: `parseQuery` utility removed; `IRouteMatcher` now includes query parsing; `RouteBuilder.middleware` and `handle` are now strictly typed per builder instance. - Add `isHandler` and `isMiddleware` runtime type guards for validation in `HttpKernel`. - Introduce `createEmptyContext` for constructing default context objects. - Support custom HTTP error handlers (`404`, `500`) via `IHttpKernelConfig.httpErrorHandlers`. - Default error handlers return meaningful HTTP status text (e.g., "Not Found"). - Replace legacy `parseQuery` logic with integrated query extraction via `createRouteMatcher`. - Strongly type `RouteBuilder.middleware()` and `.handle()` methods without generic overrides. - Simplify `HttpKernel.handle()` and `executePipeline()` through precise control flow and validation. - Remove deprecated `registerRoute.ts` and `HttpKernelConfig.ts` in favor of colocated type exports. - Add tests for integrated query parsing in `createRouteMatcher`. - Improve error handling tests: middleware/handler validation, double `next()` call, thrown exceptions. - Replace `assertRejects` with plain response code checks (via updated error handling). - Removed `parseQuery.ts` and all related tests — query parsing is now built into route matching. - `IRouteMatcher` signature changed to return `{ params, query }` instead of only `params`. - `HttpKernelConfig` now uses `DeepPartial` and includes `httpErrorHandlers`. - `RouteBuilder`'s generics are simplified for better DX and improved type safety. This refactor improves clarity, test coverage, and runtime safety of the request lifecycle while reducing boilerplate and eliminating duplicated query handling logic. Signed-off-by: Max P. <Mail@MPassarello.de>
37 lines
1.3 KiB
TypeScript
37 lines
1.3 KiB
TypeScript
import { Params } from '../Types/mod.ts';
|
|
import { IRouteDefinition } from './IRouteDefinition.ts';
|
|
import { IRouteMatch } from './IRouteMatch.ts';
|
|
|
|
/**
|
|
* Defines a route matcher function that evaluates whether a route applies to a given request.
|
|
*
|
|
* If the route matches, the matcher returns an object containing extracted route parameters.
|
|
* Otherwise, it returns `null`.
|
|
*/
|
|
export interface IRouteMatcher {
|
|
/**
|
|
* Evaluates whether the given URL and request match a defined route.
|
|
*
|
|
* @param url - The full URL of the incoming request.
|
|
* @param req - The raw Request object (may be used for context or headers).
|
|
* @returns An object containing path parameters if matched, or `null` if not matched.
|
|
*/
|
|
(url: URL, req: Request): null | IRouteMatch;
|
|
}
|
|
|
|
/**
|
|
* Represents a factory for creating route matcher functions from route definitions.
|
|
*
|
|
* This allows the matcher logic to be injected or replaced (e.g. for testing,
|
|
* pattern libraries, or advanced routing scenarios).
|
|
*/
|
|
export interface IRouteMatcherFactory {
|
|
/**
|
|
* Creates a matcher function based on a given route definition.
|
|
*
|
|
* @param def - The route definition (static or dynamic).
|
|
* @returns A matcher function that checks if a request matches and extracts parameters.
|
|
*/
|
|
(def: IRouteDefinition): IRouteMatcher;
|
|
}
|