Skip to content

Commit b5ed444

Browse files
committed
Add injectable token provider for bearer tokens
1 parent 3409792 commit b5ed444

File tree

4 files changed

+44
-16
lines changed

4 files changed

+44
-16
lines changed

ng-swagger-gen.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,12 +1167,21 @@ function processServices(swagger, models, options) {
11671167
descriptor.operationIds
11681168
);
11691169

1170+
var hasBearerAuth = !!(def.security && (def.security.filter(v => v && v.hasOwnProperty('bearer'))).length > 0);
1171+
11701172
var parameters = def.parameters || [];
11711173

11721174
if (methodParameters) {
11731175
parameters = parameters.concat(methodParameters);
11741176
}
11751177

1178+
var tokenParameter = null;
1179+
if (hasBearerAuth) {
1180+
var tokenParameterIdx = parameters.findIndex(param => !!(param.in === 'header' && (param.name || '').match(/authorization/i)));
1181+
var param = parameters.splice(tokenParameterIdx, 1)[0];
1182+
tokenParameter = { paramName: param.name, paramIn: param.in };
1183+
}
1184+
11761185
var paramsClass = null;
11771186
var paramsClassComments = null;
11781187
if (parameters.length >= minParamsForContainer) {
@@ -1197,6 +1206,7 @@ function processServices(swagger, models, options) {
11971206
var paramDescriptor = {
11981207
paramName: param.name,
11991208
paramIn: param.in,
1209+
paramPlaceholderVar: 'paramValue_' + paramVar,
12001210
paramVar: paramVar,
12011211
paramFullVar: (paramsClass == null ? '' : 'params.') + paramVar,
12021212
paramRequired: param.required === true || param.in === 'path',
@@ -1296,6 +1306,7 @@ function processServices(swagger, models, options) {
12961306
operationHttpResponseType: '__StrictHttpResponse<' + resultType + '>',
12971307
operationComments: toComments(docString, 1),
12981308
operationParameters: operationParameters,
1309+
tokenParameter: tokenParameter,
12991310
operationResponses: operationResponses,
13001311
};
13011312
var modelResult = models[normalizeModelName(removeBrackets(resultType))];

templates/operationResponse.mustache

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,21 @@
1111
__body = __formData;{{/operationIsMultipart}}
1212
{{#operationParameters}}{{>parameter}}
1313
{{/operationParameters}}
14-
let req = new HttpRequest<any>(
15-
'{{operationMethod}}',
16-
this.rootUrl + `{{{operationPathExpression}}}`,
17-
__body,
18-
{
19-
headers: __headers,
20-
params: __params,
21-
responseType: '{{{operationResponseType}}}'
22-
});
14+
return {{#tokenParameter}}this.tokenProvider('{{paramName}}'){{/tokenParameter}}{{^tokenParameter}}__of(null){{/tokenParameter}}.pipe(
15+
__switchMap(_token => {
16+
{{#tokenParameter}}if (_token != null) __headers = __headers.set('{{paramName}}', _token.toString());
17+
{{/tokenParameter}}let req = new HttpRequest<any>(
18+
'{{operationMethod}}',
19+
this.rootUrl + `{{{operationPathExpression}}}`,
20+
__body,
21+
{
22+
headers: __headers,
23+
params: __params,
24+
responseType: '{{{operationResponseType}}}'
25+
});
2326

24-
return this.http.request<any>(req).pipe(
27+
return this.http.request<any>(req);
28+
}),
2529
__filter(_r => _r instanceof HttpResponse),
2630
__map((_r) => {
2731
{{#operationIsVoid}}return (_r as HttpResponse<any>).clone({ body: null }) as {{{operationHttpResponseType}}}{{/operationIsVoid

templates/service.mustache

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
/* tslint:disable */
2-
import { Injectable } from '@angular/core';
2+
import { Injectable, InjectionToken, Optional, Inject } from '@angular/core';
33
import { HttpClient, HttpRequest, HttpResponse, HttpHeaders } from '@angular/common/http';
44
import { BaseService as __BaseService } from '../base-service';
55
import { {{ configurationClass }} as __Configuration } from '../{{configurationFile}}';
66
import { StrictHttpResponse as __StrictHttpResponse } from '../strict-http-response';
7-
import { Observable as __Observable } from 'rxjs';
8-
import { map as __map, filter as __filter } from 'rxjs/operators';
7+
import { Observable as __Observable, of as __of } from 'rxjs';
8+
import { map as __map, filter as __filter, switchMap as __switchMap } from 'rxjs/operators';
9+
10+
interface {{serviceClass}}TokenProvider {
11+
getToken: (name: string) => __Observable<string>;
12+
}
13+
14+
const {{serviceClass}}TokenProviderInjectionToken = new InjectionToken<{{serviceClass}}TokenProvider>('{{serviceClass}}TokenProviderInjectionToken')
915

1016
{{#serviceDependencies}}import { {{modelClass}} } from '../models/{{modelFile}}';
1117
{{/serviceDependencies}}
@@ -15,12 +21,19 @@ import { map as __map, filter as __filter } from 'rxjs/operators';
1521
class {{serviceClass}} extends __BaseService {
1622
{{#serviceOperations}}{{>operationEndpoints}}{{/serviceOperations}}
1723

24+
private tokenProvider: (name: string) => __Observable<string> = _ => __of(null);
25+
1826
constructor(
1927
config: __Configuration,
20-
http: HttpClient
28+
http: HttpClient,
29+
@Optional() @Inject({{serviceClass}}TokenProviderInjectionToken) tokenProvider: {{serviceClass}}TokenProvider,
2130
) {
2231
super(config, http);
32+
if (tokenProvider) {
33+
this.tokenProvider = (v: string) => tokenProvider.getToken(v);
34+
}
2335
}
36+
2437
{{#serviceOperations}}{{>operationResponse}}{{>operationBody}}{{/serviceOperations}}
2538
}
2639

@@ -34,4 +47,4 @@ module {{serviceClass}} {
3447
{{/operationParamsClass}}{{/serviceOperations}}
3548
}
3649

37-
export { {{serviceClass}} }
50+
export { {{serviceClass}}, {{serviceClass}}TokenProvider, {{serviceClass}}TokenProviderInjectionToken }

templates/services.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{{#services}}
2-
export { {{serviceClass}} } from './services/{{serviceFile}}';
2+
export { {{serviceClass}}, {{serviceClass}}TokenProvider, {{serviceClass}}TokenProviderInjectionToken } from './services/{{serviceFile}}';
33
{{/services}}

0 commit comments

Comments
 (0)