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

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #1296 from hey-api/fix/client-fetch-parse-as-charset

fix: handle charset in auto parseAs detection

authored by

Lubos and committed by
GitHub
2822bc4f ee7a411d

+107 -25
+5
.changeset/lazy-badgers-retire.md
··· 1 + --- 2 + '@hey-api/client-fetch': patch 3 + --- 4 + 5 + fix: handle charset in auto parseAs detection
+11 -7
packages/client-fetch/src/utils.ts
··· 326 326 * Infers parseAs value from provided Content-Type header. 327 327 */ 328 328 export const getParseAs = ( 329 - content: string | null, 329 + contentType: string | null, 330 330 ): Exclude<Config['parseAs'], 'auto' | 'stream'> => { 331 - if (!content) { 331 + if (!contentType) { 332 332 return; 333 333 } 334 334 335 - // TODO: parser - better detection of MIME types 336 - if (content.startsWith('application/json') || content.endsWith('+json')) { 335 + const cleanContent = contentType.split(';')[0].trim(); 336 + 337 + if ( 338 + cleanContent.startsWith('application/json') || 339 + cleanContent.endsWith('+json') 340 + ) { 337 341 return 'json'; 338 342 } 339 343 340 - if (content === 'multipart/form-data') { 344 + if (cleanContent === 'multipart/form-data') { 341 345 return 'formData'; 342 346 } 343 347 344 348 if ( 345 349 ['application/', 'audio/', 'image/', 'video/'].some((type) => 346 - content.startsWith(type), 350 + cleanContent.startsWith(type), 347 351 ) 348 352 ) { 349 353 return 'blob'; 350 354 } 351 355 352 - if (content.startsWith('text/')) { 356 + if (cleanContent.startsWith('text/')) { 353 357 return 'text'; 354 358 } 355 359 };
+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 + });
+11 -7
packages/openapi-ts/test/__snapshots__/test/generated/v3-hey-api-client-fetch-bundle/client/utils.ts.snap
··· 326 326 * Infers parseAs value from provided Content-Type header. 327 327 */ 328 328 export const getParseAs = ( 329 - content: string | null, 329 + contentType: string | null, 330 330 ): Exclude<Config['parseAs'], 'auto' | 'stream'> => { 331 - if (!content) { 331 + if (!contentType) { 332 332 return; 333 333 } 334 334 335 - // TODO: parser - better detection of MIME types 336 - if (content.startsWith('application/json') || content.endsWith('+json')) { 335 + const cleanContent = contentType.split(';')[0].trim(); 336 + 337 + if ( 338 + cleanContent.startsWith('application/json') || 339 + cleanContent.endsWith('+json') 340 + ) { 337 341 return 'json'; 338 342 } 339 343 340 - if (content === 'multipart/form-data') { 344 + if (cleanContent === 'multipart/form-data') { 341 345 return 'formData'; 342 346 } 343 347 344 348 if ( 345 349 ['application/', 'audio/', 'image/', 'video/'].some((type) => 346 - content.startsWith(type), 350 + cleanContent.startsWith(type), 347 351 ) 348 352 ) { 349 353 return 'blob'; 350 354 } 351 355 352 - if (content.startsWith('text/')) { 356 + if (cleanContent.startsWith('text/')) { 353 357 return 'text'; 354 358 } 355 359 };
+11 -7
packages/openapi-ts/test/__snapshots__/test/generated/v3-hey-api-client-fetch-bundle_transform/client/utils.ts.snap
··· 326 326 * Infers parseAs value from provided Content-Type header. 327 327 */ 328 328 export const getParseAs = ( 329 - content: string | null, 329 + contentType: string | null, 330 330 ): Exclude<Config['parseAs'], 'auto' | 'stream'> => { 331 - if (!content) { 331 + if (!contentType) { 332 332 return; 333 333 } 334 334 335 - // TODO: parser - better detection of MIME types 336 - if (content.startsWith('application/json') || content.endsWith('+json')) { 335 + const cleanContent = contentType.split(';')[0].trim(); 336 + 337 + if ( 338 + cleanContent.startsWith('application/json') || 339 + cleanContent.endsWith('+json') 340 + ) { 337 341 return 'json'; 338 342 } 339 343 340 - if (content === 'multipart/form-data') { 344 + if (cleanContent === 'multipart/form-data') { 341 345 return 'formData'; 342 346 } 343 347 344 348 if ( 345 349 ['application/', 'audio/', 'image/', 'video/'].some((type) => 346 - content.startsWith(type), 350 + cleanContent.startsWith(type), 347 351 ) 348 352 ) { 349 353 return 'blob'; 350 354 } 351 355 352 - if (content.startsWith('text/')) { 356 + if (cleanContent.startsWith('text/')) { 353 357 return 'text'; 354 358 } 355 359 };
+3 -4
packages/openapi-ts/test/sample.cjs
··· 13 13 input: { 14 14 // include: 15 15 // '^(#/components/schemas/import|#/paths/api/v{api-version}/simple/options)$', 16 - // path: './test/spec/3.1.x/discriminator-one-of.yaml', 17 16 path: './test/spec/3.0.x/full.json', 18 17 // path: 'https://mongodb-mms-prod-build-server.s3.amazonaws.com/openapi/2caffd88277a4e27c95dcefc7e3b6a63a3b03297-v2-2023-11-15.json', 19 18 }, ··· 31 30 { 32 31 // asClass: true, 33 32 // include... 34 - // name: '@hey-api/services', 33 + name: '@hey-api/services', 35 34 // operationId: false, 36 35 // serviceNameBuilder: '^Parameters', 37 36 }, ··· 45 44 // enums: 'javascript', 46 45 name: '@hey-api/types', 47 46 // style: 'PascalCase', 48 - tree: true, 47 + // tree: true, 49 48 }, 50 49 { 51 50 // name: '@tanstack/react-query', 52 51 }, 53 52 { 54 53 // name: 'zod', 55 - name: 'fastify', 54 + // name: 'fastify', 56 55 }, 57 56 ], 58 57 // useOptions: false,