-
Notifications
You must be signed in to change notification settings - Fork 127
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
Allow having interceptos from both defaults and per request #319
Comments
$fetch()
interceptors after $fetch.create()
interceptors$fetch()
interceptors after $fetch.create()
interceptors
$fetch()
interceptors after $fetch.create()
interceptors
@pi0 Maybe: $fetch.create({
interceptorsStrategy: 'manual', // 'manual' | 'overwrite'
async onResponse(ctx){
const data = await ctx.options.onResponse?.(ctx)
// do something with data here
return data
}
}) or: $fetch.create({
onResponse: {
strategy: 'manual', // 'manual' | 'overwrite'
// or
manual: true,
async handler(ctx) {
const data = await ctx.options.onResponse?.(ctx)
// do something with data here
return data
}
}
}) or more "futuristic" - interceptor prefixed with $fetch.create({
async $onResponse(ctx){
const data = await ctx.options.onResponse?.(ctx)
// do something with data here
return data
}
}) |
That proposal did not go through but even if we do that, we shall respect first handled value from interceptors. |
100%, can we just pass handled value as second argument? Do you mean this, right? $myFetch({
onResponse(ctx, data) { ... },
onResponseError(ctx, error) { ... }
}) But for me the main question - should we automatically run interceptors one by one based on the specified strategy or just run the "default" interceptor (from $fetch.create) and let it decide what to do with the "request" interceptor? Maybe best of both worlds? - |
@pi0 Do you have any thoughts on the API? |
I recommand middleware style
user can optional run
run at end:
run at middle:
|
Describe the feature
I need to log some data on each request/response, transform the request url (e.g.
/pets/{petId}
->/pets/1
) etc.If I add
onRequest
to the$fetch.create
and then addonRequest
to the actual client:handler from
$fetch.create
will be replaced by the handler from$myFetch
.To get around this we need to create the wrapper around $fetch:
What do you think about adding some sort of interceptors "merging strategy" option?
It can be done based on the idea of this PR #353.
If we treat interceptors as an array, we can allow merging arrays from
$fetch
and$fetch.create
. So that interceptors from$fetch
and$fetch.create
can be enforced to append or prepend to the resulting array of interceptors (similar to Nuxt plugins):Alternative Original idea
so that interceptors from
$myFetch
are called after interceptors from$fetch.create
.Additional information
The text was updated successfully, but these errors were encountered: