How to create a binding TS types from RPC worker?
https://developers.cloudflare.com/workers/runtime-apis/bindings/service-bindings/#example--build-your-first-service-binding-using-rpc
Following this typescript example,
wrangler types
creates an interface Env
with type WORKER_B: Fetcher
. This leads to bad typing when I want to call WORKER_B.add()
. How this can be improved?Cloudflare Docs
Service bindings - Runtime APIs · Cloudflare Workers docs
Facilitate Worker-to-Worker communication.
5 Replies
I haven't seen a way to do that in a typegen fashion, but it can be solved by making a edit to the env object in the referencing worker
https://github.com/hntrl/durable-object-rpc-repro/blob/9804eccb6468db2d0363ca86aa1f1e96e63f7c63/packages/workerB/worker-configuration.d.ts#L4
(setting the type parameter of the Fetcher to the class of whatever it is you're trying to access)
GitHub
durable-object-rpc-repro/packages/workerB/worker-configuration.d.ts...
Contribute to hntrl/durable-object-rpc-repro development by creating an account on GitHub.
Can I call a Durable Object from a worker of another wrangler project? I want to separate DO and api-workers that call them into different projects.
So in this example I want to do this:
/DOs/src/index.ts:
/api/src/index.ts:
AFAIK not in the way you're describing. you still need to expose some way to access those stubs through an entrypoint on the parent worker of your objects. (i.e. you can't create a binding to a durable object that lives on another worker)
I have a similar want to what you're describing, and something like this is as close as I've come to what I'm hoping for.
/DOs/src/index.ts
/api/src/index.ts
There's some issues regarding types when using rpc in this fashion (and maybe some inefficiencies, I'm still trying to corner that myself).
https://discord.com/channels/595317990191398933/1341606596186341519
thank you!!!
ofc!