2015-04-29 02:07:55 -04:00
|
|
|
/// <reference path="../../typings/rx/rx.all.d.ts" />
|
|
|
|
|
|
|
|
import {Injectable} from 'angular2/src/di/decorators';
|
2015-06-13 22:48:40 -04:00
|
|
|
import {IRequestOptions, Connection} from './interfaces';
|
2015-04-29 02:07:55 -04:00
|
|
|
import {Request} from './static_request';
|
|
|
|
import {Response} from './static_response';
|
|
|
|
import {XHRBackend} from './backends/xhr_backend';
|
|
|
|
import {BaseRequestOptions} from './base_request_options';
|
2015-06-13 18:49:05 -04:00
|
|
|
import {RequestMethods} from './enums';
|
|
|
|
import {URLSearchParams} from './url_search_params';
|
2015-04-29 02:07:55 -04:00
|
|
|
import * as Rx from 'rx';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A function to perform http requests over XMLHttpRequest.
|
|
|
|
*
|
|
|
|
* #Example
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* @Component({
|
|
|
|
* appInjector: [httpBindings]
|
|
|
|
* })
|
|
|
|
* @View({
|
|
|
|
* directives: [NgFor],
|
|
|
|
* template: `
|
|
|
|
* <ul>
|
|
|
|
* <li *ng-for="#person of people">
|
|
|
|
* hello, {{person.name}}
|
|
|
|
* </li>
|
|
|
|
* </ul>
|
|
|
|
* `
|
|
|
|
* })
|
|
|
|
* class MyComponent {
|
|
|
|
* constructor(http:Http) {
|
|
|
|
* http('people.json').subscribe(res => this.people = res.json());
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* This function is bound to a single underlying connection mechanism, such as XHR, which could be
|
|
|
|
* mocked with dependency injection by replacing the `Backend` binding. For other transports, like
|
|
|
|
* JSONP or Node, a separate http function would be created, such as httpJSONP.
|
|
|
|
*
|
|
|
|
* @exportedAs angular2/http
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
|
2015-06-13 18:49:05 -04:00
|
|
|
function httpRequest(backend: XHRBackend, request: Request) {
|
|
|
|
return <Rx.Observable<Response>>(Observable.create(observer => {
|
|
|
|
var connection: Connection = backend.createConnection(request);
|
|
|
|
var internalSubscription = connection.response.subscribe(observer);
|
|
|
|
return () => {
|
|
|
|
internalSubscription.dispose();
|
|
|
|
connection.dispose();
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2015-04-29 02:07:55 -04:00
|
|
|
// Abstract
|
|
|
|
@Injectable()
|
|
|
|
export class Http {
|
2015-06-13 18:49:05 -04:00
|
|
|
constructor(private backend: XHRBackend, private defaultOptions: BaseRequestOptions) {}
|
|
|
|
|
2015-06-13 22:48:40 -04:00
|
|
|
request(url: string | Request, options?: IRequestOptions): Rx.Observable<Response> {
|
2015-06-13 19:44:32 -04:00
|
|
|
if (typeof url === 'string') {
|
|
|
|
return httpRequest(this.backend, new Request(url, this.defaultOptions.merge(options)));
|
|
|
|
} else if (url instanceof Request) {
|
|
|
|
return httpRequest(this.backend, url);
|
|
|
|
}
|
2015-06-13 18:49:05 -04:00
|
|
|
}
|
|
|
|
|
2015-06-13 22:48:40 -04:00
|
|
|
get(url: string, options?: IRequestOptions) {
|
2015-06-13 18:49:05 -04:00
|
|
|
return httpRequest(this.backend, new Request(url, this.defaultOptions.merge(options)
|
|
|
|
.merge({method: RequestMethods.GET})));
|
|
|
|
}
|
|
|
|
|
2015-06-13 22:48:40 -04:00
|
|
|
post(url: string, body: URLSearchParams | FormData | Blob | string, options?: IRequestOptions) {
|
2015-06-13 18:49:05 -04:00
|
|
|
return httpRequest(this.backend,
|
|
|
|
new Request(url, this.defaultOptions.merge(options)
|
|
|
|
|
|
|
|
.merge({body: body, method: RequestMethods.POST})));
|
|
|
|
}
|
|
|
|
|
2015-06-13 22:48:40 -04:00
|
|
|
put(url: string, body: URLSearchParams | FormData | Blob | string, options?: IRequestOptions) {
|
2015-06-13 18:49:05 -04:00
|
|
|
return httpRequest(this.backend,
|
|
|
|
new Request(url, this.defaultOptions.merge(options)
|
|
|
|
.merge({body: body, method: RequestMethods.PUT})));
|
|
|
|
}
|
|
|
|
|
2015-06-13 22:48:40 -04:00
|
|
|
delete (url: string, options?: IRequestOptions) {
|
2015-06-13 18:49:05 -04:00
|
|
|
return httpRequest(this.backend, new Request(url, this.defaultOptions.merge(options)
|
|
|
|
.merge({method: RequestMethods.DELETE})));
|
|
|
|
}
|
|
|
|
|
2015-06-13 22:48:40 -04:00
|
|
|
patch(url: string, body: URLSearchParams | FormData | Blob | string, options?: IRequestOptions) {
|
2015-06-13 18:49:05 -04:00
|
|
|
return httpRequest(this.backend,
|
|
|
|
new Request(url, this.defaultOptions.merge(options)
|
|
|
|
.merge({body: body, method: RequestMethods.PATCH})));
|
|
|
|
}
|
|
|
|
|
2015-06-13 22:48:40 -04:00
|
|
|
head(url: string, options?: IRequestOptions) {
|
2015-06-13 18:49:05 -04:00
|
|
|
return httpRequest(this.backend, new Request(url, this.defaultOptions.merge(options)
|
|
|
|
.merge({method: RequestMethods.HEAD})));
|
|
|
|
}
|
2015-04-29 02:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var Observable;
|
|
|
|
if (Rx.hasOwnProperty('default')) {
|
|
|
|
Observable = (<any>Rx).default.Rx.Observable;
|
|
|
|
} else {
|
|
|
|
Observable = Rx.Observable;
|
|
|
|
}
|
|
|
|
export function HttpFactory(backend: XHRBackend, defaultOptions: BaseRequestOptions) {
|
2015-06-13 22:48:40 -04:00
|
|
|
return function(url: string | Request, options?: IRequestOptions) {
|
2015-06-13 19:44:32 -04:00
|
|
|
if (typeof url === 'string') {
|
|
|
|
return httpRequest(backend, new Request(url, defaultOptions.merge(options)));
|
|
|
|
} else if (url instanceof Request) {
|
|
|
|
return httpRequest(backend, url);
|
|
|
|
}
|
2015-04-29 02:07:55 -04:00
|
|
|
}
|
|
|
|
}
|