-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
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
New generator: typescript-redux-query #3824
New generator: typescript-redux-query #3824
Conversation
@@ -0,0 +1,292 @@ | |||
/* | |||
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech) | |||
* Copyright 2018 SmartBear Software |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wing328 Since this was copied from the typescript-fetch
version as a starting point, does this still hold true?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know. I'm not an expert in software licensing.
@@ -0,0 +1,32 @@ | |||
#!/bin/sh | |||
|
|||
SCRIPT="$0" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add a windows version of this script in bin/windows
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done and pushed. Haven't tested them yet, I don't have easy access to a windows machine/VM currently.
return "{ [key: string]: " + this.getTypeDeclaration(inner) + "; }"; | ||
} else if (ModelUtils.isFileSchema(p)) { | ||
return "Blob"; | ||
} else if (ModelUtils.isBinarySchema(p)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
overriding with a custom --type-mappings date=string,date-time=string
would not work here I guess, see also #3842
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exploring this. I can definitely see the allure of avoiding the JS Date
and leveraging something like date-fns
or moment.js
instead.
Adding that parameter definitely doesn't work, and still produce Date
typed properties on the various models.
I would ask if this is in scope of this initial PR, or if you'd be ok w/ merging without this pieces working, and then a follow up PR to add this ability.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its ok, I just wanted to point out a potential problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it definitely is, and one I would like to see addressed in the generator at some point.
{{^isListContainer}} | ||
if (requestParameters.{{paramName}} !== undefined) { | ||
{{#isDateTime}} | ||
queryParameters['{{baseName}}'] = (requestParameters.{{paramName}} as any).toISOString(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add null-check
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I'm following this request...
queryParameters
gets initialized on line 52 if we're inside this{{#queryParameters}}
block.- The call to
toISOString()
only occurs inside the{{#isDateTime}}
check.
Is this a risk for "non-required DateTime parameters" that at the TS level get typed as foo?: Date
and about case # 2 here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
requestParameters.{{paramName}}
could be null or undefined, so it would be better to have
if (requestParameters.{{paramName}} !== undefined && requestParameters.{{paramName}} !== null) {
{{/isDateTime}} | ||
{{^isDateTime}} | ||
{{#isDate}} | ||
queryParameters['{{baseName}}'] = (requestParameters.{{paramName}} as any).toISOString().substr(0,10); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add null-check
|
||
{{/consumes}} | ||
{{#consumes.0}} | ||
headerParameters['Content-Type'] = '{{{mediaType}}}'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
support for multiple media types could be added, see #3849
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean around providing a specific Accept
header here? It seems like this code producing the Content-Type
for the request body generated is working as expected, and matches the code in #3849.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, yes, I was talking about the Accept
header.
If you want to add a simple integration test to avoid regressions, see e.g. Line 1074 in f2fe4fc
https://github.com/OpenAPITools/openapi-generator/blob/master/samples/client/petstore/typescript-angular-v7-provided-in-root/pom.xml |
@petejohansonxo do you want to add yourself to the typescript technical committee (https://github.com/OpenAPITools/openapi-generator#62---openapi-generator-technical-committee) to keep an eye on future changes of the typescript-redux-query generator? |
@petejohansonxo any update here? |
@macjohnny Sorry for not getting back to this! I appreciate the review, and will work on addressing the comments this week. |
Hi, apologies for barging in, but I came across this PR just as I was about to give up looking for a good way to integrate Redux with a Swagger specified API, so I'm very excited about it! Unfortunately, I am running in to some errors while trying to build the generated sample:
The first error is easily resolved by adding I'm coming fresh to Typescript, so I'm less clear what can be done about the second error. I suspect there's some configuration I could be missing or another dependency I should install, but I'm not sure what or who. If there's anything else I should try, or any way I could be of help in testing, please let me know! Thanks |
concerning
maybe this could be avoided by switching the two generic parameters, i.e. export interface ReduxQueryDispatch<TEntities = Entities, A extends AnyAction = ReduxQueryAction<TEntities>> { |
@macjohnny your suggested change on @petejohansonxo Any other suggestions on getting through the tsc step with redux-query? Thanks again! |
Yeah, that typescript error is an issue with the original TS generics improvements I'd sent over to the upstream of |
Upstream PR that should address this: amplitude/redux-query#155 |
7dc0534
to
5288926
Compare
With the compilation issues squared away, I've been able to begin integrating the generated client in my project, which is awesome and has allowed me very quickly start sending requests to my API. Thank you! I have a couple of usage questions that might be more obvious to me if I'd spent more time with redux-query and/or TypeScript, so please forgive me if these are naive.
Thanks again for all your hard work on this. Really appreciate the effort! |
@micahcraig Thanks for the testing!
It didn't seem possible to have the generator presume where in the If someone has some ideas for how you might automate those decisions in the generator, as maybe an optional mode, I'd be open to it, but it wasn't something we needed. Our usage so far has been to invoke the generator, then do something like (pseudo-code):
So we can benefit from the strong typing for input + output (transform), and we can also use the generator functions to instead pass to the |
@petejohansonxo do you consider this ready to be merged so that an initial version can be released? |
@petejohansonxo can you please merge the current master? |
5288926
to
337a0c2
Compare
@macjohnny Merged up with |
If the CI completes successfully I will merge it |
Got a bit further in my testing, and noticing an issue with generated mutations with multiple optional parameters. In that case, the generated Raw function gets: let queryParameters = null;
queryParameters = {};
if (requestParameters.startDate !== undefined) {
queryParameters['start_date'] = (requestParameters.startDate as any).toISOString();
}
queryParameters = {};
if (requestParameters.endDate !== undefined) {
queryParameters['end_date'] = (requestParameters.endDate as any).toISOString();
}
queryParameters = {};
if (requestParameters.types) {
queryParameters['types'] = requestParameters.types;
} The upshot of which is that only the last included optional parameter is actually included on the request. |
@micahcraig Thanks for the testing. I just pushed a fix for this in a37b9a7 |
@petejohansonxo thanks for the PR, which has been included in the v4.2.2 release: https://twitter.com/oas_generator/status/1201432648544972800 |
PR checklist
./bin/
to update Petstore sample so that CIs can verify the change. (For instance, only need to run./bin/{LANG}-petstore.sh
,./bin/openapi3/{LANG}-petstore.sh
if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in.\bin\windows\
. If contributing template-only or documentation-only changes which will change sample output, be sure to build the project first.master
,4.1.x
,5.0.x
. Default:master
.Description of the PR
This PR creates a new TypeScript generator that generates functions for consuming APIs using the redux-query library. In particular, redux-query operates on the concept of a
QueryConfig
that is an object structure that describes a query/mutation, and then the redux-query library then handles actually making the API requests, managing state of requests in Redux, and processing responses via thetransform
andupdate
callbacks.Given that, the functions exported from this generator don't actually call the operation, but instead generate a
QueryConfig
that describes an API request, and that can be passed to redux-query library from there.Authentiation
The general recommendation for auth of redux-query requests is not to add the auth details "inline" in the QueryConfig objects, but instead use a Redux middleware to update the passed in Actions and update the QueryConfig w/ the necessary auth headers, etc.
To support this, instead of "inlining" the authentication bits, we instead generate code to include in the
meta
property of theQueryConfig
a description of the requested authenticate type for the particular config. Custom middleware can be written by consumers of the generated code to inspect this meta-info at runtime and add the correct auth headers, to theQueryConfig
objects before they get passed into the redux-query library where the requests are actually sent to the API endpoints.My first generator and contribution to this project, so if I missed anything, please let me know.
Thanks!
CC: @TiFu (2017/07) @taxpon (2017/07) @sebastianhaas (2017/07) @kenisteward (2017/07) @Vrolijkx (2017/09) @macjohnny (2018/01) @nicokoenig (2018/09) @topce (2018/10) @akehir (2019/07)