@whi/cf-routing - v0.7.0
    Preparing search index...

    Class RouteHandler<E, P, D>Abstract

    Base class for route handlers in WorkerRouter

    Extend this class to create handlers for specific routes. By default, all HTTP methods throw a 405 Method Not Allowed error. Override the methods you want to support.

    Response customization:

    • Use ctx.response to modify status, statusText, or headers before returning data
    • Return a Response object directly for full control
    • Return plain data for default JSON serialization
    class UserHandler extends RouteHandler<Env, { id: string }> {
    async get(ctx) {
    return { userId: ctx.params.id };
    }

    async post(ctx) {
    const body = await ctx.request.json();
    // Customize response
    ctx.response.status = 201;
    ctx.response.headers.set('X-Created-Id', ctx.params.id);
    return { userId: ctx.params.id, created: true, data: body };
    }
    }

    router.defineRouteHandler('/users/:id', UserHandler);

    Type Parameters

    • E extends Env = Env

      Environment type

    • P extends Params = Params

      Route parameters type

    • D = Record<string, any>

      Data type for middleware-set data

    Index

    Constructors

    Properties

    Methods

    Constructors

    Properties

    path: string
    log: Logger

    Methods

    • Define CORS configuration for this route handler. Override this method to enable CORS with dynamic configuration based on the request. The same config is automatically applied to both OPTIONS preflight and actual responses.

      Return undefined to use the router's default CORS config (if any).

      Parameters

      Returns CorsConfig<unknown, unknown> | undefined

      class MyHandler extends RouteHandler<Env> {
      cors(ctx): CorsConfig | undefined {
      const origin = ctx.request.headers.get('Origin');
      if (origin?.endsWith('.myapp.com')) {
      return { origins: origin, credentials: true };
      }
      return undefined; // Use router default or no CORS
      }

      async get(ctx) {
      return { data: 'hello' };
      }
      }

    post