ofetch icon indicating copy to clipboard operation
ofetch copied to clipboard

feat: retry callback

Open enkot opened this issue 1 year ago โ€ข 4 comments

๐Ÿ”— 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.

enkot avatar Feb 05 '24 20:02 enkot

My 2 cents: shouldRetry seems to better describe what this does.

gustavopch avatar Feb 07 '24 23:02 gustavopch

@pi0 Could you please check? I think it's a really flexible solution.

enkot avatar Mar 07 '24 22:03 enkot

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

cernymatej avatar Jun 03 '24 11:06 cernymatej