14 lines
713 B
TypeScript
14 lines
713 B
TypeScript
import type { HttpResponse } from "../contracts/typegen/http/response.ts";
|
|
import type { RequestContext } from "../contracts/typegen/http/request.ts";
|
|
import type { RouteDefinition } from "./router.ts";
|
|
|
|
export type RouteGuard = (context: RequestContext, route: RouteDefinition) => Promise<void | HttpResponse> | void | HttpResponse;
|
|
|
|
export class RoutePipeline {
|
|
private readonly guards: RouteGuard[] = [];
|
|
use(guard: RouteGuard): this { this.guards.push(guard); return this; }
|
|
async run(context: RequestContext, route: RouteDefinition): Promise<HttpResponse | undefined> {
|
|
for (const guard of this.guards) { const result = await guard(context, route); if (result) return result; }
|
|
return undefined;
|
|
}
|
|
}
|