feat: 项目初始化
This commit is contained in:
43
src/lib/query/query-client.ts
Normal file
43
src/lib/query/query-client.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { QueryClient } from '@tanstack/react-query'
|
||||
import {
|
||||
QUERY_DEFAULT_GC_TIME_MS,
|
||||
QUERY_DEFAULT_STALE_TIME_MS,
|
||||
QUERY_RETRY_LIMIT,
|
||||
QUERY_RETRYABLE_STATUS_CODES,
|
||||
} from '@/constants'
|
||||
|
||||
import { ApiError } from '../api/api-error'
|
||||
|
||||
const retryableStatusCodes = new Set<number>(QUERY_RETRYABLE_STATUS_CODES)
|
||||
|
||||
function shouldRetryRequest(error: unknown) {
|
||||
if (!(error instanceof ApiError)) {
|
||||
return true
|
||||
}
|
||||
|
||||
if (error.status === null) {
|
||||
return true
|
||||
}
|
||||
|
||||
return retryableStatusCodes.has(error.status) || error.status >= 500
|
||||
}
|
||||
|
||||
export function createQueryClient() {
|
||||
return new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: {
|
||||
staleTime: QUERY_DEFAULT_STALE_TIME_MS,
|
||||
gcTime: QUERY_DEFAULT_GC_TIME_MS,
|
||||
refetchOnWindowFocus: false,
|
||||
retry(failureCount, error) {
|
||||
return failureCount < QUERY_RETRY_LIMIT && shouldRetryRequest(error)
|
||||
},
|
||||
},
|
||||
mutations: {
|
||||
retry: false,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export const queryClient = createQueryClient()
|
||||
Reference in New Issue
Block a user