diff --git a/ng-swagger-gen.js b/ng-swagger-gen.js index db072ff..90f4681 100644 --- a/ng-swagger-gen.js +++ b/ng-swagger-gen.js @@ -1167,12 +1167,21 @@ function processServices(swagger, models, options) { descriptor.operationIds ); + var hasBearerAuth = !!(def.security && (def.security.filter(v => v && v.hasOwnProperty('bearer'))).length > 0); + var parameters = def.parameters || []; if (methodParameters) { parameters = parameters.concat(methodParameters); } + var tokenParameter = null; + if (hasBearerAuth) { + var tokenParameterIdx = parameters.findIndex(param => !!(param.in === 'header' && (param.name || '').match(/authorization/i))); + var param = parameters.splice(tokenParameterIdx, 1)[0]; + tokenParameter = { paramName: param.name, paramIn: param.in }; + } + var paramsClass = null; var paramsClassComments = null; if (parameters.length >= minParamsForContainer) { @@ -1197,6 +1206,7 @@ function processServices(swagger, models, options) { var paramDescriptor = { paramName: param.name, paramIn: param.in, + paramPlaceholderVar: 'paramValue_' + paramVar, paramVar: paramVar, paramFullVar: (paramsClass == null ? '' : 'params.') + paramVar, paramRequired: param.required === true || param.in === 'path', @@ -1296,6 +1306,7 @@ function processServices(swagger, models, options) { operationHttpResponseType: '__StrictHttpResponse<' + resultType + '>', operationComments: toComments(docString, 1), operationParameters: operationParameters, + tokenParameter: tokenParameter, operationResponses: operationResponses, }; var modelResult = models[normalizeModelName(removeBrackets(resultType))]; diff --git a/templates/operationResponse.mustache b/templates/operationResponse.mustache index 723717f..7021e5e 100644 --- a/templates/operationResponse.mustache +++ b/templates/operationResponse.mustache @@ -11,17 +11,21 @@ __body = __formData;{{/operationIsMultipart}} {{#operationParameters}}{{>parameter}} {{/operationParameters}} - let req = new HttpRequest( - '{{operationMethod}}', - this.rootUrl + `{{{operationPathExpression}}}`, - __body, - { - headers: __headers, - params: __params, - responseType: '{{{operationResponseType}}}' - }); + return {{#tokenParameter}}this.tokenProvider('{{paramName}}'){{/tokenParameter}}{{^tokenParameter}}__of(null){{/tokenParameter}}.pipe( + __switchMap(_token => { + {{#tokenParameter}}if (_token != null) __headers = __headers.set('{{paramName}}', _token.toString()); + {{/tokenParameter}}let req = new HttpRequest( + '{{operationMethod}}', + this.rootUrl + `{{{operationPathExpression}}}`, + __body, + { + headers: __headers, + params: __params, + responseType: '{{{operationResponseType}}}' + }); - return this.http.request(req).pipe( + return this.http.request(req); + }), __filter(_r => _r instanceof HttpResponse), __map((_r) => { {{#operationIsVoid}}return (_r as HttpResponse).clone({ body: null }) as {{{operationHttpResponseType}}}{{/operationIsVoid diff --git a/templates/service.mustache b/templates/service.mustache index 78fa92b..eae17c2 100644 --- a/templates/service.mustache +++ b/templates/service.mustache @@ -1,11 +1,17 @@ /* tslint:disable */ -import { Injectable } from '@angular/core'; +import { Injectable, InjectionToken, Optional, Inject } from '@angular/core'; import { HttpClient, HttpRequest, HttpResponse, HttpHeaders } from '@angular/common/http'; import { BaseService as __BaseService } from '../base-service'; import { {{ configurationClass }} as __Configuration } from '../{{configurationFile}}'; import { StrictHttpResponse as __StrictHttpResponse } from '../strict-http-response'; -import { Observable as __Observable } from 'rxjs'; -import { map as __map, filter as __filter } from 'rxjs/operators'; +import { Observable as __Observable, of as __of } from 'rxjs'; +import { map as __map, filter as __filter, switchMap as __switchMap } from 'rxjs/operators'; + +interface {{serviceClass}}TokenProvider { + getToken: (name: string) => __Observable; +} + +const {{serviceClass}}TokenProviderInjectionToken = new InjectionToken<{{serviceClass}}TokenProvider>('{{serviceClass}}TokenProviderInjectionToken') {{#serviceDependencies}}import { {{modelClass}} } from '../models/{{modelFile}}'; {{/serviceDependencies}} @@ -15,12 +21,19 @@ import { map as __map, filter as __filter } from 'rxjs/operators'; class {{serviceClass}} extends __BaseService { {{#serviceOperations}}{{>operationEndpoints}}{{/serviceOperations}} + private tokenProvider: (name: string) => __Observable = _ => __of(null); + constructor( config: __Configuration, - http: HttpClient + http: HttpClient, + @Optional() @Inject({{serviceClass}}TokenProviderInjectionToken) tokenProvider: {{serviceClass}}TokenProvider, ) { super(config, http); + if (tokenProvider) { + this.tokenProvider = (v: string) => tokenProvider.getToken(v); + } } + {{#serviceOperations}}{{>operationResponse}}{{>operationBody}}{{/serviceOperations}} } @@ -34,4 +47,4 @@ module {{serviceClass}} { {{/operationParamsClass}}{{/serviceOperations}} } -export { {{serviceClass}} } +export { {{serviceClass}}, {{serviceClass}}TokenProvider, {{serviceClass}}TokenProviderInjectionToken } diff --git a/templates/services.mustache b/templates/services.mustache index b2ed50d..41a8762 100644 --- a/templates/services.mustache +++ b/templates/services.mustache @@ -1,3 +1,3 @@ {{#services}} -export { {{serviceClass}} } from './services/{{serviceFile}}'; +export { {{serviceClass}}, {{serviceClass}}TokenProvider, {{serviceClass}}TokenProviderInjectionToken } from './services/{{serviceFile}}'; {{/services}}