Skip to content

Commit 538fdea

Browse files
rasmusfabermacjohnny
authored andcommitted
typescript-node: support bearer token authentication (#4633)
* Make typescript-node support bearer token authentication and add support for intercepting request * Explicit typing of interceptors * Update modules/openapi-generator/src/main/resources/typescript-node/api-single.mustache Formatting Co-Authored-By: Esteban Gehring <esteban.gehring@gmail.com> * Build Petstore
1 parent a839203 commit 538fdea

10 files changed

Lines changed: 407 additions & 64 deletions

File tree

modules/openapi-generator/src/main/resources/typescript-node/api-single.mustache

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import http = require('http');
77
import { {{classname}} } from '../{{filename}}';
88
{{/imports}}
99

10-
import { ObjectSerializer, Authentication, VoidAuth } from '../model/models';
10+
import { ObjectSerializer, Authentication, VoidAuth, Interceptor } from '../model/models';
1111
{{#hasAuthMethods}}
12-
import { HttpBasicAuth, ApiKeyAuth, OAuth } from '../model/models';
12+
import { HttpBasicAuth, HttpBearerAuth, ApiKeyAuth, OAuth } from '../model/models';
1313
{{/hasAuthMethods}}
1414

1515
import { HttpError, RequestFile } from './apis';
@@ -43,9 +43,12 @@ export class {{classname}} {
4343
'default': <Authentication>new VoidAuth(),
4444
{{#hasAuthMethods}}
4545
{{#authMethods}}
46-
{{#isBasic}}
46+
{{#isBasicBasic}}
4747
'{{name}}': new HttpBasicAuth(),
48-
{{/isBasic}}
48+
{{/isBasicBasic}}
49+
{{#isBasicBearer}}
50+
'{{name}}': new HttpBearerAuth(),
51+
{{/isBasicBearer}}
4952
{{#isApiKey}}
5053
'{{name}}': new ApiKeyAuth({{#isKeyInHeader}}'header'{{/isKeyInHeader}}{{#isKeyInQuery}}'query'{{/isKeyInQuery}}{{#isKeyInCookie}}'cookie'{{/isKeyInCookie}}, '{{keyParamName}}'),
5154
{{/isApiKey}}
@@ -56,19 +59,21 @@ export class {{classname}} {
5659
{{/hasAuthMethods}}
5760
}
5861

62+
protected interceptors: Interceptor[] = [];
63+
5964
constructor(basePath?: string);
6065
{{#authMethods}}
61-
{{#isBasic}}
66+
{{#isBasicBasic}}
6267
constructor(username: string, password: string, basePath?: string);
63-
{{/isBasic}}
68+
{{/isBasicBasic}}
6469
{{/authMethods}}
6570
constructor(basePathOrUsername: string, password?: string, basePath?: string) {
6671
if (password) {
6772
{{#authMethods}}
68-
{{#isBasic}}
73+
{{#isBasicBasic}}
6974
this.username = basePathOrUsername;
7075
this.password = password
71-
{{/isBasic}}
76+
{{/isBasicBasic}}
7277
{{/authMethods}}
7378
if (basePath) {
7479
this.basePath = basePath;
@@ -101,15 +106,22 @@ export class {{classname}} {
101106
}
102107
{{#hasAuthMethods}}
103108
{{#authMethods}}
104-
{{#isBasic}}
109+
{{#isBasicBasic}}
110+
105111
set username(username: string) {
106112
this.authentications.{{name}}.username = username;
107113
}
108114

109115
set password(password: string) {
110116
this.authentications.{{name}}.password = password;
111117
}
112-
{{/isBasic}}
118+
{{/isBasicBasic}}
119+
{{#isBasicBearer}}
120+
121+
set accessToken(accessToken: string | (() => string)) {
122+
this.authentications.{{name}}.accessToken = accessToken;
123+
}
124+
{{/isBasicBearer}}
113125
{{#isOAuth}}
114126

115127
set accessToken(token: string) {
@@ -119,6 +131,10 @@ export class {{classname}} {
119131
{{/authMethods}}
120132
{{/hasAuthMethods}}
121133

134+
public addInterceptor(interceptor: Interceptor) {
135+
this.interceptors.push(interceptor);
136+
}
137+
122138
{{#operation}}
123139
/**
124140
* {{&notes}}
@@ -204,7 +220,13 @@ export class {{classname}} {
204220

205221
{{/authMethods}}
206222
authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions));
207-
return authenticationPromise.then(() => {
223+
224+
let interceptorPromise = authenticationPromise;
225+
for (const interceptor of this.interceptors) {
226+
interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions));
227+
}
228+
229+
return interceptorPromise.then(() => {
208230
if (Object.keys(localVarFormParams).length) {
209231
if (localVarUseFormData) {
210232
(<any>localVarRequestOptions).formData = localVarFormParams;

modules/openapi-generator/src/main/resources/typescript-node/models.mustache

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,19 @@ export class HttpBasicAuth implements Authentication {
180180
}
181181
}
182182
183+
export class HttpBearerAuth implements Authentication {
184+
public accessToken: string | (() => string) = '';
185+
186+
applyToRequest(requestOptions: localVarRequest.Options): void {
187+
if (requestOptions && requestOptions.headers) {
188+
const accessToken = typeof this.accessToken === 'function'
189+
? this.accessToken()
190+
: this.accessToken;
191+
requestOptions.headers["Authorization"] = "Bearer " + accessToken;
192+
}
193+
}
194+
}
195+
183196
export class ApiKeyAuth implements Authentication {
184197
public apiKey: string = '';
185198
@@ -219,4 +232,6 @@ export class VoidAuth implements Authentication {
219232
applyToRequest(_: localVarRequest.Options): void {
220233
// Do nothing
221234
}
222-
}
235+
}
236+
237+
export type Interceptor = (requestOptions: localVarRequest.Options) => (Promise<void> | void);

samples/client/petstore/typescript-node/default/api/petApi.ts

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import http = require('http');
1717
import { ApiResponse } from '../model/apiResponse';
1818
import { Pet } from '../model/pet';
1919

20-
import { ObjectSerializer, Authentication, VoidAuth } from '../model/models';
21-
import { HttpBasicAuth, ApiKeyAuth, OAuth } from '../model/models';
20+
import { ObjectSerializer, Authentication, VoidAuth, Interceptor } from '../model/models';
21+
import { HttpBasicAuth, HttpBearerAuth, ApiKeyAuth, OAuth } from '../model/models';
2222

2323
import { HttpError, RequestFile } from './apis';
2424

@@ -43,6 +43,8 @@ export class PetApi {
4343
'api_key': new ApiKeyAuth('header', 'api_key'),
4444
}
4545

46+
protected interceptors: Interceptor[] = [];
47+
4648
constructor(basePath?: string);
4749
constructor(basePathOrUsername: string, password?: string, basePath?: string) {
4850
if (password) {
@@ -80,6 +82,10 @@ export class PetApi {
8082
this.authentications.petstore_auth.accessToken = token;
8183
}
8284

85+
public addInterceptor(interceptor: Interceptor) {
86+
this.interceptors.push(interceptor);
87+
}
88+
8389
/**
8490
*
8591
* @summary Add a new pet to the store
@@ -114,7 +120,13 @@ export class PetApi {
114120
authenticationPromise = authenticationPromise.then(() => this.authentications.petstore_auth.applyToRequest(localVarRequestOptions));
115121

116122
authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions));
117-
return authenticationPromise.then(() => {
123+
124+
let interceptorPromise = authenticationPromise;
125+
for (const interceptor of this.interceptors) {
126+
interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions));
127+
}
128+
129+
return interceptorPromise.then(() => {
118130
if (Object.keys(localVarFormParams).length) {
119131
if (localVarUseFormData) {
120132
(<any>localVarRequestOptions).formData = localVarFormParams;
@@ -173,7 +185,13 @@ export class PetApi {
173185
authenticationPromise = authenticationPromise.then(() => this.authentications.petstore_auth.applyToRequest(localVarRequestOptions));
174186

175187
authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions));
176-
return authenticationPromise.then(() => {
188+
189+
let interceptorPromise = authenticationPromise;
190+
for (const interceptor of this.interceptors) {
191+
interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions));
192+
}
193+
194+
return interceptorPromise.then(() => {
177195
if (Object.keys(localVarFormParams).length) {
178196
if (localVarUseFormData) {
179197
(<any>localVarRequestOptions).formData = localVarFormParams;
@@ -240,7 +258,13 @@ export class PetApi {
240258
authenticationPromise = authenticationPromise.then(() => this.authentications.petstore_auth.applyToRequest(localVarRequestOptions));
241259

242260
authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions));
243-
return authenticationPromise.then(() => {
261+
262+
let interceptorPromise = authenticationPromise;
263+
for (const interceptor of this.interceptors) {
264+
interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions));
265+
}
266+
267+
return interceptorPromise.then(() => {
244268
if (Object.keys(localVarFormParams).length) {
245269
if (localVarUseFormData) {
246270
(<any>localVarRequestOptions).formData = localVarFormParams;
@@ -308,7 +332,13 @@ export class PetApi {
308332
authenticationPromise = authenticationPromise.then(() => this.authentications.petstore_auth.applyToRequest(localVarRequestOptions));
309333

310334
authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions));
311-
return authenticationPromise.then(() => {
335+
336+
let interceptorPromise = authenticationPromise;
337+
for (const interceptor of this.interceptors) {
338+
interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions));
339+
}
340+
341+
return interceptorPromise.then(() => {
312342
if (Object.keys(localVarFormParams).length) {
313343
if (localVarUseFormData) {
314344
(<any>localVarRequestOptions).formData = localVarFormParams;
@@ -373,7 +403,13 @@ export class PetApi {
373403
authenticationPromise = authenticationPromise.then(() => this.authentications.api_key.applyToRequest(localVarRequestOptions));
374404

375405
authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions));
376-
return authenticationPromise.then(() => {
406+
407+
let interceptorPromise = authenticationPromise;
408+
for (const interceptor of this.interceptors) {
409+
interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions));
410+
}
411+
412+
return interceptorPromise.then(() => {
377413
if (Object.keys(localVarFormParams).length) {
378414
if (localVarUseFormData) {
379415
(<any>localVarRequestOptions).formData = localVarFormParams;
@@ -431,7 +467,13 @@ export class PetApi {
431467
authenticationPromise = authenticationPromise.then(() => this.authentications.petstore_auth.applyToRequest(localVarRequestOptions));
432468

433469
authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions));
434-
return authenticationPromise.then(() => {
470+
471+
let interceptorPromise = authenticationPromise;
472+
for (const interceptor of this.interceptors) {
473+
interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions));
474+
}
475+
476+
return interceptorPromise.then(() => {
435477
if (Object.keys(localVarFormParams).length) {
436478
if (localVarUseFormData) {
437479
(<any>localVarRequestOptions).formData = localVarFormParams;
@@ -498,7 +540,13 @@ export class PetApi {
498540
authenticationPromise = authenticationPromise.then(() => this.authentications.petstore_auth.applyToRequest(localVarRequestOptions));
499541

500542
authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions));
501-
return authenticationPromise.then(() => {
543+
544+
let interceptorPromise = authenticationPromise;
545+
for (const interceptor of this.interceptors) {
546+
interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions));
547+
}
548+
549+
return interceptorPromise.then(() => {
502550
if (Object.keys(localVarFormParams).length) {
503551
if (localVarUseFormData) {
504552
(<any>localVarRequestOptions).formData = localVarFormParams;
@@ -573,7 +621,13 @@ export class PetApi {
573621
authenticationPromise = authenticationPromise.then(() => this.authentications.petstore_auth.applyToRequest(localVarRequestOptions));
574622

575623
authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions));
576-
return authenticationPromise.then(() => {
624+
625+
let interceptorPromise = authenticationPromise;
626+
for (const interceptor of this.interceptors) {
627+
interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions));
628+
}
629+
630+
return interceptorPromise.then(() => {
577631
if (Object.keys(localVarFormParams).length) {
578632
if (localVarUseFormData) {
579633
(<any>localVarRequestOptions).formData = localVarFormParams;

samples/client/petstore/typescript-node/default/api/storeApi.ts

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import http = require('http');
1616
/* tslint:disable:no-unused-locals */
1717
import { Order } from '../model/order';
1818

19-
import { ObjectSerializer, Authentication, VoidAuth } from '../model/models';
20-
import { HttpBasicAuth, ApiKeyAuth, OAuth } from '../model/models';
19+
import { ObjectSerializer, Authentication, VoidAuth, Interceptor } from '../model/models';
20+
import { HttpBasicAuth, HttpBearerAuth, ApiKeyAuth, OAuth } from '../model/models';
2121

2222
import { HttpError, RequestFile } from './apis';
2323

@@ -41,6 +41,8 @@ export class StoreApi {
4141
'api_key': new ApiKeyAuth('header', 'api_key'),
4242
}
4343

44+
protected interceptors: Interceptor[] = [];
45+
4446
constructor(basePath?: string);
4547
constructor(basePathOrUsername: string, password?: string, basePath?: string) {
4648
if (password) {
@@ -74,6 +76,10 @@ export class StoreApi {
7476
(this.authentications as any)[StoreApiApiKeys[key]].apiKey = value;
7577
}
7678

79+
public addInterceptor(interceptor: Interceptor) {
80+
this.interceptors.push(interceptor);
81+
}
82+
7783
/**
7884
* For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors
7985
* @summary Delete purchase order by ID
@@ -106,7 +112,13 @@ export class StoreApi {
106112

107113
let authenticationPromise = Promise.resolve();
108114
authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions));
109-
return authenticationPromise.then(() => {
115+
116+
let interceptorPromise = authenticationPromise;
117+
for (const interceptor of this.interceptors) {
118+
interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions));
119+
}
120+
121+
return interceptorPromise.then(() => {
110122
if (Object.keys(localVarFormParams).length) {
111123
if (localVarUseFormData) {
112124
(<any>localVarRequestOptions).formData = localVarFormParams;
@@ -163,7 +175,13 @@ export class StoreApi {
163175
authenticationPromise = authenticationPromise.then(() => this.authentications.api_key.applyToRequest(localVarRequestOptions));
164176

165177
authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions));
166-
return authenticationPromise.then(() => {
178+
179+
let interceptorPromise = authenticationPromise;
180+
for (const interceptor of this.interceptors) {
181+
interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions));
182+
}
183+
184+
return interceptorPromise.then(() => {
167185
if (Object.keys(localVarFormParams).length) {
168186
if (localVarUseFormData) {
169187
(<any>localVarRequestOptions).formData = localVarFormParams;
@@ -226,7 +244,13 @@ export class StoreApi {
226244

227245
let authenticationPromise = Promise.resolve();
228246
authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions));
229-
return authenticationPromise.then(() => {
247+
248+
let interceptorPromise = authenticationPromise;
249+
for (const interceptor of this.interceptors) {
250+
interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions));
251+
}
252+
253+
return interceptorPromise.then(() => {
230254
if (Object.keys(localVarFormParams).length) {
231255
if (localVarUseFormData) {
232256
(<any>localVarRequestOptions).formData = localVarFormParams;
@@ -289,7 +313,13 @@ export class StoreApi {
289313

290314
let authenticationPromise = Promise.resolve();
291315
authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions));
292-
return authenticationPromise.then(() => {
316+
317+
let interceptorPromise = authenticationPromise;
318+
for (const interceptor of this.interceptors) {
319+
interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions));
320+
}
321+
322+
return interceptorPromise.then(() => {
293323
if (Object.keys(localVarFormParams).length) {
294324
if (localVarUseFormData) {
295325
(<any>localVarRequestOptions).formData = localVarFormParams;

0 commit comments

Comments
 (0)