Router/types.ts

61 lines
1.2 KiB
TypeScript
Executable File

/**
* @module
* BearMetal Router types
*/
/**
* @description a context object for a request
*/
export interface RouterContext {
url: URL;
params: Record<string, string | undefined>;
state: Record<string, unknown>;
pattern: URLPattern;
request: Request;
}
/**
* @description a function that handles incoming requests
*/
export type Handler = (
req: Request,
ctx: RouterContext,
) => Promise<Response> | Response;
/**
* @description a middleware function
*/
export type Middleware = (
req: Request,
ctx: RouterContext,
next: () => Promise<Response>,
) => Promise<Response>;
/**
* @description a route configuration
*/
export interface RouteConfig {
pattern: URLPattern;
handlers: { [method: string]: Handler };
}
/**
* @description a middleware configuration
*/
export interface MiddlewareConfig {
pattern: URLPattern;
handler: Middleware;
path: string;
}
/**
* @description a route configurator
*/
export interface RouteConfigurator {
get(handler: Handler): RouteConfigurator;
post(handler: Handler): RouteConfigurator;
put(handler: Handler): RouteConfigurator;
delete(handler: Handler): RouteConfigurator;
patch(handler: Handler): RouteConfigurator;
options(handler: Handler): RouteConfigurator;
}