restful-react
restful-react copied to clipboard
Rejecting responses with HTTP failure codes has unfortunate consequences
Thanks for making restful-react! It's a great library and tool (I'm using it to generate code from an OpenAPI spec).
I really like how useGet works:
- The promise never rejects, so you can
awaiton it safely without it throwing an error - It's easy to check if
dataorerroris defined, and then act on it. (If I was to change anything, there are some cool things you can do with conditional types, so that the compiler can automatically infer thatdataorerroris defined, but never both)
However, useMutate has some drawbacks:
- The promise can throw, meaning that
awaitis not safe without appending an empty.thenclause:
Furthermore, even though theawait useMutate("path", props).catch(() => { /* pass */})errorfield fromuseMutateis of typeGetDataError<TError>, the compiler cannot infer that in the promise rejection clause, because failed promises can contain anything. - There's no
datafield, to accompanyerror. That means that I'll have to use two mechanisms:awaitonmutateto read the result of my operation, and checkerrorfor any failures.
I'd think it would make a lot of sense for useMutate to be more similar to useGet:
- The promise returned by
mutateshould never reject, and also resolve tovoid - A new field
data: TData | nullshould be added, to mirror the existingerror.
Do you have any thoughts on this? I'd be happy to contribute some code to implement this functionality.
If I submitted a PR for this, would it be considered for merging?
Hi @torkelrogstad,
Thanks for the feedback! 😃
For info, we have 24 occurences of .catch in our codebase, so even if we break the API this should not take so long to migrate. So yes, we would considered to merge if the partern is better (and I also think we can improve this)
Just some considerations to have in mind, we have a lot of this kind of logic:
const MyComp = () => {
const { mutate: createResource } = useMutate();
return (
<Button
onClick={() =>
createResource()
.then(() => dispatch("success"))
.catch(() => dispatch("error"))
}
/>
);
};
Basically, redirect on success / display a nice message / refetch a list. I wondering how this will look like with a promesse that doesn't throw.
For the error & data part in useGet, both can be defined if I remember well in case of a refetch, so you can display an error and keep your stale data 😉
What do you think of this?
const MyComp = () => {
const { mutate: createResource, data, error } = useMutate();
return (
<Button
onClick={async () => {
await createResource()
if (error) {
return dispatch("error", error) // error has type GetDataError<TError>
}
dispatch("success", data))
}}
/>
);
};
It very closely mimics useGet, which I think is good for two reasons:
useGethas a very pleasant API- Consistency is always good:-)
@torkelrogstad Looks nice, always worried about how React behave with this error state in middle of a callback, but if this is working this looks good.
@TejasQ any thoughts about this?
I think typically one would want to handle the dispatch in a useEffect, so rewriting the above you might have:
const MyComp = () => {
const { mutate: createResource, data, error } = useMutate();
useEffect(() => {
if (error) {
dispatch("error", error)
}
}, [error]);
useEffect(() => {
if (data) {
dispatch("data", data)
}
}, [data]);
return (
<Button
onClick={() => createResource()}}
/>
);
};
which I think mirrors the typical pattern for useGet-handling as well.
@amacleay-cohere you're absolutely right, this seems like the idiomatic hooks way of handling this. I'm a bit swamped with work right now, but hopefully I can sit down next week with an attempt at this.
Thank you so much, @torkelrogstad!