Skip to content

Instantly share code, notes, and snippets.

@gabrielbauman
gabrielbauman / RpcDurableObject.ts
Last active October 5, 2023 22:20
RPC-style Cloudflare durable objects
/**
* RpcDurableObject is a base class for DurableObjects that want to be called in an RPC style. Derive your object
* from this class and implement your methods as normal methods. There is no need to implement the fetch method. Then,
* use the buildRpcClient function to create a proxy object and call methods on the durable object as if it were a
* local object!
*
* This hides the complexity of calling a Durable Object from a worker and allows you to focus on the business logic
* of your application. It also allows you to use TypeScript to type your RPC methods; your worker and the proxy object
* should both implement the same interface containing the RPC methods.
*