2015-04-28 23:07:55 -07:00
|
|
|
import {ConnectionBackend, Connection} from '../interfaces';
|
2015-06-19 12:14:12 -07:00
|
|
|
import {ReadyStates, RequestMethods, RequestMethodsMap} from '../enums';
|
2015-04-28 23:07:55 -07:00
|
|
|
import {Request} from '../static_request';
|
|
|
|
import {Response} from '../static_response';
|
|
|
|
import {Injectable} from 'angular2/di';
|
|
|
|
import {BrowserXHR} from './browser_xhr';
|
2015-06-19 12:14:12 -07:00
|
|
|
import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
|
|
|
|
import {isPresent, ENUM_INDEX} from 'angular2/src/facade/lang';
|
2015-04-28 23:07:55 -07:00
|
|
|
|
2015-06-09 15:18:57 -07:00
|
|
|
/**
|
|
|
|
* Creates connections using `XMLHttpRequest`. Given a fully-qualified
|
|
|
|
* request, an `XHRConnection` will immediately create an `XMLHttpRequest` object and send the
|
|
|
|
* request.
|
|
|
|
*
|
|
|
|
* This class would typically not be created or interacted with directly inside applications, though
|
|
|
|
* the {@link MockConnection} may be interacted with in tests.
|
|
|
|
*/
|
2015-04-28 23:07:55 -07:00
|
|
|
export class XHRConnection implements Connection {
|
|
|
|
request: Request;
|
2015-06-09 15:18:57 -07:00
|
|
|
/**
|
|
|
|
* Response
|
|
|
|
* [Subject](https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/subjects/subject.md)
|
|
|
|
* which emits a single {@link Response} value on load event of `XMLHttpRequest`.
|
|
|
|
*/
|
2015-06-19 12:14:12 -07:00
|
|
|
response: EventEmitter; //<Response>;
|
2015-04-28 23:07:55 -07:00
|
|
|
readyState: ReadyStates;
|
|
|
|
private _xhr;
|
2015-06-19 12:14:12 -07:00
|
|
|
constructor(req: Request, browserXHR: BrowserXHR) {
|
|
|
|
// TODO: get rid of this when enum lookups are available in ts2dart
|
|
|
|
// https://github.com/angular/ts2dart/issues/221
|
|
|
|
var requestMethodsMap = new RequestMethodsMap();
|
2015-04-28 23:07:55 -07:00
|
|
|
this.request = req;
|
2015-06-19 12:14:12 -07:00
|
|
|
this.response = new EventEmitter();
|
|
|
|
this._xhr = browserXHR.build();
|
2015-06-09 15:18:57 -07:00
|
|
|
// TODO(jeffbcross): implement error listening/propagation
|
2015-06-19 12:14:12 -07:00
|
|
|
this._xhr.open(requestMethodsMap.getMethod(ENUM_INDEX(req.method)), req.url);
|
2015-04-28 23:07:55 -07:00
|
|
|
this._xhr.addEventListener(
|
|
|
|
'load',
|
2015-06-19 12:14:12 -07:00
|
|
|
(_) => {ObservableWrapper.callNext(
|
|
|
|
this.response, new Response({
|
|
|
|
body: isPresent(this._xhr.response) ? this._xhr.response : this._xhr.responseText
|
|
|
|
}))});
|
2015-04-28 23:07:55 -07:00
|
|
|
// TODO(jeffbcross): make this more dynamic based on body type
|
|
|
|
this._xhr.send(this.request.text());
|
|
|
|
}
|
|
|
|
|
2015-06-09 15:18:57 -07:00
|
|
|
/**
|
|
|
|
* Calls abort on the underlying XMLHttpRequest.
|
|
|
|
*/
|
2015-04-28 23:07:55 -07:00
|
|
|
dispose(): void { this._xhr.abort(); }
|
|
|
|
}
|
|
|
|
|
2015-06-09 15:18:57 -07:00
|
|
|
/**
|
|
|
|
* Creates {@link XHRConnection} instances.
|
|
|
|
*
|
|
|
|
* This class would typically not be used by end users, but could be
|
|
|
|
* overridden if a different backend implementation should be used,
|
|
|
|
* such as in a node backend.
|
|
|
|
*
|
|
|
|
* #Example
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* import {Http, MyNodeBackend, httpInjectables, BaseRequestOptions} from 'angular2/http';
|
|
|
|
* @Component({
|
|
|
|
* appInjector: [
|
|
|
|
* httpInjectables,
|
|
|
|
* bind(Http).toFactory((backend, options) => {
|
|
|
|
* return new Http(backend, options);
|
|
|
|
* }, [MyNodeBackend, BaseRequestOptions])]
|
|
|
|
* })
|
|
|
|
* class MyComponent {
|
|
|
|
* constructor(http:Http) {
|
|
|
|
* http('people.json').subscribe(res => this.people = res.json());
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
**/
|
2015-04-28 23:07:55 -07:00
|
|
|
@Injectable()
|
|
|
|
export class XHRBackend implements ConnectionBackend {
|
2015-06-19 12:14:12 -07:00
|
|
|
constructor(private _browserXHR: BrowserXHR) {}
|
2015-04-28 23:07:55 -07:00
|
|
|
createConnection(request: Request): XHRConnection {
|
2015-06-19 12:14:12 -07:00
|
|
|
return new XHRConnection(request, this._browserXHR);
|
2015-04-28 23:07:55 -07:00
|
|
|
}
|
|
|
|
}
|