From 8e20cf6cc7ad8dea4d8fbfab32efc296476629b5 Mon Sep 17 00:00:00 2001 From: Minsu Kim Date: Tue, 16 Jan 2024 18:34:58 +0900 Subject: [PATCH 1/7] fix: Add try-catch block to prepare for JSON.parse errors. --- source/core/Ky.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/source/core/Ky.ts b/source/core/Ky.ts index 62e3d977..7fe94909 100644 --- a/source/core/Ky.ts +++ b/source/core/Ky.ts @@ -100,6 +100,13 @@ export class Ky { if (options.parseJson) { return options.parseJson(await response.text()); } + + // If error causes when call JSON.parse + try { + return response[type](); + } catch { + return await response.text() + } } return response[type](); From b45633c921bddf75d24a8aa4c00cf0cf28a51eee Mon Sep 17 00:00:00 2001 From: Minsu Kim Date: Tue, 16 Jan 2024 18:53:36 +0900 Subject: [PATCH 2/7] fix: fix eslint error --- source/core/Ky.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/core/Ky.ts b/source/core/Ky.ts index 7fe94909..f4ac5914 100644 --- a/source/core/Ky.ts +++ b/source/core/Ky.ts @@ -103,9 +103,9 @@ export class Ky { // If error causes when call JSON.parse try { - return response[type](); + return (await response.json()); } catch { - return await response.text() + return response.text(); } } From 245cc67f79fff7d19a53546785201d936fe6c798 Mon Sep 17 00:00:00 2001 From: Minsu Kim Date: Wed, 17 Jan 2024 17:23:54 +0900 Subject: [PATCH 3/7] fix: remove try-catch block and add check request header --- source/core/Ky.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/source/core/Ky.ts b/source/core/Ky.ts index f4ac5914..c6dcd9b5 100644 --- a/source/core/Ky.ts +++ b/source/core/Ky.ts @@ -101,10 +101,7 @@ export class Ky { return options.parseJson(await response.text()); } - // If error causes when call JSON.parse - try { - return (await response.json()); - } catch { + if (response.headers.get('accept')?.startsWith('text')) { return response.text(); } } From acd0f8b80d7f36467004340439f6a1f86d2a73c8 Mon Sep 17 00:00:00 2001 From: Minsu Kim Date: Wed, 17 Jan 2024 17:24:56 +0900 Subject: [PATCH 4/7] fix: use ky.request.header --- source/core/Ky.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/core/Ky.ts b/source/core/Ky.ts index c6dcd9b5..ce495277 100644 --- a/source/core/Ky.ts +++ b/source/core/Ky.ts @@ -101,7 +101,7 @@ export class Ky { return options.parseJson(await response.text()); } - if (response.headers.get('accept')?.startsWith('text')) { + if (ky.request.headers.get('accept')?.startsWith('text')) { return response.text(); } } From 33699db31d448024c2d1c2b365b1db691bc968ee Mon Sep 17 00:00:00 2001 From: Minsu Kim Date: Wed, 17 Jan 2024 17:35:17 +0900 Subject: [PATCH 5/7] feat: add test case accept starts with 'text' --- test/main.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/main.ts b/test/main.ts index 7c9933eb..b60fc901 100644 --- a/test/main.ts +++ b/test/main.ts @@ -266,6 +266,23 @@ test('.json() with invalid JSON body', async t => { await server.close(); }); +test('.json() with invalid JSON body and accept starts with text', async t => { + const server = await createHttpTestServer(); + server.get('/', async (request, response) => { + t.is(request.headers.accept, 'text/plain'); + response.end('not json'); + }); + + // I think the code is a bit weird in this situation. + const responseJson = await ky.get(server.url).json(); + const responseText = await ky.get(server.url).text(); + + t.deepEqual(responseJson, 'not json'); + t.deepEqual(responseText, 'not json'); + + await server.close(); +}); + test('.json() with empty body', async t => { t.plan(2); From ffda3e53b1b45e6d5a9866a7c9490aa8d8eaafb8 Mon Sep 17 00:00:00 2001 From: Minsu Kim Date: Wed, 17 Jan 2024 17:46:09 +0900 Subject: [PATCH 6/7] fix: deepEqual -> is --- test/main.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/main.ts b/test/main.ts index b60fc901..8400bfd1 100644 --- a/test/main.ts +++ b/test/main.ts @@ -277,8 +277,8 @@ test('.json() with invalid JSON body and accept starts with text', async t => { const responseJson = await ky.get(server.url).json(); const responseText = await ky.get(server.url).text(); - t.deepEqual(responseJson, 'not json'); - t.deepEqual(responseText, 'not json'); + t.is(responseJson, 'not json'); + t.is(responseText, 'not json'); await server.close(); }); From 7347dae3a4f699dde87982e9aee05e6706f382a7 Mon Sep 17 00:00:00 2001 From: Minsu Kim Date: Wed, 17 Jan 2024 17:57:53 +0900 Subject: [PATCH 7/7] fix: add accept header --- test/main.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/main.ts b/test/main.ts index 8400bfd1..7a7c8061 100644 --- a/test/main.ts +++ b/test/main.ts @@ -269,12 +269,11 @@ test('.json() with invalid JSON body', async t => { test('.json() with invalid JSON body and accept starts with text', async t => { const server = await createHttpTestServer(); server.get('/', async (request, response) => { - t.is(request.headers.accept, 'text/plain'); response.end('not json'); }); // I think the code is a bit weird in this situation. - const responseJson = await ky.get(server.url).json(); + const responseJson = await ky.get(server.url, {headers: {accept: 'text/plain'}}).json(); const responseText = await ky.get(server.url).text(); t.is(responseJson, 'not json');