-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
[typescript-nestjs] Accept async function for fetching access token #18190
Conversation
modules/openapi-generator/src/main/resources/typescript-nestjs/api.module.mustache
Show resolved
Hide resolved
modules/openapi-generator/src/main/resources/typescript-nestjs/api.service.mustache
Show resolved
Hide resolved
@@ -144,17 +147,16 @@ export class {{classname}} { | |||
{{/isBasicBasic}} | |||
{{#isBasicBearer}} | |||
if (typeof this.configuration.accessToken === 'function') { | |||
headers['Authorization'] = `Bearer ${this.configuration.accessToken()}`; | |||
accessTokenObservable = from(Promise.resolve(this.configuration.accessToken())); |
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.
Encapsulation with Promise.resolve(...)
allows from
to be used irrespective of whether the (returned) accesstoken is a Promise or not
return accessTokenObservable.pipe( | ||
map((accessToken) => { | ||
if(accessToken) { | ||
headers['Authorization'] = `Bearer ${accessToken}`; | ||
} | ||
|
||
return this.httpClient.{{httpMethod}}{{^isResponseFile}}<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>{{/isResponseFile}}(`${this.basePath}{{{path}}}`,{{#isBodyAllowed}} | ||
{{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}{{#hasFormParams}}convertFormParamsToString ? formParams!.toString() : formParams!{{/hasFormParams}}{{^hasFormParams}}null{{/hasFormParams}}{{/bodyParam}},{{/isBodyAllowed}} | ||
{ | ||
{{#hasQueryParams}} | ||
params: queryParameters, | ||
{{/hasQueryParams}} | ||
{{#isResponseFile}} | ||
responseType: "blob", | ||
{{/isResponseFile}} | ||
withCredentials: this.configuration.withCredentials, | ||
headers: headers | ||
} | ||
); | ||
}) |
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 couldn’t find a clean way to combine templating with observables+operators - will be nice if anyone has ideas on how to structure this :)
CC @macjohnny |
modules/openapi-generator/src/main/resources/typescript-nestjs/api.service.mustache
Outdated
Show resolved
Hide resolved
modules/openapi-generator/src/main/resources/typescript-nestjs/api.service.mustache
Outdated
Show resolved
Hide resolved
@achie27 thanks for your contribution! |
This PR is for #18048.
PR checklist
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*
.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
master
(upcoming 7.1.0 minor release - breaking changes with fallbacks),8.0.x
(breaking changes without fallbacks)