From 8fbaf07719a5f7678b54b453b9d88339fe5e5900 Mon Sep 17 00:00:00 2001 From: Alice Jonsson <10475857+AllieJonsson@users.noreply.github.com> Date: Mon, 23 Dec 2024 14:20:03 +0100 Subject: [PATCH] feat: handle ndjson response type in fetch client (#1722) * feat: stream ndjson responses * feat: add swr and query support * feat: allow user to aggregate only some operations * fix: move ndjson setting to override.fetch * fix: add specification and tests for ndjson * chore: add docs, correct configs * fix: return the response in case of ndjson stream * fix: add comment of type * fix: revert unnecessary changes --- packages/fetch/src/index.ts | 17 +++++++++++++---- tests/configs/fetch.config.ts | 2 +- tests/configs/react-query.config.ts | 8 ++++---- tests/configs/svelte-query.config.ts | 8 ++++---- tests/configs/swr.config.ts | 2 +- tests/configs/vue-query.config.ts | 8 ++++---- 6 files changed, 27 insertions(+), 18 deletions(-) diff --git a/packages/fetch/src/index.ts b/packages/fetch/src/index.ts index e3e5e1400..cd9a72c81 100644 --- a/packages/fetch/src/index.ts +++ b/packages/fetch/src/index.ts @@ -104,15 +104,19 @@ ${ } }\n`; + const isNdJson = response.contentTypes.some( + (c) => c === 'application/nd-json' || c === 'application/x-ndjson', + ); const responseTypeName = fetchResponseTypeName( override.fetch.includeHttpResponseReturnType, - response.definition.success, + isNdJson ? 'Response' : response.definition.success, operationName, ); + const responseTypeImplementation = override.fetch .includeHttpResponseReturnType ? `export type ${responseTypeName} = { - data: ${response.definition.success || 'unknown'}; + ${isNdJson ? 'stream: Response' : `data: ${response.definition.success || 'unknown'}`}; status: number; headers: Headers; }\n\n` @@ -165,8 +169,13 @@ ${ ${fetchBodyOption} } `; - const fetchResponseImplementation = `const res = await fetch(${fetchFnOptions} - ) + const fetchResponseImplementation = isNdJson + ? `const stream = await fetch(${fetchFnOptions}) + + ${override.fetch.includeHttpResponseReturnType ? 'return { status: stream.status, stream, headers: stream.headers }' : `return stream`} + ` + : `const res = await fetch(${fetchFnOptions}) + const data = await res.json() ${override.fetch.includeHttpResponseReturnType ? 'return { status: res.status, data, headers: res.headers }' : `return data as ${responseTypeName}`} diff --git a/tests/configs/fetch.config.ts b/tests/configs/fetch.config.ts index ec8e88631..f4493063a 100644 --- a/tests/configs/fetch.config.ts +++ b/tests/configs/fetch.config.ts @@ -84,7 +84,7 @@ export default defineConfig({ client: 'fetch', override: { fetch: { - includeHttpStatusReturnType: false, + includeHttpResponseReturnType: false, }, }, }, diff --git a/tests/configs/react-query.config.ts b/tests/configs/react-query.config.ts index 531ad989d..e5d5a7c6b 100644 --- a/tests/configs/react-query.config.ts +++ b/tests/configs/react-query.config.ts @@ -61,18 +61,18 @@ export default defineConfig({ target: '../specifications/petstore.yaml', }, }, - httpClientFetchWithIncludeHttpStatusReturnType: { + httpClientFetchWithIncludeHttpResponseReturnType: { output: { target: - '../generated/react-query/http-client-fetch-with-include-http-status-return-type/endpoints.ts', + '../generated/react-query/http-client-fetch-with-include-http-response-return-type/endpoints.ts', schemas: - '../generated/react-query/http-client-fetch-with-include-http-status-return-type/model', + '../generated/react-query/http-client-fetch-with-include-http-response-return-type/model', mode: 'tags-split', client: 'react-query', httpClient: 'fetch', override: { fetch: { - includeHttpStatusReturnType: false, + includeHttpResponseReturnType: false, }, }, }, diff --git a/tests/configs/svelte-query.config.ts b/tests/configs/svelte-query.config.ts index 827f394e5..d1550fbbf 100644 --- a/tests/configs/svelte-query.config.ts +++ b/tests/configs/svelte-query.config.ts @@ -70,18 +70,18 @@ export default defineConfig({ target: '../specifications/petstore.yaml', }, }, - httpClientFetchWithIncludeHttpStatusReturnType: { + httpClientFetchWithIncludeHttpResponseReturnType: { output: { target: - '../generated/svelte-query/http-client-fetch-with-include-http-status-return-type/endpoints.ts', + '../generated/svelte-query/http-client-fetch-with-include-http-response-return-type/endpoints.ts', schemas: - '../generated/svelte-query/http-client-fetch-with-include-http-status-return-type/model', + '../generated/svelte-query/http-client-fetch-with-include-http-response-return-type/model', mode: 'tags-split', client: 'svelte-query', httpClient: 'fetch', override: { fetch: { - includeHttpStatusReturnType: false, + includeHttpResponseReturnType: false, }, }, }, diff --git a/tests/configs/swr.config.ts b/tests/configs/swr.config.ts index 251e77009..ed42b2e0f 100644 --- a/tests/configs/swr.config.ts +++ b/tests/configs/swr.config.ts @@ -63,7 +63,7 @@ export default defineConfig({ target: '../specifications/petstore.yaml', }, }, - httpClientFetchWithIncludeHttpStatusReturnType: { + httpClientFetchWithIncludeHttpResponseReturnType: { output: { target: '../generated/swr/http-client-fetch-with-include-http_status_return-type/endpoints.ts', diff --git a/tests/configs/vue-query.config.ts b/tests/configs/vue-query.config.ts index e3000475a..40172a35b 100644 --- a/tests/configs/vue-query.config.ts +++ b/tests/configs/vue-query.config.ts @@ -70,18 +70,18 @@ export default defineConfig({ target: '../specifications/petstore.yaml', }, }, - httpClientFetchWithIncludeHttpStatusReturnType: { + httpClientFetchWithIncludeHttpResponseReturnType: { output: { target: - '../generated/vue-query/http-client-fetch-with-include-http-status-return-type/endpoints.ts', + '../generated/vue-query/http-client-fetch-with-include-http-response-return-type/endpoints.ts', schemas: - '../generated/vue-query/http-client-fetch-with-include-http-status-return-type/model', + '../generated/vue-query/http-client-fetch-with-include-http-response-return-type/model', mode: 'tags-split', client: 'vue-query', httpClient: 'fetch', override: { fetch: { - includeHttpStatusReturnType: false, + includeHttpResponseReturnType: false, }, }, },