2015-06-19 15:14:12 -04:00
|
|
|
import {RequestMethods, RequestModesOpts, RequestCredentialsOpts, RequestCacheOpts} from './enums';
|
|
|
|
import {RequestOptions} from './base_request_options';
|
2015-04-29 02:07:55 -04:00
|
|
|
import {Headers} from './headers';
|
2015-06-24 03:27:07 -04:00
|
|
|
import {
|
|
|
|
BaseException,
|
|
|
|
RegExpWrapper,
|
|
|
|
CONST_EXPR,
|
|
|
|
isPresent,
|
2015-07-13 14:47:10 -04:00
|
|
|
isJsObject,
|
|
|
|
StringWrapper
|
2015-08-20 17:28:25 -04:00
|
|
|
} from 'angular2/src/core/facade/lang';
|
2015-04-29 02:07:55 -04:00
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
// TODO(jeffbcross): properly implement body accessors
|
|
|
|
/**
|
2015-06-24 03:27:07 -04:00
|
|
|
* Creates `Request` instances from provided values.
|
2015-06-09 18:18:57 -04:00
|
|
|
*
|
|
|
|
* 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-06-19 15:14:12 -04:00
|
|
|
export class Request {
|
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-06-19 15:14:12 -04:00
|
|
|
/** Url of the remote resource */
|
|
|
|
url: string;
|
|
|
|
// TODO: support URLSearchParams | FormData | Blob | ArrayBuffer
|
|
|
|
private _body: string;
|
|
|
|
cache: RequestCacheOpts;
|
2015-06-24 03:27:07 -04:00
|
|
|
constructor(requestOptions: RequestOptions) {
|
|
|
|
// TODO: assert that url is present
|
2015-07-13 14:47:10 -04:00
|
|
|
let url = requestOptions.url;
|
2015-06-19 15:14:12 -04:00
|
|
|
this.url = requestOptions.url;
|
2015-07-13 14:47:10 -04:00
|
|
|
if (isPresent(requestOptions.search)) {
|
|
|
|
let search = requestOptions.search.toString();
|
|
|
|
if (search.length > 0) {
|
|
|
|
let prefix = '?';
|
|
|
|
if (StringWrapper.contains(this.url, '?')) {
|
|
|
|
prefix = (this.url[this.url.length - 1] == '&') ? '' : '&';
|
|
|
|
}
|
|
|
|
// TODO: just delete search-query-looking string in url?
|
|
|
|
this.url = url + prefix + search;
|
|
|
|
}
|
|
|
|
}
|
2015-06-19 15:14:12 -04:00
|
|
|
this._body = requestOptions.body;
|
|
|
|
this.method = requestOptions.method;
|
2015-04-29 02:07:55 -04:00
|
|
|
// TODO(jeffbcross): implement behavior
|
2015-06-19 15:14:12 -04:00
|
|
|
this.mode = requestOptions.mode;
|
2015-04-29 02:07:55 -04:00
|
|
|
// Defaults to 'omit', consistent with browser
|
|
|
|
// TODO(jeffbcross): implement behavior
|
2015-06-19 15:14:12 -04:00
|
|
|
this.credentials = requestOptions.credentials;
|
2015-06-24 03:27:07 -04:00
|
|
|
this.headers = new Headers(requestOptions.headers);
|
2015-06-19 15:14:12 -04:00
|
|
|
this.cache = requestOptions.cache;
|
2015-04-29 02:07:55 -04:00
|
|
|
}
|
|
|
|
|
2015-06-24 03:27:07 -04:00
|
|
|
|
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.
|
|
|
|
*/
|
2015-06-19 15:14:12 -04:00
|
|
|
text(): String { return isPresent(this._body) ? this._body.toString() : ''; }
|
2015-04-29 02:07:55 -04:00
|
|
|
}
|