CI: Update Pages (2025-11-23 11:20:49)
This commit is contained in:
25
v0.2.1/src/Errors/InvalidHttpMethodError.ts
Normal file
25
v0.2.1/src/Errors/InvalidHttpMethodError.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Represents an error thrown when an incoming HTTP method
|
||||
* is not among the recognized set of valid HTTP methods.
|
||||
*
|
||||
* This is typically used in routers or request dispatchers
|
||||
* to enforce allowed methods and produce 405-like behavior.
|
||||
*/
|
||||
export class InvalidHttpMethodError extends Error {
|
||||
/**
|
||||
* The invalid method that triggered this error.
|
||||
*/
|
||||
public readonly method: unknown;
|
||||
|
||||
/**
|
||||
* A fixed HTTP status code representing "Method Not Allowed".
|
||||
*/
|
||||
public readonly status: number = 405;
|
||||
|
||||
constructor(method: unknown) {
|
||||
const label = typeof method === 'string' ? method : '[non-string]';
|
||||
super(`Unsupported HTTP method: ${label}`);
|
||||
this.name = 'InvalidHttpMethodError';
|
||||
this.method = method;
|
||||
}
|
||||
}
|
||||
3
v0.2.1/src/Errors/mod.ts
Normal file
3
v0.2.1/src/Errors/mod.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
// deno-coverage-ignore-file
|
||||
|
||||
export { InvalidHttpMethodError } from './InvalidHttpMethodError.ts';
|
||||
144
v0.2.1/src/HttpKernel.ts
Normal file
144
v0.2.1/src/HttpKernel.ts
Normal file
@@ -0,0 +1,144 @@
|
||||
import type {
|
||||
IContext,
|
||||
IHttpKernel,
|
||||
IHttpKernelConfig,
|
||||
IInternalRoute,
|
||||
IRouteBuilder,
|
||||
IRouteDefinition,
|
||||
} from './Interfaces/mod.ts';
|
||||
import {
|
||||
type DeepPartial,
|
||||
HTTP_404_NOT_FOUND,
|
||||
HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
HttpStatusTextMap,
|
||||
} from './Types/mod.ts';
|
||||
import { RouteBuilder } from './RouteBuilder.ts';
|
||||
import { createEmptyContext, normalizeError } from './Utils/mod.ts';
|
||||
|
||||
/**
|
||||
* The `HttpKernel` is the central routing engine that manages the full HTTP request lifecycle.
|
||||
*
|
||||
* It enables:
|
||||
* - Dynamic and static route registration via a fluent API
|
||||
* - Execution of typed middleware chains and final route handlers
|
||||
* - Injection of response decorators and factory overrides
|
||||
* - Fine-grained error handling via typed status-code-based handlers
|
||||
*
|
||||
* The kernel is designed with generics for flexible context typing, strong type safety,
|
||||
* and a clear extension point for advanced routing, DI, or tracing logic.
|
||||
*
|
||||
* @typeParam TContext - The global context type used for all requests handled by this kernel.
|
||||
*/
|
||||
export class HttpKernel<TContext extends IContext = IContext>
|
||||
implements IHttpKernel<TContext> {
|
||||
private cfg: IHttpKernelConfig<TContext>;
|
||||
|
||||
/**
|
||||
* The list of registered route definitions, including method, matcher,
|
||||
* middleware pipeline, and final handler.
|
||||
*/
|
||||
private routes: IInternalRoute<TContext>[] = [];
|
||||
|
||||
/**
|
||||
* Initializes the `HttpKernel` with optional configuration overrides.
|
||||
*
|
||||
* Default components such as the route builder factory, response decorator,
|
||||
* and 404/500 error handlers can be replaced by injecting a partial config.
|
||||
* Any omitted values fall back to sensible defaults.
|
||||
*
|
||||
* @param config - Partial kernel configuration. Missing fields are filled with defaults.
|
||||
*/
|
||||
public constructor(
|
||||
config?: DeepPartial<IHttpKernelConfig<TContext>>,
|
||||
) {
|
||||
this.cfg = {
|
||||
decorateResponse: (res) => res,
|
||||
routeBuilderFactory: RouteBuilder,
|
||||
httpErrorHandlers: {
|
||||
[HTTP_404_NOT_FOUND]: () =>
|
||||
new Response(HttpStatusTextMap[HTTP_404_NOT_FOUND], {
|
||||
status: HTTP_404_NOT_FOUND,
|
||||
}),
|
||||
[HTTP_500_INTERNAL_SERVER_ERROR]: () =>
|
||||
new Response(
|
||||
HttpStatusTextMap[HTTP_500_INTERNAL_SERVER_ERROR],
|
||||
{
|
||||
status: HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
},
|
||||
),
|
||||
...(config?.httpErrorHandlers ?? {}),
|
||||
},
|
||||
...config,
|
||||
} as IHttpKernelConfig<TContext>;
|
||||
|
||||
this.handle = this.handle.bind(this);
|
||||
this.registerRoute = this.registerRoute.bind(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public route<_TContext extends IContext = TContext>(
|
||||
definition: IRouteDefinition,
|
||||
): IRouteBuilder<_TContext> {
|
||||
return new this.cfg.routeBuilderFactory(
|
||||
this.registerRoute,
|
||||
definition,
|
||||
) as IRouteBuilder<_TContext>;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public async handle(request: Request): Promise<Response> {
|
||||
const url = new URL(request.url);
|
||||
const method = request.method.toUpperCase();
|
||||
|
||||
for (const route of this.routes) {
|
||||
if (route.method !== method) continue;
|
||||
const match = route.matcher(url, request);
|
||||
if (match) {
|
||||
const ctx: TContext = {
|
||||
req: request,
|
||||
params: match.params,
|
||||
query: match.query,
|
||||
state: {},
|
||||
} as TContext;
|
||||
try {
|
||||
const response = await route.runRoute(ctx);
|
||||
return this.cfg.decorateResponse(response, ctx);
|
||||
} catch (e) {
|
||||
return await this.handleInternalError(ctx, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return this.cfg.httpErrorHandlers[HTTP_404_NOT_FOUND](
|
||||
createEmptyContext<TContext>(request),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finalizes and registers a route within the kernel.
|
||||
*
|
||||
* This method is invoked internally by the route builder once
|
||||
* `.handle()` is called. It appends the route to the internal list.
|
||||
*
|
||||
* @param route - A fully constructed internal route object.
|
||||
*/
|
||||
private registerRoute<_TContext extends IContext = TContext>(
|
||||
route: IInternalRoute<_TContext>,
|
||||
): void {
|
||||
this.routes.push(route as unknown as IInternalRoute<TContext>);
|
||||
}
|
||||
|
||||
private handleInternalError = (
|
||||
ctx: TContext,
|
||||
err?: unknown,
|
||||
): Response | Promise<Response> => {
|
||||
return this.cfg.httpErrorHandlers[HTTP_500_INTERNAL_SERVER_ERROR](
|
||||
ctx,
|
||||
normalizeError(err),
|
||||
);
|
||||
};
|
||||
}
|
||||
53
v0.2.1/src/Interfaces/IContext.ts
Normal file
53
v0.2.1/src/Interfaces/IContext.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import type { Params, Query, State } from '../Types/mod.ts';
|
||||
|
||||
/**
|
||||
* Represents the complete context for a single HTTP request,
|
||||
* passed through the middleware pipeline and to the final route handler.
|
||||
*
|
||||
* This context object encapsulates all relevant runtime data for a request,
|
||||
* including the original request, path parameters, query parameters,
|
||||
* and a shared, mutable application state.
|
||||
*
|
||||
* @template TState Structured per-request state shared across middlewares and handlers.
|
||||
* @template TParams Parsed URL path parameters, typically derived from route templates.
|
||||
* @template TQuery Parsed query string parameters, preserving multi-value semantics.
|
||||
*/
|
||||
export interface IContext<
|
||||
TState extends State = State,
|
||||
TParams extends Params = Params,
|
||||
TQuery extends Query = Query,
|
||||
> {
|
||||
/**
|
||||
* The original HTTP request object as received by Deno.
|
||||
* Contains all standard fields like headers, method, body, etc.
|
||||
*/
|
||||
req: Request;
|
||||
|
||||
/**
|
||||
* Route parameters parsed from the URL path, based on route definitions
|
||||
* that include dynamic segments (e.g., `/users/:id` → `{ id: "123" }`).
|
||||
*
|
||||
* These parameters are considered read-only and are set by the router.
|
||||
*/
|
||||
params: TParams;
|
||||
|
||||
/**
|
||||
* Query parameters extracted from the request URL's search string.
|
||||
*
|
||||
* Values may occur multiple times (e.g., `?tag=ts&tag=deno`), and are therefore
|
||||
* represented as either a string or an array of strings, depending on occurrence.
|
||||
*
|
||||
* Use this field to access filters, flags, pagination info, or similar modifiers.
|
||||
*/
|
||||
query: TQuery;
|
||||
|
||||
/**
|
||||
* A typed, mutable object used to pass structured data between middlewares and handlers.
|
||||
*
|
||||
* This object is ideal for sharing validated input, user identity, trace information,
|
||||
* or other contextual state throughout the request lifecycle.
|
||||
*
|
||||
* Type-safe access to fields is ensured by the generic `TState` type.
|
||||
*/
|
||||
state: TState;
|
||||
}
|
||||
40
v0.2.1/src/Interfaces/IHttpErrorHandlers.ts
Normal file
40
v0.2.1/src/Interfaces/IHttpErrorHandlers.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import type { IContext } from '../Interfaces/mod.ts';
|
||||
import type { 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>;
|
||||
}
|
||||
49
v0.2.1/src/Interfaces/IHttpKernel.ts
Normal file
49
v0.2.1/src/Interfaces/IHttpKernel.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import type { IContext } from './IContext.ts';
|
||||
import type { IRouteBuilder } from './IRouteBuilder.ts';
|
||||
import type { IRouteDefinition } from './IRouteDefinition.ts';
|
||||
|
||||
/**
|
||||
* The `IHttpKernel` interface defines the public API for a type-safe, middleware-driven HTTP dispatching system.
|
||||
*
|
||||
* Implementations of this interface are responsible for:
|
||||
* - Registering routes with optional per-route context typing
|
||||
* - Handling incoming requests by matching and dispatching to appropriate handlers
|
||||
* - Managing the complete middleware pipeline and final response generation
|
||||
*
|
||||
* The kernel operates on a customizable `IContext` type to support strongly typed request parameters, state,
|
||||
* and query values across the entire routing lifecycle.
|
||||
*
|
||||
* @typeParam TContext - The default context type used for all routes unless overridden per-route.
|
||||
*/
|
||||
export interface IHttpKernel<TContext extends IContext = IContext> {
|
||||
/**
|
||||
* Registers a new HTTP route (static or dynamic) and returns a route builder for middleware/handler chaining.
|
||||
*
|
||||
* This method supports contextual polymorphism via the `_TContext` type parameter, enabling fine-grained
|
||||
* typing of route-specific `params`, `query`, and `state` values. The route is not registered until
|
||||
* `.handle()` is called on the returned builder.
|
||||
*
|
||||
* @typeParam _TContext - An optional override for the context type specific to this route.
|
||||
* Falls back to the global `TContext` of the kernel if omitted.
|
||||
*
|
||||
* @param definition - A route definition specifying the HTTP method and path or custom matcher.
|
||||
* @returns A fluent builder interface to define middleware and attach a final handler.
|
||||
*/
|
||||
route<_TContext extends IContext = TContext>(
|
||||
definition: IRouteDefinition,
|
||||
): IRouteBuilder<_TContext>;
|
||||
|
||||
/**
|
||||
* Handles an incoming HTTP request and produces a `Response`.
|
||||
*
|
||||
* The kernel matches the request against all registered routes by method and matcher,
|
||||
* constructs a typed context, and executes the middleware/handler pipeline.
|
||||
* If no route matches, a 404 error handler is invoked.
|
||||
*
|
||||
* This method is designed to be passed directly to `Deno.serve()` or similar server frameworks.
|
||||
*
|
||||
* @param request - The incoming HTTP request object.
|
||||
* @returns A `Promise` resolving to a complete HTTP response.
|
||||
*/
|
||||
handle(request: Request): Promise<Response>;
|
||||
}
|
||||
10
v0.2.1/src/Interfaces/IHttpKernelConfig.ts
Normal file
10
v0.2.1/src/Interfaces/IHttpKernelConfig.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import type { ResponseDecorator } from '../Types/mod.ts';
|
||||
import type { IContext } from './IContext.ts';
|
||||
import type { IHttpErrorHandlers } from './IHttpErrorHandlers.ts';
|
||||
import type { IRouteBuilderFactory } from './IRouteBuilder.ts';
|
||||
|
||||
export interface IHttpKernelConfig<TContext extends IContext = IContext> {
|
||||
decorateResponse: ResponseDecorator<TContext>;
|
||||
routeBuilderFactory: IRouteBuilderFactory;
|
||||
httpErrorHandlers: IHttpErrorHandlers<TContext>;
|
||||
}
|
||||
64
v0.2.1/src/Interfaces/IInternalRoute.ts
Normal file
64
v0.2.1/src/Interfaces/IInternalRoute.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import type { Handler, HttpMethod, Middleware } from '../Types/mod.ts';
|
||||
import type { IContext, IRouteMatcher } from './mod.ts';
|
||||
|
||||
/**
|
||||
* Represents an internally registered route within the HttpKernel.
|
||||
*
|
||||
* Contains all data required to match an incoming request and dispatch it
|
||||
* through the associated middleware chain and final handler.
|
||||
*/
|
||||
export interface IInternalRoute<TContext extends IContext = IContext> {
|
||||
/**
|
||||
* The HTTP method (e.g. 'GET', 'POST') that this route responds to.
|
||||
* The method should always be in uppercase.
|
||||
*/
|
||||
method: HttpMethod;
|
||||
|
||||
/**
|
||||
* A matcher function used to determine whether this route matches a given request.
|
||||
*
|
||||
* If the matcher returns `null`, the route does not apply to the request.
|
||||
* If it returns a params object, the route is considered matched and the extracted
|
||||
* parameters are passed into the request context.
|
||||
*
|
||||
* @param url - The parsed URL object from the incoming request.
|
||||
* @param req - The original Request object.
|
||||
* @returns An object with extracted path parameters, or `null` if not matched.
|
||||
*/
|
||||
matcher: IRouteMatcher;
|
||||
|
||||
/**
|
||||
* An ordered list of middleware functions to be executed before the handler.
|
||||
*/
|
||||
middlewares: Middleware<TContext>[];
|
||||
|
||||
/**
|
||||
* The final handler that generates the HTTP response after all middleware has run.
|
||||
*/
|
||||
handler: Handler<TContext>;
|
||||
|
||||
/**
|
||||
* The fully compiled execution pipeline for this route.
|
||||
*
|
||||
* This function is generated at route registration time and encapsulates the
|
||||
* entire middleware chain as well as the final handler. It is called by the
|
||||
* HttpKernel during request dispatch when a route has been matched.
|
||||
*
|
||||
* Internally, `runRoute` ensures that each middleware is invoked in the correct order
|
||||
* and receives a `next()` callback to pass control downstream. The final handler is
|
||||
* invoked once all middleware has completed or short-circuited the pipeline.
|
||||
*
|
||||
* It is guaranteed that:
|
||||
* - The function is statically compiled and does not perform dynamic dispatching.
|
||||
* - Each middleware can only call `next()` once; repeated invocations will throw.
|
||||
* - The return value is either a `Response` or a Promise resolving to one.
|
||||
*
|
||||
* @param ctx - The context object carrying route, request, response and other scoped data.
|
||||
* @returns A `Response` object or a Promise resolving to a `Response`.
|
||||
*
|
||||
* @throws {Error} If a middleware calls `next()` more than once.
|
||||
*/
|
||||
runRoute: (
|
||||
ctx: TContext,
|
||||
) => Promise<Response> | Response;
|
||||
}
|
||||
39
v0.2.1/src/Interfaces/IRouteBuilder.ts
Normal file
39
v0.2.1/src/Interfaces/IRouteBuilder.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import type { Handler, Middleware } from '../Types/mod.ts';
|
||||
import type { IInternalRoute } from './IInternalRoute.ts';
|
||||
import type { IRouteDefinition } from './IRouteDefinition.ts';
|
||||
import type { IContext } from './mod.ts';
|
||||
|
||||
export interface IRouteBuilderFactory<TContext extends IContext = IContext> {
|
||||
new (
|
||||
registerRoute: (route: IInternalRoute<TContext>) => void,
|
||||
def: IRouteDefinition,
|
||||
mws?: Middleware<TContext>[],
|
||||
): IRouteBuilder<TContext>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides a fluent API to build a single route configuration by chaining
|
||||
* middleware and setting the final request handler.
|
||||
*/
|
||||
export interface IRouteBuilder<TContext extends IContext = IContext> {
|
||||
/**
|
||||
* Adds a middleware to the current route.
|
||||
* Middleware will be executed in the order of registration.
|
||||
*
|
||||
* @param mw - A middleware function.
|
||||
* @returns The route builder for further chaining.
|
||||
*/
|
||||
middleware(
|
||||
mw: Middleware<TContext>,
|
||||
): IRouteBuilder<TContext>;
|
||||
|
||||
/**
|
||||
* Sets the final request handler for the route.
|
||||
* Calling this finalizes the route and registers it in the kernel.
|
||||
*
|
||||
* @param handler - The function to execute when this route is matched.
|
||||
*/
|
||||
handle(
|
||||
handler: Handler<TContext>,
|
||||
): void;
|
||||
}
|
||||
91
v0.2.1/src/Interfaces/IRouteDefinition.ts
Normal file
91
v0.2.1/src/Interfaces/IRouteDefinition.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
import { type HttpMethod, isHttpMethod } from '../Types/mod.ts';
|
||||
import type { IRouteMatcher } from './IRouteMatcher.ts';
|
||||
|
||||
/**
|
||||
* Defines a static route using a path pattern with optional parameters.
|
||||
*
|
||||
* Suitable for conventional routes like "/users/:id", which can be parsed
|
||||
* into named parameters using a path-matching library.
|
||||
*/
|
||||
export interface IStaticRouteDefinition {
|
||||
/**
|
||||
* The HTTP method this route should match (e.g. "GET", "POST").
|
||||
*/
|
||||
method: HttpMethod;
|
||||
|
||||
/**
|
||||
* A static path pattern for the route, which may include named parameters
|
||||
* (e.g. "/caches/:id"). Internally, this can be converted to a regex matcher.
|
||||
*/
|
||||
path: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines a dynamic route using a custom matcher function instead of a static path.
|
||||
*
|
||||
* Useful for complex URL structures that cannot easily be expressed using a static pattern,
|
||||
* such as routes with variable prefixes or conditional segment logic.
|
||||
*/
|
||||
export interface IDynamicRouteDefinition {
|
||||
/**
|
||||
* The HTTP method this route should match (e.g. "GET", "POST").
|
||||
*/
|
||||
method: HttpMethod;
|
||||
|
||||
/**
|
||||
* A custom matcher function that receives the parsed URL and raw request.
|
||||
* If the function returns `null`, the route does not match.
|
||||
* If the function returns a params object, the route is considered matched.
|
||||
*/
|
||||
matcher: IRouteMatcher;
|
||||
}
|
||||
|
||||
/**
|
||||
* A route definition can either be a conventional static route with a path pattern,
|
||||
* or a dynamic route with a custom matcher function for advanced matching logic.
|
||||
*/
|
||||
export type IRouteDefinition = IStaticRouteDefinition | IDynamicRouteDefinition;
|
||||
|
||||
/**
|
||||
* Type guard to check whether a route definition is a valid static route definition.
|
||||
*
|
||||
* Ensures that the object:
|
||||
* - has a `method` property of type `HttpMethod`
|
||||
* - has a `path` property of type `string`
|
||||
* - does NOT have a `matcher` function (to avoid ambiguous mixed types)
|
||||
*/
|
||||
export function isStaticRouteDefinition(
|
||||
def: IRouteDefinition,
|
||||
): def is IStaticRouteDefinition {
|
||||
return (
|
||||
def &&
|
||||
typeof def === 'object' &&
|
||||
'method' in def &&
|
||||
isHttpMethod(def.method) &&
|
||||
'path' in def &&
|
||||
typeof (def as { path?: unknown }).path === 'string' &&
|
||||
!('matcher' in def)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Type guard to check whether a route definition is a valid dynamic route definition.
|
||||
*
|
||||
* Ensures that the object:
|
||||
* - has a `method` property of type `HttpMethod`
|
||||
* - has a `matcher` property of type `function`
|
||||
* - does NOT have a `path` property (to avoid ambiguous mixed types)
|
||||
*/
|
||||
export function isDynamicRouteDefinition(
|
||||
def: IRouteDefinition,
|
||||
): def is IDynamicRouteDefinition {
|
||||
return (
|
||||
def &&
|
||||
typeof def === 'object' &&
|
||||
'method' in def &&
|
||||
isHttpMethod(def.method) &&
|
||||
'matcher' in def &&
|
||||
typeof (def as { matcher?: unknown }).matcher === 'function' &&
|
||||
!('path' in def)
|
||||
);
|
||||
}
|
||||
6
v0.2.1/src/Interfaces/IRouteMatch.ts
Normal file
6
v0.2.1/src/Interfaces/IRouteMatch.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import type { Params, Query } from '../Types/mod.ts';
|
||||
|
||||
export interface IRouteMatch {
|
||||
params?: Params;
|
||||
query?: Query;
|
||||
}
|
||||
35
v0.2.1/src/Interfaces/IRouteMatcher.ts
Normal file
35
v0.2.1/src/Interfaces/IRouteMatcher.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import type { IRouteDefinition } from './IRouteDefinition.ts';
|
||||
import type { 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;
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import { assertEquals } from 'https://deno.land/std@0.204.0/assert/mod.ts';
|
||||
import {
|
||||
type IRouteDefinition,
|
||||
isDynamicRouteDefinition,
|
||||
isStaticRouteDefinition,
|
||||
} from '../IRouteDefinition.ts';
|
||||
|
||||
Deno.test('isStaticRouteDefinition returns true for static route', () => {
|
||||
const staticDef: IRouteDefinition = {
|
||||
method: 'GET',
|
||||
path: '/users/:id',
|
||||
};
|
||||
|
||||
assertEquals(isStaticRouteDefinition(staticDef), true);
|
||||
assertEquals(isDynamicRouteDefinition(staticDef), false);
|
||||
});
|
||||
|
||||
Deno.test('isDynamicRouteDefinition returns true for dynamic route', () => {
|
||||
const dynamicDef: IRouteDefinition = {
|
||||
method: 'POST',
|
||||
matcher: (_url, _req) => ({ params: {} }),
|
||||
};
|
||||
|
||||
assertEquals(isDynamicRouteDefinition(dynamicDef), true);
|
||||
assertEquals(isStaticRouteDefinition(dynamicDef), false);
|
||||
});
|
||||
|
||||
Deno.test('isStaticRouteDefinition returns false for invalid object', () => {
|
||||
const invalidDef = {
|
||||
method: 'GET',
|
||||
} as unknown as IRouteDefinition;
|
||||
|
||||
assertEquals(isStaticRouteDefinition(invalidDef), false);
|
||||
});
|
||||
|
||||
Deno.test('isDynamicRouteDefinition returns false for object with no matcher', () => {
|
||||
const def = {
|
||||
method: 'DELETE',
|
||||
path: '/something',
|
||||
};
|
||||
|
||||
assertEquals(isDynamicRouteDefinition(def as IRouteDefinition), false);
|
||||
});
|
||||
19
v0.2.1/src/Interfaces/mod.ts
Normal file
19
v0.2.1/src/Interfaces/mod.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
// deno-coverage-ignore-file
|
||||
|
||||
export type { IContext } from './IContext.ts';
|
||||
export type { IHttpErrorHandlers } from './IHttpErrorHandlers.ts';
|
||||
export type { IHttpKernel } from './IHttpKernel.ts';
|
||||
export type { IHttpKernelConfig } from './IHttpKernelConfig.ts';
|
||||
export type { IInternalRoute } from './IInternalRoute.ts';
|
||||
export type { IRouteBuilder, IRouteBuilderFactory } from './IRouteBuilder.ts';
|
||||
export {
|
||||
isDynamicRouteDefinition,
|
||||
isStaticRouteDefinition,
|
||||
} from './IRouteDefinition.ts';
|
||||
export type {
|
||||
IDynamicRouteDefinition,
|
||||
IRouteDefinition,
|
||||
IStaticRouteDefinition,
|
||||
} from './IRouteDefinition.ts';
|
||||
export type { IRouteMatch } from './IRouteMatch.ts';
|
||||
export type { IRouteMatcher, IRouteMatcherFactory } from './IRouteMatcher.ts';
|
||||
148
v0.2.1/src/RouteBuilder.ts
Normal file
148
v0.2.1/src/RouteBuilder.ts
Normal file
@@ -0,0 +1,148 @@
|
||||
import type { IRouteMatcherFactory } from './Interfaces/IRouteMatcher.ts';
|
||||
import type {
|
||||
IContext,
|
||||
IInternalRoute,
|
||||
IRouteBuilder,
|
||||
IRouteDefinition,
|
||||
} from './Interfaces/mod.ts';
|
||||
import { isHandler } from './Types/Handler.ts';
|
||||
import {
|
||||
type Handler,
|
||||
isMiddleware,
|
||||
type Middleware,
|
||||
type RegisterRoute,
|
||||
} from './Types/mod.ts';
|
||||
import { createRouteMatcher } from './Utils/createRouteMatcher.ts';
|
||||
|
||||
/**
|
||||
* Provides a fluent builder interface for defining a single route,
|
||||
* including HTTP method, path or matcher, middleware chain and final handler.
|
||||
*
|
||||
* This builder is stateless and immutable; each chained call returns a new instance.
|
||||
*/
|
||||
export class RouteBuilder<TContext extends IContext = IContext>
|
||||
implements IRouteBuilder<TContext> {
|
||||
/**
|
||||
* Constructs a new instance of the route builder.
|
||||
*
|
||||
* @param registerRoute - A delegate used to register the finalized route definition.
|
||||
* @param def - The route definition (static path or dynamic matcher).
|
||||
* @param mws - The list of middleware functions collected so far (default: empty).
|
||||
*/
|
||||
constructor(
|
||||
private readonly registerRoute: RegisterRoute<TContext>,
|
||||
private readonly def: IRouteDefinition,
|
||||
private readonly mws: Middleware<TContext>[] = [],
|
||||
private readonly matcherFactory: IRouteMatcherFactory =
|
||||
createRouteMatcher,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Adds a middleware function to the current route definition.
|
||||
*
|
||||
* Middleware is executed in the order it is added.
|
||||
* Returns a new builder instance with the additional middleware appended.
|
||||
*
|
||||
* @param mw - A middleware function to be executed before the handler.
|
||||
* @returns A new `RouteBuilder` instance for continued chaining.
|
||||
*/
|
||||
middleware(
|
||||
mw: Middleware<TContext>,
|
||||
): IRouteBuilder<TContext> {
|
||||
return new RouteBuilder<TContext>(
|
||||
this.registerRoute,
|
||||
this.def,
|
||||
[...this.mws, mw],
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finalizes the route by assigning the handler and registering the route.
|
||||
*
|
||||
* Internally constructs a matcher function from the route definition
|
||||
* and passes all route data to the registration delegate.
|
||||
*
|
||||
* @param handler - The final request handler for this route.
|
||||
*/
|
||||
handle(
|
||||
handler: Handler<TContext>,
|
||||
): void {
|
||||
const matcher = this.matcherFactory(this.def);
|
||||
this.registerRoute({
|
||||
method: this.def.method,
|
||||
matcher,
|
||||
middlewares: this.mws,
|
||||
handler: handler,
|
||||
runRoute: this.compile({
|
||||
middlewares: this.mws,
|
||||
handler: handler,
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Compiles the middleware chain and handler into a single executable function.
|
||||
*
|
||||
* This method constructs a statically linked function chain by reducing all middleware
|
||||
* and the final handler into one composed `runRoute` function. Each middleware receives
|
||||
* a `next()` callback that invokes the next function in the chain.
|
||||
*
|
||||
* Additionally, the returned function ensures that `next()` can only be called once
|
||||
* per middleware. If `next()` is invoked multiple times within the same middleware,
|
||||
* a runtime `Error` is thrown, preventing unintended double-processing.
|
||||
*
|
||||
* Type safety is enforced at compile time:
|
||||
* - If the final handler does not match the expected signature, a `TypeError` is thrown.
|
||||
* - If any middleware does not conform to the middleware interface, a `TypeError` is thrown.
|
||||
*
|
||||
* @param route - A partial route object containing middleware and handler,
|
||||
* excluding `matcher`, `method`, and `runRoute`.
|
||||
* @returns A composed route execution function that takes a context object
|
||||
* and returns a `Promise<Response>`.
|
||||
*
|
||||
* @throws {TypeError} If the handler or any middleware function is invalid.
|
||||
* @throws {Error} If a middleware calls `next()` more than once during execution.
|
||||
*/
|
||||
private compile(
|
||||
route: Omit<
|
||||
IInternalRoute<TContext>,
|
||||
'runRoute' | 'matcher' | 'method'
|
||||
>,
|
||||
): (
|
||||
ctx: TContext,
|
||||
) => Promise<Response> {
|
||||
if (!isHandler<TContext>(route.handler)) {
|
||||
throw new TypeError(
|
||||
'Route handler must be a function returning a Promise<Response>.',
|
||||
);
|
||||
}
|
||||
let composed = route.handler;
|
||||
|
||||
for (let i = route.middlewares.length - 1; i >= 0; i--) {
|
||||
if (!isMiddleware<TContext>(route.middlewares[i])) {
|
||||
throw new TypeError(
|
||||
`Middleware at index ${i} is not a valid function.`,
|
||||
);
|
||||
}
|
||||
|
||||
const current = route.middlewares[i];
|
||||
const next = composed;
|
||||
|
||||
composed = async (ctx: TContext): Promise<Response> => {
|
||||
let called = false;
|
||||
|
||||
return await current(ctx, async () => {
|
||||
if (called) {
|
||||
throw new Error(
|
||||
`next() called multiple times in middleware at index ${i}`,
|
||||
);
|
||||
}
|
||||
called = true;
|
||||
return await next(ctx);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
return composed;
|
||||
}
|
||||
}
|
||||
4
v0.2.1/src/Types/DeepPartial.ts
Normal file
4
v0.2.1/src/Types/DeepPartial.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export type DeepPartial<T> = {
|
||||
[P in keyof T]?: T[P] extends object ? DeepPartial<T[P]>
|
||||
: T[P];
|
||||
};
|
||||
57
v0.2.1/src/Types/Handler.ts
Normal file
57
v0.2.1/src/Types/Handler.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import type { IContext } from '../Interfaces/mod.ts';
|
||||
|
||||
/**
|
||||
* Represents a final request handler responsible for producing an HTTP response.
|
||||
*
|
||||
* The handler is the terminal stage of the middleware pipeline and is responsible
|
||||
* for processing the incoming request and generating the final `Response`.
|
||||
*
|
||||
* It receives the fully-typed request context, which includes the original request,
|
||||
* parsed route parameters, query parameters, and any shared state populated by prior middleware.
|
||||
*
|
||||
* @template TContext The specific context type for this handler, including typed `state`, `params`, and `query`.
|
||||
*/
|
||||
type Handler<TContext extends IContext = IContext> = (
|
||||
ctx: TContext,
|
||||
) => Promise<Response>;
|
||||
|
||||
/**
|
||||
* Represents a handler function with an associated name.
|
||||
*
|
||||
* This is useful for debugging, logging, or when you need to reference
|
||||
* the handler by name in your application.
|
||||
*
|
||||
* @template TContext The specific context type for this handler, including typed `state`, `params`, and `query`.
|
||||
*/
|
||||
type NamedHandler<TContext extends IContext = IContext> =
|
||||
& Handler<TContext>
|
||||
& { name?: string };
|
||||
|
||||
export type { NamedHandler as Handler };
|
||||
|
||||
/**
|
||||
* Type guard to determine whether a given value is a valid `IHandler` function.
|
||||
*
|
||||
* This function checks whether the input is a function and whether it returns
|
||||
* a `Promise<Response>` when called. Due to TypeScript's structural typing and
|
||||
* the lack of runtime type information, only minimal runtime validation is possible.
|
||||
*
|
||||
* @param value - The value to test.
|
||||
* @returns `true` if the value is a function that appears to conform to `IHandler`.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* const candidate = async (ctx: IContext) => new Response("ok");
|
||||
* if (isHandler(candidate)) {
|
||||
* // candidate is now typed as IHandler<IContext>
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
export function isHandler<TContext extends IContext = IContext>(
|
||||
value: unknown,
|
||||
): value is Handler<TContext> {
|
||||
return (
|
||||
typeof value === 'function' &&
|
||||
value.length === 1 // ctx
|
||||
);
|
||||
}
|
||||
28
v0.2.1/src/Types/HttpErrorHandler.ts
Normal file
28
v0.2.1/src/Types/HttpErrorHandler.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import type { IContext } from '../Interfaces/mod.ts';
|
||||
|
||||
/**
|
||||
* Defines a handler function for errors that occur during the execution
|
||||
* of middleware or route handlers within the HTTP kernel.
|
||||
*
|
||||
* This function receives both the request context and the thrown error,
|
||||
* and is responsible for producing an appropriate HTTP `Response`.
|
||||
*
|
||||
* Typical use cases include:
|
||||
* - Mapping known error types to specific HTTP status codes.
|
||||
* - Generating structured error responses (e.g. JSON error payloads).
|
||||
* - Logging errors centrally with request metadata.
|
||||
*
|
||||
* The handler may return the response synchronously or asynchronously.
|
||||
*
|
||||
* @template TContext - The specific request context type, allowing typed access to route parameters,
|
||||
* query parameters, and per-request state when formatting error responses.
|
||||
*
|
||||
* @param context - The active request context at the time the error occurred.
|
||||
* @param error - The exception or error that was thrown during request processing.
|
||||
*
|
||||
* @returns A `Response` object or a `Promise` resolving to one, to be sent to the client.
|
||||
*/
|
||||
export type HttpErrorHandler<TContext extends IContext = IContext> = (
|
||||
context?: Partial<TContext>,
|
||||
error?: Error,
|
||||
) => Promise<Response> | Response;
|
||||
52
v0.2.1/src/Types/HttpMethod.ts
Normal file
52
v0.2.1/src/Types/HttpMethod.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* A constant list of all supported HTTP methods according to RFC 7231 and RFC 5789.
|
||||
*
|
||||
* This array serves both as a runtime value list for validation
|
||||
* and as the basis for deriving the `HttpMethod` union type.
|
||||
*
|
||||
* Note: The list is immutable and should not be modified at runtime.
|
||||
*/
|
||||
export const validHttpMethods = [
|
||||
'GET',
|
||||
'POST',
|
||||
'PUT',
|
||||
'PATCH',
|
||||
'DELETE',
|
||||
'HEAD',
|
||||
'OPTIONS',
|
||||
] as const;
|
||||
|
||||
/**
|
||||
* A union type representing all valid HTTP methods recognized by this application.
|
||||
*
|
||||
* This type is derived directly from the `validHttpMethods` constant,
|
||||
* ensuring type safety and consistency between type system and runtime checks.
|
||||
*
|
||||
* Example:
|
||||
* ```ts
|
||||
* const method: HttpMethod = 'POST'; // ✅ valid
|
||||
* const method: HttpMethod = 'FOO'; // ❌ Type error
|
||||
* ```
|
||||
*/
|
||||
export type HttpMethod = typeof validHttpMethods[number];
|
||||
|
||||
/**
|
||||
* Type guard to verify whether a given value is a valid HTTP method.
|
||||
*
|
||||
* This function checks both the type and content of the value
|
||||
* and is suitable for runtime validation of inputs (e.g., from HTTP requests).
|
||||
*
|
||||
* Example:
|
||||
* ```ts
|
||||
* if (isHttpMethod(input)) {
|
||||
* // input is now typed as HttpMethod
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* @param value - The value to test (typically a string from a request).
|
||||
* @returns `true` if the value is a valid `HttpMethod`, otherwise `false`.
|
||||
*/
|
||||
export function isHttpMethod(value: unknown): value is HttpMethod {
|
||||
return typeof value === 'string' &&
|
||||
validHttpMethods.includes(value as HttpMethod);
|
||||
}
|
||||
189
v0.2.1/src/Types/HttpStatusCode.ts
Normal file
189
v0.2.1/src/Types/HttpStatusCode.ts
Normal file
@@ -0,0 +1,189 @@
|
||||
// Informational responses
|
||||
/** Indicates that the request was received and the client can continue. */
|
||||
export const HTTP_100_CONTINUE = 100;
|
||||
/** The server is switching protocols as requested by the client. */
|
||||
export const HTTP_101_SWITCHING_PROTOCOLS = 101;
|
||||
/** The server has received and is processing the request, but no response is available yet. */
|
||||
export const HTTP_102_PROCESSING = 102;
|
||||
|
||||
// Successful responses
|
||||
/** The request has succeeded. */
|
||||
export const HTTP_200_OK = 200;
|
||||
/** The request has succeeded and a new resource has been created as a result. */
|
||||
export const HTTP_201_CREATED = 201;
|
||||
/** The request has been accepted for processing, but the processing is not complete. */
|
||||
export const HTTP_202_ACCEPTED = 202;
|
||||
/** The server has successfully fulfilled the request and there is no content to send. */
|
||||
export const HTTP_204_NO_CONTENT = 204;
|
||||
|
||||
// Redirection messages
|
||||
/** The resource has been moved permanently to a new URI. */
|
||||
export const HTTP_301_MOVED_PERMANENTLY = 301;
|
||||
/** The resource resides temporarily under a different URI. */
|
||||
export const HTTP_302_FOUND = 302;
|
||||
/** Indicates that the resource has not been modified since the last request. */
|
||||
export const HTTP_304_NOT_MODIFIED = 304;
|
||||
|
||||
// Client error responses
|
||||
/** The server could not understand the request due to invalid syntax. */
|
||||
export const HTTP_400_BAD_REQUEST = 400;
|
||||
/** The request requires user authentication. */
|
||||
export const HTTP_401_UNAUTHORIZED = 401;
|
||||
/** The server understood the request but refuses to authorize it. */
|
||||
export const HTTP_403_FORBIDDEN = 403;
|
||||
/** The server cannot find the requested resource. */
|
||||
export const HTTP_404_NOT_FOUND = 404;
|
||||
/** The request method is known by the server but is not supported by the target resource. */
|
||||
export const HTTP_405_METHOD_NOT_ALLOWED = 405;
|
||||
/** The request could not be completed due to a conflict with the current state of the resource. */
|
||||
export const HTTP_409_CONFLICT = 409;
|
||||
/** The server understands the content type but was unable to process the contained instructions. */
|
||||
export const HTTP_422_UNPROCESSABLE_ENTITY = 422;
|
||||
/** The user has sent too many requests in a given amount of time. */
|
||||
export const HTTP_429_TOO_MANY_REQUESTS = 429;
|
||||
|
||||
// Server error responses
|
||||
/** The server encountered an unexpected condition that prevented it from fulfilling the request. */
|
||||
export const HTTP_500_INTERNAL_SERVER_ERROR = 500;
|
||||
/** The server does not support the functionality required to fulfill the request. */
|
||||
export const HTTP_501_NOT_IMPLEMENTED = 501;
|
||||
/** The server, while acting as a gateway or proxy, received an invalid response from the upstream server. */
|
||||
export const HTTP_502_BAD_GATEWAY = 502;
|
||||
/** The server is not ready to handle the request, often due to maintenance or overload. */
|
||||
export const HTTP_503_SERVICE_UNAVAILABLE = 503;
|
||||
/** The server is acting as a gateway and cannot get a response in time. */
|
||||
export const HTTP_504_GATEWAY_TIMEOUT = 504;
|
||||
|
||||
/**
|
||||
* A constant list of supported HTTP status codes used by this application.
|
||||
*
|
||||
* These constants are grouped by category and used to construct the union type `HttpStatusCode`.
|
||||
*/
|
||||
export const validHttpStatusCodes = [
|
||||
// Informational
|
||||
HTTP_100_CONTINUE,
|
||||
HTTP_101_SWITCHING_PROTOCOLS,
|
||||
HTTP_102_PROCESSING,
|
||||
|
||||
// Successful
|
||||
HTTP_200_OK,
|
||||
HTTP_201_CREATED,
|
||||
HTTP_202_ACCEPTED,
|
||||
HTTP_204_NO_CONTENT,
|
||||
|
||||
// Redirection
|
||||
HTTP_301_MOVED_PERMANENTLY,
|
||||
HTTP_302_FOUND,
|
||||
HTTP_304_NOT_MODIFIED,
|
||||
|
||||
// Client Errors
|
||||
HTTP_400_BAD_REQUEST,
|
||||
HTTP_401_UNAUTHORIZED,
|
||||
HTTP_403_FORBIDDEN,
|
||||
HTTP_404_NOT_FOUND,
|
||||
HTTP_405_METHOD_NOT_ALLOWED,
|
||||
HTTP_409_CONFLICT,
|
||||
HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
HTTP_429_TOO_MANY_REQUESTS,
|
||||
|
||||
// Server Errors
|
||||
HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
HTTP_501_NOT_IMPLEMENTED,
|
||||
HTTP_502_BAD_GATEWAY,
|
||||
HTTP_503_SERVICE_UNAVAILABLE,
|
||||
HTTP_504_GATEWAY_TIMEOUT,
|
||||
] as const;
|
||||
|
||||
/**
|
||||
* A constant list of HTTP error codes that are commonly used in the application.
|
||||
*/
|
||||
export const validHttpErrorCodes = [
|
||||
// Client Errors
|
||||
HTTP_400_BAD_REQUEST,
|
||||
HTTP_401_UNAUTHORIZED,
|
||||
HTTP_403_FORBIDDEN,
|
||||
HTTP_404_NOT_FOUND,
|
||||
HTTP_405_METHOD_NOT_ALLOWED,
|
||||
HTTP_409_CONFLICT,
|
||||
HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
HTTP_429_TOO_MANY_REQUESTS,
|
||||
|
||||
// Server Errors
|
||||
HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
HTTP_501_NOT_IMPLEMENTED,
|
||||
HTTP_502_BAD_GATEWAY,
|
||||
HTTP_503_SERVICE_UNAVAILABLE,
|
||||
HTTP_504_GATEWAY_TIMEOUT,
|
||||
] as const;
|
||||
|
||||
/**
|
||||
* Maps each supported HTTP status code to its standard status message.
|
||||
*
|
||||
* Useful for logging, diagnostics, or building custom error responses.
|
||||
*/
|
||||
export const HttpStatusTextMap: Record<
|
||||
typeof validHttpStatusCodes[number],
|
||||
string
|
||||
> = {
|
||||
[HTTP_100_CONTINUE]: 'Continue',
|
||||
[HTTP_101_SWITCHING_PROTOCOLS]: 'Switching Protocols',
|
||||
[HTTP_102_PROCESSING]: 'Processing',
|
||||
|
||||
[HTTP_200_OK]: 'OK',
|
||||
[HTTP_201_CREATED]: 'Created',
|
||||
[HTTP_202_ACCEPTED]: 'Accepted',
|
||||
[HTTP_204_NO_CONTENT]: 'No Content',
|
||||
|
||||
[HTTP_301_MOVED_PERMANENTLY]: 'Moved Permanently',
|
||||
[HTTP_302_FOUND]: 'Found',
|
||||
[HTTP_304_NOT_MODIFIED]: 'Not Modified',
|
||||
|
||||
[HTTP_400_BAD_REQUEST]: 'Bad Request',
|
||||
[HTTP_401_UNAUTHORIZED]: 'Unauthorized',
|
||||
[HTTP_403_FORBIDDEN]: 'Forbidden',
|
||||
[HTTP_404_NOT_FOUND]: 'Not Found',
|
||||
[HTTP_405_METHOD_NOT_ALLOWED]: 'Method Not Allowed',
|
||||
[HTTP_409_CONFLICT]: 'Conflict',
|
||||
[HTTP_422_UNPROCESSABLE_ENTITY]: 'Unprocessable Entity',
|
||||
[HTTP_429_TOO_MANY_REQUESTS]: 'Too Many Requests',
|
||||
|
||||
[HTTP_500_INTERNAL_SERVER_ERROR]: 'Internal Server Error',
|
||||
[HTTP_501_NOT_IMPLEMENTED]: 'Not Implemented',
|
||||
[HTTP_502_BAD_GATEWAY]: 'Bad Gateway',
|
||||
[HTTP_503_SERVICE_UNAVAILABLE]: 'Service Unavailable',
|
||||
[HTTP_504_GATEWAY_TIMEOUT]: 'Gateway Timeout',
|
||||
};
|
||||
|
||||
/**
|
||||
* A union type representing commonly used HTTP status codes.
|
||||
*
|
||||
* This type ensures consistency between runtime and type-level status code handling.
|
||||
*
|
||||
* Example:
|
||||
* ```ts
|
||||
* const status: HttpStatusCode = 404; // ✅ valid
|
||||
* const status: HttpStatusCode = 418; // ❌ Type error (unless added to list)
|
||||
* ```
|
||||
*/
|
||||
export type HttpStatusCode = typeof validHttpStatusCodes[number];
|
||||
|
||||
/**
|
||||
* Type guard to check whether a given value is a valid HTTP status code.
|
||||
*
|
||||
* This is useful for validating numeric values received from external input,
|
||||
* ensuring they conform to known HTTP semantics.
|
||||
*
|
||||
* Example:
|
||||
* ```ts
|
||||
* if (isHttpStatusCode(value)) {
|
||||
* // value is now typed as HttpStatusCode
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* @param value - The numeric value to check.
|
||||
* @returns `true` if the value is a recognized HTTP status code, otherwise `false`.
|
||||
*/
|
||||
export function isHttpStatusCode(value: unknown): value is HttpStatusCode {
|
||||
return typeof value === 'number' &&
|
||||
validHttpStatusCodes.includes(value as HttpStatusCode);
|
||||
}
|
||||
51
v0.2.1/src/Types/Middleware.ts
Normal file
51
v0.2.1/src/Types/Middleware.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import type { IContext } from '../Interfaces/IContext.ts';
|
||||
|
||||
/**
|
||||
* Represents a middleware function in the HTTP request pipeline.
|
||||
*
|
||||
* Middleware is a core mechanism to intercept, observe, or modify the request lifecycle.
|
||||
* It can be used for tasks such as logging, authentication, input validation,
|
||||
* metrics collection, or response transformation.
|
||||
*
|
||||
* Each middleware receives a fully-typed request context and a `next()` function
|
||||
* to invoke the next stage of the pipeline. Middleware may choose to short-circuit
|
||||
* the pipeline by returning a `Response` early.
|
||||
*
|
||||
* @template TContext The specific context type for this middleware, including state, params, and query information.
|
||||
*/
|
||||
type Middleware<TContext extends IContext = IContext> = (
|
||||
ctx: TContext,
|
||||
next: () => Promise<Response>,
|
||||
) => Promise<Response>;
|
||||
|
||||
/**
|
||||
* Represents a middleware function with an associated name.
|
||||
*
|
||||
* This is useful for debugging, logging, or when you need to reference
|
||||
* the middleware by name in your application.
|
||||
*
|
||||
* @template TContext The specific context type for this middleware, including state, params, and query information.
|
||||
*/
|
||||
type NamedMiddleware<TContext extends IContext = IContext> =
|
||||
& Middleware<TContext>
|
||||
& { name?: string };
|
||||
|
||||
export type { NamedMiddleware as Middleware };
|
||||
|
||||
/**
|
||||
* Type guard to verify whether a given value is a valid `IMiddleware` function.
|
||||
*
|
||||
* This guard checks whether the input is a function that accepts exactly two arguments.
|
||||
* Note: This is a structural check and cannot fully guarantee the semantics of a middleware.
|
||||
*
|
||||
* @param value - The value to test.
|
||||
* @returns `true` if the value is structurally a valid middleware function.
|
||||
*/
|
||||
export function isMiddleware<TContext extends IContext = IContext>(
|
||||
value: unknown,
|
||||
): value is Middleware<TContext> {
|
||||
return (
|
||||
typeof value === 'function' &&
|
||||
value.length === 2 // ctx, next
|
||||
);
|
||||
}
|
||||
10
v0.2.1/src/Types/Params.ts
Normal file
10
v0.2.1/src/Types/Params.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
/**
|
||||
* Represents route parameters parsed from dynamic segments in the URL path.
|
||||
*
|
||||
* This type is typically derived from route definitions with placeholders,
|
||||
* such as `/users/:id`, which would yield `{ id: "123" }`.
|
||||
*
|
||||
* All values are strings and should be considered read-only, as they are
|
||||
* extracted by the router and should not be modified by application code.
|
||||
*/
|
||||
export type Params = Record<string, string>;
|
||||
12
v0.2.1/src/Types/Query.ts
Normal file
12
v0.2.1/src/Types/Query.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* Represents the parsed query parameters from the request URL.
|
||||
*
|
||||
* Query parameters originate from the URL search string (e.g. `?filter=active&tags=ts&tags=deno`)
|
||||
* and may contain single or multiple values per key.
|
||||
*
|
||||
* All values are expressed as strings or arrays of strings, depending on how often
|
||||
* the key occurs. This structure preserves the raw semantics of the query.
|
||||
*
|
||||
* For normalized single-value access, prefer custom DTOs or wrapper utilities.
|
||||
*/
|
||||
export type Query = Record<string, string | string[]>;
|
||||
16
v0.2.1/src/Types/RegisterRoute.ts
Normal file
16
v0.2.1/src/Types/RegisterRoute.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import type { IContext } from '../Interfaces/IContext.ts';
|
||||
import type { IInternalRoute } from '../Interfaces/mod.ts';
|
||||
|
||||
/**
|
||||
* A type alias for the internal route registration function used by the `HttpKernel`.
|
||||
*
|
||||
* This function accepts a fully constructed internal route, including method, matcher,
|
||||
* middleware chain, and final handler, and registers it for dispatching.
|
||||
*
|
||||
* Typically passed into `RouteBuilder` instances to enable fluent API chaining.
|
||||
*
|
||||
* @template TContext The context type associated with the route being registered.
|
||||
*/
|
||||
export type RegisterRoute<TContext extends IContext = IContext> = (
|
||||
route: IInternalRoute<TContext>,
|
||||
) => void;
|
||||
30
v0.2.1/src/Types/ResponseDecorator.ts
Normal file
30
v0.2.1/src/Types/ResponseDecorator.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import type { IContext } from '../Interfaces/mod.ts';
|
||||
|
||||
/**
|
||||
* A function that modifies or enriches an outgoing HTTP response before it is returned to the client.
|
||||
*
|
||||
* This decorator can be used to inject headers (e.g., CORS, security), apply global transformations,
|
||||
* or wrap responses for logging, analytics, or debugging purposes.
|
||||
*
|
||||
* It is called exactly once at the end of the middleware/handler pipeline,
|
||||
* allowing central response customization without interfering with business logic.
|
||||
*
|
||||
* @param res - The original `Response` object produced by the route handler or middleware chain.
|
||||
* @returns A modified or wrapped `Response` object to be sent back to the client.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* const addCors: ResponseDecorator = (res) => {
|
||||
* const headers = new Headers(res.headers);
|
||||
* headers.set("Access-Control-Allow-Origin", "*");
|
||||
* return new Response(res.body, {
|
||||
* status: res.status,
|
||||
* headers,
|
||||
* });
|
||||
* };
|
||||
* ```
|
||||
*/
|
||||
export type ResponseDecorator<TContext extends IContext = IContext> = (
|
||||
res: Response,
|
||||
ctx: TContext,
|
||||
) => Response;
|
||||
9
v0.2.1/src/Types/State.ts
Normal file
9
v0.2.1/src/Types/State.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* Represents the per-request state object shared across the middleware pipeline.
|
||||
*
|
||||
* This type defines the base structure for custom state definitions,
|
||||
* which can be extended with concrete fields like user data, request metadata, etc.
|
||||
*
|
||||
* Custom `TState` types must extend this base to ensure compatibility.
|
||||
*/
|
||||
export type State = Record<string, unknown>;
|
||||
40
v0.2.1/src/Types/__tests__/HttpMethod.test.ts
Normal file
40
v0.2.1/src/Types/__tests__/HttpMethod.test.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { assertEquals } from 'https://deno.land/std/assert/mod.ts';
|
||||
import { isHttpMethod, validHttpMethods } from '../HttpMethod.ts';
|
||||
|
||||
Deno.test('isHttpMethod: returns true for all valid methods', () => {
|
||||
for (const method of validHttpMethods) {
|
||||
const result = isHttpMethod(method);
|
||||
assertEquals(result, true, `Expected "${method}" to be valid`);
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test('isHttpMethod: returns false for lowercase or unknown strings', () => {
|
||||
const invalid = [
|
||||
'get',
|
||||
'post',
|
||||
'FETCH',
|
||||
'TRACE',
|
||||
'CONNECT',
|
||||
'INVALID',
|
||||
'',
|
||||
' ',
|
||||
];
|
||||
|
||||
for (const method of invalid) {
|
||||
const result = isHttpMethod(method);
|
||||
assertEquals(result, false, `Expected "${method}" to be invalid`);
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test('isHttpMethod: returns false for non-string inputs', () => {
|
||||
const invalidInputs = [null, undefined, 123, {}, [], true, Symbol('GET')];
|
||||
|
||||
for (const input of invalidInputs) {
|
||||
const result = isHttpMethod(input);
|
||||
assertEquals(
|
||||
result,
|
||||
false,
|
||||
`Expected non-string input to be invalid: ${String(input)}`,
|
||||
);
|
||||
}
|
||||
});
|
||||
35
v0.2.1/src/Types/__tests__/HttpStatusCode.test.ts
Normal file
35
v0.2.1/src/Types/__tests__/HttpStatusCode.test.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
// src/Types/__tests__/HttpStatusCode.test.ts
|
||||
import { assertEquals } from 'https://deno.land/std@0.204.0/assert/mod.ts';
|
||||
import { isHttpStatusCode, validHttpStatusCodes } from '../HttpStatusCode.ts';
|
||||
|
||||
Deno.test('isHttpStatusCode: returns true for all valid status codes', () => {
|
||||
for (const code of validHttpStatusCodes) {
|
||||
assertEquals(
|
||||
isHttpStatusCode(code),
|
||||
true,
|
||||
`Expected ${code} to be valid`,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test('isHttpStatusCode: returns false for invalid status codes', () => {
|
||||
const invalidInputs = [99, 600, 1234, -1, 0, 999];
|
||||
for (const val of invalidInputs) {
|
||||
assertEquals(
|
||||
isHttpStatusCode(val),
|
||||
false,
|
||||
`Expected ${val} to be invalid`,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test('isHttpStatusCode: returns false for non-numeric values', () => {
|
||||
const invalid = ['200', null, undefined, {}, [], true];
|
||||
for (const val of invalid) {
|
||||
assertEquals(
|
||||
isHttpStatusCode(val),
|
||||
false,
|
||||
`Expected ${val} to be invalid`,
|
||||
);
|
||||
}
|
||||
});
|
||||
45
v0.2.1/src/Types/mod.ts
Normal file
45
v0.2.1/src/Types/mod.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
// deno-coverage-ignore-file
|
||||
|
||||
export type { DeepPartial } from './DeepPartial.ts';
|
||||
export { isHandler } from './Handler.ts';
|
||||
export type { Handler } from './Handler.ts';
|
||||
export type { HttpErrorHandler } from './HttpErrorHandler.ts';
|
||||
export { isHttpMethod, validHttpMethods } from './HttpMethod.ts';
|
||||
export type { HttpMethod } from './HttpMethod.ts';
|
||||
export {
|
||||
HTTP_100_CONTINUE,
|
||||
HTTP_101_SWITCHING_PROTOCOLS,
|
||||
HTTP_102_PROCESSING,
|
||||
HTTP_200_OK,
|
||||
HTTP_201_CREATED,
|
||||
HTTP_202_ACCEPTED,
|
||||
HTTP_204_NO_CONTENT,
|
||||
HTTP_301_MOVED_PERMANENTLY,
|
||||
HTTP_302_FOUND,
|
||||
HTTP_304_NOT_MODIFIED,
|
||||
HTTP_400_BAD_REQUEST,
|
||||
HTTP_401_UNAUTHORIZED,
|
||||
HTTP_403_FORBIDDEN,
|
||||
HTTP_404_NOT_FOUND,
|
||||
HTTP_405_METHOD_NOT_ALLOWED,
|
||||
HTTP_409_CONFLICT,
|
||||
HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
HTTP_429_TOO_MANY_REQUESTS,
|
||||
HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
HTTP_501_NOT_IMPLEMENTED,
|
||||
HTTP_502_BAD_GATEWAY,
|
||||
HTTP_503_SERVICE_UNAVAILABLE,
|
||||
HTTP_504_GATEWAY_TIMEOUT,
|
||||
HttpStatusTextMap,
|
||||
isHttpStatusCode,
|
||||
validHttpErrorCodes,
|
||||
validHttpStatusCodes,
|
||||
} from './HttpStatusCode.ts';
|
||||
export type { HttpStatusCode } from './HttpStatusCode.ts';
|
||||
export { isMiddleware } from './Middleware.ts';
|
||||
export type { Middleware } from './Middleware.ts';
|
||||
export type { Params } from './Params.ts';
|
||||
export type { Query } from './Query.ts';
|
||||
export type { RegisterRoute } from './RegisterRoute.ts';
|
||||
export type { ResponseDecorator } from './ResponseDecorator.ts';
|
||||
export type { State } from './State.ts';
|
||||
28
v0.2.1/src/Utils/__tests__/createEmptyContext.test.ts
Normal file
28
v0.2.1/src/Utils/__tests__/createEmptyContext.test.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { assertEquals } from 'https://deno.land/std/assert/mod.ts';
|
||||
import { createEmptyContext } from '../createEmptyContext.ts';
|
||||
import type { IContext } from '../../Interfaces/mod.ts';
|
||||
|
||||
Deno.test('createEmptyContext: returns default-initialized context', () => {
|
||||
const request = new Request('http://localhost');
|
||||
const ctx = createEmptyContext(request);
|
||||
|
||||
assertEquals(ctx.req, request);
|
||||
assertEquals(ctx.params, {});
|
||||
assertEquals(ctx.query, {});
|
||||
assertEquals(ctx.state, {});
|
||||
});
|
||||
|
||||
Deno.test('createEmptyContext: preserves generic type compatibility', () => {
|
||||
interface MyContext
|
||||
extends
|
||||
IContext<{ userId: string }, { id: string }, { verbose: string }> {}
|
||||
|
||||
const req = new Request('http://localhost');
|
||||
const ctx = createEmptyContext<MyContext>(req);
|
||||
|
||||
// All properties exist and are empty
|
||||
assertEquals(ctx.params, {} as MyContext['params']);
|
||||
assertEquals(ctx.query, {} as MyContext['query']);
|
||||
assertEquals(ctx.state, {} as MyContext['state']);
|
||||
assertEquals(ctx.req, req);
|
||||
});
|
||||
118
v0.2.1/src/Utils/__tests__/createRouteMatcher.test.ts
Normal file
118
v0.2.1/src/Utils/__tests__/createRouteMatcher.test.ts
Normal file
@@ -0,0 +1,118 @@
|
||||
import {
|
||||
assert,
|
||||
assertEquals,
|
||||
assertStrictEquals,
|
||||
} from 'https://deno.land/std/assert/mod.ts';
|
||||
import type { IRouteDefinition } from '../../Interfaces/mod.ts';
|
||||
import { createRouteMatcher } from '../../mod.ts';
|
||||
|
||||
// Dummy request
|
||||
const dummyRequest = new Request('http://localhost');
|
||||
|
||||
Deno.test('createRouteMatcher: static route matches and extracts params', () => {
|
||||
const def: IRouteDefinition = { method: 'GET', path: '/users/:id' };
|
||||
const matcher = createRouteMatcher(def);
|
||||
|
||||
const result = matcher(new URL('http://localhost/users/42'), dummyRequest);
|
||||
|
||||
assert(result);
|
||||
assertEquals(result.params, { id: '42' });
|
||||
});
|
||||
|
||||
Deno.test('createRouteMatcher: static route with multiple params', () => {
|
||||
const def: IRouteDefinition = { method: 'GET', path: '/repo/:owner/:name' };
|
||||
const matcher = createRouteMatcher(def);
|
||||
|
||||
const result = matcher(
|
||||
new URL('http://localhost/repo/max/wiki'),
|
||||
dummyRequest,
|
||||
);
|
||||
|
||||
assert(result);
|
||||
assertEquals(result.params, { owner: 'max', name: 'wiki' });
|
||||
});
|
||||
|
||||
Deno.test('createRouteMatcher: static route does not match wrong path', () => {
|
||||
const def: IRouteDefinition = { method: 'GET', path: '/users/:id' };
|
||||
const matcher = createRouteMatcher(def);
|
||||
|
||||
const result = matcher(new URL('http://localhost/posts/42'), dummyRequest);
|
||||
|
||||
assertStrictEquals(result, null);
|
||||
});
|
||||
|
||||
Deno.test('createRouteMatcher: uses custom matcher if provided', () => {
|
||||
const def: IRouteDefinition = {
|
||||
method: 'GET',
|
||||
matcher: (url) => url.pathname === '/ping' ? { params: {} } : null,
|
||||
};
|
||||
const matcher = createRouteMatcher(def);
|
||||
|
||||
const result = matcher(new URL('http://localhost/ping'), dummyRequest);
|
||||
assert(result);
|
||||
assertEquals(result.params, {});
|
||||
});
|
||||
|
||||
Deno.test('createRouteMatcher: extracts single query param', () => {
|
||||
const def: IRouteDefinition = { method: 'GET', path: '/search' };
|
||||
const matcher = createRouteMatcher(def);
|
||||
|
||||
const url = new URL('http://localhost/search?q=deno');
|
||||
const result = matcher(url, dummyRequest);
|
||||
|
||||
assert(result);
|
||||
assertEquals(result.params, {}); // no path params
|
||||
assertEquals(result.query, { q: 'deno' }); // single key → string
|
||||
});
|
||||
|
||||
Deno.test('createRouteMatcher: duplicate query keys become array', () => {
|
||||
const def: IRouteDefinition = { method: 'GET', path: '/tags' };
|
||||
const matcher = createRouteMatcher(def);
|
||||
|
||||
const url = new URL('http://localhost/tags?tag=js&tag=ts&tag=deno');
|
||||
const result = matcher(url, dummyRequest);
|
||||
|
||||
assert(result);
|
||||
assertEquals(result.params, {});
|
||||
assertEquals(result.query, { tag: ['js', 'ts', 'deno'] }); // multi → string[]
|
||||
});
|
||||
|
||||
Deno.test('createRouteMatcher: mix of single and duplicate keys', () => {
|
||||
const def: IRouteDefinition = { method: 'GET', path: '/filter/:type' };
|
||||
const matcher = createRouteMatcher(def);
|
||||
|
||||
const url = new URL('http://localhost/filter/repo?lang=ts&lang=js&page=2');
|
||||
const result = matcher(url, dummyRequest);
|
||||
|
||||
assert(result);
|
||||
assertEquals(result.params, { type: 'repo' });
|
||||
assertEquals(result.query, {
|
||||
lang: ['ts', 'js'], // duplicated
|
||||
page: '2', // single
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test('createRouteMatcher: no query parameters returns empty object', () => {
|
||||
const def: IRouteDefinition = { method: 'GET', path: '/info' };
|
||||
const matcher = createRouteMatcher(def);
|
||||
|
||||
const url = new URL('http://localhost/info');
|
||||
const result = matcher(url, dummyRequest);
|
||||
|
||||
assert(result);
|
||||
assertEquals(result.params, {});
|
||||
assertEquals(result.query, {}); // empty
|
||||
});
|
||||
|
||||
Deno.test('createRouteMatcher: retains array order of duplicate keys', () => {
|
||||
const def: IRouteDefinition = { method: 'GET', path: '/order' };
|
||||
const matcher = createRouteMatcher(def);
|
||||
|
||||
const url = new URL(
|
||||
'http://localhost/order?item=first&item=second&item=third',
|
||||
);
|
||||
const result = matcher(url, dummyRequest);
|
||||
|
||||
assert(result);
|
||||
assertEquals(result.query?.item, ['first', 'second', 'third']);
|
||||
});
|
||||
35
v0.2.1/src/Utils/__tests__/normalizeError.test.ts
Normal file
35
v0.2.1/src/Utils/__tests__/normalizeError.test.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import {
|
||||
assertEquals,
|
||||
assertInstanceOf,
|
||||
} from 'https://deno.land/std/assert/mod.ts';
|
||||
import { normalizeError } from '../normalizeError.ts';
|
||||
|
||||
Deno.test('normalizeError: preserves Error instances', () => {
|
||||
const original = new Error('original');
|
||||
const result = normalizeError(original);
|
||||
|
||||
assertInstanceOf(result, Error);
|
||||
assertEquals(result, original);
|
||||
});
|
||||
|
||||
Deno.test('normalizeError: converts string to Error', () => {
|
||||
const result = normalizeError('something went wrong');
|
||||
|
||||
assertInstanceOf(result, Error);
|
||||
assertEquals(result.message, 'something went wrong');
|
||||
});
|
||||
|
||||
Deno.test('normalizeError: converts number to Error', () => {
|
||||
const result = normalizeError(404);
|
||||
|
||||
assertInstanceOf(result, Error);
|
||||
assertEquals(result.message, '404');
|
||||
});
|
||||
|
||||
Deno.test('normalizeError: converts plain object to Error', () => {
|
||||
const input = { error: true, msg: 'Invalid' };
|
||||
const result = normalizeError(input);
|
||||
|
||||
assertInstanceOf(result, Error);
|
||||
assertEquals(result.message, JSON.stringify(input));
|
||||
});
|
||||
30
v0.2.1/src/Utils/createEmptyContext.ts
Normal file
30
v0.2.1/src/Utils/createEmptyContext.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import type { IContext } from '../Interfaces/mod.ts';
|
||||
import type { Params, Query, State } from '../Types/mod.ts';
|
||||
|
||||
/**
|
||||
* Creates an empty request context suitable for fallback handlers (e.g., 404 or 500 errors).
|
||||
*
|
||||
* This function is primarily intended for cases where no route matched, but a context-compatible
|
||||
* object is still needed to invoke a generic error handler. All context fields are initialized
|
||||
* to their default empty values (`{}` for params, query, and state).
|
||||
*
|
||||
* @template TContext - The expected context type, typically extending `IContext`.
|
||||
* @param req - The original HTTP request object from `Deno.serve()`.
|
||||
* @returns A minimal context object compatible with `TContext`.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* const ctx = createEmptyContext<MyContext>(request);
|
||||
* return httpErrorHandlers[404](ctx);
|
||||
* ```
|
||||
*/
|
||||
export function createEmptyContext<TContext extends IContext = IContext>(
|
||||
req: Request,
|
||||
): TContext {
|
||||
return {
|
||||
req,
|
||||
params: {} as Params,
|
||||
query: {} as Query,
|
||||
state: {} as State,
|
||||
} as TContext;
|
||||
}
|
||||
54
v0.2.1/src/Utils/createRouteMatcher.ts
Normal file
54
v0.2.1/src/Utils/createRouteMatcher.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
// createRouteMatcher.ts
|
||||
|
||||
import {
|
||||
type IRouteDefinition,
|
||||
type IRouteMatch,
|
||||
type IRouteMatcher,
|
||||
isDynamicRouteDefinition,
|
||||
} from '../Interfaces/mod.ts';
|
||||
import type { Params, Query } from '../Types/mod.ts';
|
||||
|
||||
/**
|
||||
* Transforms a route definition into a matcher using Deno's URLPattern API.
|
||||
*
|
||||
* @param def - Static path pattern or custom matcher.
|
||||
* @returns IRouteMatcher that returns `{ params, query }` or `null`.
|
||||
*/
|
||||
export function createRouteMatcher(
|
||||
def: IRouteDefinition,
|
||||
): IRouteMatcher {
|
||||
// 1. Allow users to provide their own matcher
|
||||
if (isDynamicRouteDefinition(def)) {
|
||||
return def.matcher;
|
||||
}
|
||||
|
||||
// 2. Build URLPattern; supports :id, *wildcards, regex groups, etc.
|
||||
const pattern = new URLPattern({ pathname: def.path });
|
||||
|
||||
// 3. The actual matcher closure
|
||||
return (url: URL): IRouteMatch | null => {
|
||||
const result = pattern.exec(url);
|
||||
|
||||
// 3a. Path did not match
|
||||
if (!result) return null;
|
||||
|
||||
// 3b. Extract route params
|
||||
const params: Params = {};
|
||||
for (const [key, value] of Object.entries(result.pathname.groups)) {
|
||||
if (value) {
|
||||
params[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
// 3c. Extract query parameters – keep duplicates as arrays
|
||||
const query: Query = {};
|
||||
for (const key of url.searchParams.keys()) {
|
||||
const values = url.searchParams.getAll(key); // → string[]
|
||||
query[key] = values.length === 1
|
||||
? values[0] // single → "foo"
|
||||
: values; // multi → ["foo","bar"]
|
||||
}
|
||||
|
||||
return { params, query };
|
||||
};
|
||||
}
|
||||
5
v0.2.1/src/Utils/mod.ts
Normal file
5
v0.2.1/src/Utils/mod.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
// deno-coverage-ignore-file
|
||||
|
||||
export { createEmptyContext } from './createEmptyContext.ts';
|
||||
export { createRouteMatcher } from './createRouteMatcher.ts';
|
||||
export { normalizeError } from './normalizeError.ts';
|
||||
30
v0.2.1/src/Utils/normalizeError.ts
Normal file
30
v0.2.1/src/Utils/normalizeError.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Normalizes any thrown value to a proper `Error` instance.
|
||||
*
|
||||
* This is useful when handling unknown thrown values that may be:
|
||||
* - strings (e.g. `throw "oops"`)
|
||||
* - numbers (e.g. `throw 404`)
|
||||
* - objects that are not instances of `Error`
|
||||
*
|
||||
* Ensures that downstream error handling logic always receives a consistent `Error` object.
|
||||
*
|
||||
* @param unknownError - Any value that might have been thrown.
|
||||
* @returns A valid `Error` instance wrapping the original input.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* try {
|
||||
* throw "something went wrong";
|
||||
* } catch (e) {
|
||||
* const err = normalizeError(e);
|
||||
* console.error(err.message); // "something went wrong"
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
export function normalizeError(unknownError: unknown): Error {
|
||||
return unknownError instanceof Error ? unknownError : new Error(
|
||||
typeof unknownError === 'string'
|
||||
? unknownError
|
||||
: JSON.stringify(unknownError),
|
||||
);
|
||||
}
|
||||
87
v0.2.1/src/__bench__/HttpKernel.bench.ts
Normal file
87
v0.2.1/src/__bench__/HttpKernel.bench.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import type { IRouteDefinition } from '../Interfaces/mod.ts';
|
||||
import { HttpKernel } from '../mod.ts';
|
||||
|
||||
const CONCURRENT_REQUESTS = 10000;
|
||||
|
||||
// Deno.bench('Simple request', async (b) => {
|
||||
// const kernel = new HttpKernel();
|
||||
|
||||
// const def: IRouteDefinition = { method: 'GET', path: '/hello' };
|
||||
// kernel.route(def).handle((_ctx) => {
|
||||
// return Promise.resolve(new Response('OK', { status: 200 }));
|
||||
// });
|
||||
// b.start();
|
||||
// await kernel.handle(
|
||||
// new Request('http://localhost/hello', { method: 'GET' }),
|
||||
// );
|
||||
// b.end();
|
||||
// });
|
||||
|
||||
Deno.bench('Simple request (parallel)', async (b) => {
|
||||
const kernel = new HttpKernel();
|
||||
|
||||
const def: IRouteDefinition = { method: 'GET', path: '/hello' };
|
||||
kernel.route(def).handle((_ctx) => {
|
||||
return Promise.resolve(new Response('OK', { status: 200 }));
|
||||
});
|
||||
|
||||
const requests = Array.from(
|
||||
{ length: CONCURRENT_REQUESTS },
|
||||
() =>
|
||||
kernel.handle(
|
||||
new Request('http://localhost/hello', { method: 'GET' }),
|
||||
),
|
||||
);
|
||||
|
||||
b.start();
|
||||
await Promise.all(requests);
|
||||
b.end();
|
||||
});
|
||||
|
||||
// Deno.bench('Complex request', async (b) => {
|
||||
// const kernel = new HttpKernel();
|
||||
|
||||
// kernel.route({ method: 'GET', path: '/test' })
|
||||
// .middleware(async (_ctx, next) => {
|
||||
// return await next();
|
||||
// })
|
||||
// .middleware(async (_ctx, next) => {
|
||||
// return await next();
|
||||
// })
|
||||
// .handle((_ctx) => {
|
||||
// return Promise.resolve(new Response('done'));
|
||||
// });
|
||||
|
||||
// b.start();
|
||||
// await kernel.handle(
|
||||
// new Request('http://localhost/test', { method: 'GET' }),
|
||||
// );
|
||||
// b.end();
|
||||
// });
|
||||
|
||||
Deno.bench('Complex request (parallel)', async (b) => {
|
||||
const kernel = new HttpKernel();
|
||||
|
||||
kernel.route({ method: 'GET', path: '/test' })
|
||||
.middleware(async (_ctx, next) => {
|
||||
return await next();
|
||||
})
|
||||
.middleware(async (_ctx, next) => {
|
||||
return await next();
|
||||
})
|
||||
.handle((_ctx) => {
|
||||
return Promise.resolve(new Response('done'));
|
||||
});
|
||||
|
||||
const requests = Array.from(
|
||||
{ length: CONCURRENT_REQUESTS },
|
||||
() =>
|
||||
kernel.handle(
|
||||
new Request('http://localhost/test', { method: 'GET' }),
|
||||
),
|
||||
);
|
||||
|
||||
b.start();
|
||||
await Promise.all(requests);
|
||||
b.end();
|
||||
});
|
||||
185
v0.2.1/src/__tests__/HttpKernel.test.ts
Normal file
185
v0.2.1/src/__tests__/HttpKernel.test.ts
Normal file
@@ -0,0 +1,185 @@
|
||||
import {
|
||||
assertEquals,
|
||||
assertThrows,
|
||||
} from 'https://deno.land/std@0.204.0/assert/mod.ts';
|
||||
import { HttpKernel } from '../HttpKernel.ts';
|
||||
import type { IRouteDefinition } from '../Interfaces/mod.ts';
|
||||
|
||||
Deno.test('HttpKernel: matches static route and executes handler', async () => {
|
||||
const kernel = new HttpKernel();
|
||||
|
||||
const def: IRouteDefinition = { method: 'GET', path: '/hello' };
|
||||
let called = false;
|
||||
|
||||
kernel.route(def).handle((_ctx) => {
|
||||
called = true;
|
||||
return Promise.resolve(new Response('OK', { status: 200 }));
|
||||
});
|
||||
|
||||
const res = await kernel.handle(
|
||||
new Request('http://localhost/hello', { method: 'GET' }),
|
||||
);
|
||||
assertEquals(res.status, 200);
|
||||
assertEquals(await res.text(), 'OK');
|
||||
assertEquals(called, true);
|
||||
});
|
||||
|
||||
Deno.test('HttpKernel: supports dynamic matcher', async () => {
|
||||
const kernel = new HttpKernel();
|
||||
const def: IRouteDefinition = {
|
||||
method: 'GET',
|
||||
matcher: (url) => url.pathname === '/dyn' ? { params: {} } : null,
|
||||
};
|
||||
|
||||
kernel.route(def).handle((_ctx) =>
|
||||
Promise.resolve(new Response('Dyn', { status: 200 }))
|
||||
);
|
||||
|
||||
const res = await kernel.handle(new Request('http://localhost/dyn'));
|
||||
assertEquals(res.status, 200);
|
||||
assertEquals(await res.text(), 'Dyn');
|
||||
});
|
||||
|
||||
Deno.test('HttpKernel: calls middleware in order and passes to handler', async () => {
|
||||
const kernel = new HttpKernel();
|
||||
const calls: string[] = [];
|
||||
|
||||
kernel.route({ method: 'GET', path: '/test' })
|
||||
.middleware(async (_ctx, next) => {
|
||||
calls.push('mw1');
|
||||
return await next();
|
||||
})
|
||||
.middleware(async (_ctx, next) => {
|
||||
calls.push('mw2');
|
||||
return await next();
|
||||
})
|
||||
.handle((_ctx) => {
|
||||
calls.push('handler');
|
||||
return Promise.resolve(new Response('done'));
|
||||
});
|
||||
|
||||
const res = await kernel.handle(
|
||||
new Request('http://localhost/test', { method: 'GET' }),
|
||||
);
|
||||
assertEquals(await res.text(), 'done');
|
||||
assertEquals(calls, ['mw1', 'mw2', 'handler']);
|
||||
});
|
||||
|
||||
Deno.test('HttpKernel: middleware short-circuits pipeline', async () => {
|
||||
const kernel = new HttpKernel();
|
||||
const calls: string[] = [];
|
||||
|
||||
kernel.route({ method: 'GET', path: '/stop' })
|
||||
.middleware((_ctx, _next) => {
|
||||
calls.push('mw1');
|
||||
return Promise.resolve(new Response('blocked', { status: 403 }));
|
||||
})
|
||||
.middleware((_ctx, _next) => {
|
||||
calls.push('mw2');
|
||||
return Promise.resolve(new Response('should-not-call'));
|
||||
})
|
||||
.handle((_ctx) => {
|
||||
calls.push('handler');
|
||||
return Promise.resolve(new Response('ok'));
|
||||
});
|
||||
|
||||
const res = await kernel.handle(
|
||||
new Request('http://localhost/stop', { method: 'GET' }),
|
||||
);
|
||||
assertEquals(res.status, 403);
|
||||
assertEquals(await res.text(), 'blocked');
|
||||
assertEquals(calls, ['mw1']);
|
||||
});
|
||||
|
||||
Deno.test('HttpKernel: invalid middleware or handler signature throws at compile time', () => {
|
||||
const kernel = new HttpKernel();
|
||||
|
||||
// Middleware with wrong signature (missing ctx, next)
|
||||
assertThrows(
|
||||
() => {
|
||||
kernel.route({ method: 'GET', path: '/bad-mw' })
|
||||
// @ts-expect-error invalid middleware
|
||||
.middleware(() => new Response('invalid'))
|
||||
.handle((_ctx) => Promise.resolve(new Response('ok')));
|
||||
},
|
||||
TypeError,
|
||||
'Middleware at index 0 is not a valid function.',
|
||||
);
|
||||
|
||||
// Handler with wrong signature (no ctx)
|
||||
assertThrows(
|
||||
() => {
|
||||
kernel.route({ method: 'GET', path: '/bad-handler' })
|
||||
.middleware(async (_ctx, next) => await next())
|
||||
// @ts-expect-error invalid handler
|
||||
.handle(() => new Response('invalid'));
|
||||
},
|
||||
TypeError,
|
||||
'Route handler must be a function returning a Promise<Response>.',
|
||||
);
|
||||
});
|
||||
|
||||
Deno.test('HttpKernel: 404 for unmatched route', async () => {
|
||||
const kernel = new HttpKernel();
|
||||
const res = await kernel.handle(new Request('http://localhost/nothing'));
|
||||
assertEquals(res.status, 404);
|
||||
});
|
||||
|
||||
Deno.test('HttpKernel: skips route with wrong method', async () => {
|
||||
const kernel = new HttpKernel();
|
||||
|
||||
kernel.route({ method: 'POST', path: '/only-post' })
|
||||
.handle((_ctx) => Promise.resolve(new Response('nope')));
|
||||
|
||||
const res = await kernel.handle(
|
||||
new Request('http://localhost/only-post', { method: 'GET' }),
|
||||
);
|
||||
assertEquals(res.status, 404);
|
||||
});
|
||||
|
||||
Deno.test('HttpKernel: throws on next() called twice', async () => {
|
||||
const kernel = new HttpKernel();
|
||||
|
||||
kernel.route({ method: 'GET', path: '/bad' })
|
||||
.middleware(async (_ctx, next) => {
|
||||
await next();
|
||||
await next(); // ❌
|
||||
return new Response('should never reach');
|
||||
})
|
||||
.handle((_ctx) => Promise.resolve(new Response('OK')));
|
||||
|
||||
const res = await kernel.handle(new Request('http://localhost/bad'));
|
||||
assertEquals(res.status, 500);
|
||||
assertEquals(await res.text(), 'Internal Server Error');
|
||||
});
|
||||
|
||||
Deno.test('HttpKernel: handler throws → error propagates', async () => {
|
||||
const kernel = new HttpKernel();
|
||||
|
||||
kernel.route({ method: 'GET', path: '/throw' })
|
||||
.handle((_ctx) => {
|
||||
throw new Error('fail!');
|
||||
});
|
||||
|
||||
const res = await kernel.handle(new Request('http://localhost/throw'));
|
||||
assertEquals(res.status, 500);
|
||||
assertEquals(await res.text(), 'Internal Server Error');
|
||||
});
|
||||
|
||||
Deno.test('HttpKernel: returns 500 if no handler or middleware defined', async () => {
|
||||
const kernel = new HttpKernel();
|
||||
|
||||
// Force-manual Registrierung mit `handler: undefined`
|
||||
// Umgehen des Builders zur Simulation dieses Edge-Cases
|
||||
kernel['routes'].push({
|
||||
method: 'GET',
|
||||
matcher: (url) => url.pathname === '/fail' ? { params: {} } : null,
|
||||
middlewares: [],
|
||||
// @ts-expect-error absichtlich ungültiger Handler
|
||||
handler: undefined,
|
||||
});
|
||||
|
||||
const res = await kernel.handle(new Request('http://localhost/fail'));
|
||||
assertEquals(res.status, 500);
|
||||
assertEquals(await res.text(), 'Internal Server Error');
|
||||
});
|
||||
140
v0.2.1/src/__tests__/RouteBuilder.test.ts
Normal file
140
v0.2.1/src/__tests__/RouteBuilder.test.ts
Normal file
@@ -0,0 +1,140 @@
|
||||
import {
|
||||
assert,
|
||||
assertEquals,
|
||||
assertNotEquals,
|
||||
assertThrows,
|
||||
} from 'https://deno.land/std@0.204.0/assert/mod.ts';
|
||||
import type { IInternalRoute, IRouteDefinition } from '../Interfaces/mod.ts';
|
||||
import { RouteBuilder } from '../mod.ts';
|
||||
import type { Handler, Middleware } from '../Types/mod.ts';
|
||||
|
||||
// Dummy objects
|
||||
// deno-lint-ignore require-await
|
||||
const dummyHandler: Handler = async (_) => new Response('ok');
|
||||
// deno-lint-ignore require-await
|
||||
const wrongHandler: Handler = async () => new Response('ok'); // Wrong signature, no ctx
|
||||
const dummyMiddleware: Middleware = async (_, next) => await next();
|
||||
// deno-lint-ignore require-await
|
||||
const wrongMiddleware: Middleware = async () => new Response('ok'); // Wrong signature, no ctx, next
|
||||
const dummyDef: IRouteDefinition = { method: 'GET', path: '/hello' };
|
||||
const dummyMatcher = () => ({ params: {} });
|
||||
|
||||
Deno.test('middleware: throws if middleware signature is wrong', () => {
|
||||
const builder = new RouteBuilder(() => {}, dummyDef);
|
||||
assertThrows(
|
||||
() => builder.middleware(wrongMiddleware).handle(dummyHandler),
|
||||
TypeError,
|
||||
'Middleware at index 0 is not a valid function.',
|
||||
);
|
||||
});
|
||||
|
||||
Deno.test('middleware: single middleware is registered correctly', () => {
|
||||
let registered: IInternalRoute | null = null as IInternalRoute | null;
|
||||
|
||||
const builder = new RouteBuilder((r) => registered = r, dummyDef)
|
||||
.middleware(dummyMiddleware);
|
||||
|
||||
builder.handle(dummyHandler);
|
||||
|
||||
assert(registered);
|
||||
assertEquals(registered?.middlewares.length, 1);
|
||||
assertEquals(registered?.middlewares[0], dummyMiddleware);
|
||||
});
|
||||
|
||||
Deno.test('middleware: middleware is chained immutably', () => {
|
||||
const builder1 = new RouteBuilder(() => {}, dummyDef);
|
||||
const builder2 = builder1.middleware(dummyMiddleware);
|
||||
|
||||
assertNotEquals(builder1, builder2);
|
||||
});
|
||||
|
||||
Deno.test('middleware: preserves order of middleware', () => {
|
||||
const mw1: Middleware = async (_, next) => await next();
|
||||
const mw2: Middleware = async (_, next) => await next();
|
||||
|
||||
let result: IInternalRoute | null = null as IInternalRoute | null;
|
||||
|
||||
const builder = new RouteBuilder((r) => result = r, dummyDef)
|
||||
.middleware(mw1)
|
||||
.middleware(mw2);
|
||||
|
||||
builder.handle(dummyHandler);
|
||||
|
||||
assert(result);
|
||||
assertEquals(result!.middlewares, [mw1, mw2]);
|
||||
});
|
||||
|
||||
Deno.test('handle: throws if handler signature is wrong', () => {
|
||||
const builder = new RouteBuilder(() => {}, dummyDef);
|
||||
assertThrows(
|
||||
() => builder.handle(wrongHandler),
|
||||
TypeError,
|
||||
'Route handler must be a function returning a Promise<Response>.',
|
||||
);
|
||||
});
|
||||
|
||||
Deno.test('handle: uppercases method', () => {
|
||||
let result: IInternalRoute | null = null as IInternalRoute | null;
|
||||
|
||||
new RouteBuilder((r) => result = r, { method: 'POST', path: '/x' })
|
||||
.handle(dummyHandler);
|
||||
|
||||
assertEquals(result?.method, 'POST');
|
||||
});
|
||||
|
||||
Deno.test('handle: works with no middleware', async () => {
|
||||
let route: IInternalRoute | null = null as IInternalRoute | null;
|
||||
|
||||
const builder = new RouteBuilder((r) => route = r, dummyDef);
|
||||
builder.handle(dummyHandler);
|
||||
|
||||
assert(route);
|
||||
assertEquals(route?.middlewares.length, 0);
|
||||
|
||||
const request = new Request('http://localhost');
|
||||
|
||||
const res1 = await route?.handler({
|
||||
req: request,
|
||||
params: {},
|
||||
state: {},
|
||||
query: {},
|
||||
});
|
||||
const res2 = await dummyHandler({
|
||||
req: request,
|
||||
params: {},
|
||||
state: {},
|
||||
query: {},
|
||||
});
|
||||
|
||||
assertEquals(res1?.status, res2?.status);
|
||||
assertEquals(await res1?.text(), await res2?.text());
|
||||
});
|
||||
|
||||
Deno.test('handle: uses custom matcher factory', () => {
|
||||
let called = false;
|
||||
|
||||
const factory = (_def: IRouteDefinition) => {
|
||||
called = true;
|
||||
return dummyMatcher;
|
||||
};
|
||||
|
||||
let route: IInternalRoute | null = null as IInternalRoute | null;
|
||||
|
||||
new RouteBuilder((r) => route = r, dummyDef, [], factory).handle(
|
||||
dummyHandler,
|
||||
);
|
||||
|
||||
assert(called);
|
||||
assert(route);
|
||||
assertEquals(route!.matcher, dummyMatcher);
|
||||
});
|
||||
|
||||
Deno.test('handle: throws if matcher factory throws', () => {
|
||||
const faultyFactory = () => {
|
||||
throw new Error('matcher fail');
|
||||
};
|
||||
|
||||
const builder = new RouteBuilder(() => {}, dummyDef, [], faultyFactory);
|
||||
|
||||
assertThrows(() => builder.handle(dummyHandler), Error, 'matcher fail');
|
||||
});
|
||||
16
v0.2.1/src/mod.ts
Normal file
16
v0.2.1/src/mod.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
// deno-coverage-ignore-file
|
||||
export { HttpKernel } from './HttpKernel.ts';
|
||||
export { RouteBuilder } from './RouteBuilder.ts';
|
||||
export { createRouteMatcher } from './Utils/createRouteMatcher.ts';
|
||||
|
||||
// Errors
|
||||
export * from './Errors/mod.ts';
|
||||
|
||||
// Interfaces
|
||||
export * from './Interfaces/mod.ts';
|
||||
|
||||
// Types
|
||||
export * from './Types/mod.ts';
|
||||
|
||||
// Utils
|
||||
export * from './Utils/mod.ts';
|
||||
Reference in New Issue
Block a user