ofetch
ofetch copied to clipboard
feat: retry callback
๐ Linked issue
https://github.com/unjs/ofetch/issues/358
โ 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)
- [x] โจ 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 PR adds support for a callback for retry option, which is a function that takes a fetch context object and count of retries and returns a boolean (true if the request needs to be retried):
await $fetch('/cart', {
retry: (ctx, count) => {
return count <= 3 && ctx.error?.code === '007'
}
})
It runs onError and after a response, so the developer can decide when the request needs to be retried.
๐ Checklist
- [x] I have linked an issue or discussion.
- [x] I have updated the documentation accordingly.
My 2 cents: shouldRetry seems to better describe what this does.
@pi0 Could you please check? I think it's a really flexible solution.
I love this idea. Probably link my issue as well as this should cover that use case too: https://github.com/unjs/ofetch/issues/355