| 1 |
import type { |
| 2 |
BaseQueryApi, |
| 3 |
BaseQueryArg, |
| 4 |
BaseQueryEnhancer, |
| 5 |
BaseQueryError, |
| 6 |
BaseQueryExtraOptions, |
| 7 |
BaseQueryFn, |
| 8 |
BaseQueryMeta, |
| 9 |
} from './baseQueryTypes' |
| 10 |
import type { FetchBaseQueryError } from './fetchBaseQuery' |
| 11 |
import { HandledError } from './HandledError' |
| 12 |
|
| 13 |
/** |
| 14 |
* Exponential backoff based on the attempt number. |
| 15 |
* |
| 16 |
* @remarks |
| 17 |
* 1. 600ms * random(0.4, 1.4) |
| 18 |
* 2. 1200ms * random(0.4, 1.4) |
| 19 |
* 3. 2400ms * random(0.4, 1.4) |
| 20 |
* 4. 4800ms * random(0.4, 1.4) |
| 21 |
* 5. 9600ms * random(0.4, 1.4) |
| 22 |
* |
| 23 |
* @param attempt - Current attempt |
| 24 |
* @param maxRetries - Maximum number of retries |
| 25 |
*/ |
| 26 |
async function defaultBackoff(attempt: number = 0, maxRetries: number = 5) { |
| 27 |
const attempts = Math.min(attempt, maxRetries) |
| 28 |
|
| 29 |
const timeout = ~~((Math.random() + 0.4) * (300 << attempts)) // Force a positive int in the case we make this an option |
| 30 |
await new Promise((resolve) => |
| 31 |
setTimeout((res: any) => resolve(res), timeout), |
| 32 |
) |
| 33 |
} |
| 34 |
|
| 35 |
type RetryConditionFunction = ( |
| 36 |
error: BaseQueryError<BaseQueryFn>, |
| 37 |
args: BaseQueryArg<BaseQueryFn>, |
| 38 |
extraArgs: { |
| 39 |
attempt: number |
| 40 |
baseQueryApi: BaseQueryApi |
| 41 |
extraOptions: BaseQueryExtraOptions<BaseQueryFn> & RetryOptions |
| 42 |
}, |
| 43 |
) => boolean |
| 44 |
|
| 45 |
export type RetryOptions = { |
| 46 |
/** |
| 47 |
* Function used to determine delay between retries |
| 48 |
*/ |
| 49 |
backoff?: (attempt: number, maxRetries: number) => Promise<void> |
| 50 |
} & ( |
| 51 |
| { |
| 52 |
/** |
| 53 |
* How many times the query will be retried (default: 5) |
| 54 |
*/ |
| 55 |
maxRetries?: number |
| 56 |
retryCondition?: undefined |
| 57 |
} |
| 58 |
| { |
| 59 |
/** |
| 60 |
* Callback to determine if a retry should be attempted. |
| 61 |
* Return `true` for another retry and `false` to quit trying prematurely. |
| 62 |
*/ |
| 63 |
retryCondition?: RetryConditionFunction |
| 64 |
maxRetries?: undefined |
| 65 |
} |
| 66 |
) |
| 67 |
|
| 68 |
function fail<BaseQuery extends BaseQueryFn = BaseQueryFn>( |
| 69 |
error: BaseQueryError<BaseQuery>, |
| 70 |
meta?: BaseQueryMeta<BaseQuery>, |
| 71 |
): never { |
| 72 |
throw Object.assign(new HandledError({ error, meta }), { |
| 73 |
throwImmediately: true, |
| 74 |
}) |
| 75 |
} |
| 76 |
|
| 77 |
const EMPTY_OPTIONS = {} |
| 78 |
|
| 79 |
const retryWithBackoff: BaseQueryEnhancer< |
| 80 |
unknown, |
| 81 |
RetryOptions, |
| 82 |
RetryOptions | void |
| 83 |
> = (baseQuery, defaultOptions) => async (args, api, extraOptions) => { |
| 84 |
// We need to figure out `maxRetries` before we define `defaultRetryCondition. |
| 85 |
// This is probably goofy, but ought to work. |
| 86 |
// Put our defaults in one array, filter out undefineds, grab the last value. |
| 87 |
const possibleMaxRetries: number[] = [ |
| 88 |
5, |
| 89 |
((defaultOptions as any) || EMPTY_OPTIONS).maxRetries, |
| 90 |
((extraOptions as any) || EMPTY_OPTIONS).maxRetries, |
| 91 |
].filter((x) => x !== undefined) |
| 92 |
const [maxRetries] = possibleMaxRetries.slice(-1) |
| 93 |
|
| 94 |
const defaultRetryCondition: RetryConditionFunction = (_, __, { attempt }) => |
| 95 |
attempt <= maxRetries |
| 96 |
|
| 97 |
const options: { |
| 98 |
maxRetries: number |
| 99 |
backoff: typeof defaultBackoff |
| 100 |
retryCondition: typeof defaultRetryCondition |
| 101 |
} = { |
| 102 |
maxRetries, |
| 103 |
backoff: defaultBackoff, |
| 104 |
retryCondition: defaultRetryCondition, |
| 105 |
...defaultOptions, |
| 106 |
...extraOptions, |
| 107 |
} |
| 108 |
let retry = 0 |
| 109 |
|
| 110 |
while (true) { |
| 111 |
try { |
| 112 |
const result = await baseQuery(args, api, extraOptions) |
| 113 |
// baseQueries _should_ return an error property, so we should check for that and throw it to continue retrying |
| 114 |
if (result.error) { |
| 115 |
throw new HandledError(result) |
| 116 |
} |
| 117 |
return result |
| 118 |
} catch (e: any) { |
| 119 |
retry++ |
| 120 |
|
| 121 |
if (e.throwImmediately) { |
| 122 |
if (e instanceof HandledError) { |
| 123 |
return e.value |
| 124 |
} |
| 125 |
|
| 126 |
// We don't know what this is, so we have to rethrow it |
| 127 |
throw e |
| 128 |
} |
| 129 |
|
| 130 |
if ( |
| 131 |
e instanceof HandledError && |
| 132 |
!options.retryCondition(e.value.error as FetchBaseQueryError, args, { |
| 133 |
attempt: retry, |
| 134 |
baseQueryApi: api, |
| 135 |
extraOptions, |
| 136 |
}) |
| 137 |
) { |
| 138 |
return e.value |
| 139 |
} |
| 140 |
await options.backoff(retry, options.maxRetries) |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* A utility that can wrap `baseQuery` in the API definition to provide retries with a basic exponential backoff. |
| 147 |
* |
| 148 |
* @example |
| 149 |
* |
| 150 |
* ```ts |
| 151 |
* // codeblock-meta title="Retry every request 5 times by default" |
| 152 |
* import { createApi, fetchBaseQuery, retry } from '@reduxjs/toolkit/query/react' |
| 153 |
* interface Post { |
| 154 |
* id: number |
| 155 |
* name: string |
| 156 |
* } |
| 157 |
* type PostsResponse = Post[] |
| 158 |
* |
| 159 |
* // maxRetries: 5 is the default, and can be omitted. Shown for documentation purposes. |
| 160 |
* const staggeredBaseQuery = retry(fetchBaseQuery({ baseUrl: '/' }), { maxRetries: 5 }); |
| 161 |
* export const api = createApi({ |
| 162 |
* baseQuery: staggeredBaseQuery, |
| 163 |
* endpoints: (build) => ({ |
| 164 |
* getPosts: build.query<PostsResponse, void>({ |
| 165 |
* query: () => ({ url: 'posts' }), |
| 166 |
* }), |
| 167 |
* getPost: build.query<PostsResponse, string>({ |
| 168 |
* query: (id) => ({ url: `post/${id}` }), |
| 169 |
* extraOptions: { maxRetries: 8 }, // You can override the retry behavior on each endpoint |
| 170 |
* }), |
| 171 |
* }), |
| 172 |
* }); |
| 173 |
* |
| 174 |
* export const { useGetPostsQuery, useGetPostQuery } = api; |
| 175 |
* ``` |
| 176 |
*/ |
| 177 |
export const retry = /* @__PURE__ */ Object.assign(retryWithBackoff, { fail }) |