-
-
Notifications
You must be signed in to change notification settings - Fork 528
Consider using codegen instead of generics #1779
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks for suggesting but if you read all the about sections of why this project exists and goals it solves, this entire philosophy is to specifically NOT codegen. To change this would mean rewriting everything. If you need codegen, that’s great! There are lots of other options that provide this. But those are unrelated to this project. |
I'm not talking about swagger-style codegen. I'm talking about metaprogramming as an alternative to generics. The API would still be the same, i.e.
It would just resolve all of the problems that generics are currently causing. |
Yeah I think this |
I understand your frustration; there have been some issues with the last couple releases. But to pose the entire library needs a rewrite and to “cut my losses“ isn’t a fair representation of what this library has achieved over many years of advancements and how many people use it (and is running in production in several stacks; I’m just sorry it doesn’t work for yours at the moment). If you’d like to create your own codegen library there’s always room at the table for more ideas and input! But rewriting this isn’t necessary. A few bugs don’t necessitate a rewrite 🙂. That’s why it’s still at 0.x, mind you. |
Ok, well thanks for the rationale. I'll keep an eye on this project. I really like the idea of this project and I was sold on it, but after playing with it for a few days I've found a few pain points that make it not quite ready for prime time (for my needs, at least) due to the bugs. In the meantime I think I'll probably roll my own API-compatible codegen implementation of |
Actually, looking through the code, I don't think it would need a complete rewrite to achieve what I'm talking about. I think you could dogfood the So, for example, instead of:
You would generate something similar, but not exactly, like this (if petstore yaml were the input):
And then the beauty if this is that not only is the typescript super easy to understand, it fixes all the issues I've been encountering:
I know this is an issue with generics, because if I just extract the types from I don't think this is completely unreasonable, because I might fork the repo this weekend and try my hand at it, I don't think it would take more than a couple hundred lines of code to generate the parts of |
@drwpow I made typed-fetch as a reference implementation in case you missed my comment in the other issue. I could see a future where:
Generates not just the component types, but also the init types and the client interface as well like I've done here: https://github.com/RPGillespie6/typed-fetch/blob/main/examples/petstore-openapi.d.ts#L366 I think that would vastly simplify the implementation of openapi-fetch such that you wouldn't need deeply nested generics and you could use an actual debugger for debugging. It would also eliminate the need for openapi-fetch's index.d.ts and allow you to focus on just the implementation file. |
That’s awesome, thanks for making! I’d be happy to link that in the docs of alternate solutions. Again, this whole project was created as a response to codegen. Yes, generating TypeScript types is, codegen, but it was done so that a generated SDK wouldn’t have to be created. Those tend to be bulky, and slow, and harder to maintain (to your point, they don’t have to be, but if you look at how Swagger codegen has gone, and other codegen projects that have more open issues than this project, you can at least see a pattern). I’m saying this not to discourage you at all—I think you creating this is great, and I want to see you succeed (and prove me wrong!). I want codegen to be in a better place. It’s just that what you’re describing, historically, has been an anti-goal of these libraries from the beginning. And that’s the beauty of open source—just providing more options available to people. |
Sure, I would appreciate that, but if openapi-fetch ever reaches 1.X I'll probably discontinue maintaining typed-fetch just because I'd rather centralize on battle tested tooling that lots of people use vs. fracture the space further
Just curious, but do you consider typed-fetch to be a generated SDK based on what you see in https://github.com/RPGillespie6/typed-fetch/blob/main/examples/petstore-openapi.d.ts? IMO, it's nearly identical to openapi-typescript in what it generates with the addition of some helper types for fetch request and response so Also note the shape of the types in typed-fetch are somewhat arbitrary and could be changed. For example
could be changed to
without affecting the functionality of typed-fetch. I just flattened it to simplify type pick-and-place. |
I wouldn’t personally worry about “fracturing the space;” that’s thinking in product terms. Open Source is meant to explore new ideas, and put more ideas out into the world. The more, the merrier.
Running a CLI command to generate FWIW, |
Ah, but that's not what typed-fetch does nor what I'm proposing. There is only ever 1 codegen step, and it's the step to generate the schema. For openapi-fetch it would be:
For typed-fetch the equivalent command is:
These commands are equivalent, and the only difference between them at the moment is that Both typed-fetch and openapi-fetch use codegen-free static implementations:
|
I guess one argument could be that you don't want to pollute the "pure" openapi-typescript schema with extra types that would only ever be used by openapi-fetch, but you could always gate the extra types behind a flag:
However, I'm currently of the opinion though that extra potentially unused types are not a big deal though because tsc will just ignore/discard them.
Do you have an example "really really large schema" you could share that I could use for testing typed-fetch (and seeing how TS language server behaves, etc)? Actually, I guess I could just generate a bogus huge schema. What would you consider "really really large"? 1000 endpoints? 10000? |
Ok, I generated some bogus petstore schemas: petstore-big-schemas.zip. One has ~1000 endpoints, the other has ~10000 endpoints. With With At any rate though, I would hope it's pretty rare people have http apis with 10000 endpoints (and either way, I couldn't get the TS server to timeout or freeze). |
Description
Consider using codegen instead of generics for openapi-fetch. Generics seem to come with a host of problems and limitations, including:
client.POST
call, I can't read the doc string provided in the openapi spec description field because of genericsPATCH
in intellisense for which there are no pathsYou already have to do a codegen step for
openapi-typescript
, why not just cut out the middleman and do:Then you can do:
I don't think it would be significantly bigger because most of the codegen will be type declarations, overloads, etc which get stripped out. And I'm sure there's some shared code that could be leveraged from
openapi-typescript
Checklist
The text was updated successfully, but these errors were encountered: