Skip to content

Commit

Permalink
[typescript-nestjs] Accept async function for fetching access token (O…
Browse files Browse the repository at this point in the history
…penAPITools#18190)

* feat: starts accepting async function for fetching access token in typescript-nestjs

* fix: adds Provider in the import to fix build error

* chore: deletes package lock

* chore: deletes package lock for v6

* fix: switches to switchMap for map
  • Loading branch information
achie27 authored Apr 22, 2024
1 parent 406d00f commit c64d569
Show file tree
Hide file tree
Showing 9 changed files with 709 additions and 307 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DynamicModule, Module, Global, Provider } from '@nestjs/common';
import { HttpModule, HttpService } from '@nestjs/axios';
{{/useAxiosHttpModule}}
{{^useAxiosHttpModule}}
import { DynamicModule, HttpService, HttpModule, Module, Global } from '@nestjs/common';
import { DynamicModule, HttpService, HttpModule, Module, Global, Provider } from '@nestjs/common';
{{/useAxiosHttpModule}}
import { AsyncConfiguration, Configuration, ConfigurationFactory } from './configuration';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import { HttpService } from '@nestjs/axios';
import { HttpService, Inject, Injectable, Optional } from '@nestjs/common';
{{/useAxiosHttpModule}}
import { AxiosResponse } from 'axios';
import { Observable } from 'rxjs';
import { Observable, from, of, switchMap } from 'rxjs';
{{#imports}}
import { {{classname}} } from '../{{filename}}';
{{/imports}}
import { Configuration } from '../configuration';
import { COLLECTION_FORMATS } from '../variables';
{{#withInterfaces}}
import { {{classname}}Interface } from './{{classFilename}}Interface';
{{/withInterfaces}}
Expand Down Expand Up @@ -117,6 +118,8 @@ export class {{classname}} {
{{/isArray}}
{{/headerParams}}

let accessTokenObservable: Observable<any> = of(null);

{{#authMethods}}
// authentication ({{name}}) required
{{#isApiKey}}
Expand Down Expand Up @@ -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()));
} else if (this.configuration.accessToken) {
headers['Authorization'] = `Bearer ${this.configuration.accessToken}`;
accessTokenObservable = from(Promise.resolve(this.configuration.accessToken));
}
{{/isBasicBearer}}
{{#isOAuth}}
if (this.configuration.accessToken) {
const accessToken = typeof this.configuration.accessToken === 'function'
? this.configuration.accessToken()
: this.configuration.accessToken;
headers['Authorization'] = 'Bearer ' + accessToken;
accessTokenObservable = typeof this.configuration.accessToken === 'function'
? from(Promise.resolve(this.configuration.accessToken()))
: from(Promise.resolve(this.configuration.accessToken))
}

{{/isOAuth}}
Expand Down Expand Up @@ -224,18 +226,26 @@ export class {{classname}} {
{{/formParams}}

{{/hasFormParams}}
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
}
return accessTokenObservable.pipe(
switchMap((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
}
);
})
);
}
{{/operation}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DynamicModule, HttpService, HttpModule, Module, Global } from '@nestjs/common';
import { DynamicModule, HttpService, HttpModule, Module, Global, Provider } from '@nestjs/common';
import { AsyncConfiguration, Configuration, ConfigurationFactory } from './configuration';

import { PetService } from './api/pet.service';
Expand Down
Loading

0 comments on commit c64d569

Please sign in to comment.