-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathquery-client.ts
More file actions
75 lines (60 loc) · 1.99 KB
/
query-client.ts
File metadata and controls
75 lines (60 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// src/infra/query/client/query-client.ts
import { MutationCache, QueryCache, QueryClient } from '@tanstack/react-query'
import { Freshness } from '@/shared/services/api/query/policy/freshness'
import { normalizeError } from '@/shared/utils/normalize-error'
import { showErrorToast } from '@/shared/utils/toast'
export function createQueryClient() {
const queryCache = new QueryCache({
onError: error => {
const e = normalizeError(error)
// avoid spamming toasts when offline
if (e.code === 'NETWORK_OFFLINE') return
if (__DEV__) console.log('[RQ][QUERY][ERROR]', e)
showErrorToast(e)
},
})
const mutationCache = new MutationCache({
onError: error => {
const e = normalizeError(error)
// avoid spamming toasts when offline
if (e.code === 'NETWORK_OFFLINE') return
if (__DEV__) console.log('[RQ][MUTATION][ERROR]', e)
showErrorToast(e)
},
})
return new QueryClient({
queryCache,
mutationCache,
defaultOptions: {
queries: {
// nearRealtime profile by default
staleTime: Freshness.nearRealtime.staleTime,
gcTime: Freshness.nearRealtime.gcTime,
refetchOnReconnect: true,
throwOnError: false,
retry: (failureCount, error: unknown) => {
const e = normalizeError(error)
// never retry when offline
if (e.code === 'NETWORK_OFFLINE') return false
// retry only on 5xx/429
if (e.status && (e.status >= 500 || e.status === 429)) {
return failureCount < 2
}
return false
},
},
mutations: {
throwOnError: false,
retry: (failureCount, error: unknown) => {
const e = normalizeError(error)
// never retry when offline
if (e.code === 'NETWORK_OFFLINE') return false
if (e.status && (e.status >= 500 || e.status === 429)) {
return failureCount < 2
}
return false
},
},
},
})
}