Skip to content

Commit

Permalink
fix(api-client): http options without headers and params constructing…
Browse files Browse the repository at this point in the history
… (AOT compatible)

closes #47

Signed-off-by: Vojtech Masek <[email protected]>
  • Loading branch information
vmasek committed May 22, 2018
1 parent 539ba46 commit eca03c3
Show file tree
Hide file tree
Showing 8 changed files with 716 additions and 676 deletions.
20 changes: 15 additions & 5 deletions templates/ngx-module-export.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,26 @@ export * from './models';

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

/**
* provided options, headers and params will be used as default for each request
*/
export interface DefaultHttpOptions {
headers?: {[key: string]: string};
params?: {[key: string]: string};
reportProgress?: boolean;
withCredentials?: boolean;
}

export interface HttpOptions {
headers?: HttpHeaders, // provided headers will be used as default for each request
params?: HttpParams, // provided params will be used as default for each request
reportProgress?: boolean,
withCredentials?: boolean,
headers?: HttpHeaders;
params?: HttpParams;
reportProgress?: boolean;
withCredentials?: boolean;
}

export interface APIClientModuleConfig {
domain?: string;
httpOptions?: HttpOptions;
httpOptions?: DefaultHttpOptions;
}

@NgModule({})
Expand Down
12 changes: 6 additions & 6 deletions templates/ngx-service.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Inject, Injectable, InjectionToken, Optional } from '@angular/core';
import { Observable } from 'rxjs';
import { HttpOptions } from './';
import { DefaultHttpOptions, HttpOptions } from './';

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

constructor(private http: HttpClient,
@Optional() @Inject(USE_DOMAIN) domain: string,
@Optional() @Inject(USE_HTTP_OPTIONS) options: HttpOptions) {
@Optional() @Inject(USE_HTTP_OPTIONS) options: DefaultHttpOptions) {
if (domain) {
this.domain = domain;
}

this.options = {
headers: options && options.headers ? options.headers : new HttpHeaders(),
params: options && options.params ? options.params : new HttpParams(),
headers: new HttpHeaders(options && options.headers ? options.headers : {}),
params: new HttpParams(options && options.params ? options.params : {}),
...(options && options.reportProgress ? { reportProgress: options.reportProgress } : {}),
...(options && options.withCredentials ? { withCredentials: options.withCredentials } : {})
};
Expand All @@ -52,10 +52,10 @@ export class APIClient {
{{/parameters}}
},
{{/parameters.length}}
passedOptions?: HttpOptions
requestHttpOptions?: HttpOptions
): Observable<{{&response}}> {
const path = `{{&path}}`;
const options: APIHttpOptions = {...this.options, ...passedOptions};
const options: APIHttpOptions = {...this.options, ...requestHttpOptions};

{{#parameters}}
{{#isQueryParameter}}
Expand Down
Loading

0 comments on commit eca03c3

Please sign in to comment.