import { HttpHandler, HttpRequest, HttpResponse } from "@http4t/core/contract"; import { RouteFailed, WrongRoute } from "./lenses"; import { Route, Routes, ApiFor } from "./routes"; export interface RequestLifecycle { /** * The Router is about to start matching a request * * Router will use the returned request from this function, so the RequestLifecycle can add a request id here, * for example, */ begin(request: HttpRequest): Promise; /** * This was the wrong route * * Router will continue looking for a matching route */ mismatch(request: HttpRequest, routeKey: string, route: Route, reason: WrongRoute): Promise; /** * This route successfully handled the request and returned response. * * The Router will not consider any more routes, and will return the result of this function. */ match(request: HttpRequest, routeKey: string, route: Route, response: HttpResponse): Promise; /** * This was the right route to handle the request, but the request is not valid for this route (e.g. the body is * not serialized correctly). For example because the url was matched to a route but the body was not valid json. * * The Router will not consider any more routes, and will return the result of this function, which would normally * be the response in the reason parameter. You might have a debug RequestLifecycle that adds reason.problems to the * body of the response, and a production handler that returns the unmodified `reason.response`. */ clientError(request: HttpRequest, routeKey: string, route: Route, reason: RouteFailed): Promise; /** * Router has tried every available route, and none matched the request * * Router will return the result of this function, normally a 404 * * A debug RequestLifecycle might set the HttpResponse body to be a list of each route tried and its * WrongRoute.problems. */ noMatchFound(request: HttpRequest): Promise; /** * Either a route or another method in this RequestLifecycle threw an unexpected exception. * * The Router will not consider any more routes, and will return the result of this method, which would usually be * a 500 * * *NB: If this method ever throws an exception the router will print stack trace and return an empty 500* */ serverError(request: HttpRequest, routeKey: string, route: Route, error: any): Promise; } export type RoutingContext = { route: keyof ApiFor; }; export type ApiBuilder = (request: HttpRequest, context: RoutingContext) => Promise>; export declare class Router implements HttpHandler { readonly routes: TRoutes; readonly api: ApiBuilder; readonly lifecycle: RequestLifecycle; private readonly alphaOrderedRoutes; constructor(routes: TRoutes, api: ApiBuilder, lifecycle: RequestLifecycle); handle(originalRequest: HttpRequest): Promise; private executeApiFn; } export declare function buildRouter(routes: TRoutes, api: ApiFor | ApiBuilder, lifecycle?: RequestLifecycle): HttpHandler; //# sourceMappingURL=router.d.ts.map