Skip to content

Commit

Permalink
Merge pull request #1507 from nimobeeren/fix/dont-parse-without-conte…
Browse files Browse the repository at this point in the history
…nt-type
  • Loading branch information
mrlubos authored Dec 24, 2024
2 parents c8f9856 + e2e1410 commit 253ed28
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 75 deletions.
5 changes: 5 additions & 0 deletions .changeset/wise-poets-flow.md
Original file line number Diff line number Diff line change
@@ -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`
2 changes: 1 addition & 1 deletion packages/client-fetch/src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('getParseAs', () => {
}> = [
{
content: null,
parseAs: undefined,
parseAs: 'stream',
},
{
content: 'application/json',
Expand Down
12 changes: 6 additions & 6 deletions packages/client-fetch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions packages/client-fetch/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,11 @@ export const createQuerySerializer = <T = unknown>({
*/
export const getParseAs = (
contentType: string | null,
): Exclude<Config['parseAs'], 'auto' | 'stream'> => {
): Exclude<Config['parseAs'], 'auto'> => {
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();
Expand Down
66 changes: 0 additions & 66 deletions packages/client-fetch/test/utils.test.ts

This file was deleted.

0 comments on commit 253ed28

Please sign in to comment.