refactor(core): enhance HttpKernel pipeline and matcher system with full context and error handling
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>
This commit is contained in:
40
src/Interfaces/IHttpErrorHandlers.ts
Normal file
40
src/Interfaces/IHttpErrorHandlers.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { IContext } from '../Interfaces/mod.ts';
|
||||
import { HttpErrorHandler, validHttpErrorCodes } from '../Types/mod.ts';
|
||||
|
||||
/**
|
||||
* A mapping of HTTP status codes to their corresponding error handlers.
|
||||
*
|
||||
* This interface defines required handlers for common critical status codes (404 and 500)
|
||||
* and allows optional handlers for all other known error codes defined in `validHttpErrorCodes`.
|
||||
*
|
||||
* This hybrid approach ensures predictable handling for key failure cases,
|
||||
* while remaining flexible for less common codes.
|
||||
*
|
||||
* @template TContext - The context type used in all error handlers.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* const errorHandlers: IHttpErrorHandlers = {
|
||||
* 404: (ctx) => new Response("Not Found", { status: 404 }),
|
||||
* 500: (ctx, err) => {
|
||||
* console.error(err);
|
||||
* return new Response("Internal Server Error", { status: 500 });
|
||||
* },
|
||||
* 429: (ctx) => new Response("Too Many Requests", { status: 429 }),
|
||||
* };
|
||||
* ```
|
||||
*/
|
||||
export interface IHttpErrorHandlers<TContext extends IContext = IContext>
|
||||
extends
|
||||
Partial<
|
||||
Record<
|
||||
Exclude<typeof validHttpErrorCodes[number], 404 | 500>,
|
||||
HttpErrorHandler<TContext>
|
||||
>
|
||||
> {
|
||||
/** Required error handler for HTTP 404 (Not Found). */
|
||||
404: HttpErrorHandler<TContext>;
|
||||
|
||||
/** Required error handler for HTTP 500 (Internal Server Error). */
|
||||
500: HttpErrorHandler<TContext>;
|
||||
}
|
Reference in New Issue
Block a user