Skip to content

Commit eca03c3

Browse files
committed
fix(api-client): http options without headers and params constructing (AOT compatible)
closes #47 Signed-off-by: Vojtech Masek <[email protected]>
1 parent 539ba46 commit eca03c3

File tree

8 files changed

+716
-676
lines changed

8 files changed

+716
-676
lines changed

templates/ngx-module-export.mustache

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,26 @@ export * from './models';
1010

1111
export { APIClient } from './api-client.service';
1212

13+
/**
14+
* provided options, headers and params will be used as default for each request
15+
*/
16+
export interface DefaultHttpOptions {
17+
headers?: {[key: string]: string};
18+
params?: {[key: string]: string};
19+
reportProgress?: boolean;
20+
withCredentials?: boolean;
21+
}
22+
1323
export interface HttpOptions {
14-
headers?: HttpHeaders, // provided headers will be used as default for each request
15-
params?: HttpParams, // provided params will be used as default for each request
16-
reportProgress?: boolean,
17-
withCredentials?: boolean,
24+
headers?: HttpHeaders;
25+
params?: HttpParams;
26+
reportProgress?: boolean;
27+
withCredentials?: boolean;
1828
}
1929

2030
export interface APIClientModuleConfig {
2131
domain?: string;
22-
httpOptions?: HttpOptions;
32+
httpOptions?: DefaultHttpOptions;
2333
}
2434

2535
@NgModule({})

templates/ngx-service.mustache

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
44
import { Inject, Injectable, InjectionToken, Optional } from '@angular/core';
55
import { Observable } from 'rxjs';
6-
import { HttpOptions } from './';
6+
import { DefaultHttpOptions, HttpOptions } from './';
77

88
{{#definitions.length}}
99
import * as models from './models';
@@ -29,15 +29,15 @@ export class APIClient {
2929

3030
constructor(private http: HttpClient,
3131
@Optional() @Inject(USE_DOMAIN) domain: string,
32-
@Optional() @Inject(USE_HTTP_OPTIONS) options: HttpOptions) {
32+
@Optional() @Inject(USE_HTTP_OPTIONS) options: DefaultHttpOptions) {
3333
3434
if (domain) {
3535
this.domain = domain;
3636
}
3737

3838
this.options = {
39-
headers: options && options.headers ? options.headers : new HttpHeaders(),
40-
params: options && options.params ? options.params : new HttpParams(),
39+
headers: new HttpHeaders(options && options.headers ? options.headers : {}),
40+
params: new HttpParams(options && options.params ? options.params : {}),
4141
...(options && options.reportProgress ? { reportProgress: options.reportProgress } : {}),
4242
...(options && options.withCredentials ? { withCredentials: options.withCredentials } : {})
4343
};
@@ -52,10 +52,10 @@ export class APIClient {
5252
{{/parameters}}
5353
},
5454
{{/parameters.length}}
55-
passedOptions?: HttpOptions
55+
requestHttpOptions?: HttpOptions
5656
): Observable<{{&response}}> {
5757
const path = `{{&path}}`;
58-
const options: APIHttpOptions = {...this.options, ...passedOptions};
58+
const options: APIHttpOptions = {...this.options, ...requestHttpOptions};
5959

6060
{{#parameters}}
6161
{{#isQueryParameter}}

0 commit comments

Comments
 (0)