Skip to content
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

Merged
merged 6 commits into from
Apr 22, 2024

Conversation

achie27
Copy link
Contributor

@achie27 achie27 commented Mar 21, 2024

This PR is for #18048.

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh ./bin/configs/*.yaml
    ./bin/utils/export_docs_generators.sh
    
    (For Windows users, please run the script in Git BASH)
    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.
  • File the PR against the correct branch: master (upcoming 7.1.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@@ -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()));
Copy link
Contributor Author

@achie27 achie27 Mar 21, 2024

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

Comment on lines 229 to 248
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
}
);
})
Copy link
Contributor Author

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 :)

@achie27
Copy link
Contributor Author

achie27 commented Mar 21, 2024

CC @macjohnny

@macjohnny
Copy link
Member

@achie27 thanks for your contribution!

@achie27 achie27 requested a review from macjohnny March 30, 2024 12:21
@macjohnny macjohnny merged commit c64d569 into OpenAPITools:master Apr 22, 2024
14 checks passed
@wing328 wing328 added this to the 7.6.0 milestone May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants