2 var __extends = (this && this.__extends) || (function () {
3 var extendStatics = function (d, b) {
4 extendStatics = Object.setPrototypeOf ||
5 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7 return extendStatics(d, b);
9 return function (d, b) {
11 function __() { this.constructor = d; }
12 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15 Object.defineProperty(exports, "__esModule", { value: true });
16 var root_1 = require("../../util/root");
17 var Observable_1 = require("../../Observable");
18 var Subscriber_1 = require("../../Subscriber");
19 var map_1 = require("../../operators/map");
20 function getCORSRequest() {
21 if (root_1.root.XMLHttpRequest) {
22 return new root_1.root.XMLHttpRequest();
24 else if (!!root_1.root.XDomainRequest) {
25 return new root_1.root.XDomainRequest();
28 throw new Error('CORS is not supported by your browser');
31 function getXMLHttpRequest() {
32 if (root_1.root.XMLHttpRequest) {
33 return new root_1.root.XMLHttpRequest();
38 var progIds = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'];
39 for (var i = 0; i < 3; i++) {
42 if (new root_1.root.ActiveXObject(progId)) {
49 return new root_1.root.ActiveXObject(progId);
52 throw new Error('XMLHttpRequest is not supported by your browser');
56 function ajaxGet(url, headers) {
57 if (headers === void 0) { headers = null; }
58 return new AjaxObservable({ method: 'GET', url: url, headers: headers });
60 exports.ajaxGet = ajaxGet;
61 function ajaxPost(url, body, headers) {
62 return new AjaxObservable({ method: 'POST', url: url, body: body, headers: headers });
64 exports.ajaxPost = ajaxPost;
65 function ajaxDelete(url, headers) {
66 return new AjaxObservable({ method: 'DELETE', url: url, headers: headers });
68 exports.ajaxDelete = ajaxDelete;
69 function ajaxPut(url, body, headers) {
70 return new AjaxObservable({ method: 'PUT', url: url, body: body, headers: headers });
72 exports.ajaxPut = ajaxPut;
73 function ajaxPatch(url, body, headers) {
74 return new AjaxObservable({ method: 'PATCH', url: url, body: body, headers: headers });
76 exports.ajaxPatch = ajaxPatch;
77 var mapResponse = map_1.map(function (x, index) { return x.response; });
78 function ajaxGetJSON(url, headers) {
79 return mapResponse(new AjaxObservable({
86 exports.ajaxGetJSON = ajaxGetJSON;
87 var AjaxObservable = (function (_super) {
88 __extends(AjaxObservable, _super);
89 function AjaxObservable(urlOrRequest) {
90 var _this = _super.call(this) || this;
93 createXHR: function () {
94 return this.crossDomain ? getCORSRequest() : getXMLHttpRequest();
97 withCredentials: false,
100 responseType: 'json',
103 if (typeof urlOrRequest === 'string') {
104 request.url = urlOrRequest;
107 for (var prop in urlOrRequest) {
108 if (urlOrRequest.hasOwnProperty(prop)) {
109 request[prop] = urlOrRequest[prop];
113 _this.request = request;
116 AjaxObservable.prototype._subscribe = function (subscriber) {
117 return new AjaxSubscriber(subscriber, this.request);
119 AjaxObservable.create = (function () {
120 var create = function (urlOrRequest) {
121 return new AjaxObservable(urlOrRequest);
123 create.get = ajaxGet;
124 create.post = ajaxPost;
125 create.delete = ajaxDelete;
126 create.put = ajaxPut;
127 create.patch = ajaxPatch;
128 create.getJSON = ajaxGetJSON;
131 return AjaxObservable;
132 }(Observable_1.Observable));
133 exports.AjaxObservable = AjaxObservable;
134 var AjaxSubscriber = (function (_super) {
135 __extends(AjaxSubscriber, _super);
136 function AjaxSubscriber(destination, request) {
137 var _this = _super.call(this, destination) || this;
138 _this.request = request;
140 var headers = request.headers = request.headers || {};
141 if (!request.crossDomain && !_this.getHeader(headers, 'X-Requested-With')) {
142 headers['X-Requested-With'] = 'XMLHttpRequest';
144 var contentTypeHeader = _this.getHeader(headers, 'Content-Type');
145 if (!contentTypeHeader && !(root_1.root.FormData && request.body instanceof root_1.root.FormData) && typeof request.body !== 'undefined') {
146 headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
148 request.body = _this.serializeBody(request.body, _this.getHeader(request.headers, 'Content-Type'));
152 AjaxSubscriber.prototype.next = function (e) {
154 var _a = this, xhr = _a.xhr, request = _a.request, destination = _a.destination;
157 result = new AjaxResponse(e, xhr, request);
160 return destination.error(err);
162 destination.next(result);
164 AjaxSubscriber.prototype.send = function () {
165 var _a = this, request = _a.request, _b = _a.request, user = _b.user, method = _b.method, url = _b.url, async = _b.async, password = _b.password, headers = _b.headers, body = _b.body;
167 var xhr = this.xhr = request.createXHR();
168 this.setupEvents(xhr, request);
170 xhr.open(method, url, async, user, password);
173 xhr.open(method, url, async);
176 xhr.timeout = request.timeout;
177 xhr.responseType = request.responseType;
179 if ('withCredentials' in xhr) {
180 xhr.withCredentials = !!request.withCredentials;
182 this.setHeaders(xhr, headers);
194 AjaxSubscriber.prototype.serializeBody = function (body, contentType) {
195 if (!body || typeof body === 'string') {
198 else if (root_1.root.FormData && body instanceof root_1.root.FormData) {
202 var splitIndex = contentType.indexOf(';');
203 if (splitIndex !== -1) {
204 contentType = contentType.substring(0, splitIndex);
207 switch (contentType) {
208 case 'application/x-www-form-urlencoded':
209 return Object.keys(body).map(function (key) { return encodeURIComponent(key) + "=" + encodeURIComponent(body[key]); }).join('&');
210 case 'application/json':
211 return JSON.stringify(body);
216 AjaxSubscriber.prototype.setHeaders = function (xhr, headers) {
217 for (var key in headers) {
218 if (headers.hasOwnProperty(key)) {
219 xhr.setRequestHeader(key, headers[key]);
223 AjaxSubscriber.prototype.getHeader = function (headers, headerName) {
224 for (var key in headers) {
225 if (key.toLowerCase() === headerName.toLowerCase()) {
231 AjaxSubscriber.prototype.setupEvents = function (xhr, request) {
232 var progressSubscriber = request.progressSubscriber;
233 function xhrTimeout(e) {
234 var _a = xhrTimeout, subscriber = _a.subscriber, progressSubscriber = _a.progressSubscriber, request = _a.request;
235 if (progressSubscriber) {
236 progressSubscriber.error(e);
240 error = new exports.AjaxTimeoutError(this, request);
245 subscriber.error(error);
247 xhr.ontimeout = xhrTimeout;
248 xhrTimeout.request = request;
249 xhrTimeout.subscriber = this;
250 xhrTimeout.progressSubscriber = progressSubscriber;
251 if (xhr.upload && 'withCredentials' in xhr) {
252 if (progressSubscriber) {
254 xhrProgress_1 = function (e) {
255 var progressSubscriber = xhrProgress_1.progressSubscriber;
256 progressSubscriber.next(e);
258 if (root_1.root.XDomainRequest) {
259 xhr.onprogress = xhrProgress_1;
262 xhr.upload.onprogress = xhrProgress_1;
264 xhrProgress_1.progressSubscriber = progressSubscriber;
267 xhrError_1 = function (e) {
268 var _a = xhrError_1, progressSubscriber = _a.progressSubscriber, subscriber = _a.subscriber, request = _a.request;
269 if (progressSubscriber) {
270 progressSubscriber.error(e);
274 error = new exports.AjaxError('ajax error', this, request);
279 subscriber.error(error);
281 xhr.onerror = xhrError_1;
282 xhrError_1.request = request;
283 xhrError_1.subscriber = this;
284 xhrError_1.progressSubscriber = progressSubscriber;
286 function xhrReadyStateChange(e) {
289 xhr.onreadystatechange = xhrReadyStateChange;
290 xhrReadyStateChange.subscriber = this;
291 xhrReadyStateChange.progressSubscriber = progressSubscriber;
292 xhrReadyStateChange.request = request;
293 function xhrLoad(e) {
294 var _a = xhrLoad, subscriber = _a.subscriber, progressSubscriber = _a.progressSubscriber, request = _a.request;
295 if (this.readyState === 4) {
296 var status_1 = this.status === 1223 ? 204 : this.status;
297 var response = (this.responseType === 'text' ? (this.response || this.responseText) : this.response);
298 if (status_1 === 0) {
299 status_1 = response ? 200 : 0;
301 if (status_1 < 400) {
302 if (progressSubscriber) {
303 progressSubscriber.complete();
306 subscriber.complete();
309 if (progressSubscriber) {
310 progressSubscriber.error(e);
314 error = new exports.AjaxError('ajax error ' + status_1, this, request);
319 subscriber.error(error);
323 xhr.onload = xhrLoad;
324 xhrLoad.subscriber = this;
325 xhrLoad.progressSubscriber = progressSubscriber;
326 xhrLoad.request = request;
328 AjaxSubscriber.prototype.unsubscribe = function () {
329 var _a = this, done = _a.done, xhr = _a.xhr;
330 if (!done && xhr && xhr.readyState !== 4 && typeof xhr.abort === 'function') {
333 _super.prototype.unsubscribe.call(this);
335 return AjaxSubscriber;
336 }(Subscriber_1.Subscriber));
337 exports.AjaxSubscriber = AjaxSubscriber;
338 var AjaxResponse = (function () {
339 function AjaxResponse(originalEvent, xhr, request) {
340 this.originalEvent = originalEvent;
342 this.request = request;
343 this.status = xhr.status;
344 this.responseType = xhr.responseType || request.responseType;
345 this.response = parseXhrResponse(this.responseType, xhr);
349 exports.AjaxResponse = AjaxResponse;
350 var AjaxErrorImpl = (function () {
351 function AjaxErrorImpl(message, xhr, request) {
353 this.message = message;
354 this.name = 'AjaxError';
356 this.request = request;
357 this.status = xhr.status;
358 this.responseType = xhr.responseType || request.responseType;
359 this.response = parseXhrResponse(this.responseType, xhr);
362 AjaxErrorImpl.prototype = Object.create(Error.prototype);
363 return AjaxErrorImpl;
365 exports.AjaxError = AjaxErrorImpl;
366 function parseJson(xhr) {
367 if ('response' in xhr) {
368 return xhr.responseType ? xhr.response : JSON.parse(xhr.response || xhr.responseText || 'null');
371 return JSON.parse(xhr.responseText || 'null');
374 function parseXhrResponse(responseType, xhr) {
375 switch (responseType) {
377 return parseJson(xhr);
379 return xhr.responseXML;
382 return ('response' in xhr) ? xhr.response : xhr.responseText;
385 function AjaxTimeoutErrorImpl(xhr, request) {
386 exports.AjaxError.call(this, 'ajax timeout', xhr, request);
387 this.name = 'AjaxTimeoutError';
390 exports.AjaxTimeoutError = AjaxTimeoutErrorImpl;
391 //# sourceMappingURL=AjaxObservable.js.map