UNPKG

1.63 kBTypeScriptView Raw
1import type { BaseQueryEnhancer } from './baseQueryTypes';
2interface StaggerOptions {
3 /**
4 * How many times the query will be retried (default: 5)
5 */
6 maxRetries?: number;
7 /**
8 * Function used to determine delay between retries
9 */
10 backoff?: (attempt: number, maxRetries: number) => Promise<void>;
11}
12declare function fail(e: any): never;
13/**
14 * A utility that can wrap `baseQuery` in the API definition to provide retries with a basic exponential backoff.
15 *
16 * @example
17 *
18 * ```ts
19 * // codeblock-meta title="Retry every request 5 times by default"
20 * import { createApi, fetchBaseQuery, retry } from '@reduxjs/toolkit/query/react'
21 * interface Post {
22 * id: number
23 * name: string
24 * }
25 * type PostsResponse = Post[]
26 *
27 * // maxRetries: 5 is the default, and can be omitted. Shown for documentation purposes.
28 * const staggeredBaseQuery = retry(fetchBaseQuery({ baseUrl: '/' }), { maxRetries: 5 });
29 * export const api = createApi({
30 * baseQuery: staggeredBaseQuery,
31 * endpoints: (build) => ({
32 * getPosts: build.query<PostsResponse, void>({
33 * query: () => ({ url: 'posts' }),
34 * }),
35 * getPost: build.query<PostsResponse, string>({
36 * query: (id) => ({ url: `post/${id}` }),
37 * extraOptions: { maxRetries: 8 }, // You can override the retry behavior on each endpoint
38 * }),
39 * }),
40 * });
41 *
42 * export const { useGetPostsQuery, useGetPostQuery } = api;
43 * ```
44 */
45export declare const retry: BaseQueryEnhancer<unknown, StaggerOptions, void | StaggerOptions> & {
46 fail: typeof fail;
47};
48export {};