From e237115285dc12c78f3c63b8b23dd7649af52c4a Mon Sep 17 00:00:00 2001 From: Nimo Beeren Date: Tue, 24 Dec 2024 18:59:22 +0100 Subject: [PATCH 1/3] return raw response body when no content-type response header is provided --- packages/client-fetch/src/__tests__/utils.test.ts | 2 +- packages/client-fetch/src/index.ts | 12 ++++++------ packages/client-fetch/src/utils.ts | 6 ++++-- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/client-fetch/src/__tests__/utils.test.ts b/packages/client-fetch/src/__tests__/utils.test.ts index 7189f016b..74cd0c986 100644 --- a/packages/client-fetch/src/__tests__/utils.test.ts +++ b/packages/client-fetch/src/__tests__/utils.test.ts @@ -77,7 +77,7 @@ describe('getParseAs', () => { }> = [ { content: null, - parseAs: undefined, + parseAs: 'stream', }, { content: 'application/json', diff --git a/packages/client-fetch/src/index.ts b/packages/client-fetch/src/index.ts index 7c8193f20..31dcacd23 100644 --- a/packages/client-fetch/src/index.ts +++ b/packages/client-fetch/src/index.ts @@ -93,18 +93,18 @@ export const createClient = (config: Config = {}): Client => { }; } - if (opts.parseAs === 'stream') { + const parseAs = + (opts.parseAs === 'auto' + ? getParseAs(response.headers.get('Content-Type')) + : opts.parseAs) ?? 'json'; + + if (parseAs === 'stream') { return { data: response.body, ...result, }; } - const parseAs = - (opts.parseAs === 'auto' - ? getParseAs(response.headers.get('Content-Type')) - : opts.parseAs) ?? 'json'; - let data = await response[parseAs](); if (parseAs === 'json') { if (opts.responseValidator) { diff --git a/packages/client-fetch/src/utils.ts b/packages/client-fetch/src/utils.ts index c334bd6d6..fec0d26d9 100644 --- a/packages/client-fetch/src/utils.ts +++ b/packages/client-fetch/src/utils.ts @@ -327,9 +327,11 @@ export const createQuerySerializer = ({ */ export const getParseAs = ( contentType: string | null, -): Exclude => { +): Exclude => { if (!contentType) { - return; + // If no Content-Type header is provided, the best we can do is return the raw response body, + // which is effectively the same as the 'stream' option. + return 'stream'; } const cleanContent = contentType.split(';')[0]?.trim(); From bc77b6bb8932903744596d5b22c5e1e746573d81 Mon Sep 17 00:00:00 2001 From: Nimo Beeren Date: Tue, 24 Dec 2024 18:59:37 +0100 Subject: [PATCH 2/3] remove duplicate test file --- packages/client-fetch/test/utils.test.ts | 66 ------------------------ 1 file changed, 66 deletions(-) delete mode 100644 packages/client-fetch/test/utils.test.ts diff --git a/packages/client-fetch/test/utils.test.ts b/packages/client-fetch/test/utils.test.ts deleted file mode 100644 index b204572be..000000000 --- a/packages/client-fetch/test/utils.test.ts +++ /dev/null @@ -1,66 +0,0 @@ -import { describe, expect, it } from 'vitest'; - -import { getParseAs } from '../src/utils'; - -describe('getParseAs', () => { - const scenarios: Array<{ - content: Parameters[0]; - parseAs: ReturnType; - }> = [ - { - content: null, - parseAs: undefined, - }, - { - content: 'application/json', - parseAs: 'json', - }, - { - content: 'application/ld+json', - parseAs: 'json', - }, - { - content: 'application/ld+json;charset=utf-8', - parseAs: 'json', - }, - { - content: 'application/ld+json; charset=utf-8', - parseAs: 'json', - }, - { - content: 'multipart/form-data', - parseAs: 'formData', - }, - { - content: 'application/*', - parseAs: 'blob', - }, - { - content: 'audio/*', - parseAs: 'blob', - }, - { - content: 'image/*', - parseAs: 'blob', - }, - { - content: 'video/*', - parseAs: 'blob', - }, - { - content: 'text/*', - parseAs: 'text', - }, - { - content: 'unsupported', - parseAs: undefined, - }, - ]; - - it.each(scenarios)( - 'detects $content as $parseAs', - async ({ content, parseAs }) => { - expect(getParseAs(content)).toEqual(parseAs); - }, - ); -}); From e2e1410b22c0c84c40d1b1803e9650d546350cb7 Mon Sep 17 00:00:00 2001 From: Nimo Beeren Date: Tue, 24 Dec 2024 20:19:25 +0100 Subject: [PATCH 3/3] add changeset --- .changeset/wise-poets-flow.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/wise-poets-flow.md diff --git a/.changeset/wise-poets-flow.md b/.changeset/wise-poets-flow.md new file mode 100644 index 000000000..129ec1830 --- /dev/null +++ b/.changeset/wise-poets-flow.md @@ -0,0 +1,5 @@ +--- +'@hey-api/client-fetch': minor +--- + +[BREAKING] return raw response body (of type `ReadableStream`) when `Content-Type` response header is not provided and `parseAs` is set to `auto`