Skip to content

Commit

Permalink
feat: handle ndjson response type in fetch client (#1722)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
AllieJonsson authored Dec 23, 2024
1 parent c0eba70 commit 8fbaf07
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 18 deletions.
17 changes: 13 additions & 4 deletions packages/fetch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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}`}
Expand Down
2 changes: 1 addition & 1 deletion tests/configs/fetch.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export default defineConfig({
client: 'fetch',
override: {
fetch: {
includeHttpStatusReturnType: false,
includeHttpResponseReturnType: false,
},
},
},
Expand Down
8 changes: 4 additions & 4 deletions tests/configs/react-query.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
},
Expand Down
8 changes: 4 additions & 4 deletions tests/configs/svelte-query.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion tests/configs/swr.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
8 changes: 4 additions & 4 deletions tests/configs/vue-query.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
},
Expand Down

0 comments on commit 8fbaf07

Please sign in to comment.