diff --git a/docs/src/pages/guides/fetch-client.md b/docs/src/pages/guides/fetch-client.md index 42fb5f723..4b1c74cda 100644 --- a/docs/src/pages/guides/fetch-client.md +++ b/docs/src/pages/guides/fetch-client.md @@ -97,7 +97,7 @@ export const useListPets = >( #### 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 = { @@ -106,7 +106,7 @@ module.exports = { ... override: { fetch: { - includeHttpStatusReturnType: false, + includeHttpResponseReturnType: false, }, }, }, diff --git a/docs/src/pages/reference/configuration/output.md b/docs/src/pages/reference/configuration/output.md index d60eb92dc..8a2b2302b 100644 --- a/docs/src/pages/reference/configuration/output.md +++ b/docs/src/pages/reference/configuration/output.md @@ -683,7 +683,7 @@ module.exports = { ... override: { fetch: { - includeHttpStatusReturnType: false, + includeHttpResponseReturnType: false, }, }, }, @@ -692,7 +692,7 @@ module.exports = { }; ``` -##### includeHttpStatusReturnType +##### includeHttpResponseReturnType Type: `Boolean`. Default: `true` diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index cdd83346d..0ba6766f2 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -501,7 +501,7 @@ export type SwrOptions = { }; export type FetchOptions = { - includeHttpStatusReturnType: boolean; + includeHttpResponseReturnType: boolean; }; export type InputTransformerFn = (spec: OpenAPIObject) => OpenAPIObject; diff --git a/packages/fetch/src/index.ts b/packages/fetch/src/index.ts index 9881186b5..e3e5e1400 100644 --- a/packages/fetch/src/index.ts +++ b/packages/fetch/src/index.ts @@ -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` : ''; @@ -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});`; @@ -197,11 +199,11 @@ ${ }; export const fetchResponseTypeName = ( - includeHttpStatusReturnType: boolean, + includeHttpResponseReturnType: boolean, definitionSuccessResponse: string, operationName: string, ) => { - return includeHttpStatusReturnType + return includeHttpResponseReturnType ? `${operationName}Response` : definitionSuccessResponse; }; diff --git a/packages/orval/src/utils/options.ts b/packages/orval/src/utils/options.ts index e3731e9f3..c2fa9b89d 100644 --- a/packages/orval/src/utils/options.ts +++ b/packages/orval/src/utils/options.ts @@ -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, diff --git a/packages/swr/src/client.ts b/packages/swr/src/client.ts index 8dbd4ef77..bc67c527e 100644 --- a/packages/swr/src/client.ts +++ b/packages/swr/src/client.ts @@ -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, ); diff --git a/packages/swr/src/index.ts b/packages/swr/src/index.ts index 43bf6c083..4d9af3a07 100644 --- a/packages/swr/src/index.ts +++ b/packages/swr/src/index.ts @@ -532,7 +532,7 @@ export const ${swrKeyFnName} = (${queryKeyProps}) => [\`${route}\`${ const swrMutationFetcherType = getSwrMutationFetcherType( response, httpClient, - override.fetch.includeHttpStatusReturnType, + override.fetch.includeHttpResponseReturnType, operationName, mutator, ); diff --git a/samples/hono/hono-with-fetch-client/next-app/app/gen/pets/pets.ts b/samples/hono/hono-with-fetch-client/next-app/app/gen/pets/pets.ts index 526d2ed34..5af00ea72 100644 --- a/samples/hono/hono-with-fetch-client/next-app/app/gen/pets/pets.ts +++ b/samples/hono/hono-with-fetch-client/next-app/app/gen/pets/pets.ts @@ -17,6 +17,7 @@ import type { export type listPetsResponse = { data: Pets; status: number; + headers: Headers; }; export const getListPetsUrl = (params?: ListPetsParams) => { @@ -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 }; }; /** @@ -52,6 +53,7 @@ export const listPets = async ( export type createPetsResponse = { data: Pet; status: number; + headers: Headers; }; export const getCreatePetsUrl = () => { @@ -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 }; }; /** @@ -79,6 +81,7 @@ export const createPets = async ( export type updatePetsResponse = { data: Pet; status: number; + headers: Headers; }; export const getUpdatePetsUrl = () => { @@ -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 }; }; /** @@ -106,6 +109,7 @@ export const updatePets = async ( export type showPetByIdResponse = { data: Pet; status: number; + headers: Headers; }; export const getShowPetByIdUrl = (petId: string) => { @@ -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 }; }; diff --git a/samples/next-app-with-fetch/app/gen/pets/pets.ts b/samples/next-app-with-fetch/app/gen/pets/pets.ts index 00f30ada7..a28658815 100644 --- a/samples/next-app-with-fetch/app/gen/pets/pets.ts +++ b/samples/next-app-with-fetch/app/gen/pets/pets.ts @@ -46,6 +46,7 @@ type NonReadonly = [T] extends [UnionToIntersection] export type listPetsResponse = { data: Pets; status: number; + headers: Headers; }; export const getListPetsUrl = (params?: ListPetsParams) => { @@ -78,6 +79,7 @@ export const listPets = async ( export type createPetsResponse = { data: Pet; status: number; + headers: Headers; }; export const getCreatePetsUrl = () => { @@ -102,6 +104,7 @@ export const createPets = async ( export type updatePetsResponse = { data: Pet; status: number; + headers: Headers; }; export const getUpdatePetsUrl = () => { @@ -126,6 +129,7 @@ export const updatePets = async ( export type showPetByIdResponse = { data: Pet; status: number; + headers: Headers; }; export const getShowPetByIdUrl = (petId: string) => { diff --git a/samples/next-app-with-fetch/custom-fetch.ts b/samples/next-app-with-fetch/custom-fetch.ts index 1750f435e..3a4cc883c 100644 --- a/samples/next-app-with-fetch/custom-fetch.ts +++ b/samples/next-app-with-fetch/custom-fetch.ts @@ -53,5 +53,5 @@ export const customFetch = async ( const response = await fetch(request); const data = await getBody(response); - return { status: response.status, data } as T; + return { status: response.status, data, headers: response.headers } as T; }; diff --git a/samples/react-app-with-swr/fetch-client/orval.config.ts b/samples/react-app-with-swr/fetch-client/orval.config.ts index 234c57551..3d71c015b 100644 --- a/samples/react-app-with-swr/fetch-client/orval.config.ts +++ b/samples/react-app-with-swr/fetch-client/orval.config.ts @@ -13,7 +13,7 @@ export default defineConfig({ mock: true, override: { fetch: { - includeHttpStatusReturnType: false, + includeHttpResponseReturnType: false, }, }, }, diff --git a/samples/react-query/custom-fetch/src/gen/pets/pets.ts b/samples/react-query/custom-fetch/src/gen/pets/pets.ts index 6ac942a74..4729b3b87 100644 --- a/samples/react-query/custom-fetch/src/gen/pets/pets.ts +++ b/samples/react-query/custom-fetch/src/gen/pets/pets.ts @@ -63,6 +63,7 @@ type SecondParameter any> = Parameters[1]; export type listPetsResponse = { data: Pets; status: number; + headers: Headers; }; export const getListPetsUrl = (params?: ListPetsParams) => { @@ -212,6 +213,7 @@ export function useListPets< export type createPetsResponse = { data: Pet; status: number; + headers: Headers; }; export const getCreatePetsUrl = () => { @@ -294,6 +296,7 @@ export const useCreatePets = (options?: { export type updatePetsResponse = { data: Pet; status: number; + headers: Headers; }; export const getUpdatePetsUrl = () => { @@ -376,6 +379,7 @@ export const useUpdatePets = (options?: { export type showPetByIdResponse = { data: Pet; status: number; + headers: Headers; }; export const getShowPetByIdUrl = (petId: string) => { diff --git a/samples/svelte-query/custom-fetch/src/gen/pets/pets.ts b/samples/svelte-query/custom-fetch/src/gen/pets/pets.ts index ab7cde177..44c153663 100644 --- a/samples/svelte-query/custom-fetch/src/gen/pets/pets.ts +++ b/samples/svelte-query/custom-fetch/src/gen/pets/pets.ts @@ -59,6 +59,7 @@ type SecondParameter any> = Parameters[1]; export type listPetsResponse = { data: Pets; status: number; + headers: Headers; }; export const getListPetsUrl = (params?: ListPetsParams) => { @@ -159,6 +160,7 @@ export function createListPets< export type createPetsResponse = { data: Pet; status: number; + headers: Headers; }; export const getCreatePetsUrl = () => { @@ -241,6 +243,7 @@ export const createCreatePets = (options?: { export type updatePetsResponse = { data: Pet; status: number; + headers: Headers; }; export const getUpdatePetsUrl = () => { @@ -323,6 +326,7 @@ export const createUpdatePets = (options?: { export type showPetByIdResponse = { data: Pet; status: number; + headers: Headers; }; export const getShowPetByIdUrl = (petId: string) => { diff --git a/samples/swr-with-zod/src/gen/endpoints/pets/pets.ts b/samples/swr-with-zod/src/gen/endpoints/pets/pets.ts index 53edc9ab4..3f5f5144a 100644 --- a/samples/swr-with-zod/src/gen/endpoints/pets/pets.ts +++ b/samples/swr-with-zod/src/gen/endpoints/pets/pets.ts @@ -50,6 +50,7 @@ type NonReadonly = [T] extends [UnionToIntersection] export type listPetsResponse = { data: Pets; status: number; + headers: Headers; }; export const getListPetsUrl = (params?: ListPetsParams) => { @@ -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) => @@ -124,6 +125,7 @@ export const useListPets = >( export type createPetsResponse = { data: Pet; status: number; + headers: Headers; }; export const getCreatePetsUrl = () => { @@ -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) => { @@ -192,6 +194,7 @@ export const useCreatePets = >(options?: { export type updatePetsResponse = { data: Pet; status: number; + headers: Headers; }; export const getUpdatePetsUrl = () => { @@ -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) => { @@ -260,6 +263,7 @@ export const useUpdatePets = >(options?: { export type showPetByIdResponse = { data: Pet; status: number; + headers: Headers; }; export const getShowPetByIdUrl = (petId: string) => { @@ -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) => diff --git a/samples/vue-query/custom-fetch/src/gen/pets/pets.ts b/samples/vue-query/custom-fetch/src/gen/pets/pets.ts index 9d2f2da96..5ce55d981 100644 --- a/samples/vue-query/custom-fetch/src/gen/pets/pets.ts +++ b/samples/vue-query/custom-fetch/src/gen/pets/pets.ts @@ -62,6 +62,7 @@ type SecondParameter any> = Parameters[1]; export type listPetsResponse = { data: Pets; status: number; + headers: Headers; }; export const getListPetsUrl = (params?: ListPetsParams) => { @@ -162,6 +163,7 @@ export function useListPets< export type createPetsResponse = { data: Pet; status: number; + headers: Headers; }; export const getCreatePetsUrl = () => { @@ -244,6 +246,7 @@ export const useCreatePets = (options?: { export type updatePetsResponse = { data: Pet; status: number; + headers: Headers; }; export const getUpdatePetsUrl = () => { @@ -326,6 +329,7 @@ export const useUpdatePets = (options?: { export type showPetByIdResponse = { data: Pet; status: number; + headers: Headers; }; export const getShowPetByIdUrl = (petId: string) => {