Skip to content

Commit

Permalink
feat(fetch): avoid exception when the response body has no content (o…
Browse files Browse the repository at this point in the history
  • Loading branch information
soartec-lab authored Jan 5, 2025
1 parent 9784454 commit 743a71f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 15 deletions.
8 changes: 5 additions & 3 deletions docs/src/pages/guides/fetch-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
};
Expand Down Expand Up @@ -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;
};
```

Expand Down
2 changes: 1 addition & 1 deletion packages/fetch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`}
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
};
Expand Down Expand Up @@ -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 };
};
Expand Down Expand Up @@ -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 };
};
Expand All @@ -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 };
};
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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;
};
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 @@ -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 };
};
Expand Down Expand Up @@ -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 };
};
Expand Down Expand Up @@ -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 };
};
Expand Down Expand Up @@ -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 };
};
Expand Down

0 comments on commit 743a71f

Please sign in to comment.