Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fetch): avoid exception when the response body has no content #1778

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -181,7 +181,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
Loading