fix(ajax): align intercepted and non-intercepted response behavior
This commit is contained in:
parent
abcff8c9e2
commit
362ee0e2e9
4 changed files with 45 additions and 3 deletions
|
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
"@lion/overlays": patch
|
||||
'@lion/overlays': patch
|
||||
---
|
||||
|
||||
fix: isVisible check on elements with display:contents
|
||||
|
|
|
|||
5
.changeset/soft-trains-brush.md
Normal file
5
.changeset/soft-trains-brush.md
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'@lion/ajax': patch
|
||||
---
|
||||
|
||||
Align intercepted and non-intercepted response behavior
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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', () => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue