Skip to content

Conversation

@enkot
Copy link

@enkot enkot commented Feb 5, 2024

πŸ”— Linked issue

resolves #358
resolves #355 (updated)

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

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

@gustavopch

This comment was marked as outdated.

@enkot
Copy link
Author

enkot commented Mar 7, 2024

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

@cernymatej
Copy link

I love this idea. Probably link my issue as well as this should cover that use case too: #355

@AryanShrivastava18
Copy link

@enkot By when can we expect this change to be the part of the release? Is there anything I can give a support for to push it to release?

@lacherogwu
Copy link

retryCount will be essential πŸ™

@fredericDelaporte
Copy link

@gustavopch,

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

The PR does not add a new option. It alters an existing one which currently can only be number | false, for it to also accept a callback returning a boolean or promise of a boolean. See here if you are not aware of that existing option. (Maybe @lacherogwu too.)

@enkot, thanks for the PR, I too would need this feature. I hope it can get merged. It would likely help if you could resolve the conflicts it has with current code.

But maybe it will not entirely cover my use case: an API that may return 401 on Bearer authentication with an access token, and having an endpoint taking refresh tokens for renewing the authentication. With your callback, I could attempt to renew the token
and retry the query if the refresh succeeded, assuming there is a way to alter the headers to be sent for the retry, in order to use the new bearer token.

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.

feat: support callback for retry option Ability to retry a request after an error

6 participants