@tanstack/query-core 5.0.0-alpha.81 → 5.0.0-alpha.85

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.
Files changed (109) hide show
  1. package/build/lib/hydration.cjs +12 -4
  2. package/build/lib/hydration.cjs.map +1 -1
  3. package/build/lib/hydration.d.ts +3 -1
  4. package/build/lib/hydration.d.ts.map +1 -1
  5. package/build/lib/hydration.js +12 -4
  6. package/build/lib/hydration.js.map +1 -1
  7. package/build/lib/hydration.legacy.cjs +12 -4
  8. package/build/lib/hydration.legacy.cjs.map +1 -1
  9. package/build/lib/hydration.legacy.js +12 -4
  10. package/build/lib/hydration.legacy.js.map +1 -1
  11. package/build/lib/infiniteQueryBehavior.cjs.map +1 -1
  12. package/build/lib/infiniteQueryBehavior.d.ts.map +1 -1
  13. package/build/lib/infiniteQueryBehavior.js.map +1 -1
  14. package/build/lib/infiniteQueryBehavior.legacy.cjs.map +1 -1
  15. package/build/lib/infiniteQueryBehavior.legacy.js.map +1 -1
  16. package/build/lib/infiniteQueryObserver.cjs.map +1 -1
  17. package/build/lib/infiniteQueryObserver.d.ts +2 -2
  18. package/build/lib/infiniteQueryObserver.d.ts.map +1 -1
  19. package/build/lib/infiniteQueryObserver.js.map +1 -1
  20. package/build/lib/infiniteQueryObserver.legacy.cjs.map +1 -1
  21. package/build/lib/infiniteQueryObserver.legacy.js.map +1 -1
  22. package/build/lib/mutation.cjs.map +1 -1
  23. package/build/lib/mutation.d.ts +2 -2
  24. package/build/lib/mutation.d.ts.map +1 -1
  25. package/build/lib/mutation.js.map +1 -1
  26. package/build/lib/mutation.legacy.cjs.map +1 -1
  27. package/build/lib/mutation.legacy.js.map +1 -1
  28. package/build/lib/mutationCache.cjs.map +1 -1
  29. package/build/lib/mutationCache.d.ts +3 -3
  30. package/build/lib/mutationCache.d.ts.map +1 -1
  31. package/build/lib/mutationCache.js.map +1 -1
  32. package/build/lib/mutationCache.legacy.cjs.map +1 -1
  33. package/build/lib/mutationCache.legacy.js.map +1 -1
  34. package/build/lib/mutationObserver.cjs.map +1 -1
  35. package/build/lib/mutationObserver.d.ts +3 -3
  36. package/build/lib/mutationObserver.d.ts.map +1 -1
  37. package/build/lib/mutationObserver.js.map +1 -1
  38. package/build/lib/mutationObserver.legacy.cjs.map +1 -1
  39. package/build/lib/mutationObserver.legacy.js.map +1 -1
  40. package/build/lib/queriesObserver.cjs.map +1 -1
  41. package/build/lib/queriesObserver.d.ts +2 -2
  42. package/build/lib/queriesObserver.d.ts.map +1 -1
  43. package/build/lib/queriesObserver.js.map +1 -1
  44. package/build/lib/queriesObserver.legacy.cjs.map +1 -1
  45. package/build/lib/queriesObserver.legacy.js.map +1 -1
  46. package/build/lib/query.cjs.map +1 -1
  47. package/build/lib/query.d.ts +2 -2
  48. package/build/lib/query.d.ts.map +1 -1
  49. package/build/lib/query.js.map +1 -1
  50. package/build/lib/query.legacy.cjs.map +1 -1
  51. package/build/lib/query.legacy.js.map +1 -1
  52. package/build/lib/queryCache.cjs.map +1 -1
  53. package/build/lib/queryCache.d.ts +3 -3
  54. package/build/lib/queryCache.d.ts.map +1 -1
  55. package/build/lib/queryCache.js.map +1 -1
  56. package/build/lib/queryCache.legacy.cjs.map +1 -1
  57. package/build/lib/queryCache.legacy.js.map +1 -1
  58. package/build/lib/queryClient.cjs.map +1 -1
  59. package/build/lib/queryClient.d.ts +3 -3
  60. package/build/lib/queryClient.d.ts.map +1 -1
  61. package/build/lib/queryClient.js.map +1 -1
  62. package/build/lib/queryClient.legacy.cjs.map +1 -1
  63. package/build/lib/queryClient.legacy.js.map +1 -1
  64. package/build/lib/queryObserver.cjs.map +1 -1
  65. package/build/lib/queryObserver.d.ts +3 -3
  66. package/build/lib/queryObserver.d.ts.map +1 -1
  67. package/build/lib/queryObserver.js.map +1 -1
  68. package/build/lib/queryObserver.legacy.cjs.map +1 -1
  69. package/build/lib/queryObserver.legacy.js.map +1 -1
  70. package/build/lib/retryer.cjs.map +1 -1
  71. package/build/lib/retryer.d.ts +1 -1
  72. package/build/lib/retryer.d.ts.map +1 -1
  73. package/build/lib/retryer.js.map +1 -1
  74. package/build/lib/retryer.legacy.cjs.map +1 -1
  75. package/build/lib/retryer.legacy.js.map +1 -1
  76. package/build/lib/tests/utils.d.ts +4 -3
  77. package/build/lib/tests/utils.d.ts.map +1 -1
  78. package/build/lib/types.d.ts +2 -2
  79. package/build/lib/types.d.ts.map +1 -1
  80. package/package.json +1 -1
  81. package/src/hydration.ts +8 -0
  82. package/src/infiniteQueryBehavior.ts +1 -1
  83. package/src/infiniteQueryObserver.ts +7 -7
  84. package/src/mutation.ts +5 -5
  85. package/src/mutationCache.ts +5 -5
  86. package/src/mutationObserver.ts +5 -5
  87. package/src/queriesObserver.ts +4 -4
  88. package/src/query.ts +8 -8
  89. package/src/queryCache.ts +5 -5
  90. package/src/queryClient.ts +14 -14
  91. package/src/queryObserver.ts +13 -13
  92. package/src/retryer.ts +1 -1
  93. package/src/tests/focusManager.test.tsx +1 -1
  94. package/src/tests/hydration.test.tsx +114 -3
  95. package/src/tests/infiniteQueryBehavior.test.tsx +3 -3
  96. package/src/tests/infiniteQueryObserver.test.tsx +2 -2
  97. package/src/tests/mutationCache.test.tsx +2 -2
  98. package/src/tests/mutationObserver.test.tsx +2 -2
  99. package/src/tests/mutations.test.tsx +4 -4
  100. package/src/tests/notifyManager.test.tsx +1 -1
  101. package/src/tests/onlineManager.test.tsx +1 -1
  102. package/src/tests/queriesObserver.test.tsx +3 -3
  103. package/src/tests/query.test.tsx +6 -6
  104. package/src/tests/queryCache.test.tsx +2 -2
  105. package/src/tests/queryClient.test.tsx +10 -10
  106. package/src/tests/queryObserver.test.tsx +3 -3
  107. package/src/tests/utils.test.tsx +6 -6
  108. package/src/tests/utils.ts +6 -4
  109. package/src/types.ts +2 -2
