fix(http): introduce named type for HttpParams options (#19360)

This is necessary to enable type-based optimizations with Closure.
Without explicity making these options the same named type, Closure
thinks they are different types and cannot disambiguate the `fromObject`
property.
This commit is contained in:
Jeremy Elbourn 2017-09-26 15:02:08 -07:00 committed by Victor Berchet
parent dfb8d21ef4
commit 8a0e45826a
3 changed files with 20 additions and 15 deletions

View File

@ -15,7 +15,7 @@ import {map} from 'rxjs/operator/map';
import {HttpHandler} from './backend'; import {HttpHandler} from './backend';
import {HttpHeaders} from './headers'; import {HttpHeaders} from './headers';
import {HttpParams} from './params'; import {HttpParams, HttpParamsOptions} from './params';
import {HttpRequest} from './request'; import {HttpRequest} from './request';
import {HttpEvent, HttpResponse} from './response'; import {HttpEvent, HttpResponse} from './response';
@ -366,7 +366,7 @@ export class HttpClient {
if (options.params instanceof HttpParams) { if (options.params instanceof HttpParams) {
params = options.params; params = options.params;
} else { } else {
params = new HttpParams({fromObject: options.params}); params = new HttpParams({ fromObject: options.params } as HttpParamsOptions);
} }
} }

View File

@ -73,6 +73,21 @@ interface Update {
op: 'a'|'d'|'s'; op: 'a'|'d'|'s';
} }
/** Options used to construct an `HttpParams` instance. */
export interface HttpParamsOptions {
/**
* String representation of the HTTP params in URL-query-string format. Mutually exclusive with
* `fromObject`.
*/
fromString?: string;
/** Object map of the HTTP params. Mutally exclusive with `fromString`. */
fromObject?: {[param: string]: string | string[]};
/** Encoding codec used to parse and serialize the params. */
encoder?: HttpParameterCodec;
}
/** /**
* An HTTP request/response body that represents serialized parameters, * An HTTP request/response body that represents serialized parameters,
* per the MIME type `application/x-www-form-urlencoded`. * per the MIME type `application/x-www-form-urlencoded`.
@ -87,11 +102,7 @@ export class HttpParams {
private updates: Update[]|null = null; private updates: Update[]|null = null;
private cloneFrom: HttpParams|null = null; private cloneFrom: HttpParams|null = null;
constructor(options: { constructor(options: HttpParamsOptions = {} as HttpParamsOptions) {
fromString?: string,
fromObject?: {[param: string]: string | string[]},
encoder?: HttpParameterCodec,
} = {}) {
this.encoder = options.encoder || new HttpUrlEncodingCodec(); this.encoder = options.encoder || new HttpUrlEncodingCodec();
if (!!options.fromString) { if (!!options.fromString) {
if (!!options.fromObject) { if (!!options.fromObject) {
@ -175,7 +186,7 @@ export class HttpParams {
} }
private clone(update: Update): HttpParams { private clone(update: Update): HttpParams {
const clone = new HttpParams({encoder: this.encoder}); const clone = new HttpParams({ encoder: this.encoder } as HttpParamsOptions);
clone.cloneFrom = this.cloneFrom || this; clone.cloneFrom = this.cloneFrom || this;
clone.updates = (this.updates || []).concat([update]); clone.updates = (this.updates || []).concat([update]);
return clone; return clone;

View File

@ -1580,13 +1580,7 @@ export interface HttpParameterCodec {
/** @stable */ /** @stable */
export declare class HttpParams { export declare class HttpParams {
constructor(options?: { constructor(options?: HttpParamsOptions);
fromString?: string;
fromObject?: {
[param: string]: string | string[];
};
encoder?: HttpParameterCodec;
});
append(param: string, value: string): HttpParams; append(param: string, value: string): HttpParams;
delete(param: string, value?: string): HttpParams; delete(param: string, value?: string): HttpParams;
get(param: string): string | null; get(param: string): string | null;