Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typescript-node: support bearer token authentication #4633

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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}}
Expand All @@ -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;
Expand Down Expand Up @@ -101,15 +106,22 @@ export class {{classname}} {
}
{{#hasAuthMethods}}
{{#authMethods}}
{{#isBasic}}
{{#isBasicBasic}}

set username(username: string) {
this.authentications.{{name}}.username = username;
}

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) {
Expand All @@ -119,6 +131,10 @@ export class {{classname}} {
{{/authMethods}}
{{/hasAuthMethods}}

public addInterceptor(interceptor: Interceptor) {
this.interceptors.push(interceptor);
}

{{#operation}}
/**
* {{&notes}}
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down Expand Up @@ -219,4 +232,6 @@ export class VoidAuth implements Authentication {
applyToRequest(_: localVarRequest.Options): void {
// Do nothing
}
}
}
export type Interceptor = (requestOptions: localVarRequest.Options) => (Promise<void> | void);
74 changes: 64 additions & 10 deletions samples/client/petstore/typescript-node/default/api/petApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
42 changes: 36 additions & 6 deletions samples/client/petstore/typescript-node/default/api/storeApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Loading