diff --git a/docs/src/pages/guides/fetch-client.md b/docs/src/pages/guides/fetch-client.md index 94f8b7e3f..87d0258f7 100644 --- a/docs/src/pages/guides/fetch-client.md +++ b/docs/src/pages/guides/fetch-client.md @@ -48,7 +48,8 @@ export const listPets = async ( ...options, method: 'GET', }); - const data = await res.json(); + const data: Pets = + [204, 205, 304].includes(res.status) || !res.body ? {} : await res.json(); return { status: res.status, data }; }; @@ -133,10 +134,11 @@ export const listPets = async ( ...options, method: 'GET', }); - const data = await res.json(); + const data: Pets = + [204, 205, 304].includes(res.status) || !res.body ? {} : await res.json(); - return { status: res.status, data }; -+ return data as Pet; ++ return data; }; ``` diff --git a/packages/fetch/src/index.ts b/packages/fetch/src/index.ts index ffaab0414..5a827b474 100644 --- a/packages/fetch/src/index.ts +++ b/packages/fetch/src/index.ts @@ -183,7 +183,7 @@ ${ ` : `const res = await fetch(${fetchFnOptions}) - const data:${response.definition.success} = await res.json() + const data:${response.definition.success} = ([204, 205, 304].includes(res.status) || !res.body) ? {} : await res.json() ${override.fetch.includeHttpResponseReturnType ? 'return { status: res.status, data, headers: res.headers }' : `return data as ${responseTypeName}`} `; 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 6e90ba74c..9334774a2 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 @@ -44,7 +44,8 @@ export const listPets = async ( method: 'GET', }); - const data: Pets = await res.json(); + const data: Pets = + [204, 205, 304].includes(res.status) || !res.body ? {} : await res.json(); return { status: res.status, data, headers: res.headers }; }; @@ -73,7 +74,8 @@ export const createPets = async ( body: JSON.stringify(createPetsBodyItem), }); - const data: Pet = await res.json(); + const data: Pet = + [204, 205, 304].includes(res.status) || !res.body ? {} : await res.json(); return { status: res.status, data, headers: res.headers }; }; @@ -102,7 +104,8 @@ export const updatePets = async ( body: JSON.stringify(pet), }); - const data: Pet = await res.json(); + const data: Pet = + [204, 205, 304].includes(res.status) || !res.body ? {} : await res.json(); return { status: res.status, data, headers: res.headers }; }; @@ -129,7 +132,8 @@ export const showPetById = async ( method: 'GET', }); - const data: Pet = await res.json(); + const data: Pet = + [204, 205, 304].includes(res.status) || !res.body ? {} : await res.json(); return { status: res.status, data, headers: res.headers }; }; diff --git a/samples/react-app-with-swr/fetch-client/src/api/endpoints/swaggerPetstore.ts b/samples/react-app-with-swr/fetch-client/src/api/endpoints/swaggerPetstore.ts index cfc3c0ad2..2bc088a8f 100644 --- a/samples/react-app-with-swr/fetch-client/src/api/endpoints/swaggerPetstore.ts +++ b/samples/react-app-with-swr/fetch-client/src/api/endpoints/swaggerPetstore.ts @@ -42,7 +42,8 @@ export const listPets = async ( method: 'GET', }); - const data: Pets = await res.json(); + const data: Pets = + [204, 205, 304].includes(res.status) || !res.body ? {} : await res.json(); return data as Pets; }; @@ -105,7 +106,8 @@ export const createPets = async ( body: JSON.stringify(createPetsBody), }); - const data: Pet = await res.json(); + const data: Pet = + [204, 205, 304].includes(res.status) || !res.body ? {} : await res.json(); return data as Pet; }; @@ -165,7 +167,8 @@ export const showPetById = async ( method: 'GET', }); - const data: Pet = await res.json(); + const data: Pet = + [204, 205, 304].includes(res.status) || !res.body ? {} : await res.json(); return data as Pet; }; 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 e46762ac6..11a000be7 100644 --- a/samples/swr-with-zod/src/gen/endpoints/pets/pets.ts +++ b/samples/swr-with-zod/src/gen/endpoints/pets/pets.ts @@ -76,7 +76,8 @@ export const listPets = async ( method: 'GET', }); - const data: Pets = await res.json(); + const data: Pets = + [204, 205, 304].includes(res.status) || !res.body ? {} : await res.json(); return { status: res.status, data, headers: res.headers }; }; @@ -144,7 +145,8 @@ export const createPets = async ( body: JSON.stringify(createPetsBodyItem), }); - const data: Pet = await res.json(); + const data: Pet = + [204, 205, 304].includes(res.status) || !res.body ? {} : await res.json(); return { status: res.status, data, headers: res.headers }; }; @@ -214,7 +216,8 @@ export const updatePets = async ( body: JSON.stringify(pet), }); - const data: Pet = await res.json(); + const data: Pet = + [204, 205, 304].includes(res.status) || !res.body ? {} : await res.json(); return { status: res.status, data, headers: res.headers }; }; @@ -282,7 +285,8 @@ export const showPetById = async ( method: 'GET', }); - const data: Pet = await res.json(); + const data: Pet = + [204, 205, 304].includes(res.status) || !res.body ? {} : await res.json(); return { status: res.status, data, headers: res.headers }; };