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 {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-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-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-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;
|
|
|
|
constructor({method, headers, body, mode, credentials, cache, url}: IRequestOptions = {}) {
|
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-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-06-24 03:27:07 -04:00
|
|
|
merge(options?: IRequestOptions): 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,
|
|
|
|
url: isPresent(options) && isPresent(options.url) ? options.url : this.url
|
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
|
|
|
}
|