unjs / unjs/ofetch

Add `retryAttempt` to `FetchContext`

Open
#536 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
TypeScript
Stars
5.4k
Forks
195
PR merge metrics
No merged PRs in 30d

Description

Describe the feature

PR #372 added the ability to pass a callback to retryDelay, which should enable exponential backoff, but currently it's tricky to implement. I couldn't find an option to get the current attempt in this callback. The mentioned PR calls for context.options.retryAttempt, which doesn't exist.

I am currently using a workaround:

function exponentialBackoff(baseRetries: number, baseDelay = 1_000) {
    return (context: FetchContext<any, 'json'>) => {
      const retryLeft = context.options.retry || 0
      const currentRetry = baseRetries - retryLeft
      return baseDelay * 2 ** currentRetry
    }
  }
Additional information
  • Would you be willing to help implement this feature?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing FetchContext and the retryDelay callback behavior described in PR #372, then locate where retry options are assembled. Add the current retry attempt to context.options so exponential backoff callbacks can use it, and verify that retryDelay receives the expected value across retries.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.