2015-06-19 15:14:12 -04:00
|
|
|
import {isString, isPresent, isBlank} from 'angular2/src/facade/lang';
|
2015-04-29 02:07:55 -04:00
|
|
|
import {Injectable} from 'angular2/src/di/decorators';
|
2015-06-19 15:14:12 -04:00
|
|
|
import {IRequestOptions, 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-06-19 15:14:12 -04:00
|
|
|
import {EventEmitter} from 'angular2/src/facade/async';
|
|
|
|
|
|
|
|
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)) {
|
|
|
|
newOptions = newOptions.merge(providedOpts);
|
|
|
|
}
|
|
|
|
if (isPresent(method)) {
|
|
|
|
return newOptions.merge({method: method, url: url});
|
|
|
|
} else {
|
|
|
|
return newOptions.merge({url: url});
|
|
|
|
}
|
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
|
|
|
|
* `request` returns an
|
|
|
|
* [Observable](https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/observable.md),
|
|
|
|
* which will emit a single {@link Response} when a response is
|
|
|
|
* received.
|
2015-04-29 02:07:55 -04:00
|
|
|
*
|
|
|
|
* #Example
|
|
|
|
*
|
|
|
|
* ```
|
2015-06-09 18:18:57 -04:00
|
|
|
* import {Http, httpInjectables} from 'angular2/http';
|
|
|
|
* @Component({selector: 'http-app', appInjector: [httpInjectables]})
|
|
|
|
* @View({templateUrl: 'people.html'})
|
|
|
|
* class PeopleComponent {
|
|
|
|
* constructor(http: Http) {
|
|
|
|
* http('people.json')
|
|
|
|
* // 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-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-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);
|
|
|
|
* http.get('request-from-mock-backend.json').subscribe((res:Response) => doSomething(res));
|
|
|
|
* ```
|
2015-04-29 02:07:55 -04:00
|
|
|
*
|
|
|
|
**/
|
|
|
|
@Injectable()
|
|
|
|
export class Http {
|
2015-06-19 15:14:12 -04:00
|
|
|
constructor(private _backend: ConnectionBackend, private _defaultOptions: BaseRequestOptions) {}
|
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-06-19 15:14:12 -04:00
|
|
|
request(url: string | Request, options?: IRequestOptions): EventEmitter {
|
|
|
|
var responseObservable: EventEmitter;
|
|
|
|
if (isString(url)) {
|
|
|
|
responseObservable = httpRequest(
|
|
|
|
this._backend,
|
|
|
|
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-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-06-19 15:14:12 -04:00
|
|
|
get(url: string, options?: IRequestOptions) {
|
|
|
|
return httpRequest(this._backend, new Request(mergeOptions(this._defaultOptions, options,
|
|
|
|
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-06-19 15:14:12 -04:00
|
|
|
post(url: string, body: string, options?: IRequestOptions) {
|
2015-06-09 18:18:57 -04:00
|
|
|
return httpRequest(this._backend,
|
2015-06-19 15:14:12 -04:00
|
|
|
new Request(mergeOptions(this._defaultOptions.merge({body: body}), 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-06-19 15:14:12 -04:00
|
|
|
put(url: string, body: string, options?: IRequestOptions) {
|
2015-06-09 18:18:57 -04:00
|
|
|
return httpRequest(this._backend,
|
2015-06-19 15:14:12 -04:00
|
|
|
new Request(mergeOptions(this._defaultOptions.merge({body: body}), 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-06-19 15:14:12 -04:00
|
|
|
delete (url: string, options?: IRequestOptions) {
|
|
|
|
return httpRequest(this._backend, new Request(mergeOptions(this._defaultOptions, options,
|
|
|
|
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-06-19 15:14:12 -04:00
|
|
|
patch(url: string, body: string, options?: IRequestOptions) {
|
2015-06-09 18:18:57 -04:00
|
|
|
return httpRequest(this._backend,
|
2015-06-19 15:14:12 -04:00
|
|
|
new Request(mergeOptions(this._defaultOptions.merge({body: body}), 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-06-19 15:14:12 -04:00
|
|
|
head(url: string, options?: IRequestOptions) {
|
|
|
|
return httpRequest(this._backend, new Request(mergeOptions(this._defaultOptions, options,
|
|
|
|
RequestMethods.HEAD, url)));
|
2015-06-13 18:49:05 -04:00
|
|
|
}
|
2015-04-29 02:07:55 -04:00
|
|
|
}
|