Skip to content

Commit

Permalink
(fix) add response headers in fetch client
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware committed Nov 9, 2024
1 parent e40b326 commit ee8e99d
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 11 deletions.
4 changes: 2 additions & 2 deletions docs/src/pages/guides/fetch-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export const useListPets = <TError = Promise<Pets | Error>>(
#### return original defined return type

When using `fetch` as an `httpClient`, by default the `fetch` response type includes http status.
If use `swr` or queries, i will be accessing things like `data.data`, which will be noisy so if you want to return a defined return type instead of an automatically generated return type, set `override.fetch.includeHttpStatusReturnType` value to `false`.
If use `swr` or queries, i will be accessing things like `data.data`, which will be noisy so if you want to return a defined return type instead of an automatically generated return type, set `override.fetch.includeHttpResponseReturnType` value to `false`.

```js
module.exports = {
Expand All @@ -106,7 +106,7 @@ module.exports = {
...
override: {
fetch: {
includeHttpStatusReturnType: false,
includeHttpResponseReturnType: false,
},
},
},
Expand Down
4 changes: 2 additions & 2 deletions docs/src/pages/reference/configuration/output.md
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ module.exports = {
...
override: {
fetch: {
includeHttpStatusReturnType: false,
includeHttpResponseReturnType: false,
},
},
},
Expand All @@ -692,7 +692,7 @@ module.exports = {
};
```

##### includeHttpStatusReturnType
##### includeHttpResponseReturnType

Type: `Boolean`.
Default: `true`
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ export type SwrOptions = {
};

export type FetchOptions = {
includeHttpStatusReturnType: boolean;
includeHttpResponseReturnType: boolean;
};

export type InputTransformerFn = (spec: OpenAPIObject) => OpenAPIObject;
Expand Down
3 changes: 2 additions & 1 deletion packages/fetch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ ${
response.definition.success,
operationName,
);
const responseTypeImplementation = override.fetch.includeHttpResponseReturnType
const responseTypeImplementation = override.fetch
.includeHttpResponseReturnType
? `export type ${responseTypeName} = {
data: ${response.definition.success || 'unknown'};
status: number;
Expand Down
5 changes: 3 additions & 2 deletions packages/orval/src/utils/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,9 @@ export const normalizeOptions = async (
provideIn: outputOptions.override?.angular?.provideIn ?? 'root',
},
fetch: {
includeHttpStatusReturnType:
outputOptions.override?.fetch?.includeHttpStatusReturnType ?? true,
includeHttpResponseReturnType:
outputOptions.override?.fetch?.includeHttpResponseReturnType ??
true,
...(outputOptions.override?.fetch ?? {}),
},
useDates: outputOptions.override?.useDates || false,
Expand Down
4 changes: 2 additions & 2 deletions packages/swr/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,13 @@ export const getSwrMutationFetcherOptionType = (
export const getSwrMutationFetcherType = (
response: GetterResponse,
httpClient: OutputHttpClient,
includeHttpStatusReturnType: boolean,
includeHttpResponseReturnType: boolean,
operationName: string,
mutator?: GeneratorMutator,
) => {
if (httpClient === OutputHttpClient.FETCH) {
const responseType = fetchResponseTypeName(
includeHttpStatusReturnType,
includeHttpResponseReturnType,
response.definition.success,
operationName,
);
Expand Down
2 changes: 1 addition & 1 deletion packages/swr/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ export const ${swrKeyFnName} = (${queryKeyProps}) => [\`${route}\`${
const swrMutationFetcherType = getSwrMutationFetcherType(
response,
httpClient,
override.fetch.includeHttpStatusReturnType,
override.fetch.includeHttpResponseReturnType,
operationName,
mutator,
);
Expand Down

0 comments on commit ee8e99d

Please sign in to comment.