source: trip-planner-front/node_modules/rxjs/internal/observable/dom/AjaxObservable.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 14.1 KB
Line 
1"use strict";
2var __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);
8 }
9 return function (d, b) {
10 extendStatics(d, b);
11 function __() { this.constructor = d; }
12 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13 };
14})();
15Object.defineProperty(exports, "__esModule", { value: true });
16var root_1 = require("../../util/root");
17var Observable_1 = require("../../Observable");
18var Subscriber_1 = require("../../Subscriber");
19var map_1 = require("../../operators/map");
20function getCORSRequest() {
21 if (root_1.root.XMLHttpRequest) {
22 return new root_1.root.XMLHttpRequest();
23 }
24 else if (!!root_1.root.XDomainRequest) {
25 return new root_1.root.XDomainRequest();
26 }
27 else {
28 throw new Error('CORS is not supported by your browser');
29 }
30}
31function getXMLHttpRequest() {
32 if (root_1.root.XMLHttpRequest) {
33 return new root_1.root.XMLHttpRequest();
34 }
35 else {
36 var progId = void 0;
37 try {
38 var progIds = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'];
39 for (var i = 0; i < 3; i++) {
40 try {
41 progId = progIds[i];
42 if (new root_1.root.ActiveXObject(progId)) {
43 break;
44 }
45 }
46 catch (e) {
47 }
48 }
49 return new root_1.root.ActiveXObject(progId);
50 }
51 catch (e) {
52 throw new Error('XMLHttpRequest is not supported by your browser');
53 }
54 }
55}
56function ajaxGet(url, headers) {
57 if (headers === void 0) { headers = null; }
58 return new AjaxObservable({ method: 'GET', url: url, headers: headers });
59}
60exports.ajaxGet = ajaxGet;
61function ajaxPost(url, body, headers) {
62 return new AjaxObservable({ method: 'POST', url: url, body: body, headers: headers });
63}
64exports.ajaxPost = ajaxPost;
65function ajaxDelete(url, headers) {
66 return new AjaxObservable({ method: 'DELETE', url: url, headers: headers });
67}
68exports.ajaxDelete = ajaxDelete;
69function ajaxPut(url, body, headers) {
70 return new AjaxObservable({ method: 'PUT', url: url, body: body, headers: headers });
71}
72exports.ajaxPut = ajaxPut;
73function ajaxPatch(url, body, headers) {
74 return new AjaxObservable({ method: 'PATCH', url: url, body: body, headers: headers });
75}
76exports.ajaxPatch = ajaxPatch;
77var mapResponse = map_1.map(function (x, index) { return x.response; });
78function ajaxGetJSON(url, headers) {
79 return mapResponse(new AjaxObservable({
80 method: 'GET',
81 url: url,
82 responseType: 'json',
83 headers: headers
84 }));
85}
86exports.ajaxGetJSON = ajaxGetJSON;
87var AjaxObservable = (function (_super) {
88 __extends(AjaxObservable, _super);
89 function AjaxObservable(urlOrRequest) {
90 var _this = _super.call(this) || this;
91 var request = {
92 async: true,
93 createXHR: function () {
94 return this.crossDomain ? getCORSRequest() : getXMLHttpRequest();
95 },
96 crossDomain: true,
97 withCredentials: false,
98 headers: {},
99 method: 'GET',
100 responseType: 'json',
101 timeout: 0
102 };
103 if (typeof urlOrRequest === 'string') {
104 request.url = urlOrRequest;
105 }
106 else {
107 for (var prop in urlOrRequest) {
108 if (urlOrRequest.hasOwnProperty(prop)) {
109 request[prop] = urlOrRequest[prop];
110 }
111 }
112 }
113 _this.request = request;
114 return _this;
115 }
116 AjaxObservable.prototype._subscribe = function (subscriber) {
117 return new AjaxSubscriber(subscriber, this.request);
118 };
119 AjaxObservable.create = (function () {
120 var create = function (urlOrRequest) {
121 return new AjaxObservable(urlOrRequest);
122 };
123 create.get = ajaxGet;
124 create.post = ajaxPost;
125 create.delete = ajaxDelete;
126 create.put = ajaxPut;
127 create.patch = ajaxPatch;
128 create.getJSON = ajaxGetJSON;
129 return create;
130 })();
131 return AjaxObservable;
132}(Observable_1.Observable));
133exports.AjaxObservable = AjaxObservable;
134var AjaxSubscriber = (function (_super) {
135 __extends(AjaxSubscriber, _super);
136 function AjaxSubscriber(destination, request) {
137 var _this = _super.call(this, destination) || this;
138 _this.request = request;
139 _this.done = false;
140 var headers = request.headers = request.headers || {};
141 if (!request.crossDomain && !_this.getHeader(headers, 'X-Requested-With')) {
142 headers['X-Requested-With'] = 'XMLHttpRequest';
143 }
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';
147 }
148 request.body = _this.serializeBody(request.body, _this.getHeader(request.headers, 'Content-Type'));
149 _this.send();
150 return _this;
151 }
152 AjaxSubscriber.prototype.next = function (e) {
153 this.done = true;
154 var _a = this, xhr = _a.xhr, request = _a.request, destination = _a.destination;
155 var result;
156 try {
157 result = new AjaxResponse(e, xhr, request);
158 }
159 catch (err) {
160 return destination.error(err);
161 }
162 destination.next(result);
163 };
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;
166 try {
167 var xhr = this.xhr = request.createXHR();
168 this.setupEvents(xhr, request);
169 if (user) {
170 xhr.open(method, url, async, user, password);
171 }
172 else {
173 xhr.open(method, url, async);
174 }
175 if (async) {
176 xhr.timeout = request.timeout;
177 xhr.responseType = request.responseType;
178 }
179 if ('withCredentials' in xhr) {
180 xhr.withCredentials = !!request.withCredentials;
181 }
182 this.setHeaders(xhr, headers);
183 if (body) {
184 xhr.send(body);
185 }
186 else {
187 xhr.send();
188 }
189 }
190 catch (err) {
191 this.error(err);
192 }
193 };
194 AjaxSubscriber.prototype.serializeBody = function (body, contentType) {
195 if (!body || typeof body === 'string') {
196 return body;
197 }
198 else if (root_1.root.FormData && body instanceof root_1.root.FormData) {
199 return body;
200 }
201 if (contentType) {
202 var splitIndex = contentType.indexOf(';');
203 if (splitIndex !== -1) {
204 contentType = contentType.substring(0, splitIndex);
205 }
206 }
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);
212 default:
213 return body;
214 }
215 };
216 AjaxSubscriber.prototype.setHeaders = function (xhr, headers) {
217 for (var key in headers) {
218 if (headers.hasOwnProperty(key)) {
219 xhr.setRequestHeader(key, headers[key]);
220 }
221 }
222 };
223 AjaxSubscriber.prototype.getHeader = function (headers, headerName) {
224 for (var key in headers) {
225 if (key.toLowerCase() === headerName.toLowerCase()) {
226 return headers[key];
227 }
228 }
229 return undefined;
230 };
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);
237 }
238 var error;
239 try {
240 error = new exports.AjaxTimeoutError(this, request);
241 }
242 catch (err) {
243 error = err;
244 }
245 subscriber.error(error);
246 }
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) {
253 var xhrProgress_1;
254 xhrProgress_1 = function (e) {
255 var progressSubscriber = xhrProgress_1.progressSubscriber;
256 progressSubscriber.next(e);
257 };
258 if (root_1.root.XDomainRequest) {
259 xhr.onprogress = xhrProgress_1;
260 }
261 else {
262 xhr.upload.onprogress = xhrProgress_1;
263 }
264 xhrProgress_1.progressSubscriber = progressSubscriber;
265 }
266 var xhrError_1;
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);
271 }
272 var error;
273 try {
274 error = new exports.AjaxError('ajax error', this, request);
275 }
276 catch (err) {
277 error = err;
278 }
279 subscriber.error(error);
280 };
281 xhr.onerror = xhrError_1;
282 xhrError_1.request = request;
283 xhrError_1.subscriber = this;
284 xhrError_1.progressSubscriber = progressSubscriber;
285 }
286 function xhrReadyStateChange(e) {
287 return;
288 }
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;
300 }
301 if (status_1 < 400) {
302 if (progressSubscriber) {
303 progressSubscriber.complete();
304 }
305 subscriber.next(e);
306 subscriber.complete();
307 }
308 else {
309 if (progressSubscriber) {
310 progressSubscriber.error(e);
311 }
312 var error = void 0;
313 try {
314 error = new exports.AjaxError('ajax error ' + status_1, this, request);
315 }
316 catch (err) {
317 error = err;
318 }
319 subscriber.error(error);
320 }
321 }
322 }
323 xhr.onload = xhrLoad;
324 xhrLoad.subscriber = this;
325 xhrLoad.progressSubscriber = progressSubscriber;
326 xhrLoad.request = request;
327 };
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') {
331 xhr.abort();
332 }
333 _super.prototype.unsubscribe.call(this);
334 };
335 return AjaxSubscriber;
336}(Subscriber_1.Subscriber));
337exports.AjaxSubscriber = AjaxSubscriber;
338var AjaxResponse = (function () {
339 function AjaxResponse(originalEvent, xhr, request) {
340 this.originalEvent = originalEvent;
341 this.xhr = xhr;
342 this.request = request;
343 this.status = xhr.status;
344 this.responseType = xhr.responseType || request.responseType;
345 this.response = parseXhrResponse(this.responseType, xhr);
346 }
347 return AjaxResponse;
348}());
349exports.AjaxResponse = AjaxResponse;
350var AjaxErrorImpl = (function () {
351 function AjaxErrorImpl(message, xhr, request) {
352 Error.call(this);
353 this.message = message;
354 this.name = 'AjaxError';
355 this.xhr = xhr;
356 this.request = request;
357 this.status = xhr.status;
358 this.responseType = xhr.responseType || request.responseType;
359 this.response = parseXhrResponse(this.responseType, xhr);
360 return this;
361 }
362 AjaxErrorImpl.prototype = Object.create(Error.prototype);
363 return AjaxErrorImpl;
364})();
365exports.AjaxError = AjaxErrorImpl;
366function parseJson(xhr) {
367 if ('response' in xhr) {
368 return xhr.responseType ? xhr.response : JSON.parse(xhr.response || xhr.responseText || 'null');
369 }
370 else {
371 return JSON.parse(xhr.responseText || 'null');
372 }
373}
374function parseXhrResponse(responseType, xhr) {
375 switch (responseType) {
376 case 'json':
377 return parseJson(xhr);
378 case 'xml':
379 return xhr.responseXML;
380 case 'text':
381 default:
382 return ('response' in xhr) ? xhr.response : xhr.responseText;
383 }
384}
385function AjaxTimeoutErrorImpl(xhr, request) {
386 exports.AjaxError.call(this, 'ajax timeout', xhr, request);
387 this.name = 'AjaxTimeoutError';
388 return this;
389}
390exports.AjaxTimeoutError = AjaxTimeoutErrorImpl;
391//# sourceMappingURL=AjaxObservable.js.map
Note: See TracBrowser for help on using the repository browser.