import { Cors } from "./cors.ts"; import { type RouteContext, type RouteDefinition, type RouteResult } from "./define-route.ts"; export interface _RouteMiddleware { (request: Request, response: Response): Promise | Response; } export interface _RouteMatch { route: RouteDefinition; url: URL; result: URLPatternResult; } export type RouteErrorHandler = (error: unknown, request: Request) => unknown; export interface FetchRouterOptions { routes?: RouteDefinition[]; errorHandler?: RouteErrorHandler; /** @unstable */ log?: boolean | _RouteMiddleware; /** @unstable */ cors?: Cors; } export declare function _corsOptionRoute(cors: Cors, router: FetchRouter): RouteDefinition<"*", {}>; /** * `FetchRouter` is a web-native router for routes defined with `defineRoute`. * * ```js * const routes = [defineRoute("..."), defineRoute("..."), defineRoute("...")]; * * const router = new FetchRouter({ routes }); * ``` * * All options to the `FetchRouter` constructor are optional * and you can create a router without any options if you want. * * `routes` are the route definitions you want the router to processes, * the router will handle a request based on the first route that matches. * So order is important. * * `errorHandler(error, request)` is called if a 5xx `HTTPError` is caught, including unknown errors. * It is called with the offending error and the request it is associated with. * * > NOTE: The `errorHandler` could do more in the future, * > like create it's own Response or mutate the existing response. * > This has not been designed and is left open to future development if it becomes important. * * `log` is an **unstable** option to turn on HTTP logging, it can be a boolean or middleware function. It also logs HTTP errors if not already configured through `errorHandler`. * * `cors` is an **unstable** option to apply a {@link CORS} instance to all requests and adds an `OPTIONS` route handler * * @group Routing */ export declare class FetchRouter { routes: RouteDefinition[]; errorHandler?: RouteErrorHandler; _middleware: _RouteMiddleware[]; constructor(options?: FetchRouterOptions); /** * Find each matching route in turn * * ```js * let request = new Request('...') * * for (const match of router.findMatches(request)) { * // do something with the request and/or break the loop * } * ``` */ findMatches(request: Request): Iterable<_RouteMatch>; /** * @internal * * Take an iterator of route matches and convert them into a HTTP Response * by executing the route's handler. * It will return the first route to return a `Response` object * or throw a `HTTPError` if no routes matched. * * ```js * const response = await router.processMatches(request, matches) * ``` */ processMatches(request: Request, matches: Iterable<_RouteMatch>): Promise; /** Execute a route's handler to generate a HTTP `Response` */ processRoute(route: RouteDefinition, base: RouteContext & { params: any; }): RouteResult; /** * Attempt to handle an error thrown from a route's handler, * checking for well-known HTTPError instance or converting unknown errors into one. * The HTTPError is then used to convert the error into a HTTP `Response`. * * If the error is server-based it will trigger the `FetchRouter`'s `errorHandler`. * * ```js * const response = router.handleError(request, new Error("Something went wrong")) * ``` */ handleError(request: Request, error: unknown): Response; /** * @internal * * Apply all configured middleware to a request/response pair in turn */ _appleMiddleware(request: Request, response: Response): Promise; /** * Process all routes and get a HTTP Response. * * ```js * const response = router.getResponse( * new Request('http://localhost/pathname') * ) * ``` * * > NOTE: it would be nice to align this with the Fetch API `fetch` method signature. */ getResponse(request: Request): Promise; } //# sourceMappingURL=fetch-router.d.ts.map