-
Notifications
You must be signed in to change notification settings - Fork 116
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
base: main
Are you sure you want to change the base?
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.
@@ -130,3 +134,5 @@ export type FetchRequest = RequestInfo; | |||
export interface SearchParameters { | |||
[key: string]: any; | |||
} | |||
|
|||
type Arrayable<T> = T | Array<T>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about this one. It seems simpler as interceptor types are only defined once, but the more verbose approach might also be viable.
onRequest?:
| ((context: FetchContext) => Promise<void> | void)
| Array<(context: FetchContext) => Promise<void> | void>;
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 π |
π 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