2015-04-29 02:07:55 -04:00
|
|
|
import {RequestMethods, RequestModesOpts, RequestCredentialsOpts} from './enums';
|
|
|
|
import {URLSearchParams} from './url_search_params';
|
2015-06-16 13:46:12 -04:00
|
|
|
import {IRequestOptions, IRequest} from './interfaces';
|
2015-04-29 02:07:55 -04:00
|
|
|
import {Headers} from './headers';
|
|
|
|
import {BaseException, RegExpWrapper} from 'angular2/src/facade/lang';
|
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
// TODO(jeffbcross): properly implement body accessors
|
|
|
|
/**
|
|
|
|
* Creates `Request` instances with default values.
|
|
|
|
*
|
|
|
|
* The Request's interface is inspired by the Request constructor defined in the [Fetch
|
|
|
|
* Spec](https://fetch.spec.whatwg.org/#request-class),
|
|
|
|
* but is considered a static value whose body can be accessed many times. There are other
|
|
|
|
* differences in the implementation, but this is the most significant.
|
|
|
|
*/
|
2015-04-29 02:07:55 -04:00
|
|
|
export class Request implements IRequest {
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* Http method with which to perform the request.
|
|
|
|
*
|
|
|
|
* Defaults to GET.
|
|
|
|
*/
|
2015-04-29 02:07:55 -04:00
|
|
|
method: RequestMethods;
|
|
|
|
mode: RequestModesOpts;
|
|
|
|
credentials: RequestCredentialsOpts;
|
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). {@link Headers} class reference.
|
2015-04-29 02:07:55 -04:00
|
|
|
*/
|
2015-06-09 18:18:57 -04:00
|
|
|
headers: Headers;
|
2015-04-29 02:07:55 -04:00
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
private _body: URLSearchParams | FormData | Blob | string;
|
|
|
|
|
|
|
|
constructor(/** Url of the remote resource */ public url: string,
|
|
|
|
{body, method = RequestMethods.GET, mode = RequestModesOpts.Cors,
|
|
|
|
credentials = RequestCredentialsOpts.Omit,
|
|
|
|
headers = new Headers()}: IRequestOptions = {}) {
|
|
|
|
this._body = body;
|
2015-04-29 02:07:55 -04:00
|
|
|
this.method = method;
|
|
|
|
// Defaults to 'cors', consistent with browser
|
|
|
|
// TODO(jeffbcross): implement behavior
|
|
|
|
this.mode = mode;
|
|
|
|
// Defaults to 'omit', consistent with browser
|
|
|
|
// TODO(jeffbcross): implement behavior
|
|
|
|
this.credentials = credentials;
|
|
|
|
this.headers = headers;
|
|
|
|
}
|
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* Returns the request's body as string, assuming that body exists. If body is undefined, return
|
|
|
|
* empty
|
|
|
|
* string.
|
|
|
|
*/
|
|
|
|
text(): String { return this._body ? this._body.toString() : ''; }
|
2015-04-29 02:07:55 -04:00
|
|
|
}
|