createClient
returning a single function
#1376
Replies: 5 comments
-
There’s not a fundamental difference in the proposal vs. the current form. If anything, the proposal is more verbose because in the current version you could just call At the end of the day it just comes down to an opinion, and I personally find the former to be simpler to remember and consume, that’s all. Originally, the separate functions were also easier for TypeScript to introspect, because functions inferring their signatures based on what the previous parameter was is just more difficult. Not impossible, just more work. Today that’s less of a concern as the types have improved. But it’s also worth pointing out the difficulty of certain type structures while the source file is composed in TypeScript, which means the code has to be written in a way that the correct definitions can be inferred (as opposed to a separate TypeScript definition file which is allowed to diverge from the runtime code, something I think we’ll change in the not-so-distant future). As far as the proposal goes I’m not open to revisiting the API format at this time. Or at least not without pointing out some clear advantage to switching beyond personal preference. I’ve converted this into a discussion for others to weigh in on if they’d like. |
Beta Was this translation helpful? Give feedback.
-
One benefit would be to ease of adding some kind of wrapper around the methods.
The:
and
are present in all the functions in my file, and I would like to somehow create a custom client for this. My first iteration (~30 mins) looks like this:
having just 1 generic function would ease such cases, I think. The |
Beta Was this translation helpful? Give feedback.
-
This seems to work pretty well:
|
Beta Was this translation helpful? Give feedback.
-
So I've played with the code some days ago and got to this solution: nicu-chiciuc@a703748 The direction I went to was simplifying as much as possible without changing the interface too much. All the existing tests pass, except for the proxy (since, I'm not sure that's an expected usage for a function) and the test that checks the existence for properties on the returned objects. Some things I figured I noticed:
would be nice to be changed to uppercase so that it wouldn't be necessary to use
I have a feeling that making these separate distinctions would allow more flexibility. The switch is pretty simple, I've done a replacement of I'm not really sure where I should go with these changes. I think that it would be nice to separate the |
Beta Was this translation helpful? Give feedback.
-
Thanks for providing additional context on all that. I wouldn’t mind adding a Again, part of the internal structure was a limitation of TypeScript and composing the runtime and types in the same file. In an upcoming change, I’d like to separate the type definitions from the runtime. Which will clean up a lot of code that was written just to appease TypeScript. But in that change, we could combine all the functions together internally for simplicity without a breaking change, and we could also add that additional |
Beta Was this translation helpful? Give feedback.
-
Description
The current return type for
createClient
returns different functions for all possible methods (GET
,POST
, etc.):https://github.com/drwpow/openapi-typescript/blob/ea65d72f602c013c8fba45836d68e48c37cc7480/packages/openapi-fetch/src/index.ts#L188-L195
The type exported is the following:
These methods have all the same definition. The only difference is the
HttpMethod
applied:https://github.com/drwpow/openapi-typescript/blob/ea65d72f602c013c8fba45836d68e48c37cc7480/packages/openapi-typescript-helpers/index.d.ts#L5-L13
Proposal
Is there a reason for not combining all these methods in one? Something like the following:
A single function (instead of
GET
,POST
, etc.) that accepts amethod
parameter of typeHttpMethod
.Wouldn’t this be easier to implement and use? Or is there something that I am not aware of regarding the current implementation?
Checklist
Beta Was this translation helpful? Give feedback.
All reactions