Cloudflare Docs
Durable Objects
Visit Durable Objects on GitHub
Set theme to dark (⇧+D)

Durable Objects TypeScript Rollup ES modules template

Create Durable Objects TypeScript Rollup using ES modules template.

export class CounterTs {
state: DurableObjectState
constructor(state: DurableObjectState, env: Env) {
this.state = state;
// Handle HTTP requests from clients.
async fetch(request: Request) {
// Apply requested action.
let url = new URL(request.url);
// Durable Object storage is automatically cached in-memory.
// Reading the same key for every request is fast.
// You can also store the value in a class member.
let value: number = await"value") || 0;
switch (url.pathname) {
case "/increment":
case "/decrement":
case "/":
// Serves the current value. No storage calls needed.
return new Response("Not found", {status: 404});
// You do not have to worry about a concurrent request having modified the value in storage.
// "input gates" will automatically protect against unwanted concurrency.
// Read-modify-write is safe.
await"value", value);
return new Response(value.toString());
interface Env {}

Find the full code for this example on GitHub.