@whi/cf-do-base - v0.1.2
    Preparing search index...

    Class BaseDurableObject<E>

    Base class for Durable Objects with built-in routing capabilities

    This class extends Cloudflare's DurableObject and automatically initializes a DurableObjectRouter instance. Subclasses can define routes in their constructor using the this.router property.

    Basic usage with route handlers

    import { BaseDurableObject } from '@whi/cf-do-base';
    import { DurableObjectRouteHandler } from '@whi/cf-routing';

    class MessageHandler extends DurableObjectRouteHandler {
    async post(request) {
    const { message } = await request.json();
    await this.ctx.storage.put('message', message);
    return { success: true };
    }
    }

    export class MessageStore extends BaseDurableObject {
    constructor(ctx, env) {
    super(ctx, env, 'message-store');
    this.router.defineRouteHandler('/message', MessageHandler);
    }
    }

    With custom router configuration

    export class CustomDO extends BaseDurableObject {
    constructor(ctx, env) {
    super(ctx, env, 'custom', { base: '/api' });
    this.router.defineRouteHandler('/health', HealthHandler);
    }
    }

    Type Parameters

    • E extends Env

      Environment type extending base Env interface

    Hierarchy

    • DurableObject<E>
      • BaseDurableObject
    Index

    Constructors

    Properties

    Methods

    Constructors

    • Create a new BaseDurableObject instance

      Type Parameters

      Parameters

      • ctx: DurableObjectState

        Durable Object state provided by Cloudflare

      • env: E

        Environment bindings containing KV namespaces, secrets, etc.

      • Optionalname: string

        Optional name for the router (used in logging). Defaults to 'unnamed'

      • Optionalrouter_args: [RouterOptions<unknown, any[]>?]

        Optional arguments passed to the underlying itty-router instance

      Returns BaseDurableObject<E>

      constructor(ctx, env) {
      super(ctx, env, 'my-durable-object');
      }

    Properties

    router: DurableObjectRouter<E>

    The DurableObjectRouter instance handling all requests

    name?: string

    Optional name for the router (used in logging). Defaults to 'unnamed'

    Methods

    • Handle incoming requests

      This method is called by Cloudflare for each HTTP request to the Durable Object. It delegates all request handling to the router.

      Parameters

      • request: Request

        The incoming HTTP request

      Returns Promise<Response>

      A Promise resolving to the HTTP response

      The fetch method is automatically called by Cloudflare Workers. You typically don't need to override this unless you need custom request preprocessing:

      async fetch(request: Request): Promise<Response> {
      // Custom preprocessing
      console.log('Received request:', request.url);

      // Delegate to router
      return super.fetch(request);
      }