Open History to see the last commit
1'use client'
2import * as React from 'react'
3import type {
4 DefaultedQueryObserverOptions,
5 Query,
6 QueryKey,
7 QueryObserverResult,
8 ThrowOnError,
9} from '@tanstack/query-core'
10import type { QueryErrorResetBoundaryValue } from './QueryErrorResetBoundary'
11β
12export const ensurePreventErrorBoundaryRetry = <
13 TQueryFnData,
14 TError,
15 TData,
16 TQueryData,
17 TQueryKey extends QueryKey,
18>(
19 options: DefaultedQueryObserverOptions<
20 TQueryFnData,
21 TError,
22 TData,
23 TQueryData,
24 TQueryKey
25 >,
26 errorResetBoundary: QueryErrorResetBoundaryValue,
27) => {
28 if (options.suspense || options.throwOnError) {
29 // Prevent retrying failed query if the error boundary has not been reset yet
30 if (!errorResetBoundary.isReset()) {
31 options.retryOnMount = false
32 }
33 }
34}
35β
36export const useClearResetErrorBoundary = (
37 errorResetBoundary: QueryErrorResetBoundaryValue,
38) => {
39 React.useEffect(() => {
40 errorResetBoundary.clearReset()
41 }, [errorResetBoundary])
42}
43β
44export function shouldThrowError<T extends (...args: Array<any>) => boolean>(
45 throwError: boolean | T | undefined,
46 params: Parameters<T>,
47): boolean {
48 // Allow throwError function to override throwing behavior on a per-error basis
49 if (typeof throwError === 'function') {
50 return throwError(...params)
51 }
52β
53 return !!throwError
54}
55β
56export const getHasError = <
57 TData,
58 TError,
59 TQueryFnData,
60 TQueryData,
61 TQueryKey extends QueryKey,
62>({
63 result,
64 errorResetBoundary,
65 throwOnError,
66 suspense,
67 query,
68}: {
69 result: QueryObserverResult<TData, TError>
70 errorResetBoundary: QueryErrorResetBoundaryValue
71 throwOnError: ThrowOnError<TQueryFnData, TError, TQueryData, TQueryKey>
72 suspense: boolean | undefined
73 query: Query<TQueryFnData, TError, TQueryData, TQueryKey> | undefined
74}) => {
75 if (suspense && result.data === undefined) {
76 return true
77 }
78 return (
79 result.isError &&
80 !errorResetBoundary.isReset() &&
81 !result.isFetching &&
82 query &&
83 shouldThrowError(throwOnError, [result.error, query])
84 )
85}