Merge pull request #2031 from ing-bank/fix/empty-response
fix: avoid JSON.parsing empty responses
This commit is contained in:
commit
ecf63407ba
3 changed files with 16 additions and 2 deletions
5
.changeset/chilly-bananas-talk.md
Normal file
5
.changeset/chilly-bananas-talk.md
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
"@lion/ajax": patch
|
||||
---
|
||||
|
||||
fix: avoid JSON.parsing empty responses
|
||||
|
|
@ -193,9 +193,11 @@ export class Ajax {
|
|||
|
||||
/** @type {any} */
|
||||
let body = responseText;
|
||||
|
||||
if (
|
||||
!response.headers.get('content-type') ||
|
||||
response.headers.get('content-type')?.includes('json')
|
||||
body.length &&
|
||||
(!response.headers.get('content-type') ||
|
||||
response.headers.get('content-type')?.includes('json'))
|
||||
) {
|
||||
try {
|
||||
body = JSON.parse(responseText);
|
||||
|
|
|
|||
|
|
@ -221,6 +221,13 @@ describe('Ajax', () => {
|
|||
}
|
||||
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', () => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue