2015-08-25 04:27:59 -04:00
|
|
|
import {RequestMethods} from './enums';
|
2015-06-19 15:14:12 -04:00
|
|
|
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 {
|
|
|
|
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-09-10 18:25:36 -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-09-17 19:35:49 -04:00
|
|
|
*
|
|
|
|
* `Request` instances are typically created by higher-level classes, like {@link Http} and
|
|
|
|
* {@link Jsonp}, but it may occasionally be useful to explicitly create `Request` instances.
|
|
|
|
* One such example is when creating services that wrap higher-level services, like {@link Http},
|
|
|
|
* where it may be useful to generate a `Request` with arbitrary headers and search params.
|
|
|
|
*
|
2015-09-24 04:32:43 -04:00
|
|
|
* ```typescript
|
2015-09-17 19:35:49 -04:00
|
|
|
* import {Injectable, Injector} from 'angular2/angular2';
|
2015-09-24 04:32:43 -04:00
|
|
|
* import {HTTP_BINDINGS, Http, Request, RequestMethods} from 'angular2/http';
|
2015-09-17 19:35:49 -04:00
|
|
|
*
|
|
|
|
* @Injectable()
|
|
|
|
* class AutoAuthenticator {
|
|
|
|
* constructor(public http:Http) {}
|
|
|
|
* request(url:string) {
|
|
|
|
* return this.http.request(new Request({
|
2015-09-24 04:32:43 -04:00
|
|
|
* method: RequestMethods.Get,
|
2015-09-17 19:35:49 -04:00
|
|
|
* url: url,
|
|
|
|
* search: 'password=123'
|
|
|
|
* }));
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* var injector = Injector.resolveAndCreate([HTTP_BINDINGS, AutoAuthenticator]);
|
|
|
|
* var authenticator = injector.get(AutoAuthenticator);
|
|
|
|
* authenticator.request('people.json').toRx().subscribe(res => {
|
|
|
|
* //URL should have included '?password=123'
|
|
|
|
* console.log('people', res.json());
|
|
|
|
* });
|
|
|
|
* ```
|
2015-06-09 18:18:57 -04:00
|
|
|
*/
|
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.
|
|
|
|
*/
|
2015-04-29 02:07:55 -04:00
|
|
|
method: RequestMethods;
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
2015-09-17 19:35:49 -04:00
|
|
|
* {@link Headers} instance
|
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;
|
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
|
|
|
|
// Defaults to 'omit', consistent with browser
|
|
|
|
// TODO(jeffbcross): implement behavior
|
2015-06-24 03:27:07 -04:00
|
|
|
this.headers = new Headers(requestOptions.headers);
|
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
|
|
|
}
|