2015-09-10 18:25:36 -04:00
|
|
|
import {isString, isPresent, isBlank} from 'angular2/src/core/facade/lang';
|
|
|
|
import {makeTypeError} from 'angular2/src/core/facade/exceptions';
|
2015-08-20 17:28:25 -04:00
|
|
|
import {Injectable} from 'angular2/src/core/di/decorators';
|
2015-08-11 18:01:29 -04:00
|
|
|
import {RequestOptionsArgs, Connection, ConnectionBackend} from './interfaces';
|
2015-04-29 02:07:55 -04:00
|
|
|
import {Request} from './static_request';
|
2015-06-19 15:14:12 -04:00
|
|
|
import {BaseRequestOptions, RequestOptions} from './base_request_options';
|
2015-06-13 18:49:05 -04:00
|
|
|
import {RequestMethods} from './enums';
|
2015-08-20 17:28:25 -04:00
|
|
|
import {EventEmitter} from 'angular2/src/core/facade/async';
|
2015-06-19 15:14:12 -04:00
|
|
|
|
|
|
|
function httpRequest(backend: ConnectionBackend, request: Request): EventEmitter {
|
|
|
|
return backend.createConnection(request).response;
|
|
|
|
}
|
2015-04-29 02:07:55 -04:00
|
|
|
|
2015-06-19 15:14:12 -04:00
|
|
|
function mergeOptions(defaultOpts, providedOpts, method, url): RequestOptions {
|
|
|
|
var newOptions = defaultOpts;
|
|
|
|
if (isPresent(providedOpts)) {
|
2015-06-24 03:27:07 -04:00
|
|
|
// Hack so Dart can used named parameters
|
|
|
|
newOptions = newOptions.merge(new RequestOptions({
|
|
|
|
method: providedOpts.method,
|
|
|
|
url: providedOpts.url,
|
2015-07-13 14:47:10 -04:00
|
|
|
search: providedOpts.search,
|
2015-06-24 03:27:07 -04:00
|
|
|
headers: providedOpts.headers,
|
2015-09-25 15:58:00 -04:00
|
|
|
body: providedOpts.body
|
2015-06-24 03:27:07 -04:00
|
|
|
}));
|
2015-06-19 15:14:12 -04:00
|
|
|
}
|
|
|
|
if (isPresent(method)) {
|
2015-06-24 03:27:07 -04:00
|
|
|
return newOptions.merge(new RequestOptions({method: method, url: url}));
|
2015-06-19 15:14:12 -04:00
|
|
|
} else {
|
2015-06-24 03:27:07 -04:00
|
|
|
return newOptions.merge(new RequestOptions({url: url}));
|
2015-06-19 15:14:12 -04:00
|
|
|
}
|
2015-06-09 18:18:57 -04:00
|
|
|
}
|
|
|
|
|
2015-04-29 02:07:55 -04:00
|
|
|
/**
|
2015-06-09 18:18:57 -04:00
|
|
|
* Performs http requests using `XMLHttpRequest` as the default backend.
|
|
|
|
*
|
|
|
|
* `Http` is available as an injectable class, with methods to perform http requests. Calling
|
2015-09-25 18:53:32 -04:00
|
|
|
* `request` returns an {@link Observable} which will emit a single {@link Response} when a
|
2015-07-01 13:15:58 -04:00
|
|
|
* response is received.
|
|
|
|
*
|
2015-04-29 02:07:55 -04:00
|
|
|
* #Example
|
|
|
|
*
|
2015-09-24 04:32:43 -04:00
|
|
|
* ```typescript
|
2015-08-11 00:42:47 -04:00
|
|
|
* import {Http, HTTP_BINDINGS} from 'angular2/http';
|
|
|
|
* @Component({selector: 'http-app', viewBindings: [HTTP_BINDINGS]})
|
2015-06-09 18:18:57 -04:00
|
|
|
* @View({templateUrl: 'people.html'})
|
|
|
|
* class PeopleComponent {
|
|
|
|
* constructor(http: Http) {
|
2015-07-01 13:15:58 -04:00
|
|
|
* http.get('people.json')
|
2015-06-09 18:18:57 -04:00
|
|
|
* // Call map on the response observable to get the parsed people object
|
|
|
|
* .map(res => res.json())
|
|
|
|
* // Subscribe to the observable to get the parsed people object and attach it to the
|
|
|
|
* // component
|
|
|
|
* .subscribe(people => this.people = people);
|
|
|
|
* }
|
2015-04-29 02:07:55 -04:00
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
2015-07-01 13:15:58 -04:00
|
|
|
*
|
|
|
|
* #Example
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* http.get('people.json').observer({next: (value) => this.people = people});
|
|
|
|
* ```
|
|
|
|
*
|
2015-06-09 18:18:57 -04:00
|
|
|
* The default construct used to perform requests, `XMLHttpRequest`, is abstracted as a "Backend" (
|
|
|
|
* {@link XHRBackend} in this case), which could be mocked with dependency injection by replacing
|
|
|
|
* the {@link XHRBackend} binding, as in the following example:
|
2015-04-29 02:07:55 -04:00
|
|
|
*
|
2015-06-09 18:18:57 -04:00
|
|
|
* #Example
|
2015-04-29 02:07:55 -04:00
|
|
|
*
|
2015-09-24 04:32:43 -04:00
|
|
|
* ```typescript
|
2015-06-09 18:18:57 -04:00
|
|
|
* import {MockBackend, BaseRequestOptions, Http} from 'angular2/http';
|
|
|
|
* var injector = Injector.resolveAndCreate([
|
|
|
|
* BaseRequestOptions,
|
|
|
|
* MockBackend,
|
|
|
|
* bind(Http).toFactory(
|
|
|
|
* function(backend, defaultOptions) {
|
|
|
|
* return new Http(backend, defaultOptions);
|
|
|
|
* },
|
|
|
|
* [MockBackend, BaseRequestOptions])
|
|
|
|
* ]);
|
|
|
|
* var http = injector.get(Http);
|
2015-09-25 18:53:32 -04:00
|
|
|
* http.get('request-from-mock-backend.json').subscribe((res:Response) => doSomething(res));
|
2015-06-09 18:18:57 -04:00
|
|
|
* ```
|
2015-04-29 02:07:55 -04:00
|
|
|
*
|
|
|
|
**/
|
|
|
|
@Injectable()
|
|
|
|
export class Http {
|
2015-07-14 20:53:04 -04:00
|
|
|
constructor(protected _backend: ConnectionBackend, protected _defaultOptions: RequestOptions) {}
|
2015-06-13 18:49:05 -04:00
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* Performs any type of http request. First argument is required, and can either be a url or
|
|
|
|
* a {@link Request} instance. If the first argument is a url, an optional {@link RequestOptions}
|
|
|
|
* object can be provided as the 2nd argument. The options object will be merged with the values
|
|
|
|
* of {@link BaseRequestOptions} before performing the request.
|
|
|
|
*/
|
2015-08-11 18:01:29 -04:00
|
|
|
request(url: string | Request, options?: RequestOptionsArgs): EventEmitter {
|
2015-06-19 15:14:12 -04:00
|
|
|
var responseObservable: EventEmitter;
|
|
|
|
if (isString(url)) {
|
|
|
|
responseObservable = httpRequest(
|
|
|
|
this._backend,
|
2015-08-26 16:40:12 -04:00
|
|
|
new Request(mergeOptions(this._defaultOptions, options, RequestMethods.Get, url)));
|
2015-06-13 19:44:32 -04:00
|
|
|
} else if (url instanceof Request) {
|
2015-06-19 15:14:12 -04:00
|
|
|
responseObservable = httpRequest(this._backend, url);
|
2015-09-18 16:30:05 -04:00
|
|
|
} else {
|
|
|
|
throw makeTypeError('First argument must be a url string or Request instance.');
|
2015-06-13 19:44:32 -04:00
|
|
|
}
|
2015-06-19 15:14:12 -04:00
|
|
|
return responseObservable;
|
2015-06-13 18:49:05 -04:00
|
|
|
}
|
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* Performs a request with `get` http method.
|
|
|
|
*/
|
2015-08-11 18:01:29 -04:00
|
|
|
get(url: string, options?: RequestOptionsArgs): EventEmitter {
|
2015-06-19 15:14:12 -04:00
|
|
|
return httpRequest(this._backend, new Request(mergeOptions(this._defaultOptions, options,
|
2015-08-26 16:40:12 -04:00
|
|
|
RequestMethods.Get, url)));
|
2015-06-13 18:49:05 -04:00
|
|
|
}
|
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* Performs a request with `post` http method.
|
|
|
|
*/
|
2015-08-11 18:01:29 -04:00
|
|
|
post(url: string, body: string, options?: RequestOptionsArgs): EventEmitter {
|
2015-06-24 03:27:07 -04:00
|
|
|
return httpRequest(
|
|
|
|
this._backend,
|
|
|
|
new Request(mergeOptions(this._defaultOptions.merge(new RequestOptions({body: body})),
|
2015-08-26 16:40:12 -04:00
|
|
|
options, RequestMethods.Post, url)));
|
2015-06-13 18:49:05 -04:00
|
|
|
}
|
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* Performs a request with `put` http method.
|
|
|
|
*/
|
2015-08-11 18:01:29 -04:00
|
|
|
put(url: string, body: string, options?: RequestOptionsArgs): EventEmitter {
|
2015-06-24 03:27:07 -04:00
|
|
|
return httpRequest(
|
|
|
|
this._backend,
|
|
|
|
new Request(mergeOptions(this._defaultOptions.merge(new RequestOptions({body: body})),
|
2015-08-26 16:40:12 -04:00
|
|
|
options, RequestMethods.Put, url)));
|
2015-06-13 18:49:05 -04:00
|
|
|
}
|
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* Performs a request with `delete` http method.
|
|
|
|
*/
|
2015-08-11 18:01:29 -04:00
|
|
|
delete (url: string, options?: RequestOptionsArgs): EventEmitter {
|
2015-06-19 15:14:12 -04:00
|
|
|
return httpRequest(this._backend, new Request(mergeOptions(this._defaultOptions, options,
|
2015-08-26 16:40:12 -04:00
|
|
|
RequestMethods.Delete, url)));
|
2015-06-13 18:49:05 -04:00
|
|
|
}
|
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* Performs a request with `patch` http method.
|
|
|
|
*/
|
2015-08-11 18:01:29 -04:00
|
|
|
patch(url: string, body: string, options?: RequestOptionsArgs): EventEmitter {
|
2015-06-24 03:27:07 -04:00
|
|
|
return httpRequest(
|
|
|
|
this._backend,
|
|
|
|
new Request(mergeOptions(this._defaultOptions.merge(new RequestOptions({body: body})),
|
2015-08-26 16:40:12 -04:00
|
|
|
options, RequestMethods.Patch, url)));
|
2015-06-13 18:49:05 -04:00
|
|
|
}
|
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* Performs a request with `head` http method.
|
|
|
|
*/
|
2015-08-11 18:01:29 -04:00
|
|
|
head(url: string, options?: RequestOptionsArgs): EventEmitter {
|
2015-06-19 15:14:12 -04:00
|
|
|
return httpRequest(this._backend, new Request(mergeOptions(this._defaultOptions, options,
|
2015-08-26 16:40:12 -04:00
|
|
|
RequestMethods.Head, url)));
|
2015-06-13 18:49:05 -04:00
|
|
|
}
|
2015-04-29 02:07:55 -04:00
|
|
|
}
|
2015-07-14 20:53:04 -04:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class Jsonp extends Http {
|
|
|
|
constructor(backend: ConnectionBackend, defaultOptions: RequestOptions) {
|
|
|
|
super(backend, defaultOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs any type of http request. First argument is required, and can either be a url or
|
|
|
|
* a {@link Request} instance. If the first argument is a url, an optional {@link RequestOptions}
|
|
|
|
* object can be provided as the 2nd argument. The options object will be merged with the values
|
|
|
|
* of {@link BaseRequestOptions} before performing the request.
|
|
|
|
*/
|
2015-08-11 18:01:29 -04:00
|
|
|
request(url: string | Request, options?: RequestOptionsArgs): EventEmitter {
|
2015-07-14 20:53:04 -04:00
|
|
|
var responseObservable: EventEmitter;
|
|
|
|
if (isString(url)) {
|
2015-08-26 16:40:12 -04:00
|
|
|
url = new Request(mergeOptions(this._defaultOptions, options, RequestMethods.Get, url));
|
2015-07-14 20:53:04 -04:00
|
|
|
}
|
|
|
|
if (url instanceof Request) {
|
2015-08-26 16:40:12 -04:00
|
|
|
if (url.method !== RequestMethods.Get) {
|
2015-07-14 20:53:04 -04:00
|
|
|
makeTypeError('JSONP requests must use GET request method.');
|
|
|
|
}
|
|
|
|
responseObservable = httpRequest(this._backend, url);
|
2015-09-18 16:30:05 -04:00
|
|
|
} else {
|
|
|
|
throw makeTypeError('First argument must be a url string or Request instance.');
|
2015-07-14 20:53:04 -04:00
|
|
|
}
|
|
|
|
return responseObservable;
|
|
|
|
}
|
|
|
|
}
|