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
This commit is contained in:
rasmusfaber 2019-11-28 12:33:43 +01:00 committed by Esteban Gehring
parent a839203e0d
commit 538fdeafb5
10 changed files with 407 additions and 64 deletions

View File

@ -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': <Authentication>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}}
/**
* {{&notes}}
@ -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) {
(<any>localVarRequestOptions).formData = localVarFormParams;

View File

@ -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
}
}
}
export type Interceptor = (requestOptions: localVarRequest.Options) => (Promise<void> | void);

View File

@ -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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>localVarRequestOptions).formData = localVarFormParams;

View File

@ -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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>localVarRequestOptions).formData = localVarFormParams;

View File

@ -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': <Authentication>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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>localVarRequestOptions).formData = localVarFormParams;

View File

@ -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
}
}
}
export type Interceptor = (requestOptions: localVarRequest.Options) => (Promise<void> | void);

View File

@ -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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>localVarRequestOptions).formData = localVarFormParams;

View File

@ -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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>localVarRequestOptions).formData = localVarFormParams;

View File

@ -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': <Authentication>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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>localVarRequestOptions).formData = localVarFormParams;

View File

@ -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
}
}
}
export type Interceptor = (requestOptions: localVarRequest.Options) => (Promise<void> | void);