Skip to content

Commit 64ee3fe

Browse files
authored
Added support for async accessToken in typescript-fetch (#9659)
1 parent 3cbc5a8 commit 64ee3fe

File tree

22 files changed

+100
-360
lines changed

22 files changed

+100
-360
lines changed

modules/openapi-generator/src/main/resources/typescript-fetch/apis.mustache

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export class {{classname}} extends runtime.BaseAPI {
167167
{{#isBasicBearer}}
168168
if (this.configuration && this.configuration.accessToken) {
169169
const token = this.configuration.accessToken;
170-
const tokenString = typeof token === 'function' ? token("{{name}}", [{{#scopes}}"{{{scope}}}"{{^-last}}, {{/-last}}{{/scopes}}]) : token;
170+
const tokenString = await token("{{name}}", [{{#scopes}}"{{{scope}}}"{{^-last}}, {{/-last}}{{/scopes}}]);
171171
172172
if (tokenString) {
173173
headerParameters["Authorization"] = `Bearer ${tokenString}`;
@@ -192,11 +192,7 @@ export class {{classname}} extends runtime.BaseAPI {
192192
{{#isOAuth}}
193193
if (this.configuration && this.configuration.accessToken) {
194194
// oauth required
195-
if (typeof this.configuration.accessToken === 'function') {
196-
headerParameters["Authorization"] = this.configuration.accessToken("{{name}}", [{{#scopes}}"{{{scope}}}"{{^-last}}, {{/-last}}{{/scopes}}]);
197-
} else {
198-
headerParameters["Authorization"] = this.configuration.accessToken;
199-
}
195+
headerParameters["Authorization"] = await this.configuration.accessToken("{{name}}", [{{#scopes}}"{{{scope}}}"{{^-last}}, {{/-last}}{{/scopes}}]);
200196
}
201197
202198
{{/isOAuth}}

modules/openapi-generator/src/main/resources/typescript-fetch/runtime.mustache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export interface ConfigurationParameters {
124124
username?: string; // parameter for basic security
125125
password?: string; // parameter for basic security
126126
apiKey?: string | ((name: string) => string); // parameter for apiKey security
127-
accessToken?: string | ((name?: string, scopes?: string[]) => string); // parameter for oauth2 security
127+
accessToken?: string | Promise<string> | ((name?: string, scopes?: string[]) => string | Promise<string>); // parameter for oauth2 security
128128
headers?: HTTPHeaders; //header params we want to use on every request
129129
credentials?: RequestCredentials; //value for the credentials param we want to use on each request
130130
}
@@ -164,10 +164,10 @@ export class Configuration {
164164
return undefined;
165165
}
166166

167-
get accessToken(): ((name: string, scopes?: string[]) => string) | undefined {
167+
get accessToken(): ((name?: string, scopes?: string[]) => string | Promise<string>) | undefined {
168168
const accessToken = this.configuration.accessToken;
169169
if (accessToken) {
170-
return typeof accessToken === 'function' ? accessToken : () => accessToken;
170+
return typeof accessToken === 'function' ? accessToken : async () => accessToken;
171171
}
172172
return undefined;
173173
}

samples/client/petstore/typescript-fetch/builds/default-v3.0/apis/FakeApi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ export class FakeApi extends runtime.BaseAPI {
732732

733733
if (this.configuration && this.configuration.accessToken) {
734734
const token = this.configuration.accessToken;
735-
const tokenString = typeof token === 'function' ? token("bearer_test", []) : token;
735+
const tokenString = await token("bearer_test", []);
736736

737737
if (tokenString) {
738738
headerParameters["Authorization"] = `Bearer ${tokenString}`;

samples/client/petstore/typescript-fetch/builds/default-v3.0/apis/PetApi.ts

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,7 @@ export class PetApi extends runtime.BaseAPI {
8787

8888
if (this.configuration && this.configuration.accessToken) {
8989
// oauth required
90-
if (typeof this.configuration.accessToken === 'function') {
91-
headerParameters["Authorization"] = this.configuration.accessToken("petstore_auth", ["write:pets", "read:pets"]);
92-
} else {
93-
headerParameters["Authorization"] = this.configuration.accessToken;
94-
}
90+
headerParameters["Authorization"] = await this.configuration.accessToken("petstore_auth", ["write:pets", "read:pets"]);
9591
}
9692

9793
const response = await this.request({
@@ -130,11 +126,7 @@ export class PetApi extends runtime.BaseAPI {
130126

131127
if (this.configuration && this.configuration.accessToken) {
132128
// oauth required
133-
if (typeof this.configuration.accessToken === 'function') {
134-
headerParameters["Authorization"] = this.configuration.accessToken("petstore_auth", ["write:pets", "read:pets"]);
135-
} else {
136-
headerParameters["Authorization"] = this.configuration.accessToken;
137-
}
129+
headerParameters["Authorization"] = await this.configuration.accessToken("petstore_auth", ["write:pets", "read:pets"]);
138130
}
139131

140132
const response = await this.request({
@@ -173,11 +165,7 @@ export class PetApi extends runtime.BaseAPI {
173165

174166
if (this.configuration && this.configuration.accessToken) {
175167
// oauth required
176-
if (typeof this.configuration.accessToken === 'function') {
177-
headerParameters["Authorization"] = this.configuration.accessToken("petstore_auth", ["write:pets", "read:pets"]);
178-
} else {
179-
headerParameters["Authorization"] = this.configuration.accessToken;
180-
}
168+
headerParameters["Authorization"] = await this.configuration.accessToken("petstore_auth", ["write:pets", "read:pets"]);
181169
}
182170

183171
const response = await this.request({
@@ -218,11 +206,7 @@ export class PetApi extends runtime.BaseAPI {
218206

219207
if (this.configuration && this.configuration.accessToken) {
220208
// oauth required
221-
if (typeof this.configuration.accessToken === 'function') {
222-
headerParameters["Authorization"] = this.configuration.accessToken("petstore_auth", ["write:pets", "read:pets"]);
223-
} else {
224-
headerParameters["Authorization"] = this.configuration.accessToken;
225-
}
209+
headerParameters["Authorization"] = await this.configuration.accessToken("petstore_auth", ["write:pets", "read:pets"]);
226210
}
227211

228212
const response = await this.request({
@@ -296,11 +280,7 @@ export class PetApi extends runtime.BaseAPI {
296280

297281
if (this.configuration && this.configuration.accessToken) {
298282
// oauth required
299-
if (typeof this.configuration.accessToken === 'function') {
300-
headerParameters["Authorization"] = this.configuration.accessToken("petstore_auth", ["write:pets", "read:pets"]);
301-
} else {
302-
headerParameters["Authorization"] = this.configuration.accessToken;
303-
}
283+
headerParameters["Authorization"] = await this.configuration.accessToken("petstore_auth", ["write:pets", "read:pets"]);
304284
}
305285

306286
const response = await this.request({
@@ -335,11 +315,7 @@ export class PetApi extends runtime.BaseAPI {
335315

336316
if (this.configuration && this.configuration.accessToken) {
337317
// oauth required
338-
if (typeof this.configuration.accessToken === 'function') {
339-
headerParameters["Authorization"] = this.configuration.accessToken("petstore_auth", ["write:pets", "read:pets"]);
340-
} else {
341-
headerParameters["Authorization"] = this.configuration.accessToken;
342-
}
318+
headerParameters["Authorization"] = await this.configuration.accessToken("petstore_auth", ["write:pets", "read:pets"]);
343319
}
344320

345321
const consumes: runtime.Consume[] = [
@@ -396,11 +372,7 @@ export class PetApi extends runtime.BaseAPI {
396372

397373
if (this.configuration && this.configuration.accessToken) {
398374
// oauth required
399-
if (typeof this.configuration.accessToken === 'function') {
400-
headerParameters["Authorization"] = this.configuration.accessToken("petstore_auth", ["write:pets", "read:pets"]);
401-
} else {
402-
headerParameters["Authorization"] = this.configuration.accessToken;
403-
}
375+
headerParameters["Authorization"] = await this.configuration.accessToken("petstore_auth", ["write:pets", "read:pets"]);
404376
}
405377

406378
const consumes: runtime.Consume[] = [
@@ -464,11 +436,7 @@ export class PetApi extends runtime.BaseAPI {
464436

465437
if (this.configuration && this.configuration.accessToken) {
466438
// oauth required
467-
if (typeof this.configuration.accessToken === 'function') {
468-
headerParameters["Authorization"] = this.configuration.accessToken("petstore_auth", ["write:pets", "read:pets"]);
469-
} else {
470-
headerParameters["Authorization"] = this.configuration.accessToken;
471-
}
439+
headerParameters["Authorization"] = await this.configuration.accessToken("petstore_auth", ["write:pets", "read:pets"]);
472440
}
473441

474442
const consumes: runtime.Consume[] = [

samples/client/petstore/typescript-fetch/builds/default-v3.0/runtime.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export interface ConfigurationParameters {
135135
username?: string; // parameter for basic security
136136
password?: string; // parameter for basic security
137137
apiKey?: string | ((name: string) => string); // parameter for apiKey security
138-
accessToken?: string | ((name?: string, scopes?: string[]) => string); // parameter for oauth2 security
138+
accessToken?: string | Promise<string> | ((name?: string, scopes?: string[]) => string | Promise<string>); // parameter for oauth2 security
139139
headers?: HTTPHeaders; //header params we want to use on every request
140140
credentials?: RequestCredentials; //value for the credentials param we want to use on each request
141141
}
@@ -175,10 +175,10 @@ export class Configuration {
175175
return undefined;
176176
}
177177

178-
get accessToken(): ((name: string, scopes?: string[]) => string) | undefined {
178+
get accessToken(): ((name?: string, scopes?: string[]) => string | Promise<string>) | undefined {
179179
const accessToken = this.configuration.accessToken;
180180
if (accessToken) {
181-
return typeof accessToken === 'function' ? accessToken : () => accessToken;
181+
return typeof accessToken === 'function' ? accessToken : async () => accessToken;
182182
}
183183
return undefined;
184184
}

samples/client/petstore/typescript-fetch/builds/default/apis/PetApi.ts

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,7 @@ export class PetApi extends runtime.BaseAPI {
8181

8282
if (this.configuration && this.configuration.accessToken) {
8383
// oauth required
84-
if (typeof this.configuration.accessToken === 'function') {
85-
headerParameters["Authorization"] = this.configuration.accessToken("petstore_auth", ["write:pets", "read:pets"]);
86-
} else {
87-
headerParameters["Authorization"] = this.configuration.accessToken;
88-
}
84+
headerParameters["Authorization"] = await this.configuration.accessToken("petstore_auth", ["write:pets", "read:pets"]);
8985
}
9086

9187
const response = await this.request({
@@ -124,11 +120,7 @@ export class PetApi extends runtime.BaseAPI {
124120

125121
if (this.configuration && this.configuration.accessToken) {
126122
// oauth required
127-
if (typeof this.configuration.accessToken === 'function') {
128-
headerParameters["Authorization"] = this.configuration.accessToken("petstore_auth", ["write:pets", "read:pets"]);
129-
} else {
130-
headerParameters["Authorization"] = this.configuration.accessToken;
131-
}
123+
headerParameters["Authorization"] = await this.configuration.accessToken("petstore_auth", ["write:pets", "read:pets"]);
132124
}
133125

134126
const response = await this.request({
@@ -167,11 +159,7 @@ export class PetApi extends runtime.BaseAPI {
167159

168160
if (this.configuration && this.configuration.accessToken) {
169161
// oauth required
170-
if (typeof this.configuration.accessToken === 'function') {
171-
headerParameters["Authorization"] = this.configuration.accessToken("petstore_auth", ["write:pets", "read:pets"]);
172-
} else {
173-
headerParameters["Authorization"] = this.configuration.accessToken;
174-
}
162+
headerParameters["Authorization"] = await this.configuration.accessToken("petstore_auth", ["write:pets", "read:pets"]);
175163
}
176164

177165
const response = await this.request({
@@ -212,11 +200,7 @@ export class PetApi extends runtime.BaseAPI {
212200

213201
if (this.configuration && this.configuration.accessToken) {
214202
// oauth required
215-
if (typeof this.configuration.accessToken === 'function') {
216-
headerParameters["Authorization"] = this.configuration.accessToken("petstore_auth", ["write:pets", "read:pets"]);
217-
} else {
218-
headerParameters["Authorization"] = this.configuration.accessToken;
219-
}
203+
headerParameters["Authorization"] = await this.configuration.accessToken("petstore_auth", ["write:pets", "read:pets"]);
220204
}
221205

222206
const response = await this.request({
@@ -290,11 +274,7 @@ export class PetApi extends runtime.BaseAPI {
290274

291275
if (this.configuration && this.configuration.accessToken) {
292276
// oauth required
293-
if (typeof this.configuration.accessToken === 'function') {
294-
headerParameters["Authorization"] = this.configuration.accessToken("petstore_auth", ["write:pets", "read:pets"]);
295-
} else {
296-
headerParameters["Authorization"] = this.configuration.accessToken;
297-
}
277+
headerParameters["Authorization"] = await this.configuration.accessToken("petstore_auth", ["write:pets", "read:pets"]);
298278
}
299279

300280
const response = await this.request({
@@ -329,11 +309,7 @@ export class PetApi extends runtime.BaseAPI {
329309

330310
if (this.configuration && this.configuration.accessToken) {
331311
// oauth required
332-
if (typeof this.configuration.accessToken === 'function') {
333-
headerParameters["Authorization"] = this.configuration.accessToken("petstore_auth", ["write:pets", "read:pets"]);
334-
} else {
335-
headerParameters["Authorization"] = this.configuration.accessToken;
336-
}
312+
headerParameters["Authorization"] = await this.configuration.accessToken("petstore_auth", ["write:pets", "read:pets"]);
337313
}
338314

339315
const consumes: runtime.Consume[] = [
@@ -390,11 +366,7 @@ export class PetApi extends runtime.BaseAPI {
390366

391367
if (this.configuration && this.configuration.accessToken) {
392368
// oauth required
393-
if (typeof this.configuration.accessToken === 'function') {
394-
headerParameters["Authorization"] = this.configuration.accessToken("petstore_auth", ["write:pets", "read:pets"]);
395-
} else {
396-
headerParameters["Authorization"] = this.configuration.accessToken;
397-
}
369+
headerParameters["Authorization"] = await this.configuration.accessToken("petstore_auth", ["write:pets", "read:pets"]);
398370
}
399371

400372
const consumes: runtime.Consume[] = [

samples/client/petstore/typescript-fetch/builds/default/runtime.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export interface ConfigurationParameters {
135135
username?: string; // parameter for basic security
136136
password?: string; // parameter for basic security
137137
apiKey?: string | ((name: string) => string); // parameter for apiKey security
138-
accessToken?: string | ((name?: string, scopes?: string[]) => string); // parameter for oauth2 security
138+
accessToken?: string | Promise<string> | ((name?: string, scopes?: string[]) => string | Promise<string>); // parameter for oauth2 security
139139
headers?: HTTPHeaders; //header params we want to use on every request
140140
credentials?: RequestCredentials; //value for the credentials param we want to use on each request
141141
}
@@ -175,10 +175,10 @@ export class Configuration {
175175
return undefined;
176176
}
177177

178-
get accessToken(): ((name: string, scopes?: string[]) => string) | undefined {
178+
get accessToken(): ((name?: string, scopes?: string[]) => string | Promise<string>) | undefined {
179179
const accessToken = this.configuration.accessToken;
180180
if (accessToken) {
181-
return typeof accessToken === 'function' ? accessToken : () => accessToken;
181+
return typeof accessToken === 'function' ? accessToken : async () => accessToken;
182182
}
183183
return undefined;
184184
}

samples/client/petstore/typescript-fetch/builds/enum/runtime.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export interface ConfigurationParameters {
135135
username?: string; // parameter for basic security
136136
password?: string; // parameter for basic security
137137
apiKey?: string | ((name: string) => string); // parameter for apiKey security
138-
accessToken?: string | ((name?: string, scopes?: string[]) => string); // parameter for oauth2 security
138+
accessToken?: string | Promise<string> | ((name?: string, scopes?: string[]) => string | Promise<string>); // parameter for oauth2 security
139139
headers?: HTTPHeaders; //header params we want to use on every request
140140
credentials?: RequestCredentials; //value for the credentials param we want to use on each request
141141
}
@@ -175,10 +175,10 @@ export class Configuration {
175175
return undefined;
176176
}
177177

178-
get accessToken(): ((name: string, scopes?: string[]) => string) | undefined {
178+
get accessToken(): ((name?: string, scopes?: string[]) => string | Promise<string>) | undefined {
179179
const accessToken = this.configuration.accessToken;
180180
if (accessToken) {
181-
return typeof accessToken === 'function' ? accessToken : () => accessToken;
181+
return typeof accessToken === 'function' ? accessToken : async () => accessToken;
182182
}
183183
return undefined;
184184
}

0 commit comments

Comments
 (0)