Skip to content

feat: retry callback - #359

Open
enkot wants to merge 5 commits into
unjs:mainfrom
enkot:retry_callback
Open

feat: retry callback#359
enkot wants to merge 5 commits into
unjs:mainfrom
enkot:retry_callback

Conversation

@enkot

@enkotenkot commented Feb 5, 2024

Copy link
Copy Markdown

🔗 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)=>{returncount<=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

enkot commented Mar 7, 2024

Copy link
Copy Markdown
Author

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

@cernymatej

Copy link
Copy Markdown

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

@AryanShrivastava18

Copy link
Copy Markdown

@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
Copy Markdown

retryCount will be essential 🙏

@fredericDelaporte

Copy link
Copy Markdown

@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 freeto 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

@enkot@gustavopch@cernymatej@AryanShrivastava18@lacherogwu@fredericDelaporte