fork of hey-api/openapi-ts because I need some additional things

Merge pull request #1507 from nimobeeren/fix/dont-parse-without-content-type

authored by

Lubos and committed by
GitHub
253ed28a c8f98569

+16 -75
+5
.changeset/wise-poets-flow.md
··· 1 + --- 2 + '@hey-api/client-fetch': minor 3 + --- 4 + 5 + [BREAKING] return raw response body (of type `ReadableStream`) when `Content-Type` response header is not provided and `parseAs` is set to `auto`
+1 -1
packages/client-fetch/src/__tests__/utils.test.ts
··· 77 77 }> = [ 78 78 { 79 79 content: null, 80 - parseAs: undefined, 80 + parseAs: 'stream', 81 81 }, 82 82 { 83 83 content: 'application/json',
+6 -6
packages/client-fetch/src/index.ts
··· 93 93 }; 94 94 } 95 95 96 - if (opts.parseAs === 'stream') { 96 + const parseAs = 97 + (opts.parseAs === 'auto' 98 + ? getParseAs(response.headers.get('Content-Type')) 99 + : opts.parseAs) ?? 'json'; 100 + 101 + if (parseAs === 'stream') { 97 102 return { 98 103 data: response.body, 99 104 ...result, 100 105 }; 101 106 } 102 - 103 - const parseAs = 104 - (opts.parseAs === 'auto' 105 - ? getParseAs(response.headers.get('Content-Type')) 106 - : opts.parseAs) ?? 'json'; 107 107 108 108 let data = await response[parseAs](); 109 109 if (parseAs === 'json') {
+4 -2
packages/client-fetch/src/utils.ts
··· 327 327 */ 328 328 export const getParseAs = ( 329 329 contentType: string | null, 330 - ): Exclude<Config['parseAs'], 'auto' | 'stream'> => { 330 + ): Exclude<Config['parseAs'], 'auto'> => { 331 331 if (!contentType) { 332 - return; 332 + // If no Content-Type header is provided, the best we can do is return the raw response body, 333 + // which is effectively the same as the 'stream' option. 334 + return 'stream'; 333 335 } 334 336 335 337 const cleanContent = contentType.split(';')[0]?.trim();
-66
packages/client-fetch/test/utils.test.ts
··· 1 - import { describe, expect, it } from 'vitest'; 2 - 3 - import { getParseAs } from '../src/utils'; 4 - 5 - describe('getParseAs', () => { 6 - const scenarios: Array<{ 7 - content: Parameters<typeof getParseAs>[0]; 8 - parseAs: ReturnType<typeof getParseAs>; 9 - }> = [ 10 - { 11 - content: null, 12 - parseAs: undefined, 13 - }, 14 - { 15 - content: 'application/json', 16 - parseAs: 'json', 17 - }, 18 - { 19 - content: 'application/ld+json', 20 - parseAs: 'json', 21 - }, 22 - { 23 - content: 'application/ld+json;charset=utf-8', 24 - parseAs: 'json', 25 - }, 26 - { 27 - content: 'application/ld+json; charset=utf-8', 28 - parseAs: 'json', 29 - }, 30 - { 31 - content: 'multipart/form-data', 32 - parseAs: 'formData', 33 - }, 34 - { 35 - content: 'application/*', 36 - parseAs: 'blob', 37 - }, 38 - { 39 - content: 'audio/*', 40 - parseAs: 'blob', 41 - }, 42 - { 43 - content: 'image/*', 44 - parseAs: 'blob', 45 - }, 46 - { 47 - content: 'video/*', 48 - parseAs: 'blob', 49 - }, 50 - { 51 - content: 'text/*', 52 - parseAs: 'text', 53 - }, 54 - { 55 - content: 'unsupported', 56 - parseAs: undefined, 57 - }, 58 - ]; 59 - 60 - it.each(scenarios)( 61 - 'detects $content as $parseAs', 62 - async ({ content, parseAs }) => { 63 - expect(getParseAs(content)).toEqual(parseAs); 64 - }, 65 - ); 66 - });