2015-04-28 23:07:55 -07:00
|
|
|
import {ConnectionBackend, Connection} from '../interfaces';
|
2015-08-26 14:26:13 +12:00
|
|
|
import {ReadyStates, RequestMethods, ResponseTypes} from '../enums';
|
2015-04-28 23:07:55 -07:00
|
|
|
import {Request} from '../static_request';
|
|
|
|
import {Response} from '../static_response';
|
2015-06-24 00:27:07 -07:00
|
|
|
import {ResponseOptions, BaseResponseOptions} from '../base_response_options';
|
2015-09-03 22:01:36 -07:00
|
|
|
import {Injectable} from 'angular2/src/core/di';
|
2015-06-24 00:27:07 -07:00
|
|
|
import {BrowserXhr} from './browser_xhr';
|
2015-08-27 13:51:32 +12:00
|
|
|
import {isPresent} from 'angular2/src/core/facade/lang';
|
2015-09-25 18:53:32 -04:00
|
|
|
var Observable = require('@reactivex/rxjs/dist/cjs/Observable');
|
2015-06-09 15:18:57 -07:00
|
|
|
/**
|
2015-09-25 18:53:32 -04: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
|
|
|
/**
|
2015-06-24 00:27:07 -07:00
|
|
|
* Response {@link EventEmitter} which emits a single {@link Response} value on load event of
|
|
|
|
* `XMLHttpRequest`.
|
2015-06-09 15:18:57 -07:00
|
|
|
*/
|
2015-09-25 18:53:32 -04:00
|
|
|
response: any; // TODO: Make generic of <Response>;
|
2015-04-28 23:07:55 -07:00
|
|
|
readyState: ReadyStates;
|
2015-06-24 00:27:07 -07:00
|
|
|
constructor(req: Request, browserXHR: BrowserXhr, baseResponseOptions?: ResponseOptions) {
|
2015-04-28 23:07:55 -07:00
|
|
|
this.request = req;
|
2015-09-25 18:53:32 -04:00
|
|
|
this.response = new Observable(responseObserver => {
|
|
|
|
let _xhr: XMLHttpRequest = browserXHR.build();
|
|
|
|
_xhr.open(RequestMethods[req.method].toUpperCase(), req.url);
|
|
|
|
// load event handler
|
|
|
|
let onLoad = () => {
|
|
|
|
// responseText is the old-school way of retrieving response (supported by IE8 & 9)
|
|
|
|
// response/responseType properties were introduced in XHR Level2 spec (supported by
|
|
|
|
// IE10)
|
|
|
|
let response = isPresent(_xhr.response) ? _xhr.response : _xhr.responseText;
|
2015-07-05 01:58:37 -07:00
|
|
|
|
2015-09-25 18:53:32 -04:00
|
|
|
// normalize IE9 bug (http://bugs.jquery.com/ticket/1450)
|
|
|
|
let status = _xhr.status === 1223 ? 204 : _xhr.status;
|
2015-07-05 01:58:37 -07:00
|
|
|
|
2015-09-25 18:53:32 -04:00
|
|
|
// fix status code when it is 0 (0 status is undocumented).
|
|
|
|
// Occurs when accessing file resources or on Android 4.1 stock browser
|
|
|
|
// while retrieving files from application cache.
|
|
|
|
if (status === 0) {
|
|
|
|
status = response ? 200 : 0;
|
|
|
|
}
|
|
|
|
var responseOptions = new ResponseOptions({body: response, status: status});
|
|
|
|
if (isPresent(baseResponseOptions)) {
|
|
|
|
responseOptions = baseResponseOptions.merge(responseOptions);
|
|
|
|
}
|
|
|
|
responseObserver.next(new Response(responseOptions));
|
|
|
|
// TODO(gdi2290): defer complete if array buffer until done
|
|
|
|
responseObserver.complete();
|
|
|
|
};
|
|
|
|
// error event handler
|
|
|
|
let onError = (err) => {
|
|
|
|
var responseOptions = new ResponseOptions({body: err, type: ResponseTypes.Error});
|
|
|
|
if (isPresent(baseResponseOptions)) {
|
|
|
|
responseOptions = baseResponseOptions.merge(responseOptions);
|
|
|
|
}
|
|
|
|
responseObserver.error(new Response(responseOptions));
|
|
|
|
};
|
2015-07-05 01:58:37 -07:00
|
|
|
|
2015-09-25 18:53:32 -04:00
|
|
|
if (isPresent(req.headers)) {
|
|
|
|
req.headers.forEach((value, name) => { _xhr.setRequestHeader(name, value); });
|
2015-06-24 00:27:07 -07:00
|
|
|
}
|
|
|
|
|
2015-09-25 18:53:32 -04:00
|
|
|
_xhr.addEventListener('load', onLoad);
|
|
|
|
_xhr.addEventListener('error', onError);
|
2015-08-08 14:17:43 -07:00
|
|
|
|
2015-09-25 18:53:32 -04:00
|
|
|
_xhr.send(this.request.text());
|
2015-07-02 01:20:09 +03:00
|
|
|
|
2015-09-25 18:53:32 -04:00
|
|
|
return () => {
|
|
|
|
_xhr.removeEventListener('load', onLoad);
|
|
|
|
_xhr.removeEventListener('error', onError);
|
|
|
|
_xhr.abort();
|
|
|
|
};
|
|
|
|
});
|
2015-04-28 23:07:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* ```
|
2015-08-20 14:28:25 -07:00
|
|
|
* import {Http, MyNodeBackend, HTTP_BINDINGS, BaseRequestOptions} from 'angular2/http';
|
2015-06-09 15:18:57 -07:00
|
|
|
* @Component({
|
2015-07-29 15:01:22 -07:00
|
|
|
* viewBindings: [
|
2015-08-10 21:42:47 -07:00
|
|
|
* HTTP_BINDINGS,
|
2015-06-09 15:18:57 -07:00
|
|
|
* bind(Http).toFactory((backend, options) => {
|
|
|
|
* return new Http(backend, options);
|
|
|
|
* }, [MyNodeBackend, BaseRequestOptions])]
|
|
|
|
* })
|
|
|
|
* class MyComponent {
|
|
|
|
* constructor(http:Http) {
|
2015-09-01 09:55:37 +02:00
|
|
|
* http('people.json').toRx().subscribe(res => this.people = res.json());
|
2015-06-09 15:18:57 -07:00
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
**/
|
2015-04-28 23:07:55 -07:00
|
|
|
@Injectable()
|
|
|
|
export class XHRBackend implements ConnectionBackend {
|
2015-06-24 00:27:07 -07:00
|
|
|
constructor(private _browserXHR: BrowserXhr, private _baseResponseOptions: ResponseOptions) {}
|
2015-04-28 23:07:55 -07:00
|
|
|
createConnection(request: Request): XHRConnection {
|
2015-06-24 00:27:07 -07:00
|
|
|
return new XHRConnection(request, this._browserXHR, this._baseResponseOptions);
|
2015-04-28 23:07:55 -07:00
|
|
|
}
|
|
|
|
}
|