@tanstack/react-query 5.0.0-alpha.4 → 5.0.0-alpha.42
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/codemods/__testfixtures__/default-import.input.tsx +94 -0
- package/build/codemods/__testfixtures__/default-import.output.tsx +96 -0
- package/build/codemods/__testfixtures__/named-import.input.tsx +96 -0
- package/build/codemods/__testfixtures__/named-import.output.tsx +98 -0
- package/build/codemods/__testfixtures__/namespaced-import.input.tsx +86 -0
- package/build/codemods/__testfixtures__/namespaced-import.output.tsx +88 -0
- package/build/codemods/__testfixtures__/parameter-is-identifier.input.tsx +49 -0
- package/build/codemods/__testfixtures__/parameter-is-identifier.output.tsx +49 -0
- package/build/codemods/__testfixtures__/parameter-is-object-expression.input.tsx +128 -0
- package/build/codemods/__testfixtures__/parameter-is-object-expression.output.tsx +175 -0
- package/build/codemods/__testfixtures__/replace-import-specifier.input.tsx +10 -0
- package/build/codemods/__testfixtures__/replace-import-specifier.output.tsx +10 -0
- package/build/codemods/__testfixtures__/type-arguments.input.tsx +25 -0
- package/build/codemods/__testfixtures__/type-arguments.output.tsx +31 -0
- package/build/codemods/__tests__/key-transformation.test.js +32 -0
- package/build/codemods/__tests__/replace-import-specifier.test.js +12 -0
- package/build/codemods/remove-overloads/__testfixtures__/default-import.input.tsx +199 -0
- package/build/codemods/remove-overloads/__testfixtures__/default-import.output.tsx +484 -0
- package/build/codemods/remove-overloads/__tests__/remove-overloads.test.js +6 -0
- package/build/codemods/remove-overloads/remove-overloads.js +59 -0
- package/build/codemods/remove-overloads/transformers/filter-aware-usage-transformer.js +153 -0
- package/build/codemods/remove-overloads/transformers/query-fn-aware-usage-transformer.js +188 -0
- package/build/codemods/remove-overloads/utils/index.js +124 -0
- package/build/codemods/remove-overloads/utils/unknown-usage-error.js +26 -0
- package/build/codemods/src/utils/index.js +205 -0
- package/build/codemods/src/v4/key-transformation.js +138 -0
- package/build/codemods/src/v4/replace-import-specifier.js +25 -0
- package/build/codemods/transformers/query-cache-transformer.js +116 -0
- package/build/codemods/transformers/query-client-transformer.js +48 -0
- package/build/codemods/transformers/use-query-like-transformer.js +32 -0
- package/build/codemods/utils/replacers/key-replacer.js +164 -0
- package/build/lib/HydrationBoundary.d.ts +1 -0
- package/build/lib/HydrationBoundary.d.ts.map +1 -0
- package/build/lib/HydrationBoundary.esm.js +1 -0
- package/build/lib/HydrationBoundary.esm.js.map +1 -1
- package/build/lib/HydrationBoundary.js +1 -0
- package/build/lib/HydrationBoundary.js.map +1 -1
- package/build/lib/HydrationBoundary.mjs +1 -0
- package/build/lib/HydrationBoundary.mjs.map +1 -1
- package/build/lib/QueryClientProvider.d.ts +2 -1
- package/build/lib/QueryClientProvider.d.ts.map +1 -0
- package/build/lib/QueryClientProvider.esm.js +1 -0
- package/build/lib/QueryClientProvider.esm.js.map +1 -1
- package/build/lib/QueryClientProvider.js +1 -0
- package/build/lib/QueryClientProvider.js.map +1 -1
- package/build/lib/QueryClientProvider.mjs +1 -0
- package/build/lib/QueryClientProvider.mjs.map +1 -1
- package/build/lib/QueryErrorResetBoundary.d.ts +2 -1
- package/build/lib/QueryErrorResetBoundary.d.ts.map +1 -0
- package/build/lib/QueryErrorResetBoundary.esm.js +1 -0
- package/build/lib/QueryErrorResetBoundary.esm.js.map +1 -1
- package/build/lib/QueryErrorResetBoundary.js +1 -0
- package/build/lib/QueryErrorResetBoundary.js.map +1 -1
- package/build/lib/QueryErrorResetBoundary.mjs +1 -0
- package/build/lib/QueryErrorResetBoundary.mjs.map +1 -1
- package/build/lib/__tests__/HydrationBoundary.test.d.ts +1 -0
- package/build/lib/__tests__/HydrationBoundary.test.d.ts.map +1 -0
- package/build/lib/__tests__/QueryClientProvider.test.d.ts +1 -0
- package/build/lib/__tests__/QueryClientProvider.test.d.ts.map +1 -0
- package/build/lib/__tests__/QueryResetErrorBoundary.test.d.ts +1 -0
- package/build/lib/__tests__/QueryResetErrorBoundary.test.d.ts.map +1 -0
- package/build/lib/__tests__/ssr-hydration.test.d.ts +1 -0
- package/build/lib/__tests__/ssr-hydration.test.d.ts.map +1 -0
- package/build/lib/__tests__/ssr.test.d.ts +1 -3
- package/build/lib/__tests__/ssr.test.d.ts.map +1 -0
- package/build/lib/__tests__/suspense.test.d.ts +1 -0
- package/build/lib/__tests__/suspense.test.d.ts.map +1 -0
- package/build/lib/__tests__/useInfiniteQuery.test.d.ts +1 -0
- package/build/lib/__tests__/useInfiniteQuery.test.d.ts.map +1 -0
- package/build/lib/__tests__/useInfiniteQuery.type.test.d.ts +1 -0
- package/build/lib/__tests__/useInfiniteQuery.type.test.d.ts.map +1 -0
- package/build/lib/__tests__/useIsFetching.test.d.ts +1 -0
- package/build/lib/__tests__/useIsFetching.test.d.ts.map +1 -0
- package/build/lib/__tests__/useMutation.test.d.ts +1 -0
- package/build/lib/__tests__/useMutation.test.d.ts.map +1 -0
- package/build/lib/__tests__/useMutationState.test.d.ts +1 -0
- package/build/lib/__tests__/useMutationState.test.d.ts.map +1 -0
- package/build/lib/__tests__/useQueries.test.d.ts +1 -0
- package/build/lib/__tests__/useQueries.test.d.ts.map +1 -0
- package/build/lib/__tests__/useQuery.test.d.ts +1 -0
- package/build/lib/__tests__/useQuery.test.d.ts.map +1 -0
- package/build/lib/__tests__/useQuery.types.test.d.ts +1 -0
- package/build/lib/__tests__/useQuery.types.test.d.ts.map +1 -0
- package/build/lib/__tests__/utils.d.ts +6 -6
- package/build/lib/__tests__/utils.d.ts.map +1 -0
- package/build/lib/errorBoundaryUtils.d.ts +4 -3
- package/build/lib/errorBoundaryUtils.d.ts.map +1 -0
- package/build/lib/errorBoundaryUtils.esm.js +4 -3
- package/build/lib/errorBoundaryUtils.esm.js.map +1 -1
- package/build/lib/errorBoundaryUtils.js +4 -3
- package/build/lib/errorBoundaryUtils.js.map +1 -1
- package/build/lib/errorBoundaryUtils.mjs +4 -3
- package/build/lib/errorBoundaryUtils.mjs.map +1 -1
- package/build/lib/index.d.ts +2 -1
- package/build/lib/index.d.ts.map +1 -0
- package/build/lib/index.esm.js +1 -1
- package/build/lib/index.js +1 -0
- package/build/lib/index.js.map +1 -1
- package/build/lib/index.mjs +1 -1
- package/build/lib/isRestoring.d.ts +1 -0
- package/build/lib/isRestoring.d.ts.map +1 -0
- package/build/lib/isRestoring.esm.js +1 -0
- package/build/lib/isRestoring.esm.js.map +1 -1
- package/build/lib/isRestoring.js +1 -0
- package/build/lib/isRestoring.js.map +1 -1
- package/build/lib/isRestoring.mjs +1 -0
- package/build/lib/isRestoring.mjs.map +1 -1
- package/build/lib/suspense.d.ts +3 -5
- package/build/lib/suspense.d.ts.map +1 -0
- package/build/lib/suspense.esm.js +2 -9
- package/build/lib/suspense.esm.js.map +1 -1
- package/build/lib/suspense.js +2 -9
- package/build/lib/suspense.js.map +1 -1
- package/build/lib/suspense.mjs +1 -8
- package/build/lib/suspense.mjs.map +1 -1
- package/build/lib/types.d.ts +11 -10
- package/build/lib/types.d.ts.map +1 -0
- package/build/lib/useBaseQuery.d.ts +1 -0
- package/build/lib/useBaseQuery.d.ts.map +1 -0
- package/build/lib/useBaseQuery.esm.js +2 -12
- package/build/lib/useBaseQuery.esm.js.map +1 -1
- package/build/lib/useBaseQuery.js +2 -12
- package/build/lib/useBaseQuery.js.map +1 -1
- package/build/lib/useBaseQuery.mjs +2 -12
- package/build/lib/useBaseQuery.mjs.map +1 -1
- package/build/lib/useInfiniteQuery.d.ts +1 -0
- package/build/lib/useInfiniteQuery.d.ts.map +1 -0
- package/build/lib/useInfiniteQuery.esm.js +1 -0
- package/build/lib/useInfiniteQuery.esm.js.map +1 -1
- package/build/lib/useInfiniteQuery.js +1 -0
- package/build/lib/useInfiniteQuery.js.map +1 -1
- package/build/lib/useInfiniteQuery.mjs +1 -0
- package/build/lib/useInfiniteQuery.mjs.map +1 -1
- package/build/lib/useIsFetching.d.ts +1 -0
- package/build/lib/useIsFetching.d.ts.map +1 -0
- package/build/lib/useIsFetching.esm.js +1 -0
- package/build/lib/useIsFetching.esm.js.map +1 -1
- package/build/lib/useIsFetching.js +1 -0
- package/build/lib/useIsFetching.js.map +1 -1
- package/build/lib/useIsFetching.mjs +1 -0
- package/build/lib/useIsFetching.mjs.map +1 -1
- package/build/lib/useMutation.d.ts +1 -0
- package/build/lib/useMutation.d.ts.map +1 -0
- package/build/lib/useMutation.esm.js +2 -1
- package/build/lib/useMutation.esm.js.map +1 -1
- package/build/lib/useMutation.js +2 -1
- package/build/lib/useMutation.js.map +1 -1
- package/build/lib/useMutation.mjs +2 -1
- package/build/lib/useMutation.mjs.map +1 -1
- package/build/lib/useMutationState.d.ts +4 -3
- package/build/lib/useMutationState.d.ts.map +1 -0
- package/build/lib/useMutationState.esm.js +1 -0
- package/build/lib/useMutationState.esm.js.map +1 -1
- package/build/lib/useMutationState.js +1 -0
- package/build/lib/useMutationState.js.map +1 -1
- package/build/lib/useMutationState.mjs +1 -0
- package/build/lib/useMutationState.mjs.map +1 -1
- package/build/lib/useQueries.d.ts +10 -8
- package/build/lib/useQueries.d.ts.map +1 -0
- package/build/lib/useQueries.esm.js +39 -27
- package/build/lib/useQueries.esm.js.map +1 -1
- package/build/lib/useQueries.js +39 -27
- package/build/lib/useQueries.js.map +1 -1
- package/build/lib/useQueries.mjs +31 -22
- package/build/lib/useQueries.mjs.map +1 -1
- package/build/lib/useQuery.d.ts +5 -2
- package/build/lib/useQuery.d.ts.map +1 -0
- package/build/lib/useQuery.esm.js +6 -1
- package/build/lib/useQuery.esm.js.map +1 -1
- package/build/lib/useQuery.js +6 -0
- package/build/lib/useQuery.js.map +1 -1
- package/build/lib/useQuery.mjs +6 -1
- package/build/lib/useQuery.mjs.map +1 -1
- package/build/lib/utils.d.ts +1 -0
- package/build/lib/utils.d.ts.map +1 -0
- package/build/lib/utils.esm.js.map +1 -1
- package/build/lib/utils.js.map +1 -1
- package/build/lib/utils.mjs.map +1 -1
- package/build/umd/index.development.js +191 -187
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +13 -6
- package/src/__tests__/HydrationBoundary.test.tsx +4 -3
- package/src/__tests__/QueryClientProvider.test.tsx +2 -1
- package/src/__tests__/QueryResetErrorBoundary.test.tsx +753 -620
- package/src/__tests__/ssr-hydration.test.tsx +11 -10
- package/src/__tests__/ssr.test.tsx +4 -7
- package/src/__tests__/suspense.test.tsx +17 -98
- package/src/__tests__/useInfiniteQuery.test.tsx +18 -16
- package/src/__tests__/useInfiniteQuery.type.test.tsx +94 -13
- package/src/__tests__/useMutation.test.tsx +25 -24
- package/src/__tests__/useMutationState.test.tsx +24 -58
- package/src/__tests__/useQueries.test.tsx +217 -154
- package/src/__tests__/useQuery.test.tsx +234 -365
- package/src/__tests__/useQuery.types.test.tsx +21 -1
- package/src/__tests__/utils.tsx +3 -2
- package/src/errorBoundaryUtils.ts +6 -5
- package/src/index.ts +1 -1
- package/src/suspense.ts +9 -15
- package/src/useBaseQuery.ts +2 -20
- package/src/useInfiniteQuery.ts +1 -0
- package/src/useIsFetching.ts +1 -0
- package/src/useMutation.ts +2 -1
- package/src/useMutationState.ts +4 -3
- package/src/useQueries.ts +44 -26
- package/src/useQuery.ts +23 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useQuery } from '../useQuery'
|
|
1
|
+
import { queryOptions, useQuery } from '../useQuery'
|
|
2
2
|
import type { Expect, Equal } from './utils'
|
|
3
3
|
import { doNotExecute } from './utils'
|
|
4
4
|
|
|
@@ -23,6 +23,26 @@ describe('initialData', () => {
|
|
|
23
23
|
})
|
|
24
24
|
})
|
|
25
25
|
|
|
26
|
+
it('TData should be defined when passed through queryOptions', () => {
|
|
27
|
+
doNotExecute(() => {
|
|
28
|
+
const options = queryOptions({
|
|
29
|
+
queryKey: ['key'],
|
|
30
|
+
queryFn: () => {
|
|
31
|
+
return {
|
|
32
|
+
wow: true,
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
initialData: {
|
|
36
|
+
wow: true,
|
|
37
|
+
},
|
|
38
|
+
})
|
|
39
|
+
const { data } = useQuery(options)
|
|
40
|
+
|
|
41
|
+
const result: Expect<Equal<{ wow: boolean }, typeof data>> = true
|
|
42
|
+
return result
|
|
43
|
+
})
|
|
44
|
+
})
|
|
45
|
+
|
|
26
46
|
it('TData should always be defined when initialData is provided as a function which ALWAYS returns the data', () => {
|
|
27
47
|
doNotExecute(() => {
|
|
28
48
|
const { data } = useQuery({
|
package/src/__tests__/utils.tsx
CHANGED
|
@@ -3,6 +3,7 @@ import { act, render } from '@testing-library/react'
|
|
|
3
3
|
import type { QueryClientConfig } from '..'
|
|
4
4
|
import { QueryClient, QueryClientProvider } from '..'
|
|
5
5
|
import * as utils from '@tanstack/query-core'
|
|
6
|
+
import { vi } from 'vitest'
|
|
6
7
|
|
|
7
8
|
export function renderWithClient(
|
|
8
9
|
client: QueryClient,
|
|
@@ -45,11 +46,11 @@ export function createQueryClient(config?: QueryClientConfig): QueryClient {
|
|
|
45
46
|
}
|
|
46
47
|
|
|
47
48
|
export function mockVisibilityState(value: DocumentVisibilityState) {
|
|
48
|
-
return
|
|
49
|
+
return vi.spyOn(document, 'visibilityState', 'get').mockReturnValue(value)
|
|
49
50
|
}
|
|
50
51
|
|
|
51
52
|
export function mockNavigatorOnLine(value: boolean) {
|
|
52
|
-
return
|
|
53
|
+
return vi.spyOn(navigator, 'onLine', 'get').mockReturnValue(value)
|
|
53
54
|
}
|
|
54
55
|
|
|
55
56
|
let queryKeyCount = 0
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
'use client'
|
|
1
2
|
import type {
|
|
2
3
|
DefaultedQueryObserverOptions,
|
|
3
4
|
Query,
|
|
4
5
|
QueryKey,
|
|
5
6
|
QueryObserverResult,
|
|
6
|
-
|
|
7
|
+
ThrowOnError,
|
|
7
8
|
} from '@tanstack/query-core'
|
|
8
9
|
import type { QueryErrorResetBoundaryValue } from './QueryErrorResetBoundary'
|
|
9
10
|
import * as React from 'react'
|
|
@@ -25,7 +26,7 @@ export const ensurePreventErrorBoundaryRetry = <
|
|
|
25
26
|
>,
|
|
26
27
|
errorResetBoundary: QueryErrorResetBoundaryValue,
|
|
27
28
|
) => {
|
|
28
|
-
if (options.suspense || options.
|
|
29
|
+
if (options.suspense || options.throwOnError) {
|
|
29
30
|
// Prevent retrying failed query if the error boundary has not been reset yet
|
|
30
31
|
if (!errorResetBoundary.isReset()) {
|
|
31
32
|
options.retryOnMount = false
|
|
@@ -50,18 +51,18 @@ export const getHasError = <
|
|
|
50
51
|
>({
|
|
51
52
|
result,
|
|
52
53
|
errorResetBoundary,
|
|
53
|
-
|
|
54
|
+
throwOnError,
|
|
54
55
|
query,
|
|
55
56
|
}: {
|
|
56
57
|
result: QueryObserverResult<TData, TError>
|
|
57
58
|
errorResetBoundary: QueryErrorResetBoundaryValue
|
|
58
|
-
|
|
59
|
+
throwOnError: ThrowOnError<TQueryFnData, TError, TQueryData, TQueryKey>
|
|
59
60
|
query: Query<TQueryFnData, TError, TQueryData, TQueryKey>
|
|
60
61
|
}) => {
|
|
61
62
|
return (
|
|
62
63
|
result.isError &&
|
|
63
64
|
!errorResetBoundary.isReset() &&
|
|
64
65
|
!result.isFetching &&
|
|
65
|
-
shouldThrowError(
|
|
66
|
+
shouldThrowError(throwOnError, [result.error, query])
|
|
66
67
|
)
|
|
67
68
|
}
|
package/src/index.ts
CHANGED
|
@@ -7,7 +7,7 @@ export * from '@tanstack/query-core'
|
|
|
7
7
|
export * from './types'
|
|
8
8
|
export { useQueries } from './useQueries'
|
|
9
9
|
export type { QueriesResults, QueriesOptions } from './useQueries'
|
|
10
|
-
export { useQuery } from './useQuery'
|
|
10
|
+
export { useQuery, queryOptions } from './useQuery'
|
|
11
11
|
export {
|
|
12
12
|
QueryClientContext,
|
|
13
13
|
QueryClientProvider,
|
package/src/suspense.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
1
|
+
import type {
|
|
2
|
+
DefaultedQueryObserverOptions,
|
|
3
|
+
QueryObserverResult,
|
|
4
|
+
QueryKey,
|
|
5
|
+
QueryObserver,
|
|
6
|
+
} from '@tanstack/query-core'
|
|
3
7
|
import type { QueryErrorResetBoundaryValue } from './QueryErrorResetBoundary'
|
|
4
|
-
import type { QueryObserverResult } from '@tanstack/query-core'
|
|
5
|
-
import type { QueryKey } from '@tanstack/query-core'
|
|
6
8
|
|
|
7
9
|
export const ensureStaleTime = (
|
|
8
10
|
defaultedOptions: DefaultedQueryObserverOptions<any, any, any, any, any>,
|
|
@@ -46,14 +48,6 @@ export const fetchOptimistic = <
|
|
|
46
48
|
observer: QueryObserver<TQueryFnData, TError, TData, TQueryData, TQueryKey>,
|
|
47
49
|
errorResetBoundary: QueryErrorResetBoundaryValue,
|
|
48
50
|
) =>
|
|
49
|
-
observer
|
|
50
|
-
.
|
|
51
|
-
|
|
52
|
-
defaultedOptions.onSuccess?.(data as TData)
|
|
53
|
-
defaultedOptions.onSettled?.(data, null)
|
|
54
|
-
})
|
|
55
|
-
.catch((error) => {
|
|
56
|
-
errorResetBoundary.clearReset()
|
|
57
|
-
defaultedOptions.onError?.(error)
|
|
58
|
-
defaultedOptions.onSettled?.(undefined, error)
|
|
59
|
-
})
|
|
51
|
+
observer.fetchOptimistic(defaultedOptions).catch(() => {
|
|
52
|
+
errorResetBoundary.clearReset()
|
|
53
|
+
})
|
package/src/useBaseQuery.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
'use client'
|
|
1
2
|
import * as React from 'react'
|
|
2
3
|
|
|
3
4
|
import type { QueryClient, QueryKey, QueryObserver } from '@tanstack/query-core'
|
|
@@ -40,25 +41,6 @@ export function useBaseQuery<
|
|
|
40
41
|
? 'isRestoring'
|
|
41
42
|
: 'optimistic'
|
|
42
43
|
|
|
43
|
-
// Include callbacks in batch renders
|
|
44
|
-
if (defaultedOptions.onError) {
|
|
45
|
-
defaultedOptions.onError = notifyManager.batchCalls(
|
|
46
|
-
defaultedOptions.onError,
|
|
47
|
-
)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
if (defaultedOptions.onSuccess) {
|
|
51
|
-
defaultedOptions.onSuccess = notifyManager.batchCalls(
|
|
52
|
-
defaultedOptions.onSuccess,
|
|
53
|
-
)
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
if (defaultedOptions.onSettled) {
|
|
57
|
-
defaultedOptions.onSettled = notifyManager.batchCalls(
|
|
58
|
-
defaultedOptions.onSettled,
|
|
59
|
-
)
|
|
60
|
-
}
|
|
61
|
-
|
|
62
44
|
ensureStaleTime(defaultedOptions)
|
|
63
45
|
ensurePreventErrorBoundaryRetry(defaultedOptions, errorResetBoundary)
|
|
64
46
|
|
|
@@ -102,7 +84,7 @@ export function useBaseQuery<
|
|
|
102
84
|
getHasError({
|
|
103
85
|
result,
|
|
104
86
|
errorResetBoundary,
|
|
105
|
-
|
|
87
|
+
throwOnError: defaultedOptions.throwOnError,
|
|
106
88
|
query: observer.getCurrentQuery(),
|
|
107
89
|
})
|
|
108
90
|
) {
|
package/src/useInfiniteQuery.ts
CHANGED
package/src/useIsFetching.ts
CHANGED
package/src/useMutation.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
'use client'
|
|
1
2
|
import * as React from 'react'
|
|
2
3
|
import type { QueryClient, DefaultError } from '@tanstack/query-core'
|
|
3
4
|
import { notifyManager, MutationObserver } from '@tanstack/query-core'
|
|
@@ -55,7 +56,7 @@ export function useMutation<
|
|
|
55
56
|
|
|
56
57
|
if (
|
|
57
58
|
result.error &&
|
|
58
|
-
shouldThrowError(observer.options.
|
|
59
|
+
shouldThrowError(observer.options.throwOnError, [result.error])
|
|
59
60
|
) {
|
|
60
61
|
throw result.error
|
|
61
62
|
}
|
package/src/useMutationState.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
'use client'
|
|
1
2
|
import * as React from 'react'
|
|
2
3
|
|
|
3
4
|
import type {
|
|
@@ -6,10 +7,10 @@ import type {
|
|
|
6
7
|
Mutation,
|
|
7
8
|
MutationCache,
|
|
8
9
|
DefaultError,
|
|
10
|
+
MutationState,
|
|
9
11
|
} from '@tanstack/query-core'
|
|
10
12
|
import { notifyManager, replaceEqualDeep } from '@tanstack/query-core'
|
|
11
13
|
import { useQueryClient } from './QueryClientProvider'
|
|
12
|
-
import type { MutationState } from '@tanstack/query-core/build/lib/mutation'
|
|
13
14
|
|
|
14
15
|
export function useIsMutating(
|
|
15
16
|
filters?: MutationFilters,
|
|
@@ -22,7 +23,7 @@ export function useIsMutating(
|
|
|
22
23
|
).length
|
|
23
24
|
}
|
|
24
25
|
|
|
25
|
-
type MutationStateOptions<TResult> = {
|
|
26
|
+
type MutationStateOptions<TResult = MutationState> = {
|
|
26
27
|
filters?: MutationFilters
|
|
27
28
|
select?: (
|
|
28
29
|
mutation: Mutation<unknown, DefaultError, unknown, unknown>,
|
|
@@ -45,7 +46,7 @@ function getResult<TResult = MutationState>(
|
|
|
45
46
|
)
|
|
46
47
|
}
|
|
47
48
|
|
|
48
|
-
export function useMutationState<TResult =
|
|
49
|
+
export function useMutationState<TResult = MutationState>(
|
|
49
50
|
options: MutationStateOptions<TResult> = {},
|
|
50
51
|
queryClient?: QueryClient,
|
|
51
52
|
): Array<TResult> {
|
package/src/useQueries.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
'use client'
|
|
1
2
|
import * as React from 'react'
|
|
2
3
|
|
|
3
4
|
import type {
|
|
@@ -6,6 +7,7 @@ import type {
|
|
|
6
7
|
QueriesPlaceholderDataFunction,
|
|
7
8
|
QueryClient,
|
|
8
9
|
DefaultError,
|
|
10
|
+
QueriesObserverOptions,
|
|
9
11
|
} from '@tanstack/query-core'
|
|
10
12
|
import { notifyManager, QueriesObserver } from '@tanstack/query-core'
|
|
11
13
|
import { useQueryClient } from './QueryClientProvider'
|
|
@@ -155,21 +157,27 @@ export type QueriesResults<
|
|
|
155
157
|
: // Fallback
|
|
156
158
|
UseQueryResult[]
|
|
157
159
|
|
|
158
|
-
export function useQueries<
|
|
160
|
+
export function useQueries<
|
|
161
|
+
T extends any[],
|
|
162
|
+
TCombinedResult = QueriesResults<T>,
|
|
163
|
+
>(
|
|
159
164
|
{
|
|
160
165
|
queries,
|
|
166
|
+
...options
|
|
161
167
|
}: {
|
|
162
168
|
queries: readonly [...QueriesOptions<T>]
|
|
169
|
+
combine?: (result: QueriesResults<T>) => TCombinedResult
|
|
163
170
|
},
|
|
164
171
|
queryClient?: QueryClient,
|
|
165
|
-
):
|
|
172
|
+
): TCombinedResult {
|
|
166
173
|
const client = useQueryClient(queryClient)
|
|
167
174
|
const isRestoring = useIsRestoring()
|
|
175
|
+
const errorResetBoundary = useQueryErrorResetBoundary()
|
|
168
176
|
|
|
169
177
|
const defaultedQueries = React.useMemo(
|
|
170
178
|
() =>
|
|
171
|
-
queries.map((
|
|
172
|
-
const defaultedOptions = client.defaultQueryOptions(
|
|
179
|
+
queries.map((opts) => {
|
|
180
|
+
const defaultedOptions = client.defaultQueryOptions(opts)
|
|
173
181
|
|
|
174
182
|
// Make sure the results are already in fetching state before subscribing or updating options
|
|
175
183
|
defaultedOptions._optimisticResults = isRestoring
|
|
@@ -181,11 +189,24 @@ export function useQueries<T extends any[]>(
|
|
|
181
189
|
[queries, client, isRestoring],
|
|
182
190
|
)
|
|
183
191
|
|
|
192
|
+
defaultedQueries.forEach((query) => {
|
|
193
|
+
ensureStaleTime(query)
|
|
194
|
+
ensurePreventErrorBoundaryRetry(query, errorResetBoundary)
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
useClearResetErrorBoundary(errorResetBoundary)
|
|
198
|
+
|
|
184
199
|
const [observer] = React.useState(
|
|
185
|
-
() =>
|
|
200
|
+
() =>
|
|
201
|
+
new QueriesObserver<TCombinedResult>(
|
|
202
|
+
client,
|
|
203
|
+
defaultedQueries,
|
|
204
|
+
options as QueriesObserverOptions<TCombinedResult>,
|
|
205
|
+
),
|
|
186
206
|
)
|
|
187
207
|
|
|
188
|
-
const optimisticResult =
|
|
208
|
+
const [optimisticResult, getCombinedResult, trackResult] =
|
|
209
|
+
observer.getOptimisticResult(defaultedQueries)
|
|
189
210
|
|
|
190
211
|
React.useSyncExternalStore(
|
|
191
212
|
React.useCallback(
|
|
@@ -202,17 +223,14 @@ export function useQueries<T extends any[]>(
|
|
|
202
223
|
React.useEffect(() => {
|
|
203
224
|
// Do not notify on updates because of changes in the options because
|
|
204
225
|
// these changes should already be reflected in the optimistic result.
|
|
205
|
-
observer.setQueries(
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
})
|
|
214
|
-
|
|
215
|
-
useClearResetErrorBoundary(errorResetBoundary)
|
|
226
|
+
observer.setQueries(
|
|
227
|
+
defaultedQueries,
|
|
228
|
+
options as QueriesObserverOptions<TCombinedResult>,
|
|
229
|
+
{
|
|
230
|
+
listeners: false,
|
|
231
|
+
},
|
|
232
|
+
)
|
|
233
|
+
}, [defaultedQueries, options, observer])
|
|
216
234
|
|
|
217
235
|
const shouldAtLeastOneSuspend = optimisticResult.some((result, index) =>
|
|
218
236
|
shouldSuspend(defaultedQueries[index], result, isRestoring),
|
|
@@ -220,14 +238,14 @@ export function useQueries<T extends any[]>(
|
|
|
220
238
|
|
|
221
239
|
const suspensePromises = shouldAtLeastOneSuspend
|
|
222
240
|
? optimisticResult.flatMap((result, index) => {
|
|
223
|
-
const
|
|
241
|
+
const opts = defaultedQueries[index]
|
|
224
242
|
const queryObserver = observer.getObservers()[index]
|
|
225
243
|
|
|
226
|
-
if (
|
|
227
|
-
if (shouldSuspend(
|
|
228
|
-
return fetchOptimistic(
|
|
244
|
+
if (opts && queryObserver) {
|
|
245
|
+
if (shouldSuspend(opts, result, isRestoring)) {
|
|
246
|
+
return fetchOptimistic(opts, queryObserver, errorResetBoundary)
|
|
229
247
|
} else if (willFetch(result, isRestoring)) {
|
|
230
|
-
void fetchOptimistic(
|
|
248
|
+
void fetchOptimistic(opts, queryObserver, errorResetBoundary)
|
|
231
249
|
}
|
|
232
250
|
}
|
|
233
251
|
return []
|
|
@@ -237,14 +255,14 @@ export function useQueries<T extends any[]>(
|
|
|
237
255
|
if (suspensePromises.length > 0) {
|
|
238
256
|
throw Promise.all(suspensePromises)
|
|
239
257
|
}
|
|
240
|
-
|
|
258
|
+
const observerQueries = observer.getQueries()
|
|
241
259
|
const firstSingleResultWhichShouldThrow = optimisticResult.find(
|
|
242
260
|
(result, index) =>
|
|
243
261
|
getHasError({
|
|
244
262
|
result,
|
|
245
263
|
errorResetBoundary,
|
|
246
|
-
|
|
247
|
-
query:
|
|
264
|
+
throwOnError: defaultedQueries[index]?.throwOnError ?? false,
|
|
265
|
+
query: observerQueries[index]!,
|
|
248
266
|
}),
|
|
249
267
|
)
|
|
250
268
|
|
|
@@ -252,5 +270,5 @@ export function useQueries<T extends any[]>(
|
|
|
252
270
|
throw firstSingleResultWhichShouldThrow.error
|
|
253
271
|
}
|
|
254
272
|
|
|
255
|
-
return
|
|
273
|
+
return getCombinedResult(trackResult())
|
|
256
274
|
}
|
package/src/useQuery.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
'use client'
|
|
1
2
|
import type { QueryClient, QueryKey, DefaultError } from '@tanstack/query-core'
|
|
2
3
|
import { QueryObserver } from '@tanstack/query-core'
|
|
3
4
|
import type {
|
|
@@ -7,6 +8,28 @@ import type {
|
|
|
7
8
|
} from './types'
|
|
8
9
|
import { useBaseQuery } from './useBaseQuery'
|
|
9
10
|
|
|
11
|
+
export function queryOptions<
|
|
12
|
+
TQueryFnData = unknown,
|
|
13
|
+
TError = DefaultError,
|
|
14
|
+
TData = TQueryFnData,
|
|
15
|
+
TQueryKey extends QueryKey = QueryKey,
|
|
16
|
+
>(
|
|
17
|
+
options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
|
|
18
|
+
): UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
|
|
19
|
+
|
|
20
|
+
export function queryOptions<
|
|
21
|
+
TQueryFnData = unknown,
|
|
22
|
+
TError = DefaultError,
|
|
23
|
+
TData = TQueryFnData,
|
|
24
|
+
TQueryKey extends QueryKey = QueryKey,
|
|
25
|
+
>(
|
|
26
|
+
options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
|
|
27
|
+
): DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
|
|
28
|
+
|
|
29
|
+
export function queryOptions(options: unknown) {
|
|
30
|
+
return options
|
|
31
|
+
}
|
|
32
|
+
|
|
10
33
|
// HOOK
|
|
11
34
|
type UndefinedInitialDataOptions<
|
|
12
35
|
TQueryFnData = unknown,
|