From 7d1854198d9806ed2aea11bc555e4d17f54b54bd Mon Sep 17 00:00:00 2001 From: "Denis Kuzmin [ github.com/3F ]" Date: Tue, 8 Jan 2019 17:43:45 +0300 Subject: [PATCH 1/3] Adds CORS support (XDomainRequest) for IE7+ still through XMLHttpRequest Details in https://github.com/developit/unfetch/issues/106 --- src/index.mjs | 6 ++++-- test/index.js | 7 ++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/index.mjs b/src/index.mjs index d4beeec..2dbb32d 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -11,8 +11,10 @@ export default function(url, options) { request.withCredentials = options.credentials=='include'; - request.onload = () => { - resolve(response()); + request.onreadystatechange = function() { + if(this.readyState === 4 && this.status === 200) { + resolve(response()); + } }; request.onerror = reject; diff --git a/test/index.js b/test/index.js index 3a9e598..92f85b3 100644 --- a/test/index.js +++ b/test/index.js @@ -20,6 +20,7 @@ describe('unfetch', () => { getAllResponseHeaders: jest.fn().mockReturnValue('X-Foo: bar\nX-Foo:baz'), open: jest.fn(), send: jest.fn(), + readyState: 4, status: 200, statusText: 'OK', responseText: '{"a":"b"}', @@ -60,10 +61,10 @@ describe('unfetch', () => { expect(xhr.send).toHaveBeenCalledWith(null); }); - expect(xhr.onload).toEqual(expect.any(Function)); + expect(xhr.onreadystatechange).toEqual(expect.any(Function)); expect(xhr.onerror).toEqual(expect.any(Function)); - xhr.onload(); + xhr.onreadystatechange(); return p; }); @@ -76,7 +77,7 @@ describe('unfetch', () => { expect(r.headers.get('X-foo')).toEqual('baz'); }); - xhr.onload(); + xhr.onreadystatechange(); return p; }); From 772b9cb184ed59700f388a4eda3c3bdc03def38a Mon Sep 17 00:00:00 2001 From: Denis Kuzmin Date: Thu, 10 Jan 2019 02:47:21 +0300 Subject: [PATCH 2/3] fix a typo with `status` --- src/index.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.mjs b/src/index.mjs index 2dbb32d..4b9bee8 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -12,7 +12,7 @@ export default function(url, options) { request.withCredentials = options.credentials=='include'; request.onreadystatechange = function() { - if(this.readyState === 4 && this.status === 200) { + if(this.readyState === 4) { resolve(response()); } }; From 5d3929464070d8946512bf736ca951ff70eb6edc Mon Sep 17 00:00:00 2001 From: "Denis Kuzmin [ github.com/3F ]" Date: Sun, 13 Jan 2019 19:10:23 +0300 Subject: [PATCH 3/3] status shouldn't be equal to zero. My full explanation of XHR impl for `load` event, here: https://github.com/developit/unfetch/pull/107 --- src/index.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.mjs b/src/index.mjs index 4b9bee8..e6f953d 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -12,7 +12,7 @@ export default function(url, options) { request.withCredentials = options.credentials=='include'; request.onreadystatechange = function() { - if(this.readyState === 4) { + if(this.readyState === 4 && this.status !== 0) { resolve(response()); } };