2015-08-20 17:28:25 -04:00
|
|
|
import {CONST_EXPR, CONST, isPresent, isString} from 'angular2/src/core/facade/lang';
|
2015-04-29 02:07:55 -04:00
|
|
|
import {Headers} from './headers';
|
|
|
|
import {RequestModesOpts, RequestMethods, RequestCacheOpts, RequestCredentialsOpts} from './enums';
|
2015-08-11 18:01:29 -04:00
|
|
|
import {RequestOptionsArgs} from './interfaces';
|
2015-04-29 02:07:55 -04:00
|
|
|
import {Injectable} from 'angular2/di';
|
2015-07-13 14:47:10 -04:00
|
|
|
import {URLSearchParams} from './url_search_params';
|
2015-04-29 02:07:55 -04:00
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
2015-06-24 03:27:07 -04:00
|
|
|
* Creates a request options object similar to the `RequestInit` description
|
|
|
|
* in the [Fetch
|
2015-06-09 18:18:57 -04:00
|
|
|
* Spec](https://fetch.spec.whatwg.org/#requestinit) to be optionally provided when instantiating a
|
2015-06-24 03:27:07 -04:00
|
|
|
* {@link Request}.
|
|
|
|
*
|
|
|
|
* All values are null by default.
|
2015-06-09 18:18:57 -04:00
|
|
|
*/
|
2015-08-11 18:01:29 -04:00
|
|
|
export class RequestOptions {
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* Http method with which to execute the request.
|
|
|
|
*
|
|
|
|
* Defaults to "GET".
|
|
|
|
*/
|
2015-06-24 03:27:07 -04:00
|
|
|
method: RequestMethods;
|
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-24 03:27:07 -04:00
|
|
|
// TODO: support FormData, Blob, URLSearchParams
|
2015-06-19 15:14:12 -04:00
|
|
|
body: string;
|
2015-06-24 03:27:07 -04:00
|
|
|
mode: RequestModesOpts;
|
2015-04-29 02:07:55 -04:00
|
|
|
credentials: RequestCredentialsOpts;
|
|
|
|
cache: RequestCacheOpts;
|
2015-06-19 15:14:12 -04:00
|
|
|
url: string;
|
2015-07-13 14:47:10 -04:00
|
|
|
search: URLSearchParams;
|
|
|
|
constructor({method, headers, body, mode, credentials, cache, url, search}:
|
2015-08-11 18:01:29 -04:00
|
|
|
RequestOptionsArgs = {}) {
|
2015-06-24 03:27:07 -04:00
|
|
|
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;
|
2015-07-13 14:47:10 -04:00
|
|
|
this.search = isPresent(search) ? (isString(search) ? new URLSearchParams(<string>(search)) :
|
|
|
|
<URLSearchParams>(search)) :
|
|
|
|
null;
|
2015-06-13 00:50:19 -04:00
|
|
|
}
|
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-08-11 18:01:29 -04:00
|
|
|
merge(options?: RequestOptionsArgs): RequestOptions {
|
2015-06-19 15:14:12 -04:00
|
|
|
return new RequestOptions({
|
2015-06-24 03:27:07 -04:00
|
|
|
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,
|
2015-07-13 14:47:10 -04:00
|
|
|
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
|
2015-06-19 15:14:12 -04:00
|
|
|
});
|
|
|
|
}
|
2015-04-29 02:07:55 -04:00
|
|
|
}
|
2015-06-13 00:50:19 -04:00
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
2015-06-24 03:27:07 -04:00
|
|
|
* Injectable version of {@link RequestOptions}, with overridable default values.
|
2015-06-09 18:18:57 -04:00
|
|
|
*
|
|
|
|
* #Example
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* import {Http, BaseRequestOptions, Request} from 'angular2/http';
|
|
|
|
* ...
|
|
|
|
* class MyComponent {
|
|
|
|
* constructor(baseRequestOptions:BaseRequestOptions, http:Http) {
|
2015-06-24 03:27:07 -04:00
|
|
|
* var options = baseRequestOptions.merge({body: 'foobar', url: 'https://foo'});
|
|
|
|
* var request = new Request(options);
|
2015-06-09 18:18:57 -04:00
|
|
|
* 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-24 03:27:07 -04:00
|
|
|
constructor() {
|
|
|
|
super({method: RequestMethods.GET, headers: new Headers(), mode: RequestModesOpts.Cors});
|
|
|
|
}
|
2015-06-13 00:50:19 -04:00
|
|
|
}
|