2015-06-13 00:50:19 -04:00
|
|
|
import {CONST_EXPR, CONST, isPresent} from 'angular2/src/facade/lang';
|
2015-04-29 02:07:55 -04:00
|
|
|
import {Headers} from './headers';
|
|
|
|
import {URLSearchParams} from './url_search_params';
|
|
|
|
import {RequestModesOpts, RequestMethods, RequestCacheOpts, RequestCredentialsOpts} from './enums';
|
2015-06-13 22:48:40 -04:00
|
|
|
import {IRequestOptions} from './interfaces';
|
2015-04-29 02:07:55 -04:00
|
|
|
import {Injectable} from 'angular2/di';
|
2015-06-13 00:50:19 -04:00
|
|
|
import {ListWrapper, StringMapWrapper} from 'angular2/src/facade/collection';
|
2015-04-29 02:07:55 -04:00
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* Creates a request options object with default properties as described 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.
|
|
|
|
*/
|
2015-06-13 22:48:40 -04:00
|
|
|
export class RequestOptions implements IRequestOptions {
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* Http method with which to execute the request.
|
|
|
|
*
|
|
|
|
* Defaults to "GET".
|
|
|
|
*/
|
2015-06-13 00:50:19 -04:00
|
|
|
method: RequestMethods = RequestMethods.GET;
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* Headers object based on the `Headers` class in the [Fetch
|
|
|
|
* Spec](https://fetch.spec.whatwg.org/#headers-class).
|
|
|
|
*/
|
2015-04-29 02:07:55 -04:00
|
|
|
headers: Headers;
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* Body to be used when creating the request.
|
|
|
|
*/
|
2015-06-13 18:49:05 -04:00
|
|
|
body: URLSearchParams | FormData | Blob | string;
|
2015-06-13 00:50:19 -04:00
|
|
|
mode: RequestModesOpts = RequestModesOpts.Cors;
|
2015-04-29 02:07:55 -04:00
|
|
|
credentials: RequestCredentialsOpts;
|
|
|
|
cache: RequestCacheOpts;
|
2015-06-13 22:48:40 -04:00
|
|
|
constructor({method, headers, body, mode, credentials, cache}: IRequestOptions = {
|
|
|
|
method: RequestMethods.GET,
|
|
|
|
mode: RequestModesOpts.Cors
|
|
|
|
}) {
|
2015-06-13 00:50:19 -04:00
|
|
|
this.method = method;
|
|
|
|
this.headers = headers;
|
|
|
|
this.body = body;
|
|
|
|
this.mode = mode;
|
|
|
|
this.credentials = credentials;
|
|
|
|
this.cache = cache;
|
|
|
|
}
|
2015-04-29 02:07:55 -04:00
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* Creates a copy of the `RequestOptions` instance, using the optional input as values to override
|
|
|
|
* existing values.
|
|
|
|
*/
|
2015-06-13 22:48:40 -04:00
|
|
|
merge(opts: IRequestOptions = {}): RequestOptions {
|
|
|
|
return new RequestOptions(StringMapWrapper.merge(this, opts));
|
2015-04-29 02:07:55 -04:00
|
|
|
}
|
|
|
|
}
|
2015-06-13 00:50:19 -04:00
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* Injectable version of {@link RequestOptions}.
|
|
|
|
*
|
|
|
|
* #Example
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* import {Http, BaseRequestOptions, Request} from 'angular2/http';
|
|
|
|
* ...
|
|
|
|
* class MyComponent {
|
|
|
|
* constructor(baseRequestOptions:BaseRequestOptions, http:Http) {
|
|
|
|
* var options = baseRequestOptions.merge({body: 'foobar'});
|
|
|
|
* var request = new Request('https://foo', options);
|
|
|
|
* http.request(request).subscribe(res => this.bars = res.json());
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
*/
|
2015-06-13 00:50:19 -04:00
|
|
|
@Injectable()
|
2015-06-13 22:48:40 -04:00
|
|
|
export class BaseRequestOptions extends RequestOptions {
|
2015-06-13 00:50:19 -04:00
|
|
|
constructor() { super(); }
|
|
|
|
}
|