-
Notifications
You must be signed in to change notification settings - Fork 129
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
onOkResponse interceptor #348
Comments
Thanks for the info. I thought that's how it was supposed to work:
When I tested in my code, with |
But TBH I think I might be using an anti-pattern in my code: onResponse(ctx) {
if (ctx.response.ok) {
const projIdOfReport = ctx.response._data.report.projectId;
activeProject.value = projects.value.find(
(proj) => proj.id === projIdOfReport
);
}
}, I am accessing the |
I wouldn't worry about
Though I agree that |
Interesting! Thanks for sharing that context. So in this case, I do think the proposed enhancement here could be useful. Because to access And a consequence of the silent failure in Checking that |
I think it would be very helpful to handle these separately. A pattern like onSuccess, onError, and onFinish makes a lot of sense to me. onResponse would fire before either response hander (if it stuck around), and onFinish would fire after the other handlers. |
Describe the feature
Would anyone think it's helpful to add a new ofetch interceptor? https://github.com/unjs/ofetch#%EF%B8%8F-interceptors
I'm thinking about an
onOkResponse
interceptor.Because in my project I have a custom
useFetch
call that has anonResponseError
interceptor as well as anonResponse
handler (for successful responses).Reason is, I must wrap my logic in
onResponse
handler withif (ctx.response.ok) { ...
. In the quest to eliminate boilerplate, I wonder if it would be valuable to add theonOkResponse
interceptor, that only runs ifctx.response.ok
istrue
- so the exact opposite ofonResponseError
which only runs ifctx.response.error
is nottrue
.This idea stemmed from me debugging why my interceptor wouldn't run for a couple hours. The root cause was a silent error in
onResponse
which blockedonResponseError
from running. (Maybe this should be a separate issue).My
onResponse
handler depends on the request body to be defined, but during an error response it isn't defined and I got a silent error. Only after wrapping my logic withif (ctx.response.ok)
does myonResponseError
interceptor run.Here is a minimal reproduction of the interceptors getting blocked, I think it will work for any other set of interceptors:
Since an error is thrown from
onResponse
--onResponseError
will not run.Additional information
The text was updated successfully, but these errors were encountered: