Skip to content

Commit 5993edb

Browse files
committed
feat: add retryIf option (#503)
1 parent d61b2fc commit 5993edb

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

src/fetch.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,19 @@ export function createFetch(globalOptions: CreateFetchOptions = {}): $Fetch {
5555
}
5656

5757
const responseCode = (context.response && context.response.status) || 500;
58-
if (
59-
retries > 0 &&
60-
(Array.isArray(context.options.retryStatusCodes)
61-
? context.options.retryStatusCodes.includes(responseCode)
62-
: retryStatusCodes.has(responseCode))
63-
) {
58+
59+
const isRetryableStatus = Array.isArray(context.options.retryStatusCodes)
60+
? context.options.retryStatusCodes.includes(responseCode)
61+
: retryStatusCodes.has(responseCode);
62+
63+
const isConditionalRetry =
64+
typeof context.options.retryIf === "function"
65+
? await context.options.retryIf(context)
66+
: false;
67+
68+
const shouldRetry = isRetryableStatus || isConditionalRetry;
69+
70+
if (retries > 0 && shouldRetry) {
6471
const retryDelay =
6572
typeof context.options.retryDelay === "function"
6673
? context.options.retryDelay(context)

src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ export interface FetchOptions<R extends ResponseType = ResponseType, T = any>
6868

6969
/** Default is [408, 409, 425, 429, 500, 502, 503, 504] */
7070
retryStatusCodes?: number[];
71+
72+
/**
73+
* Condition to retry the request.
74+
* @default false
75+
*/
76+
retryIf?: (context: FetchContext<T, R>) => boolean | Promise<boolean>;
7177
}
7278

7379
export interface ResolvedFetchOptions<

0 commit comments

Comments
 (0)