Skip to content

Commit

Permalink
feat(fetch): include error types in data type of fetch response (#1776)
Browse files Browse the repository at this point in the history
  • Loading branch information
soartec-lab authored Dec 28, 2024
1 parent 6528d65 commit 17b1517
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 21 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 @@ -20,7 +20,7 @@ module.exports = {
* @summary List all pets
*/
export type listPetsResponse = {
data: Pets;
data: Pets | BadRequest;
status: number;
};

Expand Down Expand Up @@ -120,7 +120,7 @@ module.exports = {
* @summary List all pets
*/
- export type listPetsResponse = {
- data: Pets;
- data: Pets | BadRequest;
- status: number;
- };

Expand Down
7 changes: 6 additions & 1 deletion packages/fetch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,15 @@ ${
operationName,
);

const responseDataType =
response.definition.success || response.definition.errors
? `${response.definition.success !== 'unknown' ? response.definition.success : ''}${response.definition.success !== 'unknown' && response.definition.errors !== 'unknown' ? ' | ' : ''}${response.definition.errors !== 'unknown' ? response.definition.errors : ''}`
: 'unknown';

const responseTypeImplementation = override.fetch
.includeHttpResponseReturnType
? `export type ${responseTypeName} = {
${isNdJson ? 'stream: Response' : `data: ${response.definition.success || 'unknown'}`};
${isNdJson ? 'stream: Response' : `data: ${responseDataType}`};
status: number;
headers: Headers;
}\n\n`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
import type {
CreatePetsBodyItem,
Error,
ListPetsParams,
Pet,
Pets,
Expand Down Expand Up @@ -52,7 +53,7 @@ export const listPets = async (
* @summary Create a pet
*/
export type createPetsResponse = {
data: Pet;
data: Pet | Error;
status: number;
headers: Headers;
};
Expand Down Expand Up @@ -81,7 +82,7 @@ export const createPets = async (
* @summary Update a pet
*/
export type updatePetsResponse = {
data: Pet;
data: Pet | Error;
status: number;
headers: Headers;
};
Expand Down Expand Up @@ -110,7 +111,7 @@ export const updatePets = async (
* @summary Info for a specific pet
*/
export type showPetByIdResponse = {
data: Pet;
data: Pet | Error;
status: number;
headers: Headers;
};
Expand Down
7 changes: 4 additions & 3 deletions samples/next-app-with-fetch/app/gen/pets/pets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
import type {
CreatePetsBodyItem,
Error,
ListPetsParams,
Pet,
Pets,
Expand Down Expand Up @@ -77,7 +78,7 @@ export const listPets = async (
* @summary Create a pet
*/
export type createPetsResponse = {
data: Pet;
data: Pet | Error;
status: number;
headers: Headers;
};
Expand All @@ -102,7 +103,7 @@ export const createPets = async (
* @summary Update a pet
*/
export type updatePetsResponse = {
data: Pet;
data: Pet | Error;
status: number;
headers: Headers;
};
Expand All @@ -127,7 +128,7 @@ export const updatePets = async (
* @summary Info for a specific pet
*/
export type showPetByIdResponse = {
data: Pet;
data: Pet | Error;
status: number;
headers: Headers;
};
Expand Down
6 changes: 3 additions & 3 deletions samples/react-query/custom-fetch/src/gen/pets/pets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export function useListPets<
* @summary Create a pet
*/
export type createPetsResponse = {
data: Pet;
data: Pet | Error;
status: number;
headers: Headers;
};
Expand Down Expand Up @@ -282,7 +282,7 @@ export const useCreatePets = <
* @summary Update a pet
*/
export type updatePetsResponse = {
data: Pet;
data: Pet | Error;
status: number;
headers: Headers;
};
Expand Down Expand Up @@ -372,7 +372,7 @@ export const useUpdatePets = <
* @summary Info for a specific pet
*/
export type showPetByIdResponse = {
data: Pet;
data: Pet | Error;
status: number;
headers: Headers;
};
Expand Down
6 changes: 3 additions & 3 deletions samples/svelte-query/custom-fetch/src/gen/pets/pets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export function createListPets<
* @summary Create a pet
*/
export type createPetsResponse = {
data: Pet;
data: Pet | Error;
status: number;
headers: Headers;
};
Expand Down Expand Up @@ -245,7 +245,7 @@ export const createCreatePets = <
* @summary Update a pet
*/
export type updatePetsResponse = {
data: Pet;
data: Pet | Error;
status: number;
headers: Headers;
};
Expand Down Expand Up @@ -340,7 +340,7 @@ export const createUpdatePets = <
* @summary Info for a specific pet
*/
export type showPetByIdResponse = {
data: Pet;
data: Pet | Error;
status: number;
headers: Headers;
};
Expand Down
6 changes: 3 additions & 3 deletions samples/swr-with-zod/src/gen/endpoints/pets/pets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export const useListPets = <TError = Promise<unknown>>(
* @summary Create a pet
*/
export type createPetsResponse = {
data: Pet;
data: Pet | Error;
status: number;
headers: Headers;
};
Expand Down Expand Up @@ -194,7 +194,7 @@ export const useCreatePets = <TError = Promise<Error>>(options?: {
* @summary Update a pet
*/
export type updatePetsResponse = {
data: Pet;
data: Pet | Error;
status: number;
headers: Headers;
};
Expand Down Expand Up @@ -264,7 +264,7 @@ export const useUpdatePets = <TError = Promise<Error>>(options?: {
* @summary Info for a specific pet
*/
export type showPetByIdResponse = {
data: Pet;
data: Pet | Error;
status: number;
headers: Headers;
};
Expand Down
6 changes: 3 additions & 3 deletions samples/vue-query/custom-fetch/src/gen/pets/pets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export function useListPets<
* @summary Create a pet
*/
export type createPetsResponse = {
data: Pet;
data: Pet | Error;
status: number;
headers: Headers;
};
Expand Down Expand Up @@ -252,7 +252,7 @@ export const useCreatePets = <
* @summary Update a pet
*/
export type updatePetsResponse = {
data: Pet;
data: Pet | Error;
status: number;
headers: Headers;
};
Expand Down Expand Up @@ -347,7 +347,7 @@ export const useUpdatePets = <
* @summary Info for a specific pet
*/
export type showPetByIdResponse = {
data: Pet;
data: Pet | Error;
status: number;
headers: Headers;
};
Expand Down

0 comments on commit 17b1517

Please sign in to comment.