Open Amar-Gill opened 10 months ago
Thanks for the info. I thought that's how it was supposed to work:
onResponse
handles response.ok
, whileonResponseError
handles !response.ok
When I tested in my code, with !response.ok
, onResponse
gets called first, then onResponseError
.
onResponse
will always run, whether the response is an error response or not. I was thinking of adding a separate interceptor for just the response.ok
case.
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 response._data
property. My issue was around having to wrap the code in the if (response.ok)
-- but I shouldn't be using _data
in my code because that is meant for Nuxt internals AFAIK.
I am accessing the
response._data
property. My issue was around having to wrap the code in theif (response.ok)
-- but I shouldn't be using_data
in my code because that is meant for Nuxt internals AFAIK.
I wouldn't worry about response._data
. I got curious and looked into it, and from what I gathered it's just a workaround for node-fetch
, but _data
is still meant to be used publicly.
Though I agree that response.data
would look more pleasing than response._data
.
Interesting! Thanks for sharing that context.
So in this case, I do think the proposed enhancement here could be useful. Because to access response._data
you must wrap it in if (response.ok) { ...
otherwise the interceptor will silently fail when the response is an error.
And a consequence of the silent failure in onResponse
is the intended error handler onResponseError
does not execute.
Checking that response.ok
is truthy is not a big deal for me personally, but I think it would be nice to eliminate boilerplate if we can.
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