-
-
Notifications
You must be signed in to change notification settings - Fork 431
feat(fetch): add ability to throw on error #2168
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
base: master
Are you sure you want to change the base?
Conversation
0baf8ec
to
04a85e9
Compare
@AllieJonsson In addition to this case, there is a case where error handling is done in https://github.com/orval-labs/orval/blob/master/samples/next-app-with-fetch/custom-fetch.ts#L52-L53 The issue I originally reported was simply to limit the type definition of response data to 200. Although the scope is different from the first issue, there was also a request to add default error handling. I support this response because I think it will benefit them. |
Look like merge conflicts? |
I completely forgot about custom fetch. So many questions... 😄 export type createPetsResponse200 = {
data: Pet;
status: 200;
};
export type createPetsResponseDefault = {
data: Error;
status: Exclude<HTTPStatusCodes, 200>;
};
- export type createPetsResponseComposite =
- | createPetsResponse200
- | createPetsResponseDefault;
- export type createPetsResponse = createPetsResponseComposite & {
- headers: Headers;
- };
+ export type createPetsSuccessResponse = createPetsResponse200 & {
+ headers: Headers;
+ };
+
+ export type createPetsErrorResponse = createPetsResponseDefault & {
+ headers: Headers;
+ };
export const createPets = async (
createPetsBodyItem: CreatePetsBodyItem[],
options?: RequestInit,
- ): Promise<createPetsResponse> => {
- return customFetch<createPetsResponse>(getCreatePetsUrl(), {
+ ): Promise<createPetsSuccessResponse> => {
+ return customFetch<createPetsSuccessResponse>(getCreatePetsUrl(), {
...options,
method: 'POST',
headers: { 'Content-Type': 'application/json', ...options?.headers },
body: JSON.stringify(createPetsBodyItem),
});
}; |
This is great. Thank you for these questions as they are very valuable to me 🙌 I also think the option name |
Status
READY
Description
Fixes #2047
When using a package like
swr
orquery
, they use exceptions to know if the response is an error or not.To accomplish this, I added the
shouldThrowOnError
setting. When set to true, the function is typed to return only the success type (or success types, if there are multiple), when set to false, the behavior is unchanged.When the endpoint does not have an error specified:
When the endpoint does have an error specified: