fix: avoid JSON.parsing empty responses

This commit is contained in:
thepassle 2023-07-06 11:38:27 +02:00
parent 14a8f77960
commit 08b109b2a1
2 changed files with 11 additions and 2 deletions

View file

@ -193,9 +193,11 @@ export class Ajax {
/** @type {any} */ /** @type {any} */
let body = responseText; let body = responseText;
if ( if (
!response.headers.get('content-type') || body.length &&
response.headers.get('content-type')?.includes('json') (!response.headers.get('content-type') ||
response.headers.get('content-type')?.includes('json'))
) { ) {
try { try {
body = JSON.parse(responseText); body = JSON.parse(responseText);

View file

@ -221,6 +221,13 @@ describe('Ajax', () => {
} }
expect(thrown).to.be.true; expect(thrown).to.be.true;
}); });
it('doesnt throw on empty response', async () => {
fetchStub.returns(Promise.resolve(new Response('', responseInit())));
const { response } = await ajax.fetchJson('/foo');
expect(response.ok);
});
}); });
describe('request and response interceptors', () => { describe('request and response interceptors', () => {