From 362ee0e2e9700324bef743bc161dcbc14b28ce9f Mon Sep 17 00:00:00 2001 From: Lars den Bakker Date: Thu, 26 May 2022 19:15:51 +0200 Subject: [PATCH] fix(ajax): align intercepted and non-intercepted response behavior --- .changeset/eight-years-pump.md | 2 +- .changeset/soft-trains-brush.md | 5 +++++ packages/ajax/src/Ajax.js | 17 +++++++++++++++-- packages/ajax/test/Ajax.test.js | 24 ++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 .changeset/soft-trains-brush.md diff --git a/.changeset/eight-years-pump.md b/.changeset/eight-years-pump.md index bd9967cd7..0d9a3285d 100644 --- a/.changeset/eight-years-pump.md +++ b/.changeset/eight-years-pump.md @@ -1,5 +1,5 @@ --- -"@lion/overlays": patch +'@lion/overlays': patch --- fix: isVisible check on elements with display:contents diff --git a/.changeset/soft-trains-brush.md b/.changeset/soft-trains-brush.md new file mode 100644 index 000000000..11ea4ab70 --- /dev/null +++ b/.changeset/soft-trains-brush.md @@ -0,0 +1,5 @@ +--- +'@lion/ajax': patch +--- + +Align intercepted and non-intercepted response behavior diff --git a/packages/ajax/src/Ajax.js b/packages/ajax/src/Ajax.js index 663a65b5a..94f292482 100644 --- a/packages/ajax/src/Ajax.js +++ b/packages/ajax/src/Ajax.js @@ -7,6 +7,14 @@ import { import { AjaxFetchError } from './AjaxFetchError.js'; import './typedef.js'; +/** + * @param {Response} response + * @returns {boolean} + */ +function isFailedResponse(response) { + return response.status >= 400 && response.status < 600; +} + /** * A small wrapper around `fetch`. - Allows globally registering request and response interceptors @@ -115,8 +123,13 @@ export class Ajax { // run request interceptors, returning directly and skipping the network const interceptedRequestOrResponse = await this.__interceptRequest(request); if (interceptedRequestOrResponse instanceof Response) { + const response = /** @type {CacheResponse} */ (interceptedRequestOrResponse); + response.request = request; + if (isFailedResponse(interceptedRequestOrResponse)) { + throw new AjaxFetchError(request, response); + } // prevent network request, return cached response - return interceptedRequestOrResponse; + return response; } const response = /** @type {CacheResponse} */ (await fetch(interceptedRequestOrResponse)); @@ -124,7 +137,7 @@ export class Ajax { const interceptedResponse = await this.__interceptResponse(response); - if (interceptedResponse.status >= 400 && interceptedResponse.status < 600) { + if (isFailedResponse(interceptedResponse)) { throw new AjaxFetchError(request, interceptedResponse); } return interceptedResponse; diff --git a/packages/ajax/test/Ajax.test.js b/packages/ajax/test/Ajax.test.js index 1ab548f29..f9f84f608 100644 --- a/packages/ajax/test/Ajax.test.js +++ b/packages/ajax/test/Ajax.test.js @@ -257,6 +257,30 @@ describe('Ajax', () => { const text = await response.text(); expect(text).to.equal('mock response'); }); + + it('returns the original request object', async () => { + ajax.addRequestInterceptor(async () => new Response('my response', { status: 200 })); + + const response = /** @type {CacheResponse} */ (await await ajax.fetch('/foo')); + expect(response.request).to.be.an.instanceOf(Request); + }); + + it('throws on 4xx responses returned from request interceptor', async () => { + ajax.addRequestInterceptor(async () => new Response('my response', { status: 400 })); + + let thrown = false; + try { + await ajax.fetch('/foo'); + } catch (e) { + // https://github.com/microsoft/TypeScript/issues/20024 open issue, can't type catch clause in param + const _e = /** @type {AjaxFetchError} */ (e); + expect(_e).to.be.an.instanceOf(AjaxFetchError); + expect(_e.request).to.be.an.instanceOf(Request); + expect(_e.response).to.be.an.instanceOf(Response); + thrown = true; + } + expect(thrown).to.be.true; + }); }); describe('accept-language header', () => {