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

    Class RouteHandler<E, P>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 this.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(request, env, params) {
    return { userId: params.id };
    }

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

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

    Type Parameters

    • E = any

      Environment type

    • P extends Params = Params

      Route parameters type

    Index

    Constructors

    Properties

    Methods

    Constructors

    Properties

    path: string
    log: Logger
    response: ResponseContext

    Response context for customizing the response. Modify status, statusText, or headers before returning data.

    this.response.status = 201;
    this.response.headers.set('Set-Cookie', 'session=abc123');
    return { created: true };

    Methods

    • Parameters

      • request: Request
      • env: E
      • Optionalparams: P

      Returns Promise<any>

    post

    • post(request: Request, env: E, params?: P): Promise<any>

      Parameters

      • request: Request
      • env: E
      • Optionalparams: P

      Returns Promise<any>

    • Parameters

      • request: Request
      • env: E
      • Optionalparams: P

      Returns Promise<any>

    • Parameters

      • request: Request
      • env: E
      • Optionalparams: P

      Returns Promise<any>

    • Parameters

      • request: Request
      • env: E
      • Optionalparams: P

      Returns Promise<any>