@@ -3,23 +3,22 @@ import {Headers} from './headers';
33import { RequestModesOpts , RequestMethods , RequestCacheOpts , RequestCredentialsOpts } from './enums' ;
44import { IRequestOptions } from './interfaces' ;
55import { Injectable } from 'angular2/di' ;
6- import { ListWrapper , StringMapWrapper , StringMap } from 'angular2/src/facade/collection' ;
76
8- const INITIALIZER = CONST_EXPR ( { } ) ;
97/**
10- * Creates a request options object with default properties as described in the [Fetch
8+ * Creates a request options object similar to the `RequestInit` description
9+ * in the [Fetch
1110 * Spec](https://fetch.spec.whatwg.org/#requestinit) to be optionally provided when instantiating a
12- * {@link Request}. This class is used implicitly by { @link Http} to merge in provided request
13- * options with the default options specified here. These same default options are injectable via
14- * the { @link BaseRequestOptions} class .
11+ * {@link Request}.
12+ *
13+ * All values are null by default .
1514 */
1615export class RequestOptions implements IRequestOptions {
1716 /**
1817 * Http method with which to execute the request.
1918 *
2019 * Defaults to "GET".
2120 */
22- method : RequestMethods = RequestMethods . GET ;
21+ method : RequestMethods ;
2322 /**
2423 * Headers object based on the `Headers` class in the [Fetch
2524 * Spec](https://fetch.spec.whatwg.org/#headers-class).
@@ -28,54 +27,42 @@ export class RequestOptions implements IRequestOptions {
2827 /**
2928 * Body to be used when creating the request.
3029 */
31- // TODO: support FormData, Blob, URLSearchParams, JSON
30+ // TODO: support FormData, Blob, URLSearchParams
3231 body : string ;
33- mode : RequestModesOpts = RequestModesOpts . Cors ;
32+ mode : RequestModesOpts ;
3433 credentials : RequestCredentialsOpts ;
3534 cache : RequestCacheOpts ;
3635 url : string ;
3736 constructor ( { method, headers, body, mode, credentials, cache, url} : IRequestOptions = { } ) {
38- this . method = isPresent ( method ) ? method : RequestMethods . GET ;
39- this . headers = headers ;
40- this . body = body ;
41- this . mode = isPresent ( mode ) ? mode : RequestModesOpts . Cors ;
42- this . credentials = credentials ;
43- this . cache = cache ;
44- this . url = url ;
37+ this . method = isPresent ( method ) ? method : null ;
38+ this . headers = isPresent ( headers ) ? headers : null ;
39+ this . body = isPresent ( body ) ? body : null ;
40+ this . mode = isPresent ( mode ) ? mode : null ;
41+ this . credentials = isPresent ( credentials ) ? credentials : null ;
42+ this . cache = isPresent ( cache ) ? cache : null ;
43+ this . url = isPresent ( url ) ? url : null ;
4544 }
4645
4746 /**
4847 * Creates a copy of the `RequestOptions` instance, using the optional input as values to override
4948 * existing values.
5049 */
51- merge ( { url = null , method = null , headers = null , body = null , mode = null , credentials = null ,
52- cache = null } : any = { } ) : RequestOptions {
50+ merge ( options ?: IRequestOptions ) : RequestOptions {
5351 return new RequestOptions ( {
54- method : isPresent ( method ) ? method : this . method ,
55- headers : isPresent ( headers ) ? headers : this . headers ,
56- body : isPresent ( body ) ? body : this . body ,
57- mode : isPresent ( mode ) ? mode : this . mode ,
58- credentials : isPresent ( credentials ) ? credentials : this . credentials ,
59- cache : isPresent ( cache ) ? cache : this . cache ,
60- url : isPresent ( url ) ? url : this . url
52+ method : isPresent ( options ) && isPresent ( options . method ) ? options . method : this . method ,
53+ headers : isPresent ( options ) && isPresent ( options . headers ) ? options . headers : this . headers ,
54+ body : isPresent ( options ) && isPresent ( options . body ) ? options . body : this . body ,
55+ mode : isPresent ( options ) && isPresent ( options . mode ) ? options . mode : this . mode ,
56+ credentials : isPresent ( options ) && isPresent ( options . credentials ) ? options . credentials :
57+ this . credentials ,
58+ cache : isPresent ( options ) && isPresent ( options . cache ) ? options . cache : this . cache ,
59+ url : isPresent ( options ) && isPresent ( options . url ) ? options . url : this . url
6160 } ) ;
6261 }
63-
64- static fromStringWrapper ( map : StringMap < string , any > ) : RequestOptions {
65- return new RequestOptions ( {
66- method : StringMapWrapper . get ( map , 'method' ) ,
67- headers : StringMapWrapper . get ( map , 'headers' ) ,
68- body : StringMapWrapper . get ( map , 'body' ) ,
69- mode : StringMapWrapper . get ( map , 'mode' ) ,
70- credentials : StringMapWrapper . get ( map , 'credentials' ) ,
71- cache : StringMapWrapper . get ( map , 'cache' ) ,
72- url :< string > StringMapWrapper . get ( map , 'url' )
73- } )
74- }
7562}
7663
7764/**
78- * Injectable version of {@link RequestOptions}.
65+ * Injectable version of {@link RequestOptions}, with overridable default values .
7966 *
8067 * #Example
8168 *
@@ -84,8 +71,8 @@ export class RequestOptions implements IRequestOptions {
8471 * ...
8572 * class MyComponent {
8673 * constructor(baseRequestOptions:BaseRequestOptions, http:Http) {
87- * var options = baseRequestOptions.merge({body: 'foobar'});
88- * var request = new Request('https://foo/', options);
74+ * var options = baseRequestOptions.merge({body: 'foobar', url: 'https://foo/' });
75+ * var request = new Request(options);
8976 * http.request(request).subscribe(res => this.bars = res.json());
9077 * }
9178 * }
@@ -94,5 +81,7 @@ export class RequestOptions implements IRequestOptions {
9481 */
9582@Injectable ( )
9683export class BaseRequestOptions extends RequestOptions {
97- constructor ( ) { super ( ) ; }
84+ constructor ( ) {
85+ super ( { method : RequestMethods . GET , headers : new Headers ( ) , mode : RequestModesOpts . Cors } ) ;
86+ }
9887}
0 commit comments