feat(common): implement `appendAll()` method on `HttpParams` (#20930)

Adds an `appendAll()` method to `HttpParams` that can construct the HTTP
request/response body from an object of parameters and values.

This avoids calling `append()` multiple times when multiple parameters
need to be added.

Fixes #20798

PR Close #20930
This commit is contained in:
Harun Urhan 2020-12-06 18:19:58 +01:00 committed by Jessica Janiuk
parent c9033d6193
commit 575a2d162c
3 changed files with 31 additions and 2 deletions

View File

@ -1570,6 +1570,9 @@ export declare interface HttpParameterCodec {
export declare class HttpParams { export declare class HttpParams {
constructor(options?: HttpParamsOptions); constructor(options?: HttpParamsOptions);
append(param: string, value: string): HttpParams; append(param: string, value: string): HttpParams;
appendAll(params: {
[param: string]: string | string[];
}): HttpParams;
delete(param: string, value?: string): HttpParams; delete(param: string, value?: string): HttpParams;
get(param: string): string | null; get(param: string): string | null;
getAll(param: string): string[] | null; getAll(param: string): string[] | null;

24
packages/common/http/src/params.ts Executable file → Normal file
View File

@ -209,6 +209,26 @@ export class HttpParams {
return this.clone({param, value, op: 'a'}); return this.clone({param, value, op: 'a'});
} }
/**
* Constructs a new body with appended values for the given parameter name.
* @param params parameters and values
* @return A new body with the new value.
*/
appendAll(params: {[param: string]: string|string[]}): HttpParams {
const updates: Update[] = [];
Object.keys(params).forEach(param => {
const value = params[param];
if (Array.isArray(value)) {
value.forEach(_value => {
updates.push({param, value: _value, op: 'a'});
});
} else {
updates.push({param, value, op: 'a'});
}
});
return this.clone(updates);
}
/** /**
* Replaces the value for a parameter. * Replaces the value for a parameter.
* @param param The parameter name. * @param param The parameter name.
@ -251,10 +271,10 @@ export class HttpParams {
.join('&'); .join('&');
} }
private clone(update: Update): HttpParams { private clone(update: Update|Update[]): HttpParams {
const clone = new HttpParams({encoder: this.encoder} as HttpParamsOptions); 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

@ -36,6 +36,12 @@ import {HttpParams} from '@angular/common/http/src/params';
expect(mutated.toString()).toEqual('a=b&a=c'); expect(mutated.toString()).toEqual('a=b&a=c');
}); });
it('should allow appending all parameters', () => {
const body = new HttpParams({fromString: 'a=a1&b=b1'});
const mutated = body.appendAll({a: ['a2', 'a3'], b: 'b2'});
expect(mutated.toString()).toEqual('a=a1&a=a2&a=a3&b=b1&b=b2');
});
it('should allow deletion of parameters', () => { it('should allow deletion of parameters', () => {
const body = new HttpParams({fromString: 'a=b&c=d&e=f'}); const body = new HttpParams({fromString: 'a=b&c=d&e=f'});
const mutated = body.delete('c'); const mutated = body.delete('c');