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:
@ -3,15 +3,14 @@ import {Headers} from './headers';
|
||||
import {RequestModesOpts, RequestMethods, RequestCacheOpts, RequestCredentialsOpts} from './enums';
|
||||
import {IRequestOptions} from './interfaces';
|
||||
import {Injectable} from 'angular2/di';
|
||||
import {ListWrapper, StringMapWrapper, StringMap} from 'angular2/src/facade/collection';
|
||||
|
||||
const INITIALIZER = CONST_EXPR({});
|
||||
/**
|
||||
* Creates a request options object with default properties as described in the [Fetch
|
||||
* Creates a request options object similar to the `RequestInit` description
|
||||
* in the [Fetch
|
||||
* Spec](https://fetch.spec.whatwg.org/#requestinit) to be optionally provided when instantiating a
|
||||
* {@link Request}. This class is used implicitly by {@link Http} to merge in provided request
|
||||
* options with the default options specified here. These same default options are injectable via
|
||||
* the {@link BaseRequestOptions} class.
|
||||
* {@link Request}.
|
||||
*
|
||||
* All values are null by default.
|
||||
*/
|
||||
export class RequestOptions implements IRequestOptions {
|
||||
/**
|
||||
@ -19,7 +18,7 @@ export class RequestOptions implements IRequestOptions {
|
||||
*
|
||||
* Defaults to "GET".
|
||||
*/
|
||||
method: RequestMethods = RequestMethods.GET;
|
||||
method: RequestMethods;
|
||||
/**
|
||||
* Headers object based on the `Headers` class in the [Fetch
|
||||
* Spec](https://fetch.spec.whatwg.org/#headers-class).
|
||||
@ -28,54 +27,42 @@ export class RequestOptions implements IRequestOptions {
|
||||
/**
|
||||
* Body to be used when creating the request.
|
||||
*/
|
||||
// TODO: support FormData, Blob, URLSearchParams, JSON
|
||||
// TODO: support FormData, Blob, URLSearchParams
|
||||
body: string;
|
||||
mode: RequestModesOpts = RequestModesOpts.Cors;
|
||||
mode: RequestModesOpts;
|
||||
credentials: RequestCredentialsOpts;
|
||||
cache: RequestCacheOpts;
|
||||
url: string;
|
||||
constructor({method, headers, body, mode, credentials, cache, url}: IRequestOptions = {}) {
|
||||
this.method = isPresent(method) ? method : RequestMethods.GET;
|
||||
this.headers = headers;
|
||||
this.body = body;
|
||||
this.mode = isPresent(mode) ? mode : RequestModesOpts.Cors;
|
||||
this.credentials = credentials;
|
||||
this.cache = cache;
|
||||
this.url = url;
|
||||
this.method = isPresent(method) ? method : null;
|
||||
this.headers = isPresent(headers) ? headers : null;
|
||||
this.body = isPresent(body) ? body : null;
|
||||
this.mode = isPresent(mode) ? mode : null;
|
||||
this.credentials = isPresent(credentials) ? credentials : null;
|
||||
this.cache = isPresent(cache) ? cache : null;
|
||||
this.url = isPresent(url) ? url : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a copy of the `RequestOptions` instance, using the optional input as values to override
|
||||
* existing values.
|
||||
*/
|
||||
merge({url = null, method = null, headers = null, body = null, mode = null, credentials = null,
|
||||
cache = null}: any = {}): RequestOptions {
|
||||
merge(options?: IRequestOptions): RequestOptions {
|
||||
return new RequestOptions({
|
||||
method: isPresent(method) ? method : this.method,
|
||||
headers: isPresent(headers) ? headers : this.headers,
|
||||
body: isPresent(body) ? body : this.body,
|
||||
mode: isPresent(mode) ? mode : this.mode,
|
||||
credentials: isPresent(credentials) ? credentials : this.credentials,
|
||||
cache: isPresent(cache) ? cache : this.cache,
|
||||
url: isPresent(url) ? url : this.url
|
||||
method: isPresent(options) && isPresent(options.method) ? options.method : this.method,
|
||||
headers: isPresent(options) && isPresent(options.headers) ? options.headers : this.headers,
|
||||
body: isPresent(options) && isPresent(options.body) ? options.body : this.body,
|
||||
mode: isPresent(options) && isPresent(options.mode) ? options.mode : this.mode,
|
||||
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
|
||||
});
|
||||
}
|
||||
|
||||
static fromStringWrapper(map: StringMap<string, any>): RequestOptions {
|
||||
return new RequestOptions({
|
||||
method: StringMapWrapper.get(map, 'method'),
|
||||
headers: StringMapWrapper.get(map, 'headers'),
|
||||
body: StringMapWrapper.get(map, 'body'),
|
||||
mode: StringMapWrapper.get(map, 'mode'),
|
||||
credentials: StringMapWrapper.get(map, 'credentials'),
|
||||
cache: StringMapWrapper.get(map, 'cache'),
|
||||
url:<string>StringMapWrapper.get(map, 'url')
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Injectable version of {@link RequestOptions}.
|
||||
* Injectable version of {@link RequestOptions}, with overridable default values.
|
||||
*
|
||||
* #Example
|
||||
*
|
||||
@ -84,8 +71,8 @@ export class RequestOptions implements IRequestOptions {
|
||||
* ...
|
||||
* class MyComponent {
|
||||
* constructor(baseRequestOptions:BaseRequestOptions, http:Http) {
|
||||
* var options = baseRequestOptions.merge({body: 'foobar'});
|
||||
* var request = new Request('https://foo', options);
|
||||
* var options = baseRequestOptions.merge({body: 'foobar', url: 'https://foo'});
|
||||
* var request = new Request(options);
|
||||
* http.request(request).subscribe(res => this.bars = res.json());
|
||||
* }
|
||||
* }
|
||||
@ -94,5 +81,7 @@ export class RequestOptions implements IRequestOptions {
|
||||
*/
|
||||
@Injectable()
|
||||
export class BaseRequestOptions extends RequestOptions {
|
||||
constructor() { super(); }
|
||||
constructor() {
|
||||
super({method: RequestMethods.GET, headers: new Headers(), mode: RequestModesOpts.Cors});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user