Skip to content

Commit 0e11af7

Browse files
committed
refactor: remove legacy code for IE in ajax
BREAKING CHANGE: `ajax` no longer supports IE
1 parent 7d3c4ec commit 0e11af7

File tree

4 files changed

+4
-94
lines changed

4 files changed

+4
-94
lines changed

spec/observables/dom/ajax-spec.ts

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -330,37 +330,6 @@ describe('ajax', () => {
330330
expect(complete).to.be.true;
331331
});
332332

333-
it('should fail if fails to parse response in older IE', () => {
334-
let error: any;
335-
const obj: AjaxConfig = {
336-
url: '/flibbertyJibbet',
337-
method: '',
338-
};
339-
340-
// No `response` property on the object (for older IE).
341-
MockXMLHttpRequest.noResponseProp = true;
342-
343-
ajax(obj).subscribe({
344-
next: () => {
345-
throw new Error('should not next');
346-
},
347-
error: (err: any) => {
348-
error = err;
349-
},
350-
complete: () => {
351-
throw new Error('should not complete');
352-
},
353-
});
354-
355-
MockXMLHttpRequest.mostRecent.respondWith({
356-
status: 207,
357-
responseText: 'Wee! I am text, but should be valid JSON!',
358-
});
359-
360-
expect(error instanceof SyntaxError).to.be.true;
361-
expect(error.message).to.equal('Unexpected token W in JSON at position 0');
362-
});
363-
364333
it('should fail on 404', () => {
365334
let error: any;
366335
const obj: AjaxConfig = {
@@ -1583,11 +1552,6 @@ class MockXHREventTarget {
15831552
class MockXMLHttpRequest extends MockXHREventTarget {
15841553
static readonly DONE = 4;
15851554

1586-
/**
1587-
* Set to `true` to test IE code paths.
1588-
*/
1589-
static noResponseProp = false;
1590-
15911555
private static requests: Array<MockXMLHttpRequest> = [];
15921556
private static recentRequest: MockXMLHttpRequest;
15931557

@@ -1600,7 +1564,6 @@ class MockXMLHttpRequest extends MockXHREventTarget {
16001564
}
16011565

16021566
static clearRequest(): void {
1603-
MockXMLHttpRequest.noResponseProp = false;
16041567
MockXMLHttpRequest.requests.length = 0;
16051568
MockXMLHttpRequest.recentRequest = null!;
16061569
}
@@ -1639,9 +1602,6 @@ class MockXMLHttpRequest extends MockXHREventTarget {
16391602
super();
16401603
MockXMLHttpRequest.recentRequest = this;
16411604
MockXMLHttpRequest.requests.push(this);
1642-
if (MockXMLHttpRequest.noResponseProp) {
1643-
delete this['response'];
1644-
}
16451605
}
16461606

16471607
// @ts-ignore: Property has no initializer and is not definitely assigned
@@ -1755,11 +1715,6 @@ class MockXMLHttpRequest extends MockXHREventTarget {
17551715
break;
17561716
}
17571717

1758-
// We're testing old IE, forget all of that response property stuff.
1759-
if (MockXMLHttpRequest.noResponseProp) {
1760-
delete this['response'];
1761-
}
1762-
17631718
this.triggerEvent('load', { type: 'load', total: response.total ?? 0, loaded: response.loaded ?? 0 });
17641719
this.triggerEvent('readystatechange', { type: 'readystatechange' });
17651720
}

src/internal/ajax/ajax.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -481,18 +481,7 @@ export function fromAjax<T>(init: AjaxConfig): Observable<AjaxResponse<T>> {
481481
if (status < 400) {
482482
progressSubscriber?.complete?.();
483483

484-
let response: AjaxResponse<T>;
485-
try {
486-
// This can throw in IE, because we end up needing to do a JSON.parse
487-
// of the response in some cases to produce object we'd expect from
488-
// modern browsers.
489-
response = createResponse(DOWNLOAD, event);
490-
} catch (err) {
491-
destination.error(err);
492-
return;
493-
}
494-
495-
destination.next(response);
484+
destination.next(createResponse(DOWNLOAD, event));
496485
destination.complete();
497486
} else {
498487
progressSubscriber?.error?.(event);

src/internal/ajax/errors.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,7 @@ export const AjaxError: AjaxErrorCtor = createErrorClass(
6363
this.request = request;
6464
this.status = xhr.status;
6565
this.responseType = xhr.responseType;
66-
let response: any;
67-
try {
68-
// This can throw in IE, because we have to do a JSON.parse of
69-
// the response in some cases to get the expected response property.
70-
response = getXHRResponse(xhr);
71-
} catch (err) {
72-
response = xhr.responseText;
73-
}
74-
this.response = response;
66+
this.response = getXHRResponse(xhr);
7567
}
7668
);
7769

src/internal/ajax/getXHRResponse.ts

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,11 @@
11
/**
2-
* Gets what should be in the `response` property of the XHR. However,
3-
* since we still support the final versions of IE, we need to do a little
4-
* checking here to make sure that we get the right thing back. Consequently,
5-
* we need to do a JSON.parse() in here, which *could* throw if the response
6-
* isn't valid JSON.
2+
* Gets what should be in the `response` property of the XHR.
73
*
84
* This is used both in creating an AjaxResponse, and in creating certain errors
95
* that we throw, so we can give the user whatever was in the response property.
106
*
117
* @param xhr The XHR to examine the response of
128
*/
139
export function getXHRResponse(xhr: XMLHttpRequest) {
14-
switch (xhr.responseType) {
15-
case 'json': {
16-
if ('response' in xhr) {
17-
return xhr.response;
18-
} else {
19-
// IE
20-
const ieXHR: any = xhr;
21-
return JSON.parse(ieXHR.responseText);
22-
}
23-
}
24-
case 'document':
25-
return xhr.responseXML;
26-
case 'text':
27-
default: {
28-
if ('response' in xhr) {
29-
return xhr.response;
30-
} else {
31-
// IE
32-
const ieXHR: any = xhr;
33-
return ieXHR.responseText;
34-
}
35-
}
36-
}
10+
return xhr.responseType === 'document' ? xhr.responseXML : xhr.response;
3711
}

0 commit comments

Comments
 (0)