UNPKG

edge-master

Version:
106 lines (105 loc) 4.33 kB
/// <reference types="@cloudflare/workers-types" /> import { IRouteHandler } from './types/route'; import { RequestHandlerArgs, Context, ContextWithReq, IMatcher } from './types/base'; import { IInterceptor } from './types/interceptor'; /** * EdgeController is a class for managing routes and interceptors in Cloudflare Workers. */ export declare class EdgeController { private _reqInterceptors; private _hasReqInterceptor; private _resInterceptors; private _hasResInterceptor; private _routes; private _notFoundHandler?; private _errorHandler?; constructor(); /** * Intercepts incoming requests, applies request interceptors, and returns the modified request. */ private interceptRequest; /** * Intercepts outgoing responses, applies response interceptors, and returns the modified response. */ private interceptResponse; /** * Handles incoming requests by matching routes and executing the appropriate route handler. * If no route matches, it falls back to the custom notFoundHandler or returns a 404 response. */ private handleRoutes; /** * Adds a new route to the router. * @param matcher The route matcher function. * @param routeHandler The route handler to execute when the route matches. * @param priority Optional priority (higher values are matched first, default: 0). */ addRoute(matcher: IMatcher, routeHandler: IRouteHandler, priority?: number): EdgeController; /** * Adds an interceptor to the router. * @param interceptor The interceptor to add. */ addInterceptor(interceptor: IInterceptor): EdgeController; /** * Sets a custom handler for 404 Not Found responses. * @param handler The handler function to execute when no route matches. */ onNotFound(handler: (ctx: ContextWithReq) => Promise<Response>): EdgeController; /** * Sets a custom error handler for uncaught errors. * @param handler The handler function to execute when an error occurs. */ onError(handler: (error: Error, ctx: Context) => Promise<Response>): EdgeController; /** * Adds a GET route. * @param path The path to match (will be matched as path prefix). * @param routeHandler The route handler to execute. */ GET(path: string, routeHandler: IRouteHandler): EdgeController; /** * Adds a POST route. * @param path The path to match (will be matched as path prefix). * @param routeHandler The route handler to execute. */ POST(path: string, routeHandler: IRouteHandler): EdgeController; /** * Adds a PUT route. * @param path The path to match (will be matched as path prefix). * @param routeHandler The route handler to execute. */ PUT(path: string, routeHandler: IRouteHandler): EdgeController; /** * Adds a DELETE route. * @param path The path to match (will be matched as path prefix). * @param routeHandler The route handler to execute. */ DELETE(path: string, routeHandler: IRouteHandler): EdgeController; /** * Adds a PATCH route. * @param path The path to match (will be matched as path prefix). * @param routeHandler The route handler to execute. */ PATCH(path: string, routeHandler: IRouteHandler): EdgeController; /** * Adds a HEAD route. * @param path The path to match (will be matched as path prefix). * @param routeHandler The route handler to execute. */ HEAD(path: string, routeHandler: IRouteHandler): EdgeController; /** * Adds an OPTIONS route. * @param path The path to match (will be matched as path prefix). * @param routeHandler The route handler to execute. */ OPTIONS(path: string, routeHandler: IRouteHandler): EdgeController; /** * Groups routes under a common path prefix. * @param prefix The path prefix for all routes in the group. * @param configure A function that receives a new EdgeController for adding grouped routes. */ group(prefix: string, configure: (group: EdgeController) => void): EdgeController; /** * Handles an incoming request and returns the response. * @param reqCtx The request context. */ handleRequest(reqCtx: RequestHandlerArgs): Promise<Response>; }