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

    Class DurableObjectRouter<E>

    Router for Cloudflare Durable Objects with class-based handlers and state management

    export class Counter implements DurableObject {
    private router: DurableObjectRouter<Env>;

    constructor(ctx: DurableObjectState, env: Env) {
    this.router = new DurableObjectRouter(ctx, env, 'counter')
    .defineRouteHandler('/count', CounterHandler)
    .defineRouteHandler('/reset', ResetHandler);
    }

    async fetch(request: Request): Promise<Response> {
    return this.router.handle(request);
    }
    }

    Type Parameters

    • E extends Env

      Environment type extending base Env interface

    Index

    Constructors

    • Create a new DurableObjectRouter

      Type Parameters

      Parameters

      • ctx: DurableObjectState

        Durable Object state

      • env: E

        Environment bindings

      • name: string

        Router name for logging

      • ...args: [RouterOptions<unknown, any[]>?]

        Additional arguments passed to itty-router

      Returns DurableObjectRouter<E>

    Properties

    log: Logger

    Logger instance

    name: string

    Router name for logging

    router: RouterType<unknown>

    Underlying itty-router instance

    ctx: DurableObjectState

    Durable Object state

    env: E

    Environment bindings

    Methods

    post

    • Parameters

      • path: string
      • handler_cls: new (
            ...args: [
                path: string,
                ctx: DurableObjectState<unknown>,
                env: Env,
                options?: { log?: Logger },
            ],
        ) => DurableObjectRouteHandler<E>

      Returns DurableObjectRouter<E>

    • Returns RouterType<unknown, any[], unknown>

    • Handle a request using the router

      Automatically builds the router if not already built.

      Parameters

      • request: Request

        The request to handle

      Returns Promise<Response>

      A response promise

      async fetch(request: Request): Promise<Response> {
      return this.router.handle(request);
      }