|
1 | 1 | "use client"; |
2 | 2 |
|
| 3 | +import { VERSION_2024_06_14 } from "@calcom/platform-constants"; |
3 | 4 | import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; |
| 5 | +import { isAxiosError } from "axios"; |
4 | 6 | import { useEffect } from "react"; |
5 | | - |
6 | | -import { VERSION_2024_06_14 } from "@calcom/platform-constants"; |
7 | | - |
8 | 7 | import http from "../lib/http"; |
9 | 8 | import type { CalProviderProps } from "./BaseCalProvider"; |
10 | 9 | import { BaseCalProvider } from "./BaseCalProvider"; |
11 | 10 | import type { translationKeys } from "./languages"; |
12 | 11 |
|
13 | | -const queryClient = new QueryClient(); |
| 12 | +function isRetryableError(error: unknown): boolean { |
| 13 | + if (!isAxiosError(error) || !error.response) { |
| 14 | + // network errors or timeouts — retryable |
| 15 | + return true; |
| 16 | + } |
| 17 | + const status = error.response.status; |
| 18 | + // retry on 408 (timeout), 429 (rate limit), and 5xx (server errors) |
| 19 | + return status === 408 || status === 429 || status >= 500; |
| 20 | +} |
| 21 | + |
| 22 | +const queryClient: QueryClient = new QueryClient({ |
| 23 | + defaultOptions: { |
| 24 | + queries: { |
| 25 | + retry: (failureCount: number, error: unknown): boolean => { |
| 26 | + if (!isRetryableError(error)) return false; |
| 27 | + return failureCount < 3; |
| 28 | + }, |
| 29 | + retryDelay: (attempt: number) => Math.min(1000 * 2 ** attempt, 30000), |
| 30 | + }, |
| 31 | + mutations: { |
| 32 | + retry: (failureCount: number, error: unknown): boolean => { |
| 33 | + if (!isRetryableError(error)) return false; |
| 34 | + return failureCount < 1; |
| 35 | + }, |
| 36 | + retryDelay: (attempt: number) => Math.min(1000 * 2 ** attempt, 30000), |
| 37 | + }, |
| 38 | + }, |
| 39 | +}); |
14 | 40 |
|
15 | 41 | /** |
16 | 42 | * Renders a CalProvider component. |
@@ -44,7 +70,7 @@ export function CalProvider({ |
44 | 70 | version = VERSION_2024_06_14, |
45 | 71 | organizationId, |
46 | 72 | isEmbed = false, |
47 | | -}: CalProviderProps) { |
| 73 | +}: CalProviderProps): JSX.Element { |
48 | 74 | useEffect(() => { |
49 | 75 | http.setVersionHeader(version); |
50 | 76 | }, [version]); |
@@ -74,7 +100,8 @@ export function CalProvider({ |
74 | 100 | version={version} |
75 | 101 | labels={labels as Record<translationKeys, string>} |
76 | 102 | language={language} |
77 | | - organizationId={organizationId}> |
| 103 | + organizationId={organizationId} |
| 104 | + > |
78 | 105 | {children} |
79 | 106 | </BaseCalProvider> |
80 | 107 | </QueryClientProvider> |
|
0 commit comments