unjs / unjs/ofetch

Custom retry logic

Open
#503 1 comment 4 reactions 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

Having a retry on specific status codes is great but could we get a more customizable way of getting a retry to happen? I think this is a common use case to retry a request if the response doest match criteria.

Now I need to do custom wrapper logic to get such functionality.

//custom wrapper
export async function retryCall<T>({
  callback,
  maxRetries = 5,
  isResponseMatch,
  delayMs = 1000,
}: {
  maxRetries?: number;
  isResponseMatch: (data: T) => boolean;
  callback: () => Promise<T>;
  delayMs?: number;
}): Promise<T> {
  for (let attempt = 1; attempt < maxRetries; attempt++) {
    try {
      const response = await callback();

      if (isResponseMatch(response)) {
        return response;
      }
    } catch (error) {
      throw error;
    }

    await new Promise((resolve) => setTimeout(resolve, delayMs));
  }

  return await callback();
}
await retryCall({
      callback: () => ofetch(...),
      isResponseMatch: (data) => data === 'success',
    })
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

The issue names no implementation files or tests; start by locating the existing retry handling and tests for status-code retries. Define the customizable response-matching behavior and its API, then add coverage showing that matching responses stop retries and nonmatching responses retry as configured.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.