-
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
feat: support interceptors arrays #353
Conversation
This commit allows passing interceptors as single functions (current behaviour), or arrays of functions (new behaviour). This makes it much easier to execute multiple functions on the consumer side. The alternative would be to compose all functions into one, but that's more complicated in my opinion. With this change, the following becomes possible. ```ts $fetch('/api/endpoint', { onRequest: [ () => { /* Do something */ }, () => { /* Do something else */ }, ], onResponse: [ () => { /* Do something */ }, () => { /* Do something else */ }, ], }) ``` Note that calling `$fetch.create()` and then passing interceptors when calling `$fetch()` will keep overriding the interceptors as the current behaviour. I did not want to go too much into there as I'm not sure what would be the expected behaviour. Plus, continuing to override the interceptors means that this PR is not a breaking change.
Friendly ping. π€ Is there anything I can do to make this PR move forward? |
@antoinerey @pi0 const $myFetch = $fetch.create({
onRequest: [
(ctx) => { /* Handler 1 */ }, // same as "enforce: 'default'"
{
enforce: 'post',
handler: (ctx) => { /* Handler 2 */ }
}
]
})
$myFetch({
onRequest: [
// Will be appended
(ctx) => { /* Handler 3 */ },
// If you need to prepend in some scenarios
{
enforce: 'pre',
handler: (ctx) => { /* Handler 4 */ }
}
]
})
// Interceptors will be executed in this order:
/*
Handler 4
Handler 1
Handler 3
Handler 2
*/ Now I don't like my PR anymore π |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #353 +/- ##
===========================================
+ Coverage 56.86% 69.13% +12.27%
===========================================
Files 16 16
Lines 728 499 -229
Branches 113 119 +6
===========================================
- Hits 414 345 -69
+ Misses 303 144 -159
+ Partials 11 10 -1 β View full report in Codecov by Sentry. |
Thanks for the nice PR and tests dear @antoinerey β€οΈ i finally had a chance to check on it and pushed couple of improvement refactors. |
π Linked issue
None.
β Type of change
π Description
This commit allows passing interceptors as single functions (current behaviour), or arrays of functions (new behaviour). This makes it much easier to execute multiple functions on the consumer side. The alternative would be to compose all functions into one, but that's more complicated in my opinion.
With this change, the following becomes possible.
Note that calling
$fetch.create()
and then passing interceptors when calling$fetch()
will keep overriding the interceptors as the current behaviour. I did not want to go too much into there as I'm not sure what would be the expected behaviour. Plus, continuing to override the interceptors means that this PR is not a breaking change.π Checklist