diff --git a/modules/openapi-generator/src/main/resources/typescript-node/api-single.mustache b/modules/openapi-generator/src/main/resources/typescript-node/api-single.mustache index 8ecab671c21a..922a654f44b9 100644 --- a/modules/openapi-generator/src/main/resources/typescript-node/api-single.mustache +++ b/modules/openapi-generator/src/main/resources/typescript-node/api-single.mustache @@ -7,9 +7,9 @@ import http = require('http'); import { {{classname}} } from '../{{filename}}'; {{/imports}} -import { ObjectSerializer, Authentication, VoidAuth } from '../model/models'; +import { ObjectSerializer, Authentication, VoidAuth, Interceptor } from '../model/models'; {{#hasAuthMethods}} -import { HttpBasicAuth, ApiKeyAuth, OAuth } from '../model/models'; +import { HttpBasicAuth, HttpBearerAuth, ApiKeyAuth, OAuth } from '../model/models'; {{/hasAuthMethods}} import { HttpError, RequestFile } from './apis'; @@ -43,9 +43,12 @@ export class {{classname}} { 'default': new VoidAuth(), {{#hasAuthMethods}} {{#authMethods}} -{{#isBasic}} +{{#isBasicBasic}} '{{name}}': new HttpBasicAuth(), -{{/isBasic}} +{{/isBasicBasic}} +{{#isBasicBearer}} + '{{name}}': new HttpBearerAuth(), +{{/isBasicBearer}} {{#isApiKey}} '{{name}}': new ApiKeyAuth({{#isKeyInHeader}}'header'{{/isKeyInHeader}}{{#isKeyInQuery}}'query'{{/isKeyInQuery}}{{#isKeyInCookie}}'cookie'{{/isKeyInCookie}}, '{{keyParamName}}'), {{/isApiKey}} @@ -56,19 +59,21 @@ export class {{classname}} { {{/hasAuthMethods}} } + protected interceptors: Interceptor[] = []; + constructor(basePath?: string); {{#authMethods}} -{{#isBasic}} +{{#isBasicBasic}} constructor(username: string, password: string, basePath?: string); -{{/isBasic}} +{{/isBasicBasic}} {{/authMethods}} constructor(basePathOrUsername: string, password?: string, basePath?: string) { if (password) { {{#authMethods}} -{{#isBasic}} +{{#isBasicBasic}} this.username = basePathOrUsername; this.password = password -{{/isBasic}} +{{/isBasicBasic}} {{/authMethods}} if (basePath) { this.basePath = basePath; @@ -101,7 +106,8 @@ export class {{classname}} { } {{#hasAuthMethods}} {{#authMethods}} -{{#isBasic}} +{{#isBasicBasic}} + set username(username: string) { this.authentications.{{name}}.username = username; } @@ -109,7 +115,13 @@ export class {{classname}} { set password(password: string) { this.authentications.{{name}}.password = password; } -{{/isBasic}} +{{/isBasicBasic}} +{{#isBasicBearer}} + + set accessToken(accessToken: string | (() => string)) { + this.authentications.{{name}}.accessToken = accessToken; + } +{{/isBasicBearer}} {{#isOAuth}} set accessToken(token: string) { @@ -119,6 +131,10 @@ export class {{classname}} { {{/authMethods}} {{/hasAuthMethods}} + public addInterceptor(interceptor: Interceptor) { + this.interceptors.push(interceptor); + } + {{#operation}} /** * {{¬es}} @@ -204,7 +220,13 @@ export class {{classname}} { {{/authMethods}} authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; diff --git a/modules/openapi-generator/src/main/resources/typescript-node/models.mustache b/modules/openapi-generator/src/main/resources/typescript-node/models.mustache index 963faeb22942..f129eb63e785 100644 --- a/modules/openapi-generator/src/main/resources/typescript-node/models.mustache +++ b/modules/openapi-generator/src/main/resources/typescript-node/models.mustache @@ -180,6 +180,19 @@ export class HttpBasicAuth implements Authentication { } } +export class HttpBearerAuth implements Authentication { + public accessToken: string | (() => string) = ''; + + applyToRequest(requestOptions: localVarRequest.Options): void { + if (requestOptions && requestOptions.headers) { + const accessToken = typeof this.accessToken === 'function' + ? this.accessToken() + : this.accessToken; + requestOptions.headers["Authorization"] = "Bearer " + accessToken; + } + } +} + export class ApiKeyAuth implements Authentication { public apiKey: string = ''; @@ -219,4 +232,6 @@ export class VoidAuth implements Authentication { applyToRequest(_: localVarRequest.Options): void { // Do nothing } -} \ No newline at end of file +} + +export type Interceptor = (requestOptions: localVarRequest.Options) => (Promise | void); diff --git a/samples/client/petstore/typescript-node/default/api/petApi.ts b/samples/client/petstore/typescript-node/default/api/petApi.ts index e943ad7a5824..c6794b6145d3 100644 --- a/samples/client/petstore/typescript-node/default/api/petApi.ts +++ b/samples/client/petstore/typescript-node/default/api/petApi.ts @@ -17,8 +17,8 @@ import http = require('http'); import { ApiResponse } from '../model/apiResponse'; import { Pet } from '../model/pet'; -import { ObjectSerializer, Authentication, VoidAuth } from '../model/models'; -import { HttpBasicAuth, ApiKeyAuth, OAuth } from '../model/models'; +import { ObjectSerializer, Authentication, VoidAuth, Interceptor } from '../model/models'; +import { HttpBasicAuth, HttpBearerAuth, ApiKeyAuth, OAuth } from '../model/models'; import { HttpError, RequestFile } from './apis'; @@ -43,6 +43,8 @@ export class PetApi { 'api_key': new ApiKeyAuth('header', 'api_key'), } + protected interceptors: Interceptor[] = []; + constructor(basePath?: string); constructor(basePathOrUsername: string, password?: string, basePath?: string) { if (password) { @@ -80,6 +82,10 @@ export class PetApi { this.authentications.petstore_auth.accessToken = token; } + public addInterceptor(interceptor: Interceptor) { + this.interceptors.push(interceptor); + } + /** * * @summary Add a new pet to the store @@ -114,7 +120,13 @@ export class PetApi { authenticationPromise = authenticationPromise.then(() => this.authentications.petstore_auth.applyToRequest(localVarRequestOptions)); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; @@ -173,7 +185,13 @@ export class PetApi { authenticationPromise = authenticationPromise.then(() => this.authentications.petstore_auth.applyToRequest(localVarRequestOptions)); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; @@ -240,7 +258,13 @@ export class PetApi { authenticationPromise = authenticationPromise.then(() => this.authentications.petstore_auth.applyToRequest(localVarRequestOptions)); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; @@ -308,7 +332,13 @@ export class PetApi { authenticationPromise = authenticationPromise.then(() => this.authentications.petstore_auth.applyToRequest(localVarRequestOptions)); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; @@ -373,7 +403,13 @@ export class PetApi { authenticationPromise = authenticationPromise.then(() => this.authentications.api_key.applyToRequest(localVarRequestOptions)); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; @@ -431,7 +467,13 @@ export class PetApi { authenticationPromise = authenticationPromise.then(() => this.authentications.petstore_auth.applyToRequest(localVarRequestOptions)); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; @@ -498,7 +540,13 @@ export class PetApi { authenticationPromise = authenticationPromise.then(() => this.authentications.petstore_auth.applyToRequest(localVarRequestOptions)); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; @@ -573,7 +621,13 @@ export class PetApi { authenticationPromise = authenticationPromise.then(() => this.authentications.petstore_auth.applyToRequest(localVarRequestOptions)); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; diff --git a/samples/client/petstore/typescript-node/default/api/storeApi.ts b/samples/client/petstore/typescript-node/default/api/storeApi.ts index 2acc326108e5..501a3195ab2a 100644 --- a/samples/client/petstore/typescript-node/default/api/storeApi.ts +++ b/samples/client/petstore/typescript-node/default/api/storeApi.ts @@ -16,8 +16,8 @@ import http = require('http'); /* tslint:disable:no-unused-locals */ import { Order } from '../model/order'; -import { ObjectSerializer, Authentication, VoidAuth } from '../model/models'; -import { HttpBasicAuth, ApiKeyAuth, OAuth } from '../model/models'; +import { ObjectSerializer, Authentication, VoidAuth, Interceptor } from '../model/models'; +import { HttpBasicAuth, HttpBearerAuth, ApiKeyAuth, OAuth } from '../model/models'; import { HttpError, RequestFile } from './apis'; @@ -41,6 +41,8 @@ export class StoreApi { 'api_key': new ApiKeyAuth('header', 'api_key'), } + protected interceptors: Interceptor[] = []; + constructor(basePath?: string); constructor(basePathOrUsername: string, password?: string, basePath?: string) { if (password) { @@ -74,6 +76,10 @@ export class StoreApi { (this.authentications as any)[StoreApiApiKeys[key]].apiKey = value; } + public addInterceptor(interceptor: Interceptor) { + this.interceptors.push(interceptor); + } + /** * For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors * @summary Delete purchase order by ID @@ -106,7 +112,13 @@ export class StoreApi { let authenticationPromise = Promise.resolve(); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; @@ -163,7 +175,13 @@ export class StoreApi { authenticationPromise = authenticationPromise.then(() => this.authentications.api_key.applyToRequest(localVarRequestOptions)); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; @@ -226,7 +244,13 @@ export class StoreApi { let authenticationPromise = Promise.resolve(); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; @@ -289,7 +313,13 @@ export class StoreApi { let authenticationPromise = Promise.resolve(); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; diff --git a/samples/client/petstore/typescript-node/default/api/userApi.ts b/samples/client/petstore/typescript-node/default/api/userApi.ts index 6443289f02ec..75c68547809b 100644 --- a/samples/client/petstore/typescript-node/default/api/userApi.ts +++ b/samples/client/petstore/typescript-node/default/api/userApi.ts @@ -16,7 +16,7 @@ import http = require('http'); /* tslint:disable:no-unused-locals */ import { User } from '../model/user'; -import { ObjectSerializer, Authentication, VoidAuth } from '../model/models'; +import { ObjectSerializer, Authentication, VoidAuth, Interceptor } from '../model/models'; import { HttpError, RequestFile } from './apis'; @@ -38,6 +38,8 @@ export class UserApi { 'default': new VoidAuth(), } + protected interceptors: Interceptor[] = []; + constructor(basePath?: string); constructor(basePathOrUsername: string, password?: string, basePath?: string) { if (password) { @@ -71,6 +73,10 @@ export class UserApi { (this.authentications as any)[UserApiApiKeys[key]].apiKey = value; } + public addInterceptor(interceptor: Interceptor) { + this.interceptors.push(interceptor); + } + /** * This can only be done by the logged in user. * @summary Create user @@ -103,7 +109,13 @@ export class UserApi { let authenticationPromise = Promise.resolve(); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; @@ -158,7 +170,13 @@ export class UserApi { let authenticationPromise = Promise.resolve(); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; @@ -213,7 +231,13 @@ export class UserApi { let authenticationPromise = Promise.resolve(); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; @@ -268,7 +292,13 @@ export class UserApi { let authenticationPromise = Promise.resolve(); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; @@ -330,7 +360,13 @@ export class UserApi { let authenticationPromise = Promise.resolve(); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; @@ -406,7 +442,13 @@ export class UserApi { let authenticationPromise = Promise.resolve(); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; @@ -455,7 +497,13 @@ export class UserApi { let authenticationPromise = Promise.resolve(); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; @@ -517,7 +565,13 @@ export class UserApi { let authenticationPromise = Promise.resolve(); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; diff --git a/samples/client/petstore/typescript-node/default/model/models.ts b/samples/client/petstore/typescript-node/default/model/models.ts index 7aea0f15444c..faf72498f767 100644 --- a/samples/client/petstore/typescript-node/default/model/models.ts +++ b/samples/client/petstore/typescript-node/default/model/models.ts @@ -169,6 +169,19 @@ export class HttpBasicAuth implements Authentication { } } +export class HttpBearerAuth implements Authentication { + public accessToken: string | (() => string) = ''; + + applyToRequest(requestOptions: localVarRequest.Options): void { + if (requestOptions && requestOptions.headers) { + const accessToken = typeof this.accessToken === 'function' + ? this.accessToken() + : this.accessToken; + requestOptions.headers["Authorization"] = "Bearer " + accessToken; + } + } +} + export class ApiKeyAuth implements Authentication { public apiKey: string = ''; @@ -208,4 +221,6 @@ export class VoidAuth implements Authentication { applyToRequest(_: localVarRequest.Options): void { // Do nothing } -} \ No newline at end of file +} + +export type Interceptor = (requestOptions: localVarRequest.Options) => (Promise | void); diff --git a/samples/client/petstore/typescript-node/npm/api/petApi.ts b/samples/client/petstore/typescript-node/npm/api/petApi.ts index e943ad7a5824..c6794b6145d3 100644 --- a/samples/client/petstore/typescript-node/npm/api/petApi.ts +++ b/samples/client/petstore/typescript-node/npm/api/petApi.ts @@ -17,8 +17,8 @@ import http = require('http'); import { ApiResponse } from '../model/apiResponse'; import { Pet } from '../model/pet'; -import { ObjectSerializer, Authentication, VoidAuth } from '../model/models'; -import { HttpBasicAuth, ApiKeyAuth, OAuth } from '../model/models'; +import { ObjectSerializer, Authentication, VoidAuth, Interceptor } from '../model/models'; +import { HttpBasicAuth, HttpBearerAuth, ApiKeyAuth, OAuth } from '../model/models'; import { HttpError, RequestFile } from './apis'; @@ -43,6 +43,8 @@ export class PetApi { 'api_key': new ApiKeyAuth('header', 'api_key'), } + protected interceptors: Interceptor[] = []; + constructor(basePath?: string); constructor(basePathOrUsername: string, password?: string, basePath?: string) { if (password) { @@ -80,6 +82,10 @@ export class PetApi { this.authentications.petstore_auth.accessToken = token; } + public addInterceptor(interceptor: Interceptor) { + this.interceptors.push(interceptor); + } + /** * * @summary Add a new pet to the store @@ -114,7 +120,13 @@ export class PetApi { authenticationPromise = authenticationPromise.then(() => this.authentications.petstore_auth.applyToRequest(localVarRequestOptions)); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; @@ -173,7 +185,13 @@ export class PetApi { authenticationPromise = authenticationPromise.then(() => this.authentications.petstore_auth.applyToRequest(localVarRequestOptions)); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; @@ -240,7 +258,13 @@ export class PetApi { authenticationPromise = authenticationPromise.then(() => this.authentications.petstore_auth.applyToRequest(localVarRequestOptions)); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; @@ -308,7 +332,13 @@ export class PetApi { authenticationPromise = authenticationPromise.then(() => this.authentications.petstore_auth.applyToRequest(localVarRequestOptions)); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; @@ -373,7 +403,13 @@ export class PetApi { authenticationPromise = authenticationPromise.then(() => this.authentications.api_key.applyToRequest(localVarRequestOptions)); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; @@ -431,7 +467,13 @@ export class PetApi { authenticationPromise = authenticationPromise.then(() => this.authentications.petstore_auth.applyToRequest(localVarRequestOptions)); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; @@ -498,7 +540,13 @@ export class PetApi { authenticationPromise = authenticationPromise.then(() => this.authentications.petstore_auth.applyToRequest(localVarRequestOptions)); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; @@ -573,7 +621,13 @@ export class PetApi { authenticationPromise = authenticationPromise.then(() => this.authentications.petstore_auth.applyToRequest(localVarRequestOptions)); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; diff --git a/samples/client/petstore/typescript-node/npm/api/storeApi.ts b/samples/client/petstore/typescript-node/npm/api/storeApi.ts index 2acc326108e5..501a3195ab2a 100644 --- a/samples/client/petstore/typescript-node/npm/api/storeApi.ts +++ b/samples/client/petstore/typescript-node/npm/api/storeApi.ts @@ -16,8 +16,8 @@ import http = require('http'); /* tslint:disable:no-unused-locals */ import { Order } from '../model/order'; -import { ObjectSerializer, Authentication, VoidAuth } from '../model/models'; -import { HttpBasicAuth, ApiKeyAuth, OAuth } from '../model/models'; +import { ObjectSerializer, Authentication, VoidAuth, Interceptor } from '../model/models'; +import { HttpBasicAuth, HttpBearerAuth, ApiKeyAuth, OAuth } from '../model/models'; import { HttpError, RequestFile } from './apis'; @@ -41,6 +41,8 @@ export class StoreApi { 'api_key': new ApiKeyAuth('header', 'api_key'), } + protected interceptors: Interceptor[] = []; + constructor(basePath?: string); constructor(basePathOrUsername: string, password?: string, basePath?: string) { if (password) { @@ -74,6 +76,10 @@ export class StoreApi { (this.authentications as any)[StoreApiApiKeys[key]].apiKey = value; } + public addInterceptor(interceptor: Interceptor) { + this.interceptors.push(interceptor); + } + /** * For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors * @summary Delete purchase order by ID @@ -106,7 +112,13 @@ export class StoreApi { let authenticationPromise = Promise.resolve(); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; @@ -163,7 +175,13 @@ export class StoreApi { authenticationPromise = authenticationPromise.then(() => this.authentications.api_key.applyToRequest(localVarRequestOptions)); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; @@ -226,7 +244,13 @@ export class StoreApi { let authenticationPromise = Promise.resolve(); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; @@ -289,7 +313,13 @@ export class StoreApi { let authenticationPromise = Promise.resolve(); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; diff --git a/samples/client/petstore/typescript-node/npm/api/userApi.ts b/samples/client/petstore/typescript-node/npm/api/userApi.ts index 6443289f02ec..75c68547809b 100644 --- a/samples/client/petstore/typescript-node/npm/api/userApi.ts +++ b/samples/client/petstore/typescript-node/npm/api/userApi.ts @@ -16,7 +16,7 @@ import http = require('http'); /* tslint:disable:no-unused-locals */ import { User } from '../model/user'; -import { ObjectSerializer, Authentication, VoidAuth } from '../model/models'; +import { ObjectSerializer, Authentication, VoidAuth, Interceptor } from '../model/models'; import { HttpError, RequestFile } from './apis'; @@ -38,6 +38,8 @@ export class UserApi { 'default': new VoidAuth(), } + protected interceptors: Interceptor[] = []; + constructor(basePath?: string); constructor(basePathOrUsername: string, password?: string, basePath?: string) { if (password) { @@ -71,6 +73,10 @@ export class UserApi { (this.authentications as any)[UserApiApiKeys[key]].apiKey = value; } + public addInterceptor(interceptor: Interceptor) { + this.interceptors.push(interceptor); + } + /** * This can only be done by the logged in user. * @summary Create user @@ -103,7 +109,13 @@ export class UserApi { let authenticationPromise = Promise.resolve(); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; @@ -158,7 +170,13 @@ export class UserApi { let authenticationPromise = Promise.resolve(); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; @@ -213,7 +231,13 @@ export class UserApi { let authenticationPromise = Promise.resolve(); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; @@ -268,7 +292,13 @@ export class UserApi { let authenticationPromise = Promise.resolve(); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; @@ -330,7 +360,13 @@ export class UserApi { let authenticationPromise = Promise.resolve(); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; @@ -406,7 +442,13 @@ export class UserApi { let authenticationPromise = Promise.resolve(); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; @@ -455,7 +497,13 @@ export class UserApi { let authenticationPromise = Promise.resolve(); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; @@ -517,7 +565,13 @@ export class UserApi { let authenticationPromise = Promise.resolve(); authenticationPromise = authenticationPromise.then(() => this.authentications.default.applyToRequest(localVarRequestOptions)); - return authenticationPromise.then(() => { + + let interceptorPromise = authenticationPromise; + for (const interceptor of this.interceptors) { + interceptorPromise = interceptorPromise.then(() => interceptor(localVarRequestOptions)); + } + + return interceptorPromise.then(() => { if (Object.keys(localVarFormParams).length) { if (localVarUseFormData) { (localVarRequestOptions).formData = localVarFormParams; diff --git a/samples/client/petstore/typescript-node/npm/model/models.ts b/samples/client/petstore/typescript-node/npm/model/models.ts index 7aea0f15444c..faf72498f767 100644 --- a/samples/client/petstore/typescript-node/npm/model/models.ts +++ b/samples/client/petstore/typescript-node/npm/model/models.ts @@ -169,6 +169,19 @@ export class HttpBasicAuth implements Authentication { } } +export class HttpBearerAuth implements Authentication { + public accessToken: string | (() => string) = ''; + + applyToRequest(requestOptions: localVarRequest.Options): void { + if (requestOptions && requestOptions.headers) { + const accessToken = typeof this.accessToken === 'function' + ? this.accessToken() + : this.accessToken; + requestOptions.headers["Authorization"] = "Bearer " + accessToken; + } + } +} + export class ApiKeyAuth implements Authentication { public apiKey: string = ''; @@ -208,4 +221,6 @@ export class VoidAuth implements Authentication { applyToRequest(_: localVarRequest.Options): void { // Do nothing } -} \ No newline at end of file +} + +export type Interceptor = (requestOptions: localVarRequest.Options) => (Promise | void);