Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / rxjs / _esm5 / internal / observable / dom / AjaxObservable.js
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();
10     }
11     else if (!!root.XDomainRequest) {
12         return new root.XDomainRequest();
13     }
14     else {
15         throw new Error('CORS is not supported by your browser');
16     }
17 }
18 function getXMLHttpRequest() {
19     if (root.XMLHttpRequest) {
20         return new root.XMLHttpRequest();
21     }
22     else {
23         var progId = void 0;
24         try {
25             var progIds = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'];
26             for (var i = 0; i < 3; i++) {
27                 try {
28                     progId = progIds[i];
29                     if (new root.ActiveXObject(progId)) {
30                         break;
31                     }
32                 }
33                 catch (e) {
34                 }
35             }
36             return new root.ActiveXObject(progId);
37         }
38         catch (e) {
39             throw new Error('XMLHttpRequest is not supported by your browser');
40         }
41     }
42 }
43 export function ajaxGet(url, headers) {
44     if (headers === void 0) {
45         headers = null;
46     }
47     return new AjaxObservable({ method: 'GET', url: url, headers: headers });
48 }
49 export function ajaxPost(url, body, headers) {
50     return new AjaxObservable({ method: 'POST', url: url, body: body, headers: headers });
51 }
52 export function ajaxDelete(url, headers) {
53     return new AjaxObservable({ method: 'DELETE', url: url, headers: headers });
54 }
55 export function ajaxPut(url, body, headers) {
56     return new AjaxObservable({ method: 'PUT', url: url, body: body, headers: headers });
57 }
58 export function ajaxPatch(url, body, headers) {
59     return new AjaxObservable({ method: 'PATCH', url: url, body: body, headers: headers });
60 }
61 var mapResponse = /*@__PURE__*/ map(function (x, index) { return x.response; });
62 export function ajaxGetJSON(url, headers) {
63     return mapResponse(new AjaxObservable({
64         method: 'GET',
65         url: url,
66         responseType: 'json',
67         headers: headers
68     }));
69 }
70 var AjaxObservable = /*@__PURE__*/ (function (_super) {
71     tslib_1.__extends(AjaxObservable, _super);
72     function AjaxObservable(urlOrRequest) {
73         var _this = _super.call(this) || this;
74         var request = {
75             async: true,
76             createXHR: function () {
77                 return this.crossDomain ? getCORSRequest() : getXMLHttpRequest();
78             },
79             crossDomain: true,
80             withCredentials: false,
81             headers: {},
82             method: 'GET',
83             responseType: 'json',
84             timeout: 0
85         };
86         if (typeof urlOrRequest === 'string') {
87             request.url = urlOrRequest;
88         }
89         else {
90             for (var prop in urlOrRequest) {
91                 if (urlOrRequest.hasOwnProperty(prop)) {
92                     request[prop] = urlOrRequest[prop];
93                 }
94             }
95         }
96         _this.request = request;
97         return _this;
98     }
99     AjaxObservable.prototype._subscribe = function (subscriber) {
100         return new AjaxSubscriber(subscriber, this.request);
101     };
102     AjaxObservable.create = (function () {
103         var create = function (urlOrRequest) {
104             return new AjaxObservable(urlOrRequest);
105         };
106         create.get = ajaxGet;
107         create.post = ajaxPost;
108         create.delete = ajaxDelete;
109         create.put = ajaxPut;
110         create.patch = ajaxPatch;
111         create.getJSON = ajaxGetJSON;
112         return create;
113     })();
114     return AjaxObservable;
115 }(Observable));
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;
122         _this.done = false;
123         var headers = request.headers = request.headers || {};
124         if (!request.crossDomain && !_this.getHeader(headers, 'X-Requested-With')) {
125             headers['X-Requested-With'] = 'XMLHttpRequest';
126         }
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';
130         }
131         request.body = _this.serializeBody(request.body, _this.getHeader(request.headers, 'Content-Type'));
132         _this.send();
133         return _this;
134     }
135     AjaxSubscriber.prototype.next = function (e) {
136         this.done = true;
137         var _a = this, xhr = _a.xhr, request = _a.request, destination = _a.destination;
138         var result;
139         try {
140             result = new AjaxResponse(e, xhr, request);
141         }
142         catch (err) {
143             return destination.error(err);
144         }
145         destination.next(result);
146     };
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;
149         try {
150             var xhr = this.xhr = request.createXHR();
151             this.setupEvents(xhr, request);
152             if (user) {
153                 xhr.open(method, url, async, user, password);
154             }
155             else {
156                 xhr.open(method, url, async);
157             }
158             if (async) {
159                 xhr.timeout = request.timeout;
160                 xhr.responseType = request.responseType;
161             }
162             if ('withCredentials' in xhr) {
163                 xhr.withCredentials = !!request.withCredentials;
164             }
165             this.setHeaders(xhr, headers);
166             if (body) {
167                 xhr.send(body);
168             }
169             else {
170                 xhr.send();
171             }
172         }
173         catch (err) {
174             this.error(err);
175         }
176     };
177     AjaxSubscriber.prototype.serializeBody = function (body, contentType) {
178         if (!body || typeof body === 'string') {
179             return body;
180         }
181         else if (root.FormData && body instanceof root.FormData) {
182             return body;
183         }
184         if (contentType) {
185             var splitIndex = contentType.indexOf(';');
186             if (splitIndex !== -1) {
187                 contentType = contentType.substring(0, splitIndex);
188             }
189         }
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);
195             default:
196                 return body;
197         }
198     };
199     AjaxSubscriber.prototype.setHeaders = function (xhr, headers) {
200         for (var key in headers) {
201             if (headers.hasOwnProperty(key)) {
202                 xhr.setRequestHeader(key, headers[key]);
203             }
204         }
205     };
206     AjaxSubscriber.prototype.getHeader = function (headers, headerName) {
207         for (var key in headers) {
208             if (key.toLowerCase() === headerName.toLowerCase()) {
209                 return headers[key];
210             }
211         }
212         return undefined;
213     };
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);
220             }
221             var error;
222             try {
223                 error = new AjaxTimeoutError(this, request);
224             }
225             catch (err) {
226                 error = err;
227             }
228             subscriber.error(error);
229         }
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) {
236                 var xhrProgress_1;
237                 xhrProgress_1 = function (e) {
238                     var progressSubscriber = xhrProgress_1.progressSubscriber;
239                     progressSubscriber.next(e);
240                 };
241                 if (root.XDomainRequest) {
242                     xhr.onprogress = xhrProgress_1;
243                 }
244                 else {
245                     xhr.upload.onprogress = xhrProgress_1;
246                 }
247                 xhrProgress_1.progressSubscriber = progressSubscriber;
248             }
249             var xhrError_1;
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);
254                 }
255                 var error;
256                 try {
257                     error = new AjaxError('ajax error', this, request);
258                 }
259                 catch (err) {
260                     error = err;
261                 }
262                 subscriber.error(error);
263             };
264             xhr.onerror = xhrError_1;
265             xhrError_1.request = request;
266             xhrError_1.subscriber = this;
267             xhrError_1.progressSubscriber = progressSubscriber;
268         }
269         function xhrReadyStateChange(e) {
270             return;
271         }
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;
283                 }
284                 if (status_1 < 400) {
285                     if (progressSubscriber) {
286                         progressSubscriber.complete();
287                     }
288                     subscriber.next(e);
289                     subscriber.complete();
290                 }
291                 else {
292                     if (progressSubscriber) {
293                         progressSubscriber.error(e);
294                     }
295                     var error = void 0;
296                     try {
297                         error = new AjaxError('ajax error ' + status_1, this, request);
298                     }
299                     catch (err) {
300                         error = err;
301                     }
302                     subscriber.error(error);
303                 }
304             }
305         }
306         xhr.onload = xhrLoad;
307         xhrLoad.subscriber = this;
308         xhrLoad.progressSubscriber = progressSubscriber;
309         xhrLoad.request = request;
310     };
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') {
314             xhr.abort();
315         }
316         _super.prototype.unsubscribe.call(this);
317     };
318     return AjaxSubscriber;
319 }(Subscriber));
320 export { AjaxSubscriber };
321 var AjaxResponse = /*@__PURE__*/ (function () {
322     function AjaxResponse(originalEvent, xhr, request) {
323         this.originalEvent = originalEvent;
324         this.xhr = xhr;
325         this.request = request;
326         this.status = xhr.status;
327         this.responseType = xhr.responseType || request.responseType;
328         this.response = parseXhrResponse(this.responseType, xhr);
329     }
330     return AjaxResponse;
331 }());
332 export { AjaxResponse };
333 var AjaxErrorImpl = /*@__PURE__*/ (function () {
334     function AjaxErrorImpl(message, xhr, request) {
335         Error.call(this);
336         this.message = message;
337         this.name = 'AjaxError';
338         this.xhr = xhr;
339         this.request = request;
340         this.status = xhr.status;
341         this.responseType = xhr.responseType || request.responseType;
342         this.response = parseXhrResponse(this.responseType, xhr);
343         return this;
344     }
345     AjaxErrorImpl.prototype = /*@__PURE__*/ Object.create(Error.prototype);
346     return AjaxErrorImpl;
347 })();
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');
352     }
353     else {
354         return JSON.parse(xhr.responseText || 'null');
355     }
356 }
357 function parseXhrResponse(responseType, xhr) {
358     switch (responseType) {
359         case 'json':
360             return parseJson(xhr);
361         case 'xml':
362             return xhr.responseXML;
363         case 'text':
364         default:
365             return ('response' in xhr) ? xhr.response : xhr.responseText;
366     }
367 }
368 function AjaxTimeoutErrorImpl(xhr, request) {
369     AjaxError.call(this, 'ajax timeout', xhr, request);
370     this.name = 'AjaxTimeoutError';
371     return this;
372 }
373 export var AjaxTimeoutError = AjaxTimeoutErrorImpl;
374 //# sourceMappingURL=AjaxObservable.js.map