Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

antoinerey
Copy link

@antoinerey antoinerey commented Jan 25, 2024

πŸ”— Linked issue

None.

❓ Type of change

  • πŸ“– Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to 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.

$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.

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

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>;
Copy link
Author

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>;

@antoinerey
Copy link
Author

Friendly ping. πŸ€— Is there anything I can do to make this PR move forward?

@enkot
Copy link

enkot commented Mar 14, 2024

@antoinerey @pi0
I think it might be a better alternative to my PR (#357)
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):

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 πŸ˜…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants