2015-11-06 20:34:07 -05:00
|
|
|
import {isPresent, isString} from 'angular2/src/facade/lang';
|
2015-04-29 02:07:55 -04:00
|
|
|
import {Headers} from './headers';
|
2015-12-03 16:44:14 -05:00
|
|
|
import {RequestMethod} from './enums';
|
2015-08-11 18:01:29 -04:00
|
|
|
import {RequestOptionsArgs} from './interfaces';
|
2015-11-23 13:18:04 -05:00
|
|
|
import {Injectable} from 'angular2/core';
|
2015-07-13 14:47:10 -04:00
|
|
|
import {URLSearchParams} from './url_search_params';
|
2015-09-21 03:46:16 -04:00
|
|
|
import {normalizeMethodName} from './http_utils';
|
2015-04-29 02:07:55 -04:00
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
2015-09-21 18:54:13 -04:00
|
|
|
* Creates a request options object to be optionally provided when instantiating a
|
2015-06-24 03:27:07 -04:00
|
|
|
* {@link Request}.
|
|
|
|
*
|
2015-09-21 18:54:13 -04:00
|
|
|
* This class is based on the `RequestInit` description in the [Fetch
|
|
|
|
* Spec](https://fetch.spec.whatwg.org/#requestinit).
|
|
|
|
*
|
|
|
|
* All values are null by default. Typical defaults can be found in the {@link BaseRequestOptions}
|
|
|
|
* class, which sub-classes `RequestOptions`.
|
|
|
|
*
|
|
|
|
* ### Example ([live demo](http://plnkr.co/edit/7Wvi3lfLq41aQPKlxB4O?p=preview))
|
|
|
|
*
|
|
|
|
* ```typescript
|
2015-12-03 16:44:14 -05:00
|
|
|
* import {RequestOptions, Request, RequestMethod} from 'angular2/http';
|
2015-09-21 18:54:13 -04:00
|
|
|
*
|
|
|
|
* var options = new RequestOptions({
|
2015-12-03 16:44:14 -05:00
|
|
|
* method: RequestMethod.Post,
|
2015-09-21 18:54:13 -04:00
|
|
|
* url: 'https://google.com'
|
|
|
|
* });
|
|
|
|
* var req = new Request(options);
|
2015-12-03 16:44:14 -05:00
|
|
|
* console.log('req.method:', RequestMethod[req.method]); // Post
|
2015-09-21 18:54:13 -04:00
|
|
|
* console.log('options.url:', options.url); // https://google.com
|
|
|
|
* ```
|
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
|
|
|
/**
|
2015-09-21 18:54:13 -04:00
|
|
|
* Http method with which to execute a {@link Request}.
|
2015-12-03 16:44:14 -05:00
|
|
|
* Acceptable methods are defined in the {@link RequestMethod} enum.
|
2015-06-09 18:18:57 -04:00
|
|
|
*/
|
2015-12-03 16:44:14 -05:00
|
|
|
method: RequestMethod | string;
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
2015-09-21 18:54:13 -04:00
|
|
|
* {@link Headers} to be attached to a {@link Request}.
|
2015-06-09 18:18:57 -04:00
|
|
|
*/
|
2015-04-29 02:07:55 -04:00
|
|
|
headers: Headers;
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
2015-09-21 18:54:13 -04:00
|
|
|
* Body to be used when creating a {@link Request}.
|
2015-06-09 18:18:57 -04:00
|
|
|
*/
|
2015-06-24 03:27:07 -04:00
|
|
|
// TODO: support FormData, Blob, URLSearchParams
|
2015-06-19 15:14:12 -04:00
|
|
|
body: string;
|
2015-09-21 18:54:13 -04:00
|
|
|
/**
|
|
|
|
* Url with which to perform a {@link Request}.
|
|
|
|
*/
|
2015-06-19 15:14:12 -04:00
|
|
|
url: string;
|
2015-09-21 18:54:13 -04:00
|
|
|
/**
|
|
|
|
* Search parameters to be included in a {@link Request}.
|
|
|
|
*/
|
2015-07-13 14:47:10 -04:00
|
|
|
search: URLSearchParams;
|
2015-08-25 04:27:59 -04:00
|
|
|
constructor({method, headers, body, url, search}: RequestOptionsArgs = {}) {
|
2015-09-21 03:46:16 -04:00
|
|
|
this.method = isPresent(method) ? normalizeMethodName(method) : null;
|
2015-06-24 03:27:07 -04:00
|
|
|
this.headers = isPresent(headers) ? headers : null;
|
|
|
|
this.body = isPresent(body) ? body : 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
|
2015-09-21 18:54:13 -04:00
|
|
|
* existing values. This method will not change the values of the instance on which it is being
|
|
|
|
* called.
|
|
|
|
*
|
|
|
|
* Note that `headers` and `search` will override existing values completely if present in
|
|
|
|
* the `options` object. If these values should be merged, it should be done prior to calling
|
|
|
|
* `merge` on the `RequestOptions` instance.
|
|
|
|
*
|
2015-10-19 10:37:32 -04:00
|
|
|
* ### Example ([live demo](http://plnkr.co/edit/6w8XA8YTkDRcPYpdB9dk?p=preview))
|
2015-09-21 18:54:13 -04:00
|
|
|
*
|
|
|
|
* ```typescript
|
2015-12-03 16:44:14 -05:00
|
|
|
* import {RequestOptions, Request, RequestMethod} from 'angular2/http';
|
2015-09-21 18:54:13 -04:00
|
|
|
*
|
|
|
|
* var options = new RequestOptions({
|
2015-12-03 16:44:14 -05:00
|
|
|
* method: RequestMethod.Post
|
2015-09-21 18:54:13 -04:00
|
|
|
* });
|
|
|
|
* var req = new Request(options.merge({
|
|
|
|
* url: 'https://google.com'
|
|
|
|
* }));
|
2015-12-03 16:44:14 -05:00
|
|
|
* console.log('req.method:', RequestMethod[req.method]); // Post
|
2015-09-21 18:54:13 -04:00
|
|
|
* console.log('options.url:', options.url); // null
|
|
|
|
* console.log('req.url:', req.url); // https://google.com
|
|
|
|
* ```
|
2015-06-09 18:18:57 -04:00
|
|
|
*/
|
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,
|
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-09-22 17:05:36 -04:00
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
2015-09-22 17:05:36 -04:00
|
|
|
* Subclass of {@link RequestOptions}, with default values.
|
|
|
|
*
|
|
|
|
* Default values:
|
2015-12-03 16:44:14 -05:00
|
|
|
* * method: {@link RequestMethod RequestMethod.Get}
|
2015-09-22 17:05:36 -04:00
|
|
|
* * headers: empty {@link Headers} object
|
2015-06-09 18:18:57 -04:00
|
|
|
*
|
2015-09-22 17:05:36 -04:00
|
|
|
* This class could be extended and bound to the {@link RequestOptions} class
|
|
|
|
* when configuring an {@link Injector}, in order to override the default options
|
|
|
|
* used by {@link Http} to create and send {@link Request Requests}.
|
|
|
|
*
|
|
|
|
* ### Example ([live demo](http://plnkr.co/edit/LEKVSx?p=preview))
|
2015-06-09 18:18:57 -04:00
|
|
|
*
|
2015-09-24 04:32:43 -04:00
|
|
|
* ```typescript
|
2015-12-11 18:01:37 -05:00
|
|
|
* import {provide} from 'angular2/core';
|
|
|
|
* import {bootstrap} from 'angular2/platform/browser';
|
2015-10-11 01:11:13 -04:00
|
|
|
* import {HTTP_PROVIDERS, Http, BaseRequestOptions, RequestOptions} from 'angular2/http';
|
2015-09-22 17:05:36 -04:00
|
|
|
* import {App} from './myapp';
|
|
|
|
*
|
|
|
|
* class MyOptions extends BaseRequestOptions {
|
|
|
|
* search: string = 'coreTeam=true';
|
2015-06-09 18:18:57 -04:00
|
|
|
* }
|
|
|
|
*
|
2015-10-12 14:30:34 -04:00
|
|
|
* bootstrap(App, [HTTP_PROVIDERS, provide(RequestOptions, {useClass: MyOptions})]);
|
2015-09-22 17:05:36 -04:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* The options could also be extended when manually creating a {@link Request}
|
|
|
|
* object.
|
|
|
|
*
|
|
|
|
* ### Example ([live demo](http://plnkr.co/edit/oyBoEvNtDhOSfi9YxaVb?p=preview))
|
|
|
|
*
|
|
|
|
* ```
|
2015-12-03 16:44:14 -05:00
|
|
|
* import {BaseRequestOptions, Request, RequestMethod} from 'angular2/http';
|
2015-09-22 17:05:36 -04:00
|
|
|
*
|
2015-09-22 20:21:05 -04:00
|
|
|
* var options = new BaseRequestOptions();
|
2015-09-22 17:05:36 -04:00
|
|
|
* var req = new Request(options.merge({
|
2015-12-03 16:44:14 -05:00
|
|
|
* method: RequestMethod.Post,
|
2015-09-22 17:05:36 -04:00
|
|
|
* url: 'https://google.com'
|
|
|
|
* }));
|
2015-12-03 16:44:14 -05:00
|
|
|
* console.log('req.method:', RequestMethod[req.method]); // Post
|
2015-09-22 17:05:36 -04:00
|
|
|
* console.log('options.url:', options.url); // null
|
|
|
|
* console.log('req.url:', req.url); // https://google.com
|
2015-06-09 18:18:57 -04:00
|
|
|
* ```
|
|
|
|
*/
|
2015-06-13 00:50:19 -04:00
|
|
|
@Injectable()
|
2015-06-13 22:48:40 -04:00
|
|
|
export class BaseRequestOptions extends RequestOptions {
|
2015-12-03 16:44:14 -05:00
|
|
|
constructor() { super({method: RequestMethod.Get, headers: new Headers()}); }
|
2015-06-13 00:50:19 -04:00
|
|
|
}
|