{"id":30702257,"url":"https://github.com/iterate-com/safe-durable-objects","last_synced_at":"2025-09-02T14:36:59.489Z","repository":{"id":303212924,"uuid":"1014763861","full_name":"iterate-com/safe-durable-objects","owner":"iterate-com","description":"tRPC-style Safe RPC methods for Cloudflare Durable Objects","archived":false,"fork":false,"pushed_at":"2025-08-06T09:46:36.000Z","size":106,"stargazers_count":18,"open_issues_count":1,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-08-13T02:40:16.369Z","etag":null,"topics":["cloudflare-workers","durable-objects","zod"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/safe-durable-objects","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/iterate-com.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-07-06T11:20:27.000Z","updated_at":"2025-08-08T22:04:29.000Z","dependencies_parsed_at":"2025-08-05T11:32:19.272Z","dependency_job_id":null,"html_url":"https://github.com/iterate-com/safe-durable-objects","commit_stats":null,"previous_names":["blankparticle/safe-durable-objects","iterate-com/safe-durable-objects"],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/iterate-com/safe-durable-objects","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iterate-com%2Fsafe-durable-objects","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iterate-com%2Fsafe-durable-objects/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iterate-com%2Fsafe-durable-objects/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iterate-com%2Fsafe-durable-objects/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iterate-com","download_url":"https://codeload.github.com/iterate-com/safe-durable-objects/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iterate-com%2Fsafe-durable-objects/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273298382,"owners_count":25080562,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-02T02:00:09.530Z","response_time":77,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cloudflare-workers","durable-objects","zod"],"created_at":"2025-09-02T14:36:57.993Z","updated_at":"2025-09-02T14:36:59.474Z","avatar_url":"https://github.com/iterate-com.png","language":"TypeScript","readme":"# Safe Durable Objects\n\n**tRPC-style Safe RPC methods for Cloudflare Durable Objects**\n\n[![npm version](https://badge.fury.io/js/safe-durable-objects.svg)](https://badge.fury.io/js/safe-durable-objects)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\nSafe Durable Objects brings type-safe, validated RPC methods to Cloudflare Durable Objects with a developer experience inspired by tRPC. It uses Zod for runtime validation and provides full TypeScript support.\n\nYou can access the router and schemas via `YourClass.prototype._def` or `YourClass.prototype.route._def` as you would with tRPC. This is extremely powerful as you can convert the schemas to a JSON schema and use them to convert your durable object methods into callable tools for your AI agents.\n\n## Features\n\n- 🔒 **Type-safe**: Full TypeScript support with end-to-end type safety\n- ✅ **Runtime validation**: Input and output validation using Zod schemas\n- 🎯 **tRPC-inspired API**: Familiar developer experience with `.input()`, `.output()`, and `.implement()`\n\n## Installation\n\n```bash\nnpm install safe-durable-objects zod\n# or\npnpm add safe-durable-objects zod\n# or\nyarn add safe-durable-objects zod\n# or\nbun add safe-durable-objects zod\n```\n\nYou'll also need `@cloudflare/workers-types` for TypeScript support:\n\n```bash\nnpm install -D @cloudflare/workers-types\n```\n\n## Quick Start\n\nHere's a complete example of how to use Safe Durable Objects:\n\n```typescript\nimport { z } from \"zod/v4\";\nimport { SafeDurableObjectBuilder } from \"safe-durable-objects\";\nimport { DurableObject } from \"cloudflare:workers\";\n\ntype State = {\n  count: number;\n  lastMessage: string;\n};\n\ntype Env = {\n  MY_DURABLE_OBJECT: DurableObjectNamespace\u003cMyDurableObject\u003e;\n};\n\nexport class MyDurableObject extends SafeDurableObjectBuilder(\n  // This is the base class\n  class extends DurableObject\u003cEnv\u003e {\n    state: State;\n\n    // important: make sure to make the ctx and env public, else you won't be able to access them in the router and typescript will complain\n    constructor(public ctx: DurableObjectState, public env: Env) {\n      super(ctx, env);\n      this.state = {\n        count: 0,\n        lastMessage: \"\",\n      };\n    }\n\n    setState(state: State) {\n      this.state = state;\n    }\n  },\n  (fn) =\u003e ({\n    hello: fn\n      .input(z.string())\n      .output(z.object({ message: z.string(), id: z.string() }))\n      .implement(function ({ ctx, input }) {\n        // You can access the base class methods via `this`\n        const state = this.state;\n        this.setState({\n          count: state.count + 1,\n          lastMessage: input,\n        });\n        return {\n          message: `Hello, ${input}!! state: ${JSON.stringify(state)}`,\n          id: ctx.id.toString(),\n        };\n      }),\n    ping: fn.output(z.object({ message: z.string() })).implement(function () {\n      return {\n        message: \"pong\",\n      };\n    }),\n  })\n) {}\n\nexport default {\n  async fetch(request, env, ctx) {\n    const stub = env.MY_DURABLE_OBJECT.get(\n      env.MY_DURABLE_OBJECT.idFromName(\"test\")\n    );\n    const res = await stub.hello(\"world\");\n    return Response.json(res);\n  },\n} as ExportedHandler\u003cEnv\u003e;\n```\n\n## API Reference\n\n### `SafeDurableObjectBuilder(BaseClass, routerBuilder)`\n\nCreates a new Durable Object class with safe RPC methods.\n\n#### Parameters\n\n- `BaseClass`: Your base Durable Object class\n- `routerBuilder`: A function that receives a route builder and returns an object with your RPC methods\n\n#### Route Builder API\n\nThe route builder provides a fluent API for defining RPC methods:\n\n```typescript\nfn.input(inputSchema).output(outputSchema).implement(handler);\n// or\nfn.input(inputSchema).implement(handler); // output schema is optional\n```\n\n**Note: Only `zod/v4` schemas are supported**\n\n##### `.input(schema)` (optional)\n\nDefines the input validation schema using Zod. The input will be validated at runtime.\n\n##### `.output(schema)` (optional)\n\nDefines the output validation schema using Zod. The output will be validated at runtime.\n\n##### `.implement(handler)`\n\nImplements the actual RPC method logic. The handler receives:\n\n- `ctx`: The DurableObjectState\n- `env`: The environment bindings\n- `input`: The validated input (typed according to your input schema)\n\n**If you use a `function` instead of an arrow function in the implement block, you can access the base class via `this`**\n\n## Examples\n\n### Basic Counter\n\n```typescript\nimport { z } from \"zod/v4\";\nimport { SafeDurableObjectBuilder } from \"safe-durable-objects\";\nimport { DurableObject } from \"cloudflare:workers\";\n\nexport class Counter extends SafeDurableObjectBuilder(\n  class extends DurableObject\u003cEnv\u003e {\n    private count = 0;\n\n    // important: make sure to make the ctx and env public, else you won't be able to access them in the router and typescript will complain\n    constructor(public ctx: DurableObjectState, public env: Env) {\n      super(ctx, env);\n    }\n\n    async getCount() {\n      return this.count;\n    }\n\n    async setCount(value: number) {\n      this.count = value;\n    }\n  },\n  (fn) =\u003e ({\n    increment: fn\n      .input(z.object({ by: z.number().optional().default(1) }))\n      .output(z.object({ count: z.number() }))\n      .implement(async function ({ input }) {\n        const currentCount = await this.getCount();\n        const newCount = currentCount + input.by;\n        await this.setCount(newCount);\n        return { count: newCount };\n      }),\n\n    getCount: fn\n      .input(z.void())\n      .output(z.object({ count: z.number() }))\n      .implement(async function () {\n        const count = await this.getCount();\n        return { count };\n      }),\n  })\n) {}\n```\n\n## Error Handling\n\nSafe Durable Objects automatically handles validation errors. If input validation fails, a `ZodError` will be thrown. If output validation fails, it will also throw a `ZodError`.\n\n```typescript\nconst result = await stub.hello(\"invalid input\").catch((error) =\u003e {\n  /*handle here*/\n});\n```\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n\n## License\n\nMIT © [Iterate](https://iterate.com)\n\n## Support\n\nIf you have any questions or need help, please open an issue on [GitHub](https://github.com/iterate-com/safe-durable-objects).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiterate-com%2Fsafe-durable-objects","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fiterate-com%2Fsafe-durable-objects","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiterate-com%2Fsafe-durable-objects/lists"}