openapi-typescript icon indicating copy to clipboard operation
openapi-typescript copied to clipboard

client["/endpoint"].GET style call pattern to enable go-to-definition

Open tonyxiao opened this issue 1 year ago • 3 comments

Description

Navigating from call site to docs (aka generated typing) today is quite inconvenient as vscode is not able to navigate from the path argument parameter to the type def. I would love to be able to use cmd+click to be able to go straight to the generated types for the specific endpoint. This is similar to how trpc would work

Proposal

Before

client.GET('/endpoint', { params: {} })

After

client['/endpoint'].GET({ params: {} })

This way you can cmd+click on the /endpoint part in VSCode to go straight to defintion

We can keep both call styles too (just like TRPC) for backward compat.

Checklist

tonyxiao avatar Jan 30 '24 20:01 tonyxiao

I’m all for better Intellisence and VS Code integration, but how would the runtime work? Would client have to be a getter method? And would this be able to use openapi-typescript’s types as-generated? I’d be curious to see a PoC on how this could work from both a runtime and type inference side, even with a simple schema.

drwpow avatar Feb 14 '24 04:02 drwpow

Sure. This it how it is used.

https://github.com/trpc/trpc/blob/44a1b58a3f21dae230cf290a71d93614d469e63b/examples/next-minimal-starter/src/pages/index.tsx#L8

This is how trpc implements it. I believe it uses native javascript Proxy object to accomplish it with explicitly casted typing

https://github.com/trpc/trpc/blob/44a1b58a3f21dae230cf290a71d93614d469e63b/packages/client/src/createTRPCClient.ts#L126-L145

And I don't see any reason to change openapi-typescript. I believe it should work as is.

tonyxiao avatar Feb 15 '24 07:02 tonyxiao

This is fixed in #1791 / openapi-fetch 0.11.0.

gzm0 avatar Aug 10 '24 07:08 gzm0