package/src/retryer.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { focusManager } from './focusManager'
2
2
  import { onlineManager } from './onlineManager'
3
3
  import { sleep } from './utils'
4
- import type { CancelOptions, NetworkMode, DefaultError } from './types'
4
+ import type { CancelOptions, DefaultError, NetworkMode } from './types'
5
5
 
6
6
  // TYPES
7
7
 
@@ -1,7 +1,7 @@
1
+ import { vi } from 'vitest'
1
2
  import { sleep } from '../utils'
2
3
  import { FocusManager } from '../focusManager'
3
4
  import { setIsServer } from './utils'
4
- import { vi } from 'vitest'
5
5
 
6
6
  describe('focusManager', () => {
7
7
  let focusManager: FocusManager
@@ -1,12 +1,13 @@
1
+ import { expect, vi } from 'vitest'
2
+ import { QueryCache } from '../queryCache'
3
+ import { dehydrate, hydrate } from '../hydration'
4
+ import { MutationCache } from '../mutationCache'
1
5
  import {
2
6
  createQueryClient,
3
7
  executeMutation,
4
8
  mockNavigatorOnLine,
5
9
  sleep,
6
10
  } from './utils'
7
- import { QueryCache } from '../queryCache'
8
- import { dehydrate, hydrate } from '../hydration'
9
- import { vi } from 'vitest'
10
11
 
11
12
  async function fetchData<TData>(value: TData, ms?: number): Promise<TData> {
12
13
  await sleep(ms || 0)
@@ -557,4 +558,114 @@ describe('dehydration and rehydration', () => {
557
558
  hydrationCache.find({ queryKey: ['string'] })?.state.fetchStatus,
558
559
  ).toBe('idle')
559
560
  })
561
+
562
+ test('should dehydrate and hydrate meta for queries', async () => {
563
+ const queryCache = new QueryCache()
564
+ const queryClient = createQueryClient({ queryCache })
565
+ await queryClient.prefetchQuery({
566
+ queryKey: ['meta'],
567
+ queryFn: () => Promise.resolve('meta'),
568
+ meta: {
569
+ some: 'meta',
570
+ },
571
+ })
572
+ await queryClient.prefetchQuery({
573
+ queryKey: ['no-meta'],
574
+ queryFn: () => Promise.resolve('no-meta'),
575
+ })
576
+
577
+ const dehydrated = dehydrate(queryClient)
578
+
579
+ expect(
580
+ dehydrated.queries.find((q) => q.queryHash === '["meta"]')?.meta,
581
+ ).toEqual({
582
+ some: 'meta',
583
+ })
584
+
585
+ expect(
586
+ dehydrated.queries.find((q) => q.queryHash === '["no-meta"]')?.meta,
587
+ ).toEqual(undefined)
588
+
589
+ expect(
590
+ Object.keys(
591
+ dehydrated.queries.find((q) => q.queryHash === '["no-meta"]')!,
592
+ ),
593
+ ).not.toEqual(expect.arrayContaining(['meta']))
594
+
595
+ const stringified = JSON.stringify(dehydrated)
596
+
597
+ // ---
598
+
599
+ const parsed = JSON.parse(stringified)
600
+ const hydrationCache = new QueryCache()
601
+ const hydrationClient = createQueryClient({
602
+ queryCache: hydrationCache,
603
+ })
604
+ hydrate(hydrationClient, parsed)
605
+ expect(hydrationCache.find({ queryKey: ['meta'] })?.meta).toEqual({
606
+ some: 'meta',
607
+ })
608
+ expect(hydrationCache.find({ queryKey: ['no-meta'] })?.meta).toEqual(
609
+ undefined,
610
+ )
611
+ })
612
+
613
+ test('should dehydrate and hydrate meta for mutations', async () => {
614
+ const mutationCache = new MutationCache()
615
+ const queryClient = createQueryClient({ mutationCache })
616
+
617
+ await executeMutation(
618
+ queryClient,
619
+ {
620
+ mutationKey: ['meta'],
621
+ mutationFn: () => Promise.resolve('meta'),
622
+ meta: {
623
+ some: 'meta',
624
+ },
625
+ },
626
+ undefined,
627
+ )
628
+
629
+ await executeMutation(
630
+ queryClient,
631
+ {
632
+ mutationKey: ['no-meta'],
633
+ mutationFn: () => Promise.resolve('no-meta'),
634
+ },
635
+ undefined,
636
+ )
637
+
638
+ const dehydrated = dehydrate(queryClient, {
639
+ shouldDehydrateMutation: () => true,
640
+ })
641
+
642
+ expect(Object.keys(dehydrated.mutations[0]!)).toEqual(
643
+ expect.arrayContaining(['meta']),
644
+ )
645
+ expect(dehydrated.mutations[0]?.meta).toEqual({
646
+ some: 'meta',
647
+ })
648
+
649
+ expect(Object.keys(dehydrated.mutations[1]!)).not.toEqual(
650
+ expect.arrayContaining(['meta']),
651
+ )
652
+ expect(dehydrated.mutations[1]?.meta).toEqual(undefined)
653
+
654
+ const stringified = JSON.stringify(dehydrated)
655
+
656
+ // ---
657
+
658
+ const parsed = JSON.parse(stringified)
659
+ const hydrationCache = new MutationCache()
660
+ const hydrationClient = createQueryClient({
661
+ mutationCache: hydrationCache,
662
+ })
663
+ hydrate(hydrationClient, parsed)
664
+ expect(hydrationCache.find({ mutationKey: ['meta'] })?.meta).toEqual({
665
+ some: 'meta',
666
+ })
667
+ expect(hydrationCache.find({ mutationKey: ['no-meta'] })?.meta).toEqual(
668
+ undefined,
669
+ )
670
+ })
560
671
  })
@@ -1,8 +1,8 @@
1
1
  import { waitFor } from '@testing-library/react'
2
- import type { QueryClient, QueryCache, InfiniteQueryObserverResult } from '..'
3
- import { InfiniteQueryObserver, CancelledError } from '..'
4
- import { createQueryClient, queryKey, sleep } from './utils'
5
2
  import { vi } from 'vitest'
3
+ import { CancelledError, InfiniteQueryObserver } from '..'
4
+ import { createQueryClient, queryKey, sleep } from './utils'
5
+ import type { InfiniteQueryObserverResult, QueryCache, QueryClient } from '..'
6
6
 
7
7
  describe('InfiniteQueryBehavior', () => {
8
8
  let queryClient: QueryClient
@@ -1,7 +1,7 @@
1
+ import { vi } from 'vitest'
2
+ import { InfiniteQueryObserver } from '..'
1
3
  import { createQueryClient, queryKey, sleep } from './utils'
2
4
  import type { QueryClient } from '..'
3
- import { InfiniteQueryObserver } from '..'
4
- import { vi } from 'vitest'
5
5
 
6
6
  describe('InfiniteQueryObserver', () => {
7
7
  let queryClient: QueryClient
@@ -1,7 +1,7 @@
1
1
  import { waitFor } from '@testing-library/react'
2
- import { queryKey, sleep, executeMutation, createQueryClient } from './utils'
3
- import { MutationCache, MutationObserver } from '..'
4
2
  import { vi } from 'vitest'
3
+ import { MutationCache, MutationObserver } from '..'
4
+ import { createQueryClient, executeMutation, queryKey, sleep } from './utils'
5
5
 
6
6
  describe('mutationCache', () => {
7
7
  describe('MutationCacheConfig error callbacks', () => {
@@ -1,8 +1,8 @@
1
1
  import { waitFor } from '@testing-library/react'
2
+ import { vi } from 'vitest'
3
+ import { MutationObserver } from '..'
2
4
  import { createQueryClient, sleep } from './utils'
3
5
  import type { QueryClient } from '..'
4
- import { MutationObserver } from '..'
5
- import { vi } from 'vitest'
6
6
 
7
7
  describe('mutationObserver', () => {
8
8
  let queryClient: QueryClient
@@ -1,9 +1,9 @@
1
- import type { QueryClient } from '..'
2
- import { createQueryClient, executeMutation, queryKey, sleep } from './utils'
3
- import type { MutationState } from '../mutation'
4
- import { MutationObserver } from '../mutationObserver'
5
1
  import { vi } from 'vitest'
6
2
  import { waitFor } from '@testing-library/react'
3
+ import { MutationObserver } from '../mutationObserver'
4
+ import { createQueryClient, executeMutation, queryKey, sleep } from './utils'
5
+ import type { QueryClient } from '..'
6
+ import type { MutationState } from '../mutation'
7
7
 
8
8
  describe('mutations', () => {
9
9
  let queryClient: QueryClient
@@ -1,6 +1,6 @@
1
+ import { vi } from 'vitest'
1
2
  import { createNotifyManager } from '../notifyManager'
2
3
  import { sleep } from './utils'
3
- import { vi } from 'vitest'
4
4
 
5
5
  describe('notifyManager', () => {
6
6
  it('should use default notifyFn', async () => {
@@ -1,6 +1,6 @@
1
+ import { vi } from 'vitest'
1
2
  import { OnlineManager } from '../onlineManager'
2
3
  import { setIsServer, sleep } from './utils'
3
- import { vi } from 'vitest'
4
4
 
5
5
  describe('onlineManager', () => {
6
6
  let onlineManager: OnlineManager
@@ -1,8 +1,8 @@
1
1
  import { waitFor } from '@testing-library/react'
2
- import { sleep, queryKey, createQueryClient } from './utils'
3
- import type { QueryClient, QueryObserverResult } from '..'
4
- import { QueriesObserver } from '..'
5
2
  import { vi } from 'vitest'
3
+ import { QueriesObserver } from '..'
4
+ import { createQueryClient, queryKey, sleep } from './utils'
5
+ import type { QueryClient, QueryObserverResult } from '..'
6
6
 
7
7
  describe('queriesObserver', () => {
8
8
  let queryClient: QueryClient
@@ -1,8 +1,11 @@
1
+ import { waitFor } from '@testing-library/react'
2
+ import { vi } from 'vitest'
3
+ import { QueryObserver, isCancelledError, onlineManager } from '..'
1
4
  import {
2
- sleep,
3
- queryKey,
4
- mockVisibilityState,
5
5
  createQueryClient,
6
+ mockVisibilityState,
7
+ queryKey,
8
+ sleep,
6
9
  } from './utils'
7
10
  import type {
8
11
  QueryCache,
@@ -10,9 +13,6 @@ import type {
10
13
  QueryFunctionContext,
11
14
  QueryObserverResult,
12
15
  } from '..'
13
- import { QueryObserver, isCancelledError, onlineManager } from '..'
14
- import { waitFor } from '@testing-library/react'
15
- import { vi } from 'vitest'
16
16
 
17
17
  describe('query', () => {
18
18
  let queryClient: QueryClient
@@ -1,7 +1,7 @@
1
- import { sleep, queryKey, createQueryClient } from './utils'
2
- import { QueryClient, QueryCache, QueryObserver } from '..'
3
1
  import { waitFor } from '@testing-library/react'
4
2
  import { vi } from 'vitest'
3
+ import { QueryCache, QueryClient, QueryObserver } from '..'
4
+ import { createQueryClient, queryKey, sleep } from './utils'
5
5
 
6
6
  describe('queryCache', () => {
7
7
  let queryClient: QueryClient
@@ -1,11 +1,19 @@
1
1
  import { waitFor } from '@testing-library/react'
2
2
  import '@testing-library/jest-dom'
3
3
 
4
+ import { vi } from 'vitest'
5
+ import {
6
+ MutationObserver,
7
+ QueryObserver,
8
+ focusManager,
9
+ onlineManager,
10
+ } from '..'
11
+ import { noop } from '../utils'
4
12
  import {
5
- sleep,
6
- queryKey,
7
13
  createQueryClient,
8
14
  mockNavigatorOnLine,
15
+ queryKey,
16
+ sleep,
9
17
  } from './utils'
10
18
  import type {
11
19
  QueryCache,
@@ -13,14 +21,6 @@ import type {
13
21
  QueryFunction,
14
22
  QueryObserverOptions,
15
23
  } from '..'
16
- import {
17
- MutationObserver,
18
- QueryObserver,
19
- focusManager,
20
- onlineManager,
21
- } from '..'
22
- import { noop } from '../utils'
23
- import { vi } from 'vitest'
24
24
 
25
25
  describe('queryClient', () => {
26
26
  let queryClient: QueryClient
@@ -1,7 +1,7 @@
1
- import { sleep, queryKey, expectType, createQueryClient } from './utils'
2
- import type { QueryClient, QueryObserverResult } from '..'
3
- import { QueryObserver, focusManager } from '..'
4
1
  import { vi } from 'vitest'
2
+ import { QueryObserver, focusManager } from '..'
3
+ import { createQueryClient, expectType, queryKey, sleep } from './utils'
4
+ import type { QueryClient, QueryObserverResult } from '..'
5
5
 
6
6
  describe('queryObserver', () => {
7
7
  let queryClient: QueryClient
@@ -1,17 +1,17 @@
1
+ import { vi } from 'vitest'
1
2
  import {
2
- replaceEqualDeep,
3
- partialMatchKey,
3
+ addToEnd,
4
+ addToStart,
5
+ isPlainArray,
4
6
  isPlainObject,
5
7
  matchMutation,
8
+ partialMatchKey,
9
+ replaceEqualDeep,
6
10
  scheduleMicrotask,
7
11
  sleep,
8
- isPlainArray,
9
- addToEnd,
10
- addToStart,
11
12
  } from '../utils'
12
13
  import { Mutation } from '../mutation'
13
14
  import { createQueryClient } from './utils'
14
- import { vi } from 'vitest'
15
15
 
16
16
  describe('core/utils', () => {
17
17
  describe('isPlainObject', () => {
@@ -1,19 +1,21 @@
1
1
  import { act } from '@testing-library/react'
2
2
  import { vi } from 'vitest'
3
-
4
- import type { MutationOptions, QueryClientConfig } from '..'
5
3
  import { QueryClient } from '..'
6
4
  import * as utils from '../utils'
5
+ import type { SpyInstance } from 'vitest'
6
+ import type { MutationOptions, QueryClientConfig } from '..'
7
7
 
8
8
  export function createQueryClient(config?: QueryClientConfig): QueryClient {
9
9
  return new QueryClient(config)
10
10
  }
11
11
 
12
- export function mockVisibilityState(value: DocumentVisibilityState) {
12
+ export function mockVisibilityState(
13
+ value: DocumentVisibilityState,
14
+ ): SpyInstance<[], DocumentVisibilityState> {
13
15
  return vi.spyOn(document, 'visibilityState', 'get').mockReturnValue(value)
14
16
  }
15
17
 
16
- export function mockNavigatorOnLine(value: boolean) {
18
+ export function mockNavigatorOnLine(value: boolean): SpyInstance<[], boolean> {
17
19
  return vi.spyOn(navigator, 'onLine', 'get').mockReturnValue(value)
18
20
  }
19
21
 
package/src/types.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  /* istanbul ignore file */
2
2
 
3
3
  import type { MutationState } from './mutation'
4
- import type { FetchDirection, QueryBehavior, Query } from './query'
5
- import type { RetryValue, RetryDelayValue } from './retryer'
4
+ import type { FetchDirection, Query, QueryBehavior } from './query'
5
+ import type { RetryDelayValue, RetryValue } from './retryer'
6
6
  import type { QueryFilters, QueryTypeFilter } from './utils'
7
7
  import type { QueryCache } from './queryCache'
8
8
  import type { MutationCache } from './mutationCache'