cloudflare / workers-sdk

⛅️ Home to Wrangler, the CLI for Cloudflare Workers®
https://developers.cloudflare.com/workers/
Apache License 2.0
2.69k stars 702 forks source link

🚀 Feature Request: RPC support in `wrangler types` across multiple scripts #6512

Open helloimalastair opened 2 months ago

helloimalastair commented 2 months ago

Describe the solution

Would it be possible to get the wrangler types command to generate proper types for RPC-enabled bindings, when binding to a Worker/DO that is defined in a different script? For example, Durable Object bindings pointing to a different script are always typed as

interface Env {
    DO_BINDING: DurableObjectNamespace;  /* MyDOClass from some-other-worker */;
}

which means that you cannot call any RPC methods on DO_BINDING stubs. Instead, it could be output as

interface Env {
    DO_BINDING: DurableObjectNamespace<import("../../apps/other-worker/src/index").MyDOClass>;
}

This would require a bit of work for determining the location of multiple Workers at once, and would not work when you do not have the source for all of your Workers.

CarmenPopoviciu commented 2 months ago

cc @andyjessop

threepointone commented 2 months ago

I belive wrangler types does use the type of the class now, so rpc should just work

eg: https://twitter.com/naporin24690/status/1824107980872376642

threepointone commented 2 months ago

We shouldn't be putting undefined though, do you have a repro where that happens?

helloimalastair commented 2 months ago

Ooh, that's why... I was looking in a Pages Project that bound to a DO defined in a separate Worker... The Worker itself does have the correct types.

Would it be possible to generate types across multiple Workers/Pages projects, assuming that you have a reference to the source of them all available locally?

threepointone commented 2 months ago

Yeah if we had a reference to the source it'll be straightforward to generate the type

We should definitely not generate <undefined> there, that seems like a bug anyway

helloimalastair commented 2 months ago

Oh, that's cool, I see in a comment that it does recognize the originating Worker. So it would just need a way to define multiple Workers that it could locate...

Also, my mistake on the <undefined> part. It was based on the default param DurableObjectNamespace, which is undefined, but the actual Env type has no param at all...

Sorry about that!