Skip to content

Commit

Permalink
[typescript-angular2] Allow lazy oauth token refresh (swagger-api#6486)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexrashed committed Sep 13, 2017
1 parent 5219b9c commit 83b15d1
Show file tree
Hide file tree
Showing 24 changed files with 1,709 additions and 1,297 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class {{classname}} {
}
if (configuration) {
this.configuration = configuration;
this.basePath = basePath || configuration.basePath || this.basePath;
this.basePath = basePath || configuration.basePath || this.basePath;
}
}

Expand Down Expand Up @@ -119,7 +119,7 @@ export class {{classname}} {
.replace('${' + '{{baseName}}' + '}', String({{paramName}})){{/pathParams}};

let queryParameters = new URLSearchParams();
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
let headersObservable = Observable.of(new Headers(this.defaultHeaders.toJSON())); // https://github.com/angular/angular/issues/6845

{{#allParams}}
{{#required}}
Expand Down Expand Up @@ -157,12 +157,12 @@ export class {{classname}} {
{{#headerParams}}
{{#isListContainer}}
if ({{paramName}}) {
headers.set('{{baseName}}', {{paramName}}.join(COLLECTION_FORMATS['{{collectionFormat}}']));
headersObservable = headersObservable.do((headers: Headers) => headers.set('{{baseName}}', {{paramName}}.join(COLLECTION_FORMATS['{{collectionFormat}}'])));
}
{{/isListContainer}}
{{^isListContainer}}
if ({{paramName}} !== undefined && {{paramName}} !== null) {
headers.set('{{baseName}}', String({{paramName}}));
headersObservable = headersObservable.do((headers: Headers) => headers.set('{{baseName}}', String({{paramName}})));
}
{{/isListContainer}}

Expand Down Expand Up @@ -198,7 +198,8 @@ export class {{classname}} {
{{#isApiKey}}
{{#isKeyInHeader}}
if (this.configuration.apiKeys["{{keyParamName}}"]) {
headers.set('{{keyParamName}}', this.configuration.apiKeys["{{keyParamName}}"]);
headersObservable = headersObservable.do((headers: Headers) =>
headers.set('{{keyParamName}}', this.configuration.apiKeys["{{keyParamName}}"]));
}

{{/isKeyInHeader}}
Expand All @@ -212,24 +213,27 @@ export class {{classname}} {
{{#isBasic}}
// http basic authentication required
if (this.configuration.username || this.configuration.password) {
headers.set('Authorization', 'Basic ' + btoa(this.configuration.username + ':' + this.configuration.password));
headersObservable = headersObservable.do((headers: Headers) =>
headers.set('Authorization', 'Basic ' + btoa(this.configuration.username + ':' + this.configuration.password)));
}

{{/isBasic}}
{{#isOAuth}}
// oauth required
if (this.configuration.accessToken) {
let accessToken = typeof this.configuration.accessToken === 'function'
? this.configuration.accessToken()
: this.configuration.accessToken;
headers.set('Authorization', 'Bearer ' + accessToken);
let accessTokenObservable = typeof this.configuration.accessToken === 'function'
? this.configuration.accessToken("{{name}}", [{{#scopes}}"{{{scope}}}"{{^-last}}, {{/-last}}{{/scopes}}])
: Observable.of(this.configuration.accessToken);
headersObservable = headersObservable.zip(accessTokenObservable, (headers: Headers, accessToken: string) =>
headers.set('Authorization', 'Bearer ' + accessToken));
}

{{/isOAuth}}
{{/authMethods}}

{{#bodyParam}}
headers.set('Content-Type', 'application/json');
headersObservable = headersObservable.do((headers: Headers) =>
headers.set('Content-Type', 'application/json'));

{{/bodyParam}}
{{#formParams}}
Expand All @@ -252,27 +256,31 @@ export class {{classname}} {
{{/isListContainer}}

{{/formParams}}
let requestOptions: RequestOptionsArgs = new RequestOptions({
method: {{httpMethod}},
headers: headers,
let requestOptionsObservable = headersObservable.map((headers: Headers) => {
let requestOptions: RequestOptionsArgs = new RequestOptions({
method: {{httpMethod}},
headers: headers,
{{#bodyParam}}
body: {{paramName}} == null ? '' : JSON.stringify({{paramName}}), // https://github.com/angular/angular/issues/10612
body: {{paramName}} == null ? '' : JSON.stringify({{paramName}}), // https://github.com/angular/angular/issues/10612
{{/bodyParam}}
{{#hasFormParams}}
body: formParams.toString(),
body: formParams.toString(),
{{/hasFormParams}}
{{#isResponseFile}}
responseType: ResponseContentType.Blob,
responseType: ResponseContentType.Blob,
{{/isResponseFile}}
search: queryParameters,
withCredentials:this.configuration.withCredentials
search: queryParameters,
withCredentials:this.configuration.withCredentials
});
// https://github.com/swagger-api/swagger-codegen/issues/4037
if (extraHttpRequestParams) {
requestOptions = (<any>Object).assign(requestOptions, extraHttpRequestParams);
}

return requestOptions;
});
// https://github.com/swagger-api/swagger-codegen/issues/4037
if (extraHttpRequestParams) {
requestOptions = (<any>Object).assign(requestOptions, extraHttpRequestParams);
}

return this.http.request(path, requestOptions);
return requestOptionsObservable.flatMap((requestOptions: RequestOptionsArgs) => this.http.request(path, requestOptions));
}

{{/operation}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Observable } from 'rxjs/Observable';

export interface ConfigurationParameters {
apiKeys?: {[ key: string ]: string};
username?: string;
password?: string;
accessToken?: string | (() => string);
accessToken?: string | ((name: string, scopes?: string[]) => Observable<string>);
basePath?: string;
withCredentials?: boolean;
}
Expand All @@ -11,7 +13,7 @@ export class Configuration {
apiKeys?: {[ key: string ]: string};
username?: string;
password?: string;
accessToken?: string | (() => string);
accessToken?: string | ((name: string, scopes?: string[]) => Observable<string>);
basePath?: string;
withCredentials?: boolean;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

// Statics
import 'rxjs/add/observable/throw';
import 'rxjs/add/observable/of';

// Operators
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/zip';
Loading

0 comments on commit 83b15d1

Please sign in to comment.