feat(common): accept object map for HttpClient headers & params (#18490)

Today, constructing a new GET request with headers looks like:

const headers = new HttpHeaders({
  'My-Header': 'header value',
});
http.get('/url', {headers}).subscribe(...);

This indirection is unnecessary. It'd be more ergonomic to write:

http.get('/url', {headers: {'My-Header': 'header value'}}).subscribe(...);

This commit allows that new syntax, both for HttpHeaders and HttpParams.
In the HttpParams case it also allows construction of HttpParams with a map.

PR Close #18490
This commit is contained in:
Alex Rickabaugh
2017-08-01 15:43:39 -07:00
committed by Jason Aden
parent 65e26d713c
commit 1b1d5f10a1
4 changed files with 1141 additions and 503 deletions

View File

@ -89,10 +89,24 @@ export class HttpParams {
constructor(options: {
fromString?: string,
fromObject?: {[param: string]: string | string[]},
encoder?: HttpParameterCodec,
} = {}) {
this.encoder = options.encoder || new HttpUrlEncodingCodec();
this.map = !!options.fromString ? paramParser(options.fromString, this.encoder) : null;
if (!!options.fromString) {
if (!!options.fromObject) {
throw new Error(`Cannot specify both fromString and fromObject.`);
}
this.map = paramParser(options.fromString, this.encoder);
} else if (!!options.fromObject) {
this.map = new Map<string, string[]>();
Object.keys(options.fromObject).forEach(key => {
const value = (options.fromObject as any)[key];
this.map !.set(key, Array.isArray(value) ? value : [value]);
});
} else {
this.map = null;
}
}
/**