feat(http): serialize search parameters from request options

- Extends URLSearchParams API to include operations for combining
  different URLSearchParams objects:

  These new methods include:
  setAll(otherParams): performs `this.set(key, values[0])` for each
      key/value-list pair in `otherParams`

  appendAll(otherParams): performs `this.append(key, values)` for
      each key/value-list pair in `otherParams`

  replaceAll(otherParams): for each key/value-list pair in
      `otherParams`, replaces current set of values for `key` with
      a copy of the list of values.

- RequestOptions do not merge search params automatically (because
  there are multiple ways to do this). Instead, they replace any
  existing `search` field if `search` is provided. Explicit merging
  is required if merging is desirable.

- Some extra test coverage added.

Closes #2417
Closes #3020
This commit is contained in:
Caitlin Potter
2015-07-13 14:47:10 -04:00
committed by Tobias Bosch
parent dfa5103b1d
commit 77d3668432
7 changed files with 210 additions and 18 deletions

View File

@ -1,8 +1,9 @@
import {CONST_EXPR, CONST, isPresent} from 'angular2/src/facade/lang';
import {CONST_EXPR, CONST, isPresent, isString} from 'angular2/src/facade/lang';
import {Headers} from './headers';
import {RequestModesOpts, RequestMethods, RequestCacheOpts, RequestCredentialsOpts} from './enums';
import {IRequestOptions} from './interfaces';
import {Injectable} from 'angular2/di';
import {URLSearchParams} from './url_search_params';
/**
* Creates a request options object similar to the `RequestInit` description
@ -33,7 +34,9 @@ export class RequestOptions implements IRequestOptions {
credentials: RequestCredentialsOpts;
cache: RequestCacheOpts;
url: string;
constructor({method, headers, body, mode, credentials, cache, url}: IRequestOptions = {}) {
search: URLSearchParams;
constructor({method, headers, body, mode, credentials, cache, url, search}:
IRequestOptions = {}) {
this.method = isPresent(method) ? method : null;
this.headers = isPresent(headers) ? headers : null;
this.body = isPresent(body) ? body : null;
@ -41,6 +44,9 @@ export class RequestOptions implements IRequestOptions {
this.credentials = isPresent(credentials) ? credentials : null;
this.cache = isPresent(cache) ? cache : null;
this.url = isPresent(url) ? url : null;
this.search = isPresent(search) ? (isString(search) ? new URLSearchParams(<string>(search)) :
<URLSearchParams>(search)) :
null;
}
/**
@ -56,7 +62,11 @@ export class RequestOptions implements IRequestOptions {
credentials: isPresent(options) && isPresent(options.credentials) ? options.credentials :
this.credentials,
cache: isPresent(options) && isPresent(options.cache) ? options.cache : this.cache,
url: isPresent(options) && isPresent(options.url) ? options.url : this.url
url: isPresent(options) && isPresent(options.url) ? options.url : this.url,
search: isPresent(options) && isPresent(options.search) ?
(isString(options.search) ? new URLSearchParams(<string>(options.search)) :
(<URLSearchParams>(options.search)).clone()) :
this.search
});
}
}