1 /** PURE_IMPORTS_START tslib,_.._util_root,_.._Observable,_.._Subscriber,_.._operators_map PURE_IMPORTS_END */
2 import * as tslib_1 from "tslib";
3 import { root } from '../../util/root';
4 import { Observable } from '../../Observable';
5 import { Subscriber } from '../../Subscriber';
6 import { map } from '../../operators/map';
7 function getCORSRequest() {
8 if (root.XMLHttpRequest) {
9 return new root.XMLHttpRequest();
11 else if (!!root.XDomainRequest) {
12 return new root.XDomainRequest();
15 throw new Error('CORS is not supported by your browser');
18 function getXMLHttpRequest() {
19 if (root.XMLHttpRequest) {
20 return new root.XMLHttpRequest();
25 var progIds = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'];
26 for (var i = 0; i < 3; i++) {
29 if (new root.ActiveXObject(progId)) {
36 return new root.ActiveXObject(progId);
39 throw new Error('XMLHttpRequest is not supported by your browser');
43 export function ajaxGet(url, headers) {
44 if (headers === void 0) {
47 return new AjaxObservable({ method: 'GET', url: url, headers: headers });
49 export function ajaxPost(url, body, headers) {
50 return new AjaxObservable({ method: 'POST', url: url, body: body, headers: headers });
52 export function ajaxDelete(url, headers) {
53 return new AjaxObservable({ method: 'DELETE', url: url, headers: headers });
55 export function ajaxPut(url, body, headers) {
56 return new AjaxObservable({ method: 'PUT', url: url, body: body, headers: headers });
58 export function ajaxPatch(url, body, headers) {
59 return new AjaxObservable({ method: 'PATCH', url: url, body: body, headers: headers });
61 var mapResponse = /*@__PURE__*/ map(function (x, index) { return x.response; });
62 export function ajaxGetJSON(url, headers) {
63 return mapResponse(new AjaxObservable({
70 var AjaxObservable = /*@__PURE__*/ (function (_super) {
71 tslib_1.__extends(AjaxObservable, _super);
72 function AjaxObservable(urlOrRequest) {
73 var _this = _super.call(this) || this;
76 createXHR: function () {
77 return this.crossDomain ? getCORSRequest() : getXMLHttpRequest();
80 withCredentials: false,
86 if (typeof urlOrRequest === 'string') {
87 request.url = urlOrRequest;
90 for (var prop in urlOrRequest) {
91 if (urlOrRequest.hasOwnProperty(prop)) {
92 request[prop] = urlOrRequest[prop];
96 _this.request = request;
99 AjaxObservable.prototype._subscribe = function (subscriber) {
100 return new AjaxSubscriber(subscriber, this.request);
102 AjaxObservable.create = (function () {
103 var create = function (urlOrRequest) {
104 return new AjaxObservable(urlOrRequest);
106 create.get = ajaxGet;
107 create.post = ajaxPost;
108 create.delete = ajaxDelete;
109 create.put = ajaxPut;
110 create.patch = ajaxPatch;
111 create.getJSON = ajaxGetJSON;
114 return AjaxObservable;
116 export { AjaxObservable };
117 var AjaxSubscriber = /*@__PURE__*/ (function (_super) {
118 tslib_1.__extends(AjaxSubscriber, _super);
119 function AjaxSubscriber(destination, request) {
120 var _this = _super.call(this, destination) || this;
121 _this.request = request;
123 var headers = request.headers = request.headers || {};
124 if (!request.crossDomain && !_this.getHeader(headers, 'X-Requested-With')) {
125 headers['X-Requested-With'] = 'XMLHttpRequest';
127 var contentTypeHeader = _this.getHeader(headers, 'Content-Type');
128 if (!contentTypeHeader && !(root.FormData && request.body instanceof root.FormData) && typeof request.body !== 'undefined') {
129 headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
131 request.body = _this.serializeBody(request.body, _this.getHeader(request.headers, 'Content-Type'));
135 AjaxSubscriber.prototype.next = function (e) {
137 var _a = this, xhr = _a.xhr, request = _a.request, destination = _a.destination;
140 result = new AjaxResponse(e, xhr, request);
143 return destination.error(err);
145 destination.next(result);
147 AjaxSubscriber.prototype.send = function () {
148 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;
150 var xhr = this.xhr = request.createXHR();
151 this.setupEvents(xhr, request);
153 xhr.open(method, url, async, user, password);
156 xhr.open(method, url, async);
159 xhr.timeout = request.timeout;
160 xhr.responseType = request.responseType;
162 if ('withCredentials' in xhr) {
163 xhr.withCredentials = !!request.withCredentials;
165 this.setHeaders(xhr, headers);
177 AjaxSubscriber.prototype.serializeBody = function (body, contentType) {
178 if (!body || typeof body === 'string') {
181 else if (root.FormData && body instanceof root.FormData) {
185 var splitIndex = contentType.indexOf(';');
186 if (splitIndex !== -1) {
187 contentType = contentType.substring(0, splitIndex);
190 switch (contentType) {
191 case 'application/x-www-form-urlencoded':
192 return Object.keys(body).map(function (key) { return encodeURIComponent(key) + "=" + encodeURIComponent(body[key]); }).join('&');
193 case 'application/json':
194 return JSON.stringify(body);
199 AjaxSubscriber.prototype.setHeaders = function (xhr, headers) {
200 for (var key in headers) {
201 if (headers.hasOwnProperty(key)) {
202 xhr.setRequestHeader(key, headers[key]);
206 AjaxSubscriber.prototype.getHeader = function (headers, headerName) {
207 for (var key in headers) {
208 if (key.toLowerCase() === headerName.toLowerCase()) {
214 AjaxSubscriber.prototype.setupEvents = function (xhr, request) {
215 var progressSubscriber = request.progressSubscriber;
216 function xhrTimeout(e) {
217 var _a = xhrTimeout, subscriber = _a.subscriber, progressSubscriber = _a.progressSubscriber, request = _a.request;
218 if (progressSubscriber) {
219 progressSubscriber.error(e);
223 error = new AjaxTimeoutError(this, request);
228 subscriber.error(error);
230 xhr.ontimeout = xhrTimeout;
231 xhrTimeout.request = request;
232 xhrTimeout.subscriber = this;
233 xhrTimeout.progressSubscriber = progressSubscriber;
234 if (xhr.upload && 'withCredentials' in xhr) {
235 if (progressSubscriber) {
237 xhrProgress_1 = function (e) {
238 var progressSubscriber = xhrProgress_1.progressSubscriber;
239 progressSubscriber.next(e);
241 if (root.XDomainRequest) {
242 xhr.onprogress = xhrProgress_1;
245 xhr.upload.onprogress = xhrProgress_1;
247 xhrProgress_1.progressSubscriber = progressSubscriber;
250 xhrError_1 = function (e) {
251 var _a = xhrError_1, progressSubscriber = _a.progressSubscriber, subscriber = _a.subscriber, request = _a.request;
252 if (progressSubscriber) {
253 progressSubscriber.error(e);
257 error = new AjaxError('ajax error', this, request);
262 subscriber.error(error);
264 xhr.onerror = xhrError_1;
265 xhrError_1.request = request;
266 xhrError_1.subscriber = this;
267 xhrError_1.progressSubscriber = progressSubscriber;
269 function xhrReadyStateChange(e) {
272 xhr.onreadystatechange = xhrReadyStateChange;
273 xhrReadyStateChange.subscriber = this;
274 xhrReadyStateChange.progressSubscriber = progressSubscriber;
275 xhrReadyStateChange.request = request;
276 function xhrLoad(e) {
277 var _a = xhrLoad, subscriber = _a.subscriber, progressSubscriber = _a.progressSubscriber, request = _a.request;
278 if (this.readyState === 4) {
279 var status_1 = this.status === 1223 ? 204 : this.status;
280 var response = (this.responseType === 'text' ? (this.response || this.responseText) : this.response);
281 if (status_1 === 0) {
282 status_1 = response ? 200 : 0;
284 if (status_1 < 400) {
285 if (progressSubscriber) {
286 progressSubscriber.complete();
289 subscriber.complete();
292 if (progressSubscriber) {
293 progressSubscriber.error(e);
297 error = new AjaxError('ajax error ' + status_1, this, request);
302 subscriber.error(error);
306 xhr.onload = xhrLoad;
307 xhrLoad.subscriber = this;
308 xhrLoad.progressSubscriber = progressSubscriber;
309 xhrLoad.request = request;
311 AjaxSubscriber.prototype.unsubscribe = function () {
312 var _a = this, done = _a.done, xhr = _a.xhr;
313 if (!done && xhr && xhr.readyState !== 4 && typeof xhr.abort === 'function') {
316 _super.prototype.unsubscribe.call(this);
318 return AjaxSubscriber;
320 export { AjaxSubscriber };
321 var AjaxResponse = /*@__PURE__*/ (function () {
322 function AjaxResponse(originalEvent, xhr, request) {
323 this.originalEvent = originalEvent;
325 this.request = request;
326 this.status = xhr.status;
327 this.responseType = xhr.responseType || request.responseType;
328 this.response = parseXhrResponse(this.responseType, xhr);
332 export { AjaxResponse };
333 var AjaxErrorImpl = /*@__PURE__*/ (function () {
334 function AjaxErrorImpl(message, xhr, request) {
336 this.message = message;
337 this.name = 'AjaxError';
339 this.request = request;
340 this.status = xhr.status;
341 this.responseType = xhr.responseType || request.responseType;
342 this.response = parseXhrResponse(this.responseType, xhr);
345 AjaxErrorImpl.prototype = /*@__PURE__*/ Object.create(Error.prototype);
346 return AjaxErrorImpl;
348 export var AjaxError = AjaxErrorImpl;
349 function parseJson(xhr) {
350 if ('response' in xhr) {
351 return xhr.responseType ? xhr.response : JSON.parse(xhr.response || xhr.responseText || 'null');
354 return JSON.parse(xhr.responseText || 'null');
357 function parseXhrResponse(responseType, xhr) {
358 switch (responseType) {
360 return parseJson(xhr);
362 return xhr.responseXML;
365 return ('response' in xhr) ? xhr.response : xhr.responseText;
368 function AjaxTimeoutErrorImpl(xhr, request) {
369 AjaxError.call(this, 'ajax timeout', xhr, request);
370 this.name = 'AjaxTimeoutError';
373 export var AjaxTimeoutError = AjaxTimeoutErrorImpl;
374 //# sourceMappingURL=AjaxObservable.js.map