refactor(http): remove default settings from RequestOptions constructor

The BaseRequestOptions class is responsible for declaring default values,
while the RequestOptions class is merely responsible for setting values
based on values provided in the constructor.
This commit is contained in:
Jeff Cross
2015-06-24 00:27:07 -07:00
parent 146dbf1270
commit b3d98cba77
20 changed files with 333 additions and 194 deletions

View File

@ -1,13 +1,17 @@
import {RequestMethods, RequestModesOpts, RequestCredentialsOpts, RequestCacheOpts} from './enums';
import {RequestOptions} from './base_request_options';
import {IRequestOptions} from './interfaces';
import {Headers} from './headers';
import {BaseException, RegExpWrapper, CONST_EXPR, isPresent} from 'angular2/src/facade/lang';
import {StringMap, StringMapWrapper} from 'angular2/src/facade/collection';
import {
BaseException,
RegExpWrapper,
CONST_EXPR,
isPresent,
isJsObject
} from 'angular2/src/facade/lang';
// TODO(jeffbcross): properly implement body accessors
/**
* Creates `Request` instances with default values.
* Creates `Request` instances from provided values.
*
* The Request's interface is inspired by the Request constructor defined in the [Fetch
* Spec](https://fetch.spec.whatwg.org/#request-class),
@ -33,12 +37,8 @@ export class Request {
// TODO: support URLSearchParams | FormData | Blob | ArrayBuffer
private _body: string;
cache: RequestCacheOpts;
// TODO(jeffbcross): determine way to add type to destructured args
constructor(options?: IRequestOptions) {
var requestOptions: RequestOptions = options instanceof
StringMap ? RequestOptions.fromStringWrapper(<StringMap<string, any>>options) :
<RequestOptions>options;
constructor(requestOptions: RequestOptions) {
// TODO: assert that url is present
this.url = requestOptions.url;
this._body = requestOptions.body;
this.method = requestOptions.method;
@ -47,10 +47,11 @@ export class Request {
// Defaults to 'omit', consistent with browser
// TODO(jeffbcross): implement behavior
this.credentials = requestOptions.credentials;
this.headers = requestOptions.headers;
this.headers = new Headers(requestOptions.headers);
this.cache = requestOptions.cache;
}
/**
* Returns the request's body as string, assuming that body exists. If body is undefined, return
* empty