26 lines
739 B
TypeScript
26 lines
739 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;
|
|
}
|
|
}
|