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

Merged
merged 4 commits into from
Aug 28, 2024

Conversation

antoinerey
Copy link
Contributor

@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.
src/types.ts Outdated Show resolved Hide resolved
@antoinerey
Copy link
Contributor 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 πŸ˜…

Copy link

codecov bot commented Aug 27, 2024

Codecov Report

Attention: Patch coverage is 85.18519% with 4 lines in your changes missing coverage. Please review.

Project coverage is 69.13%. Comparing base (27996d3) to head (60bdd98).
Report is 37 commits behind head on main.

Files Patch % Lines
src/fetch.ts 69.23% 4 Missing ⚠️
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.
πŸ“’ Have feedback on the report? Share it here.

@pi0
Copy link
Member

pi0 commented Aug 28, 2024

Thanks for the nice PR and tests dear @antoinerey ❀️

i finally had a chance to check on it and pushed couple of improvement refactors.

@pi0 pi0 merged commit 3baf1cc into unjs:main Aug 28, 2024
4 checks passed
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.

3 participants