fix(ajax): align intercepted and non-intercepted response behavior

This commit is contained in:
Lars den Bakker 2022-05-26 19:15:51 +02:00 committed by Ahmet Yeşil
parent abcff8c9e2
commit 362ee0e2e9
4 changed files with 45 additions and 3 deletions

View file

@ -1,5 +1,5 @@
--- ---
"@lion/overlays": patch '@lion/overlays': patch
--- ---
fix: isVisible check on elements with display:contents fix: isVisible check on elements with display:contents

View file

@ -0,0 +1,5 @@
---
'@lion/ajax': patch
---
Align intercepted and non-intercepted response behavior

View file

@ -7,6 +7,14 @@ import {
import { AjaxFetchError } from './AjaxFetchError.js'; import { AjaxFetchError } from './AjaxFetchError.js';
import './typedef.js'; import './typedef.js';
/**
* @param {Response} response
* @returns {boolean}
*/
function isFailedResponse(response) {
return response.status >= 400 && response.status < 600;
}
/** /**
* A small wrapper around `fetch`. * A small wrapper around `fetch`.
- Allows globally registering request and response interceptors - Allows globally registering request and response interceptors
@ -115,8 +123,13 @@ export class Ajax {
// run request interceptors, returning directly and skipping the network // run request interceptors, returning directly and skipping the network
const interceptedRequestOrResponse = await this.__interceptRequest(request); const interceptedRequestOrResponse = await this.__interceptRequest(request);
if (interceptedRequestOrResponse instanceof Response) { 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 // prevent network request, return cached response
return interceptedRequestOrResponse; return response;
} }
const response = /** @type {CacheResponse} */ (await fetch(interceptedRequestOrResponse)); const response = /** @type {CacheResponse} */ (await fetch(interceptedRequestOrResponse));
@ -124,7 +137,7 @@ export class Ajax {
const interceptedResponse = await this.__interceptResponse(response); const interceptedResponse = await this.__interceptResponse(response);
if (interceptedResponse.status >= 400 && interceptedResponse.status < 600) { if (isFailedResponse(interceptedResponse)) {
throw new AjaxFetchError(request, interceptedResponse); throw new AjaxFetchError(request, interceptedResponse);
} }
return interceptedResponse; return interceptedResponse;

View file

@ -257,6 +257,30 @@ describe('Ajax', () => {
const text = await response.text(); const text = await response.text();
expect(text).to.equal('mock response'); 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', () => { describe('accept-language header', () => {