Skip to content

Commit

Permalink
feat(fetch): add response headers in fetch client (#1699)
Browse files Browse the repository at this point in the history
* (fix) add response headers in fetch client

* (fix) add response headers in fetch client

* chore: regenerate sample apps

---------

Co-authored-by: melloware <mellowaredev@gmail.com>
  • Loading branch information
soartec-lab and melloware authored Nov 10, 2024
1 parent 7f1be0c commit af46ebe
Show file tree
Hide file tree
Showing 15 changed files with 52 additions and 25 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 @@ -501,7 +501,7 @@ export type SwrOptions = {
};

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

export type InputTransformerFn = (spec: OpenAPIObject) => OpenAPIObject;
Expand Down
12 changes: 7 additions & 5 deletions packages/fetch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,16 @@ ${
}\n`;

const responseTypeName = fetchResponseTypeName(
override.fetch.includeHttpStatusReturnType,
override.fetch.includeHttpResponseReturnType,
response.definition.success,
operationName,
);
const responseTypeImplementation = override.fetch.includeHttpStatusReturnType
const responseTypeImplementation = override.fetch
.includeHttpResponseReturnType
? `export type ${responseTypeName} = {
data: ${response.definition.success || 'unknown'};
status: number;
headers: Headers;
}\n\n`
: '';

Expand Down Expand Up @@ -167,7 +169,7 @@ ${
)
const data = await res.json()
${override.fetch.includeHttpStatusReturnType ? 'return { status: res.status, data }' : `return data as ${responseTypeName}`}
${override.fetch.includeHttpResponseReturnType ? 'return { status: res.status, data, headers: res.headers }' : `return data as ${responseTypeName}`}
`;
const customFetchResponseImplementation = `return ${mutator?.name}<${retrunType}>(${fetchFnOptions});`;

Expand Down Expand Up @@ -197,11 +199,11 @@ ${
};

export const fetchResponseTypeName = (
includeHttpStatusReturnType: boolean,
includeHttpResponseReturnType: boolean,
definitionSuccessResponse: string,
operationName: string,
) => {
return includeHttpStatusReturnType
return includeHttpResponseReturnType
? `${operationName}Response`
: definitionSuccessResponse;
};
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type {
export type listPetsResponse = {
data: Pets;
status: number;
headers: Headers;
};

export const getListPetsUrl = (params?: ListPetsParams) => {
Expand All @@ -43,7 +44,7 @@ export const listPets = async (
});
const data = await res.json();

return { status: res.status, data };
return { status: res.status, data, headers: res.headers };
};

/**
Expand All @@ -52,6 +53,7 @@ export const listPets = async (
export type createPetsResponse = {
data: Pet;
status: number;
headers: Headers;
};

export const getCreatePetsUrl = () => {
Expand All @@ -70,7 +72,7 @@ export const createPets = async (
});
const data = await res.json();

return { status: res.status, data };
return { status: res.status, data, headers: res.headers };
};

/**
Expand All @@ -79,6 +81,7 @@ export const createPets = async (
export type updatePetsResponse = {
data: Pet;
status: number;
headers: Headers;
};

export const getUpdatePetsUrl = () => {
Expand All @@ -97,7 +100,7 @@ export const updatePets = async (
});
const data = await res.json();

return { status: res.status, data };
return { status: res.status, data, headers: res.headers };
};

/**
Expand All @@ -106,6 +109,7 @@ export const updatePets = async (
export type showPetByIdResponse = {
data: Pet;
status: number;
headers: Headers;
};

export const getShowPetByIdUrl = (petId: string) => {
Expand All @@ -122,5 +126,5 @@ export const showPetById = async (
});
const data = await res.json();

return { status: res.status, data };
return { status: res.status, data, headers: res.headers };
};
4 changes: 4 additions & 0 deletions samples/next-app-with-fetch/app/gen/pets/pets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type NonReadonly<T> = [T] extends [UnionToIntersection<T>]
export type listPetsResponse = {
data: Pets;
status: number;
headers: Headers;
};

export const getListPetsUrl = (params?: ListPetsParams) => {
Expand Down Expand Up @@ -78,6 +79,7 @@ export const listPets = async (
export type createPetsResponse = {
data: Pet;
status: number;
headers: Headers;
};

export const getCreatePetsUrl = () => {
Expand All @@ -102,6 +104,7 @@ export const createPets = async (
export type updatePetsResponse = {
data: Pet;
status: number;
headers: Headers;
};

export const getUpdatePetsUrl = () => {
Expand All @@ -126,6 +129,7 @@ export const updatePets = async (
export type showPetByIdResponse = {
data: Pet;
status: number;
headers: Headers;
};

export const getShowPetByIdUrl = (petId: string) => {
Expand Down
2 changes: 1 addition & 1 deletion samples/next-app-with-fetch/custom-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@ export const customFetch = async <T>(
const response = await fetch(request);
const data = await getBody<T>(response);

return { status: response.status, data } as T;
return { status: response.status, data, headers: response.headers } as T;
};
2 changes: 1 addition & 1 deletion samples/react-app-with-swr/fetch-client/orval.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default defineConfig({
mock: true,
override: {
fetch: {
includeHttpStatusReturnType: false,
includeHttpResponseReturnType: false,
},
},
},
Expand Down
4 changes: 4 additions & 0 deletions samples/react-query/custom-fetch/src/gen/pets/pets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type SecondParameter<T extends (...args: any) => any> = Parameters<T>[1];
export type listPetsResponse = {
data: Pets;
status: number;
headers: Headers;
};

export const getListPetsUrl = (params?: ListPetsParams) => {
Expand Down Expand Up @@ -212,6 +213,7 @@ export function useListPets<
export type createPetsResponse = {
data: Pet;
status: number;
headers: Headers;
};

export const getCreatePetsUrl = () => {
Expand Down Expand Up @@ -294,6 +296,7 @@ export const useCreatePets = <TError = Error, TContext = unknown>(options?: {
export type updatePetsResponse = {
data: Pet;
status: number;
headers: Headers;
};

export const getUpdatePetsUrl = () => {
Expand Down Expand Up @@ -376,6 +379,7 @@ export const useUpdatePets = <TError = Error, TContext = unknown>(options?: {
export type showPetByIdResponse = {
data: Pet;
status: number;
headers: Headers;
};

export const getShowPetByIdUrl = (petId: string) => {
Expand Down
4 changes: 4 additions & 0 deletions samples/svelte-query/custom-fetch/src/gen/pets/pets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type SecondParameter<T extends (...args: any) => any> = Parameters<T>[1];
export type listPetsResponse = {
data: Pets;
status: number;
headers: Headers;
};

export const getListPetsUrl = (params?: ListPetsParams) => {
Expand Down Expand Up @@ -159,6 +160,7 @@ export function createListPets<
export type createPetsResponse = {
data: Pet;
status: number;
headers: Headers;
};

export const getCreatePetsUrl = () => {
Expand Down Expand Up @@ -241,6 +243,7 @@ export const createCreatePets = <TError = Error, TContext = unknown>(options?: {
export type updatePetsResponse = {
data: Pet;
status: number;
headers: Headers;
};

export const getUpdatePetsUrl = () => {
Expand Down Expand Up @@ -323,6 +326,7 @@ export const createUpdatePets = <TError = Error, TContext = unknown>(options?: {
export type showPetByIdResponse = {
data: Pet;
status: number;
headers: Headers;
};

export const getShowPetByIdUrl = (petId: string) => {
Expand Down
12 changes: 8 additions & 4 deletions samples/swr-with-zod/src/gen/endpoints/pets/pets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type NonReadonly<T> = [T] extends [UnionToIntersection<T>]
export type listPetsResponse = {
data: Pets;
status: number;
headers: Headers;
};

export const getListPetsUrl = (params?: ListPetsParams) => {
Expand All @@ -76,7 +77,7 @@ export const listPets = async (
});
const data = await res.json();

return { status: res.status, data };
return { status: res.status, data, headers: res.headers };
};

export const getListPetsKey = (params?: ListPetsParams) =>
Expand Down Expand Up @@ -124,6 +125,7 @@ export const useListPets = <TError = Promise<unknown>>(
export type createPetsResponse = {
data: Pet;
status: number;
headers: Headers;
};

export const getCreatePetsUrl = () => {
Expand All @@ -142,7 +144,7 @@ export const createPets = async (
});
const data = await res.json();

return { status: res.status, data };
return { status: res.status, data, headers: res.headers };
};

export const getCreatePetsMutationFetcher = (options?: RequestInit) => {
Expand Down Expand Up @@ -192,6 +194,7 @@ export const useCreatePets = <TError = Promise<Error>>(options?: {
export type updatePetsResponse = {
data: Pet;
status: number;
headers: Headers;
};

export const getUpdatePetsUrl = () => {
Expand All @@ -210,7 +213,7 @@ export const updatePets = async (
});
const data = await res.json();

return { status: res.status, data };
return { status: res.status, data, headers: res.headers };
};

export const getUpdatePetsMutationFetcher = (options?: RequestInit) => {
Expand Down Expand Up @@ -260,6 +263,7 @@ export const useUpdatePets = <TError = Promise<Error>>(options?: {
export type showPetByIdResponse = {
data: Pet;
status: number;
headers: Headers;
};

export const getShowPetByIdUrl = (petId: string) => {
Expand All @@ -276,7 +280,7 @@ export const showPetById = async (
});
const data = await res.json();

return { status: res.status, data };
return { status: res.status, data, headers: res.headers };
};

export const getShowPetByIdKey = (petId: string) =>
Expand Down
Loading

0 comments on commit af46ebe

Please sign in to comment.