1 | import { root } from '../../util/root';
|
---|
2 | import { Observable } from '../../Observable';
|
---|
3 | import { Subscriber } from '../../Subscriber';
|
---|
4 | import { map } from '../../operators/map';
|
---|
5 | function getCORSRequest() {
|
---|
6 | if (root.XMLHttpRequest) {
|
---|
7 | return new root.XMLHttpRequest();
|
---|
8 | }
|
---|
9 | else if (!!root.XDomainRequest) {
|
---|
10 | return new root.XDomainRequest();
|
---|
11 | }
|
---|
12 | else {
|
---|
13 | throw new Error('CORS is not supported by your browser');
|
---|
14 | }
|
---|
15 | }
|
---|
16 | function getXMLHttpRequest() {
|
---|
17 | if (root.XMLHttpRequest) {
|
---|
18 | return new root.XMLHttpRequest();
|
---|
19 | }
|
---|
20 | else {
|
---|
21 | let progId;
|
---|
22 | try {
|
---|
23 | const progIds = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'];
|
---|
24 | for (let i = 0; i < 3; i++) {
|
---|
25 | try {
|
---|
26 | progId = progIds[i];
|
---|
27 | if (new root.ActiveXObject(progId)) {
|
---|
28 | break;
|
---|
29 | }
|
---|
30 | }
|
---|
31 | catch (e) {
|
---|
32 | }
|
---|
33 | }
|
---|
34 | return new root.ActiveXObject(progId);
|
---|
35 | }
|
---|
36 | catch (e) {
|
---|
37 | throw new Error('XMLHttpRequest is not supported by your browser');
|
---|
38 | }
|
---|
39 | }
|
---|
40 | }
|
---|
41 | export function ajaxGet(url, headers = null) {
|
---|
42 | return new AjaxObservable({ method: 'GET', url, headers });
|
---|
43 | }
|
---|
44 | export function ajaxPost(url, body, headers) {
|
---|
45 | return new AjaxObservable({ method: 'POST', url, body, headers });
|
---|
46 | }
|
---|
47 | export function ajaxDelete(url, headers) {
|
---|
48 | return new AjaxObservable({ method: 'DELETE', url, headers });
|
---|
49 | }
|
---|
50 | export function ajaxPut(url, body, headers) {
|
---|
51 | return new AjaxObservable({ method: 'PUT', url, body, headers });
|
---|
52 | }
|
---|
53 | export function ajaxPatch(url, body, headers) {
|
---|
54 | return new AjaxObservable({ method: 'PATCH', url, body, headers });
|
---|
55 | }
|
---|
56 | const mapResponse = map((x, index) => x.response);
|
---|
57 | export function ajaxGetJSON(url, headers) {
|
---|
58 | return mapResponse(new AjaxObservable({
|
---|
59 | method: 'GET',
|
---|
60 | url,
|
---|
61 | responseType: 'json',
|
---|
62 | headers
|
---|
63 | }));
|
---|
64 | }
|
---|
65 | export class AjaxObservable extends Observable {
|
---|
66 | constructor(urlOrRequest) {
|
---|
67 | super();
|
---|
68 | const request = {
|
---|
69 | async: true,
|
---|
70 | createXHR: function () {
|
---|
71 | return this.crossDomain ? getCORSRequest() : getXMLHttpRequest();
|
---|
72 | },
|
---|
73 | crossDomain: true,
|
---|
74 | withCredentials: false,
|
---|
75 | headers: {},
|
---|
76 | method: 'GET',
|
---|
77 | responseType: 'json',
|
---|
78 | timeout: 0
|
---|
79 | };
|
---|
80 | if (typeof urlOrRequest === 'string') {
|
---|
81 | request.url = urlOrRequest;
|
---|
82 | }
|
---|
83 | else {
|
---|
84 | for (const prop in urlOrRequest) {
|
---|
85 | if (urlOrRequest.hasOwnProperty(prop)) {
|
---|
86 | request[prop] = urlOrRequest[prop];
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|
90 | this.request = request;
|
---|
91 | }
|
---|
92 | _subscribe(subscriber) {
|
---|
93 | return new AjaxSubscriber(subscriber, this.request);
|
---|
94 | }
|
---|
95 | }
|
---|
96 | AjaxObservable.create = (() => {
|
---|
97 | const create = (urlOrRequest) => {
|
---|
98 | return new AjaxObservable(urlOrRequest);
|
---|
99 | };
|
---|
100 | create.get = ajaxGet;
|
---|
101 | create.post = ajaxPost;
|
---|
102 | create.delete = ajaxDelete;
|
---|
103 | create.put = ajaxPut;
|
---|
104 | create.patch = ajaxPatch;
|
---|
105 | create.getJSON = ajaxGetJSON;
|
---|
106 | return create;
|
---|
107 | })();
|
---|
108 | export class AjaxSubscriber extends Subscriber {
|
---|
109 | constructor(destination, request) {
|
---|
110 | super(destination);
|
---|
111 | this.request = request;
|
---|
112 | this.done = false;
|
---|
113 | const headers = request.headers = request.headers || {};
|
---|
114 | if (!request.crossDomain && !this.getHeader(headers, 'X-Requested-With')) {
|
---|
115 | headers['X-Requested-With'] = 'XMLHttpRequest';
|
---|
116 | }
|
---|
117 | let contentTypeHeader = this.getHeader(headers, 'Content-Type');
|
---|
118 | if (!contentTypeHeader && !(root.FormData && request.body instanceof root.FormData) && typeof request.body !== 'undefined') {
|
---|
119 | headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
|
---|
120 | }
|
---|
121 | request.body = this.serializeBody(request.body, this.getHeader(request.headers, 'Content-Type'));
|
---|
122 | this.send();
|
---|
123 | }
|
---|
124 | next(e) {
|
---|
125 | this.done = true;
|
---|
126 | const { xhr, request, destination } = this;
|
---|
127 | let result;
|
---|
128 | try {
|
---|
129 | result = new AjaxResponse(e, xhr, request);
|
---|
130 | }
|
---|
131 | catch (err) {
|
---|
132 | return destination.error(err);
|
---|
133 | }
|
---|
134 | destination.next(result);
|
---|
135 | }
|
---|
136 | send() {
|
---|
137 | const { request, request: { user, method, url, async, password, headers, body } } = this;
|
---|
138 | try {
|
---|
139 | const xhr = this.xhr = request.createXHR();
|
---|
140 | this.setupEvents(xhr, request);
|
---|
141 | if (user) {
|
---|
142 | xhr.open(method, url, async, user, password);
|
---|
143 | }
|
---|
144 | else {
|
---|
145 | xhr.open(method, url, async);
|
---|
146 | }
|
---|
147 | if (async) {
|
---|
148 | xhr.timeout = request.timeout;
|
---|
149 | xhr.responseType = request.responseType;
|
---|
150 | }
|
---|
151 | if ('withCredentials' in xhr) {
|
---|
152 | xhr.withCredentials = !!request.withCredentials;
|
---|
153 | }
|
---|
154 | this.setHeaders(xhr, headers);
|
---|
155 | if (body) {
|
---|
156 | xhr.send(body);
|
---|
157 | }
|
---|
158 | else {
|
---|
159 | xhr.send();
|
---|
160 | }
|
---|
161 | }
|
---|
162 | catch (err) {
|
---|
163 | this.error(err);
|
---|
164 | }
|
---|
165 | }
|
---|
166 | serializeBody(body, contentType) {
|
---|
167 | if (!body || typeof body === 'string') {
|
---|
168 | return body;
|
---|
169 | }
|
---|
170 | else if (root.FormData && body instanceof root.FormData) {
|
---|
171 | return body;
|
---|
172 | }
|
---|
173 | if (contentType) {
|
---|
174 | const splitIndex = contentType.indexOf(';');
|
---|
175 | if (splitIndex !== -1) {
|
---|
176 | contentType = contentType.substring(0, splitIndex);
|
---|
177 | }
|
---|
178 | }
|
---|
179 | switch (contentType) {
|
---|
180 | case 'application/x-www-form-urlencoded':
|
---|
181 | return Object.keys(body).map(key => `${encodeURIComponent(key)}=${encodeURIComponent(body[key])}`).join('&');
|
---|
182 | case 'application/json':
|
---|
183 | return JSON.stringify(body);
|
---|
184 | default:
|
---|
185 | return body;
|
---|
186 | }
|
---|
187 | }
|
---|
188 | setHeaders(xhr, headers) {
|
---|
189 | for (let key in headers) {
|
---|
190 | if (headers.hasOwnProperty(key)) {
|
---|
191 | xhr.setRequestHeader(key, headers[key]);
|
---|
192 | }
|
---|
193 | }
|
---|
194 | }
|
---|
195 | getHeader(headers, headerName) {
|
---|
196 | for (let key in headers) {
|
---|
197 | if (key.toLowerCase() === headerName.toLowerCase()) {
|
---|
198 | return headers[key];
|
---|
199 | }
|
---|
200 | }
|
---|
201 | return undefined;
|
---|
202 | }
|
---|
203 | setupEvents(xhr, request) {
|
---|
204 | const progressSubscriber = request.progressSubscriber;
|
---|
205 | function xhrTimeout(e) {
|
---|
206 | const { subscriber, progressSubscriber, request } = xhrTimeout;
|
---|
207 | if (progressSubscriber) {
|
---|
208 | progressSubscriber.error(e);
|
---|
209 | }
|
---|
210 | let error;
|
---|
211 | try {
|
---|
212 | error = new AjaxTimeoutError(this, request);
|
---|
213 | }
|
---|
214 | catch (err) {
|
---|
215 | error = err;
|
---|
216 | }
|
---|
217 | subscriber.error(error);
|
---|
218 | }
|
---|
219 | xhr.ontimeout = xhrTimeout;
|
---|
220 | xhrTimeout.request = request;
|
---|
221 | xhrTimeout.subscriber = this;
|
---|
222 | xhrTimeout.progressSubscriber = progressSubscriber;
|
---|
223 | if (xhr.upload && 'withCredentials' in xhr) {
|
---|
224 | if (progressSubscriber) {
|
---|
225 | let xhrProgress;
|
---|
226 | xhrProgress = function (e) {
|
---|
227 | const { progressSubscriber } = xhrProgress;
|
---|
228 | progressSubscriber.next(e);
|
---|
229 | };
|
---|
230 | if (root.XDomainRequest) {
|
---|
231 | xhr.onprogress = xhrProgress;
|
---|
232 | }
|
---|
233 | else {
|
---|
234 | xhr.upload.onprogress = xhrProgress;
|
---|
235 | }
|
---|
236 | xhrProgress.progressSubscriber = progressSubscriber;
|
---|
237 | }
|
---|
238 | let xhrError;
|
---|
239 | xhrError = function (e) {
|
---|
240 | const { progressSubscriber, subscriber, request } = xhrError;
|
---|
241 | if (progressSubscriber) {
|
---|
242 | progressSubscriber.error(e);
|
---|
243 | }
|
---|
244 | let error;
|
---|
245 | try {
|
---|
246 | error = new AjaxError('ajax error', this, request);
|
---|
247 | }
|
---|
248 | catch (err) {
|
---|
249 | error = err;
|
---|
250 | }
|
---|
251 | subscriber.error(error);
|
---|
252 | };
|
---|
253 | xhr.onerror = xhrError;
|
---|
254 | xhrError.request = request;
|
---|
255 | xhrError.subscriber = this;
|
---|
256 | xhrError.progressSubscriber = progressSubscriber;
|
---|
257 | }
|
---|
258 | function xhrReadyStateChange(e) {
|
---|
259 | return;
|
---|
260 | }
|
---|
261 | xhr.onreadystatechange = xhrReadyStateChange;
|
---|
262 | xhrReadyStateChange.subscriber = this;
|
---|
263 | xhrReadyStateChange.progressSubscriber = progressSubscriber;
|
---|
264 | xhrReadyStateChange.request = request;
|
---|
265 | function xhrLoad(e) {
|
---|
266 | const { subscriber, progressSubscriber, request } = xhrLoad;
|
---|
267 | if (this.readyState === 4) {
|
---|
268 | let status = this.status === 1223 ? 204 : this.status;
|
---|
269 | let response = (this.responseType === 'text' ? (this.response || this.responseText) : this.response);
|
---|
270 | if (status === 0) {
|
---|
271 | status = response ? 200 : 0;
|
---|
272 | }
|
---|
273 | if (status < 400) {
|
---|
274 | if (progressSubscriber) {
|
---|
275 | progressSubscriber.complete();
|
---|
276 | }
|
---|
277 | subscriber.next(e);
|
---|
278 | subscriber.complete();
|
---|
279 | }
|
---|
280 | else {
|
---|
281 | if (progressSubscriber) {
|
---|
282 | progressSubscriber.error(e);
|
---|
283 | }
|
---|
284 | let error;
|
---|
285 | try {
|
---|
286 | error = new AjaxError('ajax error ' + status, this, request);
|
---|
287 | }
|
---|
288 | catch (err) {
|
---|
289 | error = err;
|
---|
290 | }
|
---|
291 | subscriber.error(error);
|
---|
292 | }
|
---|
293 | }
|
---|
294 | }
|
---|
295 | xhr.onload = xhrLoad;
|
---|
296 | xhrLoad.subscriber = this;
|
---|
297 | xhrLoad.progressSubscriber = progressSubscriber;
|
---|
298 | xhrLoad.request = request;
|
---|
299 | }
|
---|
300 | unsubscribe() {
|
---|
301 | const { done, xhr } = this;
|
---|
302 | if (!done && xhr && xhr.readyState !== 4 && typeof xhr.abort === 'function') {
|
---|
303 | xhr.abort();
|
---|
304 | }
|
---|
305 | super.unsubscribe();
|
---|
306 | }
|
---|
307 | }
|
---|
308 | export class AjaxResponse {
|
---|
309 | constructor(originalEvent, xhr, request) {
|
---|
310 | this.originalEvent = originalEvent;
|
---|
311 | this.xhr = xhr;
|
---|
312 | this.request = request;
|
---|
313 | this.status = xhr.status;
|
---|
314 | this.responseType = xhr.responseType || request.responseType;
|
---|
315 | this.response = parseXhrResponse(this.responseType, xhr);
|
---|
316 | }
|
---|
317 | }
|
---|
318 | const AjaxErrorImpl = (() => {
|
---|
319 | function AjaxErrorImpl(message, xhr, request) {
|
---|
320 | Error.call(this);
|
---|
321 | this.message = message;
|
---|
322 | this.name = 'AjaxError';
|
---|
323 | this.xhr = xhr;
|
---|
324 | this.request = request;
|
---|
325 | this.status = xhr.status;
|
---|
326 | this.responseType = xhr.responseType || request.responseType;
|
---|
327 | this.response = parseXhrResponse(this.responseType, xhr);
|
---|
328 | return this;
|
---|
329 | }
|
---|
330 | AjaxErrorImpl.prototype = Object.create(Error.prototype);
|
---|
331 | return AjaxErrorImpl;
|
---|
332 | })();
|
---|
333 | export const AjaxError = AjaxErrorImpl;
|
---|
334 | function parseJson(xhr) {
|
---|
335 | if ('response' in xhr) {
|
---|
336 | return xhr.responseType ? xhr.response : JSON.parse(xhr.response || xhr.responseText || 'null');
|
---|
337 | }
|
---|
338 | else {
|
---|
339 | return JSON.parse(xhr.responseText || 'null');
|
---|
340 | }
|
---|
341 | }
|
---|
342 | function parseXhrResponse(responseType, xhr) {
|
---|
343 | switch (responseType) {
|
---|
344 | case 'json':
|
---|
345 | return parseJson(xhr);
|
---|
346 | case 'xml':
|
---|
347 | return xhr.responseXML;
|
---|
348 | case 'text':
|
---|
349 | default:
|
---|
350 | return ('response' in xhr) ? xhr.response : xhr.responseText;
|
---|
351 | }
|
---|
352 | }
|
---|
353 | function AjaxTimeoutErrorImpl(xhr, request) {
|
---|
354 | AjaxError.call(this, 'ajax timeout', xhr, request);
|
---|
355 | this.name = 'AjaxTimeoutError';
|
---|
356 | return this;
|
---|
357 | }
|
---|
358 | export const AjaxTimeoutError = AjaxTimeoutErrorImpl;
|
---|
359 | //# sourceMappingURL=AjaxObservable.js.map |
---|