@tanstack/query-core 5.38.0 → 5.44.0

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 (138) hide show
  1. package/build/legacy/{types-BtrVwz9w.d.ts → hydration-DJZYTIMr.d.ts} +50 -6
  2. package/build/legacy/{types-BvcshvE9.d.cts → hydration-XP7CH-6g.d.cts} +50 -6
  3. package/build/legacy/hydration.cjs +40 -23
  4. package/build/legacy/hydration.cjs.map +1 -1
  5. package/build/legacy/hydration.d.cts +1 -34
  6. package/build/legacy/hydration.d.ts +1 -34
  7. package/build/legacy/hydration.js +40 -23
  8. package/build/legacy/hydration.js.map +1 -1
  9. package/build/legacy/index.d.cts +1 -2
  10. package/build/legacy/index.d.ts +1 -2
  11. package/build/legacy/infiniteQueryBehavior.cjs +1 -12
  12. package/build/legacy/infiniteQueryBehavior.cjs.map +1 -1
  13. package/build/legacy/infiniteQueryBehavior.d.cts +1 -1
  14. package/build/legacy/infiniteQueryBehavior.d.ts +1 -1
  15. package/build/legacy/infiniteQueryBehavior.js +2 -13
  16. package/build/legacy/infiniteQueryBehavior.js.map +1 -1
  17. package/build/legacy/infiniteQueryObserver.d.cts +1 -1
  18. package/build/legacy/infiniteQueryObserver.d.ts +1 -1
  19. package/build/legacy/mutation.d.cts +1 -1
  20. package/build/legacy/mutation.d.ts +1 -1
  21. package/build/legacy/mutationCache.d.cts +1 -1
  22. package/build/legacy/mutationCache.d.ts +1 -1
  23. package/build/legacy/mutationObserver.d.cts +1 -1
  24. package/build/legacy/mutationObserver.d.ts +1 -1
  25. package/build/legacy/queriesObserver.d.cts +1 -1
  26. package/build/legacy/queriesObserver.d.ts +1 -1
  27. package/build/legacy/query.cjs +13 -21
  28. package/build/legacy/query.cjs.map +1 -1
  29. package/build/legacy/query.d.cts +1 -1
  30. package/build/legacy/query.d.ts +1 -1
  31. package/build/legacy/query.js +14 -22
  32. package/build/legacy/query.js.map +1 -1
  33. package/build/legacy/queryCache.cjs.map +1 -1
  34. package/build/legacy/queryCache.d.cts +1 -1
  35. package/build/legacy/queryCache.d.ts +1 -1
  36. package/build/legacy/queryCache.js.map +1 -1
  37. package/build/legacy/queryClient.cjs +4 -2
  38. package/build/legacy/queryClient.cjs.map +1 -1
  39. package/build/legacy/queryClient.d.cts +1 -1
  40. package/build/legacy/queryClient.d.ts +1 -1
  41. package/build/legacy/queryClient.js +5 -2
  42. package/build/legacy/queryClient.js.map +1 -1
  43. package/build/legacy/queryObserver.cjs +8 -7
  44. package/build/legacy/queryObserver.cjs.map +1 -1
  45. package/build/legacy/queryObserver.d.cts +1 -1
  46. package/build/legacy/queryObserver.d.ts +1 -1
  47. package/build/legacy/queryObserver.js +9 -7
  48. package/build/legacy/queryObserver.js.map +1 -1
  49. package/build/legacy/retryer.cjs +2 -1
  50. package/build/legacy/retryer.cjs.map +1 -1
  51. package/build/legacy/retryer.d.cts +1 -1
  52. package/build/legacy/retryer.d.ts +1 -1
  53. package/build/legacy/retryer.js +2 -1
  54. package/build/legacy/retryer.js.map +1 -1
  55. package/build/legacy/types.cjs.map +1 -1
  56. package/build/legacy/types.d.cts +1 -1
  57. package/build/legacy/types.d.ts +1 -1
  58. package/build/legacy/utils.cjs +23 -0
  59. package/build/legacy/utils.cjs.map +1 -1
  60. package/build/legacy/utils.d.cts +1 -1
  61. package/build/legacy/utils.d.ts +1 -1
  62. package/build/legacy/utils.js +21 -0
  63. package/build/legacy/utils.js.map +1 -1
  64. package/build/modern/{types-BtrVwz9w.d.ts → hydration-DJZYTIMr.d.ts} +50 -6
  65. package/build/modern/{types-BvcshvE9.d.cts → hydration-XP7CH-6g.d.cts} +50 -6
  66. package/build/modern/hydration.cjs +35 -20
  67. package/build/modern/hydration.cjs.map +1 -1
  68. package/build/modern/hydration.d.cts +1 -34
  69. package/build/modern/hydration.d.ts +1 -34
  70. package/build/modern/hydration.js +35 -20
  71. package/build/modern/hydration.js.map +1 -1
  72. package/build/modern/index.d.cts +1 -2
  73. package/build/modern/index.d.ts +1 -2
  74. package/build/modern/infiniteQueryBehavior.cjs +1 -12
  75. package/build/modern/infiniteQueryBehavior.cjs.map +1 -1
  76. package/build/modern/infiniteQueryBehavior.d.cts +1 -1
  77. package/build/modern/infiniteQueryBehavior.d.ts +1 -1
  78. package/build/modern/infiniteQueryBehavior.js +2 -13
  79. package/build/modern/infiniteQueryBehavior.js.map +1 -1
  80. package/build/modern/infiniteQueryObserver.d.cts +1 -1
  81. package/build/modern/infiniteQueryObserver.d.ts +1 -1
  82. package/build/modern/mutation.d.cts +1 -1
  83. package/build/modern/mutation.d.ts +1 -1
  84. package/build/modern/mutationCache.d.cts +1 -1
  85. package/build/modern/mutationCache.d.ts +1 -1
  86. package/build/modern/mutationObserver.d.cts +1 -1
  87. package/build/modern/mutationObserver.d.ts +1 -1
  88. package/build/modern/queriesObserver.d.cts +1 -1
  89. package/build/modern/queriesObserver.d.ts +1 -1
  90. package/build/modern/query.cjs +12 -21
  91. package/build/modern/query.cjs.map +1 -1
  92. package/build/modern/query.d.cts +1 -1
  93. package/build/modern/query.d.ts +1 -1
  94. package/build/modern/query.js +13 -22
  95. package/build/modern/query.js.map +1 -1
  96. package/build/modern/queryCache.cjs.map +1 -1
  97. package/build/modern/queryCache.d.cts +1 -1
  98. package/build/modern/queryCache.d.ts +1 -1
  99. package/build/modern/queryCache.js.map +1 -1
  100. package/build/modern/queryClient.cjs +4 -2
  101. package/build/modern/queryClient.cjs.map +1 -1
  102. package/build/modern/queryClient.d.cts +1 -1
  103. package/build/modern/queryClient.d.ts +1 -1
  104. package/build/modern/queryClient.js +5 -2
  105. package/build/modern/queryClient.js.map +1 -1
  106. package/build/modern/queryObserver.cjs +8 -7
  107. package/build/modern/queryObserver.cjs.map +1 -1
  108. package/build/modern/queryObserver.d.cts +1 -1
  109. package/build/modern/queryObserver.d.ts +1 -1
  110. package/build/modern/queryObserver.js +9 -7
  111. package/build/modern/queryObserver.js.map +1 -1
  112. package/build/modern/retryer.cjs +2 -1
  113. package/build/modern/retryer.cjs.map +1 -1
  114. package/build/modern/retryer.d.cts +1 -1
  115. package/build/modern/retryer.d.ts +1 -1
  116. package/build/modern/retryer.js +2 -1
  117. package/build/modern/retryer.js.map +1 -1
  118. package/build/modern/types.cjs.map +1 -1
  119. package/build/modern/types.d.cts +1 -1
  120. package/build/modern/types.d.ts +1 -1
  121. package/build/modern/utils.cjs +23 -0
  122. package/build/modern/utils.cjs.map +1 -1
  123. package/build/modern/utils.d.cts +1 -1
  124. package/build/modern/utils.d.ts +1 -1
  125. package/build/modern/utils.js +21 -0
  126. package/build/modern/utils.js.map +1 -1
  127. package/package.json +1 -1
  128. package/src/__tests__/hydration.test.tsx +170 -0
  129. package/src/__tests__/queryObserver.test.tsx +26 -0
  130. package/src/hydration.ts +43 -21
  131. package/src/infiniteQueryBehavior.ts +2 -17
  132. package/src/query.ts +24 -29
  133. package/src/queryCache.ts +6 -1
  134. package/src/queryClient.ts +5 -2
  135. package/src/queryObserver.ts +14 -12
  136. package/src/retryer.ts +6 -1
  137. package/src/types.ts +13 -2
  138. package/src/utils.ts +50 -2
@@ -71,9 +71,10 @@ interface FetchMeta {
71
71
  direction: FetchDirection;
72
72
  };
73
73
  }
74
- interface FetchOptions {
74
+ interface FetchOptions<TData = unknown> {
75
75
  cancelRefetch?: boolean;
76
76
  meta?: FetchMeta;
77
+ initialPromise?: Promise<TData>;
77
78
  }
78
79
  interface FailedAction$1<TError> {
79
80
  type: 'failed';
@@ -122,6 +123,7 @@ declare class Query<TQueryFnData = unknown, TError = DefaultError, TData = TQuer
122
123
  observers: Array<QueryObserver<any, any, any, any, any>>;
123
124
  constructor(config: QueryConfig<TQueryFnData, TError, TData, TQueryKey>);
124
125
  get meta(): QueryMeta | undefined;
126
+ get promise(): Promise<TData> | undefined;
125
127
  setOptions(options?: QueryOptions<TQueryFnData, TError, TData, TQueryKey>): void;
126
128
  protected optionalRemove(): void;
127
129
  setData(newData: TData, options?: SetDataOptions & {
@@ -141,7 +143,7 @@ declare class Query<TQueryFnData = unknown, TError = DefaultError, TData = TQuer
141
143
  removeObserver(observer: QueryObserver<any, any, any, any, any>): void;
142
144
  getObserversCount(): number;
143
145
  invalidate(): void;
144
- fetch(options?: QueryOptions<TQueryFnData, TError, TData, TQueryKey>, fetchOptions?: FetchOptions): Promise<TData>;
146
+ fetch(options?: QueryOptions<TQueryFnData, TError, TData, TQueryKey>, fetchOptions?: FetchOptions<TQueryFnData>): Promise<TData>;
145
147
  }
146
148
  declare function fetchState<TQueryFnData, TError, TData, TQueryKey extends QueryKey>(data: TData | undefined, options: QueryOptions<TQueryFnData, TError, TData, TQueryKey>): {
147
149
  readonly error?: null | undefined;
@@ -202,6 +204,7 @@ declare function noop(): undefined;
202
204
  declare function functionalUpdate<TInput, TOutput>(updater: Updater<TInput, TOutput>, input: TInput): TOutput;
203
205
  declare function isValidTimeout(value: unknown): value is number;
204
206
  declare function timeUntilStale(updatedAt: number, staleTime?: number): number;
207
+ declare function resolveStaleTime<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(staleTime: undefined | StaleTime<TQueryFnData, TError, TData, TQueryKey>, query: Query<TQueryFnData, TError, TData, TQueryKey>): number | undefined;
205
208
  declare function matchQuery(filters: QueryFilters, query: Query<any, any, any, any>): boolean;
206
209
  declare function matchMutation(filters: MutationFilters, mutation: Mutation<any, any>): boolean;
207
210
  declare function hashQueryKeyByOptions<TQueryKey extends QueryKey = QueryKey>(queryKey: TQueryKey, options?: Pick<QueryOptions<any, any, any, any>, 'queryKeyHashFn'>): string;
@@ -233,6 +236,10 @@ declare function addToEnd<T>(items: Array<T>, item: T, max?: number): Array<T>;
233
236
  declare function addToStart<T>(items: Array<T>, item: T, max?: number): Array<T>;
234
237
  declare const skipToken: unique symbol;
235
238
  type SkipToken = typeof skipToken;
239
+ declare const ensureQueryFn: <TQueryFnData = unknown, TQueryKey extends QueryKey = QueryKey>(options: {
240
+ queryFn?: typeof skipToken | QueryFunction<TQueryFnData, TQueryKey> | undefined;
241
+ queryHash?: string | undefined;
242
+ }, fetchOptions?: FetchOptions<TQueryFnData> | undefined) => QueryFunction<TQueryFnData, TQueryKey>;
236
243
 
237
244
  interface QueryCacheConfig {
238
245
  onError?: (error: DefaultError, query: Query<unknown, unknown, unknown>) => void;
@@ -284,7 +291,7 @@ declare class QueryCache extends Subscribable<QueryCacheListener> {
284
291
  #private;
285
292
  config: QueryCacheConfig;
286
293
  constructor(config?: QueryCacheConfig);
287
- build<TQueryFnData, TError, TData, TQueryKey extends QueryKey>(client: QueryClient, options: WithRequired<QueryOptions<TQueryFnData, TError, TData, TQueryKey>, 'queryKey'>, state?: QueryState<TData, TError>): Query<TQueryFnData, TError, TData, TQueryKey>;
294
+ build<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(client: QueryClient, options: WithRequired<QueryOptions<TQueryFnData, TError, TData, TQueryKey>, 'queryKey'>, state?: QueryState<TData, TError>): Query<TQueryFnData, TError, TData, TQueryKey>;
288
295
  add(query: Query<any, any, any, any>): void;
289
296
  remove(query: Query<any, any, any, any>): void;
290
297
  clear(): void;
@@ -461,6 +468,7 @@ declare function getDefaultState<TData, TError, TVariables, TContext>(): Mutatio
461
468
 
462
469
  interface RetryerConfig<TData = unknown, TError = DefaultError> {
463
470
  fn: () => TData | Promise<TData>;
471
+ initialPromise?: Promise<TData>;
464
472
  abort?: () => void;
465
473
  onError?: (error: TError) => void;
466
474
  onSuccess?: (data: TData) => void;
@@ -507,6 +515,7 @@ type DataTag<TType, TValue> = TType & {
507
515
  [dataTagSymbol]: TValue;
508
516
  };
509
517
  type QueryFunction<T = unknown, TQueryKey extends QueryKey = QueryKey, TPageParam = never> = (context: QueryFunctionContext<TQueryKey, TPageParam>) => T | Promise<T>;
518
+ type StaleTime<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey> = number | ((query: Query<TQueryFnData, TError, TData, TQueryKey>) => number);
510
519
  type QueryPersister<T = unknown, TQueryKey extends QueryKey = QueryKey, TPageParam = never> = [TPageParam] extends [never] ? (queryFn: QueryFunction<T, TQueryKey, never>, context: QueryFunctionContext<TQueryKey>, query: Query) => T | Promise<T> : (queryFn: QueryFunction<T, TQueryKey, TPageParam>, context: QueryFunctionContext<TQueryKey>, query: Query) => T | Promise<T>;
511
520
  type QueryFunctionContext<TQueryKey extends QueryKey = QueryKey, TPageParam = never> = [TPageParam] extends [never] ? {
512
521
  queryKey: TQueryKey;
@@ -613,8 +622,9 @@ interface QueryObserverOptions<TQueryFnData = unknown, TError = DefaultError, TD
613
622
  /**
614
623
  * The time in milliseconds after data is considered stale.
615
624
  * If set to `Infinity`, the data will never be considered stale.
625
+ * If set to a function, the function will be executed with the query to compute a `staleTime`.
616
626
  */
617
- staleTime?: number;
627
+ staleTime?: StaleTime<TQueryFnData, TError, TQueryData, TQueryKey>;
618
628
  /**
619
629
  * If set to a number, the query will continuously refetch at this frequency in milliseconds.
620
630
  * If set to a function, the function will be executed with the latest data and query to compute a frequency
@@ -700,7 +710,7 @@ interface FetchQueryOptions<TQueryFnData = unknown, TError = DefaultError, TData
700
710
  * The time in milliseconds after data is considered stale.
701
711
  * If the data is fresh it will be returned from the cache.
702
712
  */
703
- staleTime?: number;
713
+ staleTime?: StaleTime<TQueryFnData, TError, TData, TQueryKey>;
704
714
  }
705
715
  interface EnsureQueryDataOptions<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, TPageParam = never> extends FetchQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam> {
706
716
  revalidateIfStale?: boolean;
@@ -1173,6 +1183,8 @@ interface QueryClientConfig {
1173
1183
  interface DefaultOptions<TError = DefaultError> {
1174
1184
  queries?: OmitKeyof<QueryObserverOptions<unknown, TError>, 'suspense' | 'queryKey'>;
1175
1185
  mutations?: MutationObserverOptions<unknown, TError, unknown, unknown>;
1186
+ hydrate?: HydrateOptions['defaultOptions'];
1187
+ dehydrate?: DehydrateOptions;
1176
1188
  }
1177
1189
  interface CancelOptions {
1178
1190
  revert?: boolean;
@@ -1186,4 +1198,36 @@ interface NotifyEvent {
1186
1198
  type: NotifyEventType;
1187
1199
  }
1188
1200
 
1189
- export { type DefaultedInfiniteQueryObserverOptions as $, type QueryKeyHashFunction as A, type GetNextPageParamFunction as B, CancelledError as C, type DefaultError as D, type InfiniteData as E, type QueryMeta as F, type GetPreviousPageParamFunction as G, type NetworkMode as H, type InitialDataFunction as I, type NotifyOnChangeProps as J, type QueryOptions as K, type InitialPageParam as L, MutationCache as M, type NoInfer as N, type OmitKeyof as O, type PlaceholderDataFunction as P, QueryCache as Q, type Register as R, type SkipToken as S, type InfiniteQueryPageParamsOptions as T, type Updater as U, type ThrowOnError as V, type QueryObserverOptions as W, type WithRequired as X, type Optional as Y, type DefaultedQueryObserverOptions as Z, type InfiniteQueryObserverOptions as _, type QueryCacheNotifyEvent as a, isValidTimeout as a$, type FetchQueryOptions as a0, type EnsureQueryDataOptions as a1, type FetchInfiniteQueryOptions as a2, type ResultOptions as a3, type RefetchOptions as a4, type InvalidateQueryFilters as a5, type RefetchQueryFilters as a6, type InvalidateOptions as a7, type ResetOptions as a8, type FetchNextPageOptions as a9, type MutateOptions as aA, type MutateFunction as aB, type MutationObserverBaseResult as aC, type MutationObserverIdleResult as aD, type MutationObserverLoadingResult as aE, type MutationObserverErrorResult as aF, type MutationObserverSuccessResult as aG, type MutationObserverResult as aH, type QueryClientConfig as aI, type DefaultOptions as aJ, type CancelOptions as aK, type SetDataOptions as aL, type NotifyEventType as aM, type NotifyEvent as aN, type QueryBehavior as aO, type ObserverFetchOptions as aP, type NotifyOptions as aQ, type FetchContext as aR, type FetchDirection as aS, type FetchMeta as aT, type FetchOptions as aU, type Action$1 as aV, type SetStateOptions as aW, fetchState as aX, type QueryTypeFilter as aY, noop as aZ, functionalUpdate as a_, type FetchPreviousPageOptions as aa, type QueryStatus as ab, type FetchStatus as ac, type QueryObserverBaseResult as ad, type QueryObserverPendingResult as ae, type QueryObserverLoadingResult as af, type QueryObserverLoadingErrorResult as ag, type QueryObserverRefetchErrorResult as ah, type QueryObserverSuccessResult as ai, type DefinedQueryObserverResult as aj, type QueryObserverResult as ak, type InfiniteQueryObserverBaseResult as al, type InfiniteQueryObserverPendingResult as am, type InfiniteQueryObserverLoadingResult as an, type InfiniteQueryObserverLoadingErrorResult as ao, type InfiniteQueryObserverRefetchErrorResult as ap, type InfiniteQueryObserverSuccessResult as aq, type DefinedInfiniteQueryObserverResult as ar, type InfiniteQueryObserverResult as as, type MutationKey as at, type MutationStatus as au, type MutationScope as av, type MutationMeta as aw, type MutationFunction as ax, type MutationOptions as ay, type MutationObserverOptions as az, QueryClient as b, timeUntilStale as b0, hashQueryKeyByOptions as b1, partialMatchKey as b2, shallowEqualObjects as b3, isPlainArray as b4, isPlainObject as b5, sleep as b6, replaceData as b7, addToEnd as b8, addToStart as b9, type QueryStore as ba, type Action as bb, getDefaultState as bc, type Retryer as bd, type RetryValue as be, type RetryDelayValue as bf, canFetch as bg, createRetryer as bh, QueryObserver as c, type MutationCacheNotifyEvent as d, MutationObserver as e, matchMutation as f, type MutationFilters as g, hashKey as h, isServer as i, type QueryFilters as j, keepPreviousData as k, isCancelledError as l, matchQuery as m, type QueryState as n, Query as o, Mutation as p, type MutationState as q, replaceEqualDeep as r, skipToken as s, type QueryKey as t, dataTagSymbol as u, type DataTag as v, type QueryFunction as w, type QueryPersister as x, type QueryFunctionContext as y, type QueriesPlaceholderDataFunction as z };
1201
+ interface DehydrateOptions {
1202
+ shouldDehydrateMutation?: (mutation: Mutation) => boolean;
1203
+ shouldDehydrateQuery?: (query: Query) => boolean;
1204
+ }
1205
+ interface HydrateOptions {
1206
+ defaultOptions?: {
1207
+ queries?: QueryOptions;
1208
+ mutations?: MutationOptions<unknown, DefaultError, unknown, unknown>;
1209
+ };
1210
+ }
1211
+ interface DehydratedMutation {
1212
+ mutationKey?: MutationKey;
1213
+ state: MutationState;
1214
+ meta?: MutationMeta;
1215
+ scope?: MutationScope;
1216
+ }
1217
+ interface DehydratedQuery {
1218
+ queryHash: string;
1219
+ queryKey: QueryKey;
1220
+ state: QueryState;
1221
+ promise?: Promise<unknown>;
1222
+ meta?: QueryMeta;
1223
+ }
1224
+ interface DehydratedState {
1225
+ mutations: Array<DehydratedMutation>;
1226
+ queries: Array<DehydratedQuery>;
1227
+ }
1228
+ declare function defaultShouldDehydrateMutation(mutation: Mutation): boolean;
1229
+ declare function defaultShouldDehydrateQuery(query: Query): boolean;
1230
+ declare function dehydrate(client: QueryClient, options?: DehydrateOptions): DehydratedState;
1231
+ declare function hydrate(client: QueryClient, dehydratedState: unknown, options?: HydrateOptions): void;
1232
+
1233
+ export { type InitialPageParam as $, dataTagSymbol as A, type DataTag as B, CancelledError as C, type DehydrateOptions as D, type QueryFunction as E, type StaleTime as F, type QueryPersister as G, type HydrateOptions as H, type QueryFunctionContext as I, type InitialDataFunction as J, type QueriesPlaceholderDataFunction as K, type QueryKeyHashFunction as L, MutationCache as M, type NoInfer as N, type OmitKeyof as O, type PlaceholderDataFunction as P, QueryCache as Q, type Register as R, type SkipToken as S, type GetPreviousPageParamFunction as T, type Updater as U, type GetNextPageParamFunction as V, type InfiniteData as W, type QueryMeta as X, type NetworkMode as Y, type NotifyOnChangeProps as Z, type QueryOptions as _, type QueryCacheNotifyEvent as a, type FetchMeta as a$, type InfiniteQueryPageParamsOptions as a0, type ThrowOnError as a1, type QueryObserverOptions as a2, type WithRequired as a3, type Optional as a4, type DefaultedQueryObserverOptions as a5, type InfiniteQueryObserverOptions as a6, type DefaultedInfiniteQueryObserverOptions as a7, type FetchQueryOptions as a8, type EnsureQueryDataOptions as a9, type InfiniteQueryObserverResult as aA, type MutationKey as aB, type MutationStatus as aC, type MutationScope as aD, type MutationMeta as aE, type MutationFunction as aF, type MutationOptions as aG, type MutationObserverOptions as aH, type MutateOptions as aI, type MutateFunction as aJ, type MutationObserverBaseResult as aK, type MutationObserverIdleResult as aL, type MutationObserverLoadingResult as aM, type MutationObserverErrorResult as aN, type MutationObserverSuccessResult as aO, type MutationObserverResult as aP, type QueryClientConfig as aQ, type DefaultOptions as aR, type CancelOptions as aS, type SetDataOptions as aT, type NotifyEventType as aU, type NotifyEvent as aV, type QueryBehavior as aW, type ObserverFetchOptions as aX, type NotifyOptions as aY, type FetchContext as aZ, type FetchDirection as a_, type FetchInfiniteQueryOptions as aa, type ResultOptions as ab, type RefetchOptions as ac, type InvalidateQueryFilters as ad, type RefetchQueryFilters as ae, type InvalidateOptions as af, type ResetOptions as ag, type FetchNextPageOptions as ah, type FetchPreviousPageOptions as ai, type QueryStatus as aj, type FetchStatus as ak, type QueryObserverBaseResult as al, type QueryObserverPendingResult as am, type QueryObserverLoadingResult as an, type QueryObserverLoadingErrorResult as ao, type QueryObserverRefetchErrorResult as ap, type QueryObserverSuccessResult as aq, type DefinedQueryObserverResult as ar, type QueryObserverResult as as, type InfiniteQueryObserverBaseResult as at, type InfiniteQueryObserverPendingResult as au, type InfiniteQueryObserverLoadingResult as av, type InfiniteQueryObserverLoadingErrorResult as aw, type InfiniteQueryObserverRefetchErrorResult as ax, type InfiniteQueryObserverSuccessResult as ay, type DefinedInfiniteQueryObserverResult as az, QueryClient as b, type FetchOptions as b0, type Action$1 as b1, type SetStateOptions as b2, fetchState as b3, type QueryTypeFilter as b4, noop as b5, functionalUpdate as b6, isValidTimeout as b7, timeUntilStale as b8, resolveStaleTime as b9, hashQueryKeyByOptions as ba, partialMatchKey as bb, shallowEqualObjects as bc, isPlainArray as bd, isPlainObject as be, sleep as bf, replaceData as bg, addToEnd as bh, addToStart as bi, ensureQueryFn as bj, type QueryStore as bk, type Action as bl, getDefaultState as bm, type Retryer as bn, type RetryValue as bo, type RetryDelayValue as bp, canFetch as bq, createRetryer as br, QueryObserver as c, type MutationCacheNotifyEvent as d, MutationObserver as e, matchMutation as f, type MutationFilters as g, hashKey as h, isServer as i, type QueryFilters as j, keepPreviousData as k, isCancelledError as l, matchQuery as m, dehydrate as n, hydrate as o, defaultShouldDehydrateQuery as p, defaultShouldDehydrateMutation as q, replaceEqualDeep as r, skipToken as s, type QueryState as t, Query as u, Mutation as v, type MutationState as w, type DehydratedState as x, type DefaultError as y, type QueryKey as z };
@@ -71,9 +71,10 @@ interface FetchMeta {
71
71
  direction: FetchDirection;
72
72
  };
73
73
  }
74
- interface FetchOptions {
74
+ interface FetchOptions<TData = unknown> {
75
75
  cancelRefetch?: boolean;
76
76
  meta?: FetchMeta;
77
+ initialPromise?: Promise<TData>;
77
78
  }
78
79
  interface FailedAction$1<TError> {
79
80
  type: 'failed';
@@ -122,6 +123,7 @@ declare class Query<TQueryFnData = unknown, TError = DefaultError, TData = TQuer
122
123
  observers: Array<QueryObserver<any, any, any, any, any>>;
123
124
  constructor(config: QueryConfig<TQueryFnData, TError, TData, TQueryKey>);
124
125
  get meta(): QueryMeta | undefined;
126
+ get promise(): Promise<TData> | undefined;
125
127
  setOptions(options?: QueryOptions<TQueryFnData, TError, TData, TQueryKey>): void;
126
128
  protected optionalRemove(): void;
127
129
  setData(newData: TData, options?: SetDataOptions & {
@@ -141,7 +143,7 @@ declare class Query<TQueryFnData = unknown, TError = DefaultError, TData = TQuer
141
143
  removeObserver(observer: QueryObserver<any, any, any, any, any>): void;
142
144
  getObserversCount(): number;
143
145
  invalidate(): void;
144
- fetch(options?: QueryOptions<TQueryFnData, TError, TData, TQueryKey>, fetchOptions?: FetchOptions): Promise<TData>;
146
+ fetch(options?: QueryOptions<TQueryFnData, TError, TData, TQueryKey>, fetchOptions?: FetchOptions<TQueryFnData>): Promise<TData>;
145
147
  }
146
148
  declare function fetchState<TQueryFnData, TError, TData, TQueryKey extends QueryKey>(data: TData | undefined, options: QueryOptions<TQueryFnData, TError, TData, TQueryKey>): {
147
149
  readonly error?: null | undefined;
@@ -202,6 +204,7 @@ declare function noop(): undefined;
202
204
  declare function functionalUpdate<TInput, TOutput>(updater: Updater<TInput, TOutput>, input: TInput): TOutput;
203
205
  declare function isValidTimeout(value: unknown): value is number;
204
206
  declare function timeUntilStale(updatedAt: number, staleTime?: number): number;
207
+ declare function resolveStaleTime<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(staleTime: undefined | StaleTime<TQueryFnData, TError, TData, TQueryKey>, query: Query<TQueryFnData, TError, TData, TQueryKey>): number | undefined;
205
208
  declare function matchQuery(filters: QueryFilters, query: Query<any, any, any, any>): boolean;
206
209
  declare function matchMutation(filters: MutationFilters, mutation: Mutation<any, any>): boolean;
207
210
  declare function hashQueryKeyByOptions<TQueryKey extends QueryKey = QueryKey>(queryKey: TQueryKey, options?: Pick<QueryOptions<any, any, any, any>, 'queryKeyHashFn'>): string;
@@ -233,6 +236,10 @@ declare function addToEnd<T>(items: Array<T>, item: T, max?: number): Array<T>;
233
236
  declare function addToStart<T>(items: Array<T>, item: T, max?: number): Array<T>;
234
237
  declare const skipToken: unique symbol;
235
238
  type SkipToken = typeof skipToken;
239
+ declare const ensureQueryFn: <TQueryFnData = unknown, TQueryKey extends QueryKey = QueryKey>(options: {
240
+ queryFn?: typeof skipToken | QueryFunction<TQueryFnData, TQueryKey> | undefined;
241
+ queryHash?: string | undefined;
242
+ }, fetchOptions?: FetchOptions<TQueryFnData> | undefined) => QueryFunction<TQueryFnData, TQueryKey>;
236
243
 
237
244
  interface QueryCacheConfig {
238
245
  onError?: (error: DefaultError, query: Query<unknown, unknown, unknown>) => void;
@@ -284,7 +291,7 @@ declare class QueryCache extends Subscribable<QueryCacheListener> {
284
291
  #private;
285
292
  config: QueryCacheConfig;
286
293
  constructor(config?: QueryCacheConfig);
287
- build<TQueryFnData, TError, TData, TQueryKey extends QueryKey>(client: QueryClient, options: WithRequired<QueryOptions<TQueryFnData, TError, TData, TQueryKey>, 'queryKey'>, state?: QueryState<TData, TError>): Query<TQueryFnData, TError, TData, TQueryKey>;
294
+ build<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(client: QueryClient, options: WithRequired<QueryOptions<TQueryFnData, TError, TData, TQueryKey>, 'queryKey'>, state?: QueryState<TData, TError>): Query<TQueryFnData, TError, TData, TQueryKey>;
288
295
  add(query: Query<any, any, any, any>): void;
289
296
  remove(query: Query<any, any, any, any>): void;
290
297
  clear(): void;
@@ -461,6 +468,7 @@ declare function getDefaultState<TData, TError, TVariables, TContext>(): Mutatio
461
468
 
462
469
  interface RetryerConfig<TData = unknown, TError = DefaultError> {
463
470
  fn: () => TData | Promise<TData>;
471
+ initialPromise?: Promise<TData>;
464
472
  abort?: () => void;
465
473
  onError?: (error: TError) => void;
466
474
  onSuccess?: (data: TData) => void;
@@ -507,6 +515,7 @@ type DataTag<TType, TValue> = TType & {
507
515
  [dataTagSymbol]: TValue;
508
516
  };
509
517
  type QueryFunction<T = unknown, TQueryKey extends QueryKey = QueryKey, TPageParam = never> = (context: QueryFunctionContext<TQueryKey, TPageParam>) => T | Promise<T>;
518
+ type StaleTime<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey> = number | ((query: Query<TQueryFnData, TError, TData, TQueryKey>) => number);
510
519
  type QueryPersister<T = unknown, TQueryKey extends QueryKey = QueryKey, TPageParam = never> = [TPageParam] extends [never] ? (queryFn: QueryFunction<T, TQueryKey, never>, context: QueryFunctionContext<TQueryKey>, query: Query) => T | Promise<T> : (queryFn: QueryFunction<T, TQueryKey, TPageParam>, context: QueryFunctionContext<TQueryKey>, query: Query) => T | Promise<T>;
511
520
  type QueryFunctionContext<TQueryKey extends QueryKey = QueryKey, TPageParam = never> = [TPageParam] extends [never] ? {
512
521
  queryKey: TQueryKey;
@@ -613,8 +622,9 @@ interface QueryObserverOptions<TQueryFnData = unknown, TError = DefaultError, TD
613
622
  /**
614
623
  * The time in milliseconds after data is considered stale.
615
624
  * If set to `Infinity`, the data will never be considered stale.
625
+ * If set to a function, the function will be executed with the query to compute a `staleTime`.
616
626
  */
617
- staleTime?: number;
627
+ staleTime?: StaleTime<TQueryFnData, TError, TQueryData, TQueryKey>;
618
628
  /**
619
629
  * If set to a number, the query will continuously refetch at this frequency in milliseconds.
620
630
  * If set to a function, the function will be executed with the latest data and query to compute a frequency
@@ -700,7 +710,7 @@ interface FetchQueryOptions<TQueryFnData = unknown, TError = DefaultError, TData
700
710
  * The time in milliseconds after data is considered stale.
701
711
  * If the data is fresh it will be returned from the cache.
702
712
  */
703
- staleTime?: number;
713
+ staleTime?: StaleTime<TQueryFnData, TError, TData, TQueryKey>;
704
714
  }
705
715
  interface EnsureQueryDataOptions<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, TPageParam = never> extends FetchQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam> {
706
716
  revalidateIfStale?: boolean;
@@ -1173,6 +1183,8 @@ interface QueryClientConfig {
1173
1183
  interface DefaultOptions<TError = DefaultError> {
1174
1184
  queries?: OmitKeyof<QueryObserverOptions<unknown, TError>, 'suspense' | 'queryKey'>;
1175
1185
  mutations?: MutationObserverOptions<unknown, TError, unknown, unknown>;
1186
+ hydrate?: HydrateOptions['defaultOptions'];
1187
+ dehydrate?: DehydrateOptions;
1176
1188
  }
1177
1189
  interface CancelOptions {
1178
1190
  revert?: boolean;
@@ -1186,4 +1198,36 @@ interface NotifyEvent {
1186
1198
  type: NotifyEventType;
1187
1199
  }
1188
1200
 
1189
- export { type DefaultedInfiniteQueryObserverOptions as $, type QueryKeyHashFunction as A, type GetNextPageParamFunction as B, CancelledError as C, type DefaultError as D, type InfiniteData as E, type QueryMeta as F, type GetPreviousPageParamFunction as G, type NetworkMode as H, type InitialDataFunction as I, type NotifyOnChangeProps as J, type QueryOptions as K, type InitialPageParam as L, MutationCache as M, type NoInfer as N, type OmitKeyof as O, type PlaceholderDataFunction as P, QueryCache as Q, type Register as R, type SkipToken as S, type InfiniteQueryPageParamsOptions as T, type Updater as U, type ThrowOnError as V, type QueryObserverOptions as W, type WithRequired as X, type Optional as Y, type DefaultedQueryObserverOptions as Z, type InfiniteQueryObserverOptions as _, type QueryCacheNotifyEvent as a, isValidTimeout as a$, type FetchQueryOptions as a0, type EnsureQueryDataOptions as a1, type FetchInfiniteQueryOptions as a2, type ResultOptions as a3, type RefetchOptions as a4, type InvalidateQueryFilters as a5, type RefetchQueryFilters as a6, type InvalidateOptions as a7, type ResetOptions as a8, type FetchNextPageOptions as a9, type MutateOptions as aA, type MutateFunction as aB, type MutationObserverBaseResult as aC, type MutationObserverIdleResult as aD, type MutationObserverLoadingResult as aE, type MutationObserverErrorResult as aF, type MutationObserverSuccessResult as aG, type MutationObserverResult as aH, type QueryClientConfig as aI, type DefaultOptions as aJ, type CancelOptions as aK, type SetDataOptions as aL, type NotifyEventType as aM, type NotifyEvent as aN, type QueryBehavior as aO, type ObserverFetchOptions as aP, type NotifyOptions as aQ, type FetchContext as aR, type FetchDirection as aS, type FetchMeta as aT, type FetchOptions as aU, type Action$1 as aV, type SetStateOptions as aW, fetchState as aX, type QueryTypeFilter as aY, noop as aZ, functionalUpdate as a_, type FetchPreviousPageOptions as aa, type QueryStatus as ab, type FetchStatus as ac, type QueryObserverBaseResult as ad, type QueryObserverPendingResult as ae, type QueryObserverLoadingResult as af, type QueryObserverLoadingErrorResult as ag, type QueryObserverRefetchErrorResult as ah, type QueryObserverSuccessResult as ai, type DefinedQueryObserverResult as aj, type QueryObserverResult as ak, type InfiniteQueryObserverBaseResult as al, type InfiniteQueryObserverPendingResult as am, type InfiniteQueryObserverLoadingResult as an, type InfiniteQueryObserverLoadingErrorResult as ao, type InfiniteQueryObserverRefetchErrorResult as ap, type InfiniteQueryObserverSuccessResult as aq, type DefinedInfiniteQueryObserverResult as ar, type InfiniteQueryObserverResult as as, type MutationKey as at, type MutationStatus as au, type MutationScope as av, type MutationMeta as aw, type MutationFunction as ax, type MutationOptions as ay, type MutationObserverOptions as az, QueryClient as b, timeUntilStale as b0, hashQueryKeyByOptions as b1, partialMatchKey as b2, shallowEqualObjects as b3, isPlainArray as b4, isPlainObject as b5, sleep as b6, replaceData as b7, addToEnd as b8, addToStart as b9, type QueryStore as ba, type Action as bb, getDefaultState as bc, type Retryer as bd, type RetryValue as be, type RetryDelayValue as bf, canFetch as bg, createRetryer as bh, QueryObserver as c, type MutationCacheNotifyEvent as d, MutationObserver as e, matchMutation as f, type MutationFilters as g, hashKey as h, isServer as i, type QueryFilters as j, keepPreviousData as k, isCancelledError as l, matchQuery as m, type QueryState as n, Query as o, Mutation as p, type MutationState as q, replaceEqualDeep as r, skipToken as s, type QueryKey as t, dataTagSymbol as u, type DataTag as v, type QueryFunction as w, type QueryPersister as x, type QueryFunctionContext as y, type QueriesPlaceholderDataFunction as z };
1201
+ interface DehydrateOptions {
1202
+ shouldDehydrateMutation?: (mutation: Mutation) => boolean;
1203
+ shouldDehydrateQuery?: (query: Query) => boolean;
1204
+ }
1205
+ interface HydrateOptions {
1206
+ defaultOptions?: {
1207
+ queries?: QueryOptions;
1208
+ mutations?: MutationOptions<unknown, DefaultError, unknown, unknown>;
1209
+ };
1210
+ }
1211
+ interface DehydratedMutation {
1212
+ mutationKey?: MutationKey;
1213
+ state: MutationState;
1214
+ meta?: MutationMeta;
1215
+ scope?: MutationScope;
1216
+ }
1217
+ interface DehydratedQuery {
1218
+ queryHash: string;
1219
+ queryKey: QueryKey;
1220
+ state: QueryState;
1221
+ promise?: Promise<unknown>;
1222
+ meta?: QueryMeta;
1223
+ }
1224
+ interface DehydratedState {
1225
+ mutations: Array<DehydratedMutation>;
1226
+ queries: Array<DehydratedQuery>;
1227
+ }
1228
+ declare function defaultShouldDehydrateMutation(mutation: Mutation): boolean;
1229
+ declare function defaultShouldDehydrateQuery(query: Query): boolean;
1230
+ declare function dehydrate(client: QueryClient, options?: DehydrateOptions): DehydratedState;
1231
+ declare function hydrate(client: QueryClient, dehydratedState: unknown, options?: HydrateOptions): void;
1232
+
1233
+ export { type InitialPageParam as $, dataTagSymbol as A, type DataTag as B, CancelledError as C, type DehydrateOptions as D, type QueryFunction as E, type StaleTime as F, type QueryPersister as G, type HydrateOptions as H, type QueryFunctionContext as I, type InitialDataFunction as J, type QueriesPlaceholderDataFunction as K, type QueryKeyHashFunction as L, MutationCache as M, type NoInfer as N, type OmitKeyof as O, type PlaceholderDataFunction as P, QueryCache as Q, type Register as R, type SkipToken as S, type GetPreviousPageParamFunction as T, type Updater as U, type GetNextPageParamFunction as V, type InfiniteData as W, type QueryMeta as X, type NetworkMode as Y, type NotifyOnChangeProps as Z, type QueryOptions as _, type QueryCacheNotifyEvent as a, type FetchMeta as a$, type InfiniteQueryPageParamsOptions as a0, type ThrowOnError as a1, type QueryObserverOptions as a2, type WithRequired as a3, type Optional as a4, type DefaultedQueryObserverOptions as a5, type InfiniteQueryObserverOptions as a6, type DefaultedInfiniteQueryObserverOptions as a7, type FetchQueryOptions as a8, type EnsureQueryDataOptions as a9, type InfiniteQueryObserverResult as aA, type MutationKey as aB, type MutationStatus as aC, type MutationScope as aD, type MutationMeta as aE, type MutationFunction as aF, type MutationOptions as aG, type MutationObserverOptions as aH, type MutateOptions as aI, type MutateFunction as aJ, type MutationObserverBaseResult as aK, type MutationObserverIdleResult as aL, type MutationObserverLoadingResult as aM, type MutationObserverErrorResult as aN, type MutationObserverSuccessResult as aO, type MutationObserverResult as aP, type QueryClientConfig as aQ, type DefaultOptions as aR, type CancelOptions as aS, type SetDataOptions as aT, type NotifyEventType as aU, type NotifyEvent as aV, type QueryBehavior as aW, type ObserverFetchOptions as aX, type NotifyOptions as aY, type FetchContext as aZ, type FetchDirection as a_, type FetchInfiniteQueryOptions as aa, type ResultOptions as ab, type RefetchOptions as ac, type InvalidateQueryFilters as ad, type RefetchQueryFilters as ae, type InvalidateOptions as af, type ResetOptions as ag, type FetchNextPageOptions as ah, type FetchPreviousPageOptions as ai, type QueryStatus as aj, type FetchStatus as ak, type QueryObserverBaseResult as al, type QueryObserverPendingResult as am, type QueryObserverLoadingResult as an, type QueryObserverLoadingErrorResult as ao, type QueryObserverRefetchErrorResult as ap, type QueryObserverSuccessResult as aq, type DefinedQueryObserverResult as ar, type QueryObserverResult as as, type InfiniteQueryObserverBaseResult as at, type InfiniteQueryObserverPendingResult as au, type InfiniteQueryObserverLoadingResult as av, type InfiniteQueryObserverLoadingErrorResult as aw, type InfiniteQueryObserverRefetchErrorResult as ax, type InfiniteQueryObserverSuccessResult as ay, type DefinedInfiniteQueryObserverResult as az, QueryClient as b, type FetchOptions as b0, type Action$1 as b1, type SetStateOptions as b2, fetchState as b3, type QueryTypeFilter as b4, noop as b5, functionalUpdate as b6, isValidTimeout as b7, timeUntilStale as b8, resolveStaleTime as b9, hashQueryKeyByOptions as ba, partialMatchKey as bb, shallowEqualObjects as bc, isPlainArray as bd, isPlainObject as be, sleep as bf, replaceData as bg, addToEnd as bh, addToStart as bi, ensureQueryFn as bj, type QueryStore as bk, type Action as bl, getDefaultState as bm, type Retryer as bn, type RetryValue as bo, type RetryDelayValue as bp, canFetch as bq, createRetryer as br, QueryObserver as c, type MutationCacheNotifyEvent as d, MutationObserver as e, matchMutation as f, type MutationFilters as g, hashKey as h, isServer as i, type QueryFilters as j, keepPreviousData as k, isCancelledError as l, matchQuery as m, dehydrate as n, hydrate as o, defaultShouldDehydrateQuery as p, defaultShouldDehydrateMutation as q, replaceEqualDeep as r, skipToken as s, type QueryState as t, Query as u, Mutation as v, type MutationState as w, type DehydratedState as x, type DefaultError as y, type QueryKey as z };
@@ -35,10 +35,21 @@ function dehydrateMutation(mutation) {
35
35
  };
36
36
  }
37
37
  function dehydrateQuery(query) {
38
+ var _a;
38
39
  return {
39
40
  state: query.state,
40
41
  queryKey: query.queryKey,
41
42
  queryHash: query.queryHash,
43
+ ...query.state.status === "pending" && {
44
+ promise: (_a = query.promise) == null ? void 0 : _a.catch((error) => {
45
+ if (process.env.NODE_ENV !== "production") {
46
+ console.error(
47
+ `A query that was dehydrated as pending ended up rejecting. [${query.queryHash}]: ${error}; The error will be redacted in production builds`
48
+ );
49
+ }
50
+ return Promise.reject(new Error("redacted"));
51
+ })
52
+ },
42
53
  ...query.meta && { meta: query.meta }
43
54
  };
44
55
  }
@@ -49,11 +60,12 @@ function defaultShouldDehydrateQuery(query) {
49
60
  return query.state.status === "success";
50
61
  }
51
62
  function dehydrate(client, options = {}) {
52
- const filterMutation = options.shouldDehydrateMutation ?? defaultShouldDehydrateMutation;
63
+ var _a, _b;
64
+ const filterMutation = options.shouldDehydrateMutation ?? ((_a = client.getDefaultOptions().dehydrate) == null ? void 0 : _a.shouldDehydrateMutation) ?? defaultShouldDehydrateMutation;
53
65
  const mutations = client.getMutationCache().getAll().flatMap(
54
66
  (mutation) => filterMutation(mutation) ? [dehydrateMutation(mutation)] : []
55
67
  );
56
- const filterQuery = options.shouldDehydrateQuery ?? defaultShouldDehydrateQuery;
68
+ const filterQuery = options.shouldDehydrateQuery ?? ((_b = client.getDefaultOptions().dehydrate) == null ? void 0 : _b.shouldDehydrateQuery) ?? defaultShouldDehydrateQuery;
57
69
  const queries = client.getQueryCache().getAll().flatMap((query) => filterQuery(query) ? [dehydrateQuery(query)] : []);
58
70
  return { mutations, queries };
59
71
  }
@@ -66,41 +78,46 @@ function hydrate(client, dehydratedState, options) {
66
78
  const mutations = dehydratedState.mutations || [];
67
79
  const queries = dehydratedState.queries || [];
68
80
  mutations.forEach(({ state, ...mutationOptions }) => {
69
- var _a;
81
+ var _a, _b;
70
82
  mutationCache.build(
71
83
  client,
72
84
  {
73
- ...(_a = options == null ? void 0 : options.defaultOptions) == null ? void 0 : _a.mutations,
85
+ ...(_a = client.getDefaultOptions().hydrate) == null ? void 0 : _a.mutations,
86
+ ...(_b = options == null ? void 0 : options.defaultOptions) == null ? void 0 : _b.mutations,
74
87
  ...mutationOptions
75
88
  },
76
89
  state
77
90
  );
78
91
  });
79
- queries.forEach(({ queryKey, state, queryHash, meta }) => {
80
- var _a;
81
- const query = queryCache.get(queryHash);
92
+ queries.forEach(({ queryKey, state, queryHash, meta, promise }) => {
93
+ var _a, _b;
94
+ let query = queryCache.get(queryHash);
82
95
  if (query) {
83
96
  if (query.state.dataUpdatedAt < state.dataUpdatedAt) {
84
97
  const { fetchStatus: _ignored, ...dehydratedQueryState } = state;
85
98
  query.setState(dehydratedQueryState);
86
99
  }
87
- return;
100
+ } else {
101
+ query = queryCache.build(
102
+ client,
103
+ {
104
+ ...(_a = client.getDefaultOptions().hydrate) == null ? void 0 : _a.queries,
105
+ ...(_b = options == null ? void 0 : options.defaultOptions) == null ? void 0 : _b.queries,
106
+ queryKey,
107
+ queryHash,
108
+ meta
109
+ },
110
+ // Reset fetch status to idle to avoid
111
+ // query being stuck in fetching state upon hydration
112
+ {
113
+ ...state,
114
+ fetchStatus: "idle"
115
+ }
116
+ );
117
+ }
118
+ if (promise) {
119
+ void query.fetch(void 0, { initialPromise: promise });
88
120
  }
89
- queryCache.build(
90
- client,
91
- {
92
- ...(_a = options == null ? void 0 : options.defaultOptions) == null ? void 0 : _a.queries,
93
- queryKey,
94
- queryHash,
95
- meta
96
- },
97
- // Reset fetch status to idle to avoid
98
- // query being stuck in fetching state upon hydration
99
- {
100
- ...state,
101
- fetchStatus: "idle"
102
- }
103
- );
104
121
  });
105
122
  }
106
123
  // Annotate the CommonJS export names for ESM import in node:
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/hydration.ts"],"sourcesContent":["import type {\n DefaultError,\n MutationKey,\n MutationMeta,\n MutationOptions,\n MutationScope,\n QueryKey,\n QueryMeta,\n QueryOptions,\n} from './types'\nimport type { QueryClient } from './queryClient'\nimport type { Query, QueryState } from './query'\nimport type { Mutation, MutationState } from './mutation'\n\n// TYPES\n\nexport interface DehydrateOptions {\n shouldDehydrateMutation?: (mutation: Mutation) => boolean\n shouldDehydrateQuery?: (query: Query) => boolean\n}\n\nexport interface HydrateOptions {\n defaultOptions?: {\n queries?: QueryOptions\n mutations?: MutationOptions<unknown, DefaultError, unknown, unknown>\n }\n}\n\ninterface DehydratedMutation {\n mutationKey?: MutationKey\n state: MutationState\n meta?: MutationMeta\n scope?: MutationScope\n}\n\ninterface DehydratedQuery {\n queryHash: string\n queryKey: QueryKey\n state: QueryState\n meta?: QueryMeta\n}\n\nexport interface DehydratedState {\n mutations: Array<DehydratedMutation>\n queries: Array<DehydratedQuery>\n}\n\n// FUNCTIONS\n\nfunction dehydrateMutation(mutation: Mutation): DehydratedMutation {\n return {\n mutationKey: mutation.options.mutationKey,\n state: mutation.state,\n ...(mutation.options.scope && { scope: mutation.options.scope }),\n ...(mutation.meta && { meta: mutation.meta }),\n }\n}\n\n// Most config is not dehydrated but instead meant to configure again when\n// consuming the de/rehydrated data, typically with useQuery on the client.\n// Sometimes it might make sense to prefetch data on the server and include\n// in the html-payload, but not consume it on the initial render.\nfunction dehydrateQuery(query: Query): DehydratedQuery {\n return {\n state: query.state,\n queryKey: query.queryKey,\n queryHash: query.queryHash,\n ...(query.meta && { meta: query.meta }),\n }\n}\n\nexport function defaultShouldDehydrateMutation(mutation: Mutation) {\n return mutation.state.isPaused\n}\n\nexport function defaultShouldDehydrateQuery(query: Query) {\n return query.state.status === 'success'\n}\n\nexport function dehydrate(\n client: QueryClient,\n options: DehydrateOptions = {},\n): DehydratedState {\n const filterMutation =\n options.shouldDehydrateMutation ?? defaultShouldDehydrateMutation\n\n const mutations = client\n .getMutationCache()\n .getAll()\n .flatMap((mutation) =>\n filterMutation(mutation) ? [dehydrateMutation(mutation)] : [],\n )\n\n const filterQuery =\n options.shouldDehydrateQuery ?? defaultShouldDehydrateQuery\n\n const queries = client\n .getQueryCache()\n .getAll()\n .flatMap((query) => (filterQuery(query) ? [dehydrateQuery(query)] : []))\n\n return { mutations, queries }\n}\n\nexport function hydrate(\n client: QueryClient,\n dehydratedState: unknown,\n options?: HydrateOptions,\n): void {\n if (typeof dehydratedState !== 'object' || dehydratedState === null) {\n return\n }\n\n const mutationCache = client.getMutationCache()\n const queryCache = client.getQueryCache()\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const mutations = (dehydratedState as DehydratedState).mutations || []\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const queries = (dehydratedState as DehydratedState).queries || []\n\n mutations.forEach(({ state, ...mutationOptions }) => {\n mutationCache.build(\n client,\n {\n ...options?.defaultOptions?.mutations,\n ...mutationOptions,\n },\n state,\n )\n })\n\n queries.forEach(({ queryKey, state, queryHash, meta }) => {\n const query = queryCache.get(queryHash)\n\n // Do not hydrate if an existing query exists with newer data\n if (query) {\n if (query.state.dataUpdatedAt < state.dataUpdatedAt) {\n // omit fetchStatus from dehydrated state\n // so that query stays in its current fetchStatus\n const { fetchStatus: _ignored, ...dehydratedQueryState } = state\n query.setState(dehydratedQueryState)\n }\n return\n }\n\n // Restore query\n queryCache.build(\n client,\n {\n ...options?.defaultOptions?.queries,\n queryKey,\n queryHash,\n meta,\n },\n // Reset fetch status to idle to avoid\n // query being stuck in fetching state upon hydration\n {\n ...state,\n fetchStatus: 'idle',\n },\n )\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAiDA,SAAS,kBAAkB,UAAwC;AACjE,SAAO;AAAA,IACL,aAAa,SAAS,QAAQ;AAAA,IAC9B,OAAO,SAAS;AAAA,IAChB,GAAI,SAAS,QAAQ,SAAS,EAAE,OAAO,SAAS,QAAQ,MAAM;AAAA,IAC9D,GAAI,SAAS,QAAQ,EAAE,MAAM,SAAS,KAAK;AAAA,EAC7C;AACF;AAMA,SAAS,eAAe,OAA+B;AACrD,SAAO;AAAA,IACL,OAAO,MAAM;AAAA,IACb,UAAU,MAAM;AAAA,IAChB,WAAW,MAAM;AAAA,IACjB,GAAI,MAAM,QAAQ,EAAE,MAAM,MAAM,KAAK;AAAA,EACvC;AACF;AAEO,SAAS,+BAA+B,UAAoB;AACjE,SAAO,SAAS,MAAM;AACxB;AAEO,SAAS,4BAA4B,OAAc;AACxD,SAAO,MAAM,MAAM,WAAW;AAChC;AAEO,SAAS,UACd,QACA,UAA4B,CAAC,GACZ;AACjB,QAAM,iBACJ,QAAQ,2BAA2B;AAErC,QAAM,YAAY,OACf,iBAAiB,EACjB,OAAO,EACP;AAAA,IAAQ,CAAC,aACR,eAAe,QAAQ,IAAI,CAAC,kBAAkB,QAAQ,CAAC,IAAI,CAAC;AAAA,EAC9D;AAEF,QAAM,cACJ,QAAQ,wBAAwB;AAElC,QAAM,UAAU,OACb,cAAc,EACd,OAAO,EACP,QAAQ,CAAC,UAAW,YAAY,KAAK,IAAI,CAAC,eAAe,KAAK,CAAC,IAAI,CAAC,CAAE;AAEzE,SAAO,EAAE,WAAW,QAAQ;AAC9B;AAEO,SAAS,QACd,QACA,iBACA,SACM;AACN,MAAI,OAAO,oBAAoB,YAAY,oBAAoB,MAAM;AACnE;AAAA,EACF;AAEA,QAAM,gBAAgB,OAAO,iBAAiB;AAC9C,QAAM,aAAa,OAAO,cAAc;AAGxC,QAAM,YAAa,gBAAoC,aAAa,CAAC;AAErE,QAAM,UAAW,gBAAoC,WAAW,CAAC;AAEjE,YAAU,QAAQ,CAAC,EAAE,OAAO,GAAG,gBAAgB,MAAM;AAzHvD;AA0HI,kBAAc;AAAA,MACZ;AAAA,MACA;AAAA,QACE,IAAG,wCAAS,mBAAT,mBAAyB;AAAA,QAC5B,GAAG;AAAA,MACL;AAAA,MACA;AAAA,IACF;AAAA,EACF,CAAC;AAED,UAAQ,QAAQ,CAAC,EAAE,UAAU,OAAO,WAAW,KAAK,MAAM;AApI5D;AAqII,UAAM,QAAQ,WAAW,IAAI,SAAS;AAGtC,QAAI,OAAO;AACT,UAAI,MAAM,MAAM,gBAAgB,MAAM,eAAe;AAGnD,cAAM,EAAE,aAAa,UAAU,GAAG,qBAAqB,IAAI;AAC3D,cAAM,SAAS,oBAAoB;AAAA,MACrC;AACA;AAAA,IACF;AAGA,eAAW;AAAA,MACT;AAAA,MACA;AAAA,QACE,IAAG,wCAAS,mBAAT,mBAAyB;AAAA,QAC5B;AAAA,QACA;AAAA,QACA;AAAA,MACF;AAAA;AAAA;AAAA,MAGA;AAAA,QACE,GAAG;AAAA,QACH,aAAa;AAAA,MACf;AAAA,IACF;AAAA,EACF,CAAC;AACH;","names":[]}
1
+ {"version":3,"sources":["../../src/hydration.ts"],"sourcesContent":["import type {\n DefaultError,\n MutationKey,\n MutationMeta,\n MutationOptions,\n MutationScope,\n QueryKey,\n QueryMeta,\n QueryOptions,\n} from './types'\nimport type { QueryClient } from './queryClient'\nimport type { Query, QueryState } from './query'\nimport type { Mutation, MutationState } from './mutation'\n\n// TYPES\n\nexport interface DehydrateOptions {\n shouldDehydrateMutation?: (mutation: Mutation) => boolean\n shouldDehydrateQuery?: (query: Query) => boolean\n}\n\nexport interface HydrateOptions {\n defaultOptions?: {\n queries?: QueryOptions\n mutations?: MutationOptions<unknown, DefaultError, unknown, unknown>\n }\n}\n\ninterface DehydratedMutation {\n mutationKey?: MutationKey\n state: MutationState\n meta?: MutationMeta\n scope?: MutationScope\n}\n\ninterface DehydratedQuery {\n queryHash: string\n queryKey: QueryKey\n state: QueryState\n promise?: Promise<unknown>\n meta?: QueryMeta\n}\n\nexport interface DehydratedState {\n mutations: Array<DehydratedMutation>\n queries: Array<DehydratedQuery>\n}\n\n// FUNCTIONS\n\nfunction dehydrateMutation(mutation: Mutation): DehydratedMutation {\n return {\n mutationKey: mutation.options.mutationKey,\n state: mutation.state,\n ...(mutation.options.scope && { scope: mutation.options.scope }),\n ...(mutation.meta && { meta: mutation.meta }),\n }\n}\n\n// Most config is not dehydrated but instead meant to configure again when\n// consuming the de/rehydrated data, typically with useQuery on the client.\n// Sometimes it might make sense to prefetch data on the server and include\n// in the html-payload, but not consume it on the initial render.\nfunction dehydrateQuery(query: Query): DehydratedQuery {\n return {\n state: query.state,\n queryKey: query.queryKey,\n queryHash: query.queryHash,\n ...(query.state.status === 'pending' && {\n promise: query.promise?.catch((error) => {\n if (process.env.NODE_ENV !== 'production') {\n console.error(\n `A query that was dehydrated as pending ended up rejecting. [${query.queryHash}]: ${error}; The error will be redacted in production builds`,\n )\n }\n return Promise.reject(new Error('redacted'))\n }),\n }),\n ...(query.meta && { meta: query.meta }),\n }\n}\n\nexport function defaultShouldDehydrateMutation(mutation: Mutation) {\n return mutation.state.isPaused\n}\n\nexport function defaultShouldDehydrateQuery(query: Query) {\n return query.state.status === 'success'\n}\n\nexport function dehydrate(\n client: QueryClient,\n options: DehydrateOptions = {},\n): DehydratedState {\n const filterMutation =\n options.shouldDehydrateMutation ??\n client.getDefaultOptions().dehydrate?.shouldDehydrateMutation ??\n defaultShouldDehydrateMutation\n\n const mutations = client\n .getMutationCache()\n .getAll()\n .flatMap((mutation) =>\n filterMutation(mutation) ? [dehydrateMutation(mutation)] : [],\n )\n\n const filterQuery =\n options.shouldDehydrateQuery ??\n client.getDefaultOptions().dehydrate?.shouldDehydrateQuery ??\n defaultShouldDehydrateQuery\n\n const queries = client\n .getQueryCache()\n .getAll()\n .flatMap((query) => (filterQuery(query) ? [dehydrateQuery(query)] : []))\n\n return { mutations, queries }\n}\n\nexport function hydrate(\n client: QueryClient,\n dehydratedState: unknown,\n options?: HydrateOptions,\n): void {\n if (typeof dehydratedState !== 'object' || dehydratedState === null) {\n return\n }\n\n const mutationCache = client.getMutationCache()\n const queryCache = client.getQueryCache()\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const mutations = (dehydratedState as DehydratedState).mutations || []\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const queries = (dehydratedState as DehydratedState).queries || []\n\n mutations.forEach(({ state, ...mutationOptions }) => {\n mutationCache.build(\n client,\n {\n ...client.getDefaultOptions().hydrate?.mutations,\n ...options?.defaultOptions?.mutations,\n ...mutationOptions,\n },\n state,\n )\n })\n\n queries.forEach(({ queryKey, state, queryHash, meta, promise }) => {\n let query = queryCache.get(queryHash)\n\n // Do not hydrate if an existing query exists with newer data\n if (query) {\n if (query.state.dataUpdatedAt < state.dataUpdatedAt) {\n // omit fetchStatus from dehydrated state\n // so that query stays in its current fetchStatus\n const { fetchStatus: _ignored, ...dehydratedQueryState } = state\n query.setState(dehydratedQueryState)\n }\n } else {\n // Restore query\n query = queryCache.build(\n client,\n {\n ...client.getDefaultOptions().hydrate?.queries,\n ...options?.defaultOptions?.queries,\n queryKey,\n queryHash,\n meta,\n },\n // Reset fetch status to idle to avoid\n // query being stuck in fetching state upon hydration\n {\n ...state,\n fetchStatus: 'idle',\n },\n )\n }\n\n if (promise) {\n // this doesn't actually fetch - it just creates a retryer\n // which will re-use the passed `initialPromise`\n void query.fetch(undefined, { initialPromise: promise })\n }\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAkDA,SAAS,kBAAkB,UAAwC;AACjE,SAAO;AAAA,IACL,aAAa,SAAS,QAAQ;AAAA,IAC9B,OAAO,SAAS;AAAA,IAChB,GAAI,SAAS,QAAQ,SAAS,EAAE,OAAO,SAAS,QAAQ,MAAM;AAAA,IAC9D,GAAI,SAAS,QAAQ,EAAE,MAAM,SAAS,KAAK;AAAA,EAC7C;AACF;AAMA,SAAS,eAAe,OAA+B;AA/DvD;AAgEE,SAAO;AAAA,IACL,OAAO,MAAM;AAAA,IACb,UAAU,MAAM;AAAA,IAChB,WAAW,MAAM;AAAA,IACjB,GAAI,MAAM,MAAM,WAAW,aAAa;AAAA,MACtC,UAAS,WAAM,YAAN,mBAAe,MAAM,CAAC,UAAU;AACvC,YAAI,QAAQ,IAAI,aAAa,cAAc;AACzC,kBAAQ;AAAA,YACN,+DAA+D,MAAM,SAAS,MAAM,KAAK;AAAA,UAC3F;AAAA,QACF;AACA,eAAO,QAAQ,OAAO,IAAI,MAAM,UAAU,CAAC;AAAA,MAC7C;AAAA,IACF;AAAA,IACA,GAAI,MAAM,QAAQ,EAAE,MAAM,MAAM,KAAK;AAAA,EACvC;AACF;AAEO,SAAS,+BAA+B,UAAoB;AACjE,SAAO,SAAS,MAAM;AACxB;AAEO,SAAS,4BAA4B,OAAc;AACxD,SAAO,MAAM,MAAM,WAAW;AAChC;AAEO,SAAS,UACd,QACA,UAA4B,CAAC,GACZ;AA7FnB;AA8FE,QAAM,iBACJ,QAAQ,6BACR,YAAO,kBAAkB,EAAE,cAA3B,mBAAsC,4BACtC;AAEF,QAAM,YAAY,OACf,iBAAiB,EACjB,OAAO,EACP;AAAA,IAAQ,CAAC,aACR,eAAe,QAAQ,IAAI,CAAC,kBAAkB,QAAQ,CAAC,IAAI,CAAC;AAAA,EAC9D;AAEF,QAAM,cACJ,QAAQ,0BACR,YAAO,kBAAkB,EAAE,cAA3B,mBAAsC,yBACtC;AAEF,QAAM,UAAU,OACb,cAAc,EACd,OAAO,EACP,QAAQ,CAAC,UAAW,YAAY,KAAK,IAAI,CAAC,eAAe,KAAK,CAAC,IAAI,CAAC,CAAE;AAEzE,SAAO,EAAE,WAAW,QAAQ;AAC9B;AAEO,SAAS,QACd,QACA,iBACA,SACM;AACN,MAAI,OAAO,oBAAoB,YAAY,oBAAoB,MAAM;AACnE;AAAA,EACF;AAEA,QAAM,gBAAgB,OAAO,iBAAiB;AAC9C,QAAM,aAAa,OAAO,cAAc;AAGxC,QAAM,YAAa,gBAAoC,aAAa,CAAC;AAErE,QAAM,UAAW,gBAAoC,WAAW,CAAC;AAEjE,YAAU,QAAQ,CAAC,EAAE,OAAO,GAAG,gBAAgB,MAAM;AAxIvD;AAyII,kBAAc;AAAA,MACZ;AAAA,MACA;AAAA,QACE,IAAG,YAAO,kBAAkB,EAAE,YAA3B,mBAAoC;AAAA,QACvC,IAAG,wCAAS,mBAAT,mBAAyB;AAAA,QAC5B,GAAG;AAAA,MACL;AAAA,MACA;AAAA,IACF;AAAA,EACF,CAAC;AAED,UAAQ,QAAQ,CAAC,EAAE,UAAU,OAAO,WAAW,MAAM,QAAQ,MAAM;AApJrE;AAqJI,QAAI,QAAQ,WAAW,IAAI,SAAS;AAGpC,QAAI,OAAO;AACT,UAAI,MAAM,MAAM,gBAAgB,MAAM,eAAe;AAGnD,cAAM,EAAE,aAAa,UAAU,GAAG,qBAAqB,IAAI;AAC3D,cAAM,SAAS,oBAAoB;AAAA,MACrC;AAAA,IACF,OAAO;AAEL,cAAQ,WAAW;AAAA,QACjB;AAAA,QACA;AAAA,UACE,IAAG,YAAO,kBAAkB,EAAE,YAA3B,mBAAoC;AAAA,UACvC,IAAG,wCAAS,mBAAT,mBAAyB;AAAA,UAC5B;AAAA,UACA;AAAA,UACA;AAAA,QACF;AAAA;AAAA;AAAA,QAGA;AAAA,UACE,GAAG;AAAA,UACH,aAAa;AAAA,QACf;AAAA,MACF;AAAA,IACF;AAEA,QAAI,SAAS;AAGX,WAAK,MAAM,MAAM,QAAW,EAAE,gBAAgB,QAAQ,CAAC;AAAA,IACzD;AAAA,EACF,CAAC;AACH;","names":[]}
@@ -1,36 +1,3 @@
1
- import { p as Mutation, o as Query, K as QueryOptions, ay as MutationOptions, D as DefaultError, b as QueryClient, at as MutationKey, q as MutationState, aw as MutationMeta, av as MutationScope, t as QueryKey, n as QueryState, F as QueryMeta } from './types-BvcshvE9.cjs';
1
+ export { D as DehydrateOptions, x as DehydratedState, H as HydrateOptions, q as defaultShouldDehydrateMutation, p as defaultShouldDehydrateQuery, n as dehydrate, o as hydrate } from './hydration-XP7CH-6g.cjs';
2
2
  import './removable.cjs';
3
3
  import './subscribable.cjs';
4
-
5
- interface DehydrateOptions {
6
- shouldDehydrateMutation?: (mutation: Mutation) => boolean;
7
- shouldDehydrateQuery?: (query: Query) => boolean;
8
- }
9
- interface HydrateOptions {
10
- defaultOptions?: {
11
- queries?: QueryOptions;
12
- mutations?: MutationOptions<unknown, DefaultError, unknown, unknown>;
13
- };
14
- }
15
- interface DehydratedMutation {
16
- mutationKey?: MutationKey;
17
- state: MutationState;
18
- meta?: MutationMeta;
19
- scope?: MutationScope;
20
- }
21
- interface DehydratedQuery {
22
- queryHash: string;
23
- queryKey: QueryKey;
24
- state: QueryState;
25
- meta?: QueryMeta;
26
- }
27
- interface DehydratedState {
28
- mutations: Array<DehydratedMutation>;
29
- queries: Array<DehydratedQuery>;
30
- }
31
- declare function defaultShouldDehydrateMutation(mutation: Mutation): boolean;
32
- declare function defaultShouldDehydrateQuery(query: Query): boolean;
33
- declare function dehydrate(client: QueryClient, options?: DehydrateOptions): DehydratedState;
34
- declare function hydrate(client: QueryClient, dehydratedState: unknown, options?: HydrateOptions): void;
35
-
36
- export { type DehydrateOptions, type DehydratedState, type HydrateOptions, defaultShouldDehydrateMutation, defaultShouldDehydrateQuery, dehydrate, hydrate };
@@ -1,36 +1,3 @@
1
- import { p as Mutation, o as Query, K as QueryOptions, ay as MutationOptions, D as DefaultError, b as QueryClient, at as MutationKey, q as MutationState, aw as MutationMeta, av as MutationScope, t as QueryKey, n as QueryState, F as QueryMeta } from './types-BtrVwz9w.js';
1
+ export { D as DehydrateOptions, x as DehydratedState, H as HydrateOptions, q as defaultShouldDehydrateMutation, p as defaultShouldDehydrateQuery, n as dehydrate, o as hydrate } from './hydration-DJZYTIMr.js';
2
2
  import './removable.js';
3
3
  import './subscribable.js';
4
-
5
- interface DehydrateOptions {
6
- shouldDehydrateMutation?: (mutation: Mutation) => boolean;
7
- shouldDehydrateQuery?: (query: Query) => boolean;
8
- }
9
- interface HydrateOptions {
10
- defaultOptions?: {
11
- queries?: QueryOptions;
12
- mutations?: MutationOptions<unknown, DefaultError, unknown, unknown>;
13
- };
14
- }
15
- interface DehydratedMutation {
16
- mutationKey?: MutationKey;
17
- state: MutationState;
18
- meta?: MutationMeta;
19
- scope?: MutationScope;
20
- }
21
- interface DehydratedQuery {
22
- queryHash: string;
23
- queryKey: QueryKey;
24
- state: QueryState;
25
- meta?: QueryMeta;
26
- }
27
- interface DehydratedState {
28
- mutations: Array<DehydratedMutation>;
29
- queries: Array<DehydratedQuery>;
30
- }
31
- declare function defaultShouldDehydrateMutation(mutation: Mutation): boolean;
32
- declare function defaultShouldDehydrateQuery(query: Query): boolean;
33
- declare function dehydrate(client: QueryClient, options?: DehydrateOptions): DehydratedState;
34
- declare function hydrate(client: QueryClient, dehydratedState: unknown, options?: HydrateOptions): void;
35
-
36
- export { type DehydrateOptions, type DehydratedState, type HydrateOptions, defaultShouldDehydrateMutation, defaultShouldDehydrateQuery, dehydrate, hydrate };
@@ -10,10 +10,21 @@ function dehydrateMutation(mutation) {
10
10
  };
11
11
  }
12
12
  function dehydrateQuery(query) {
13
+ var _a;
13
14
  return {
14
15
  state: query.state,
15
16
  queryKey: query.queryKey,
16
17
  queryHash: query.queryHash,
18
+ ...query.state.status === "pending" && {
19
+ promise: (_a = query.promise) == null ? void 0 : _a.catch((error) => {
20
+ if (process.env.NODE_ENV !== "production") {
21
+ console.error(
22
+ `A query that was dehydrated as pending ended up rejecting. [${query.queryHash}]: ${error}; The error will be redacted in production builds`
23
+ );
24
+ }
25
+ return Promise.reject(new Error("redacted"));
26
+ })
27
+ },
17
28
  ...query.meta && { meta: query.meta }
18
29
  };
19
30
  }
@@ -24,11 +35,12 @@ function defaultShouldDehydrateQuery(query) {
24
35
  return query.state.status === "success";
25
36
  }
26
37
  function dehydrate(client, options = {}) {
27
- const filterMutation = options.shouldDehydrateMutation ?? defaultShouldDehydrateMutation;
38
+ var _a, _b;
39
+ const filterMutation = options.shouldDehydrateMutation ?? ((_a = client.getDefaultOptions().dehydrate) == null ? void 0 : _a.shouldDehydrateMutation) ?? defaultShouldDehydrateMutation;
28
40
  const mutations = client.getMutationCache().getAll().flatMap(
29
41
  (mutation) => filterMutation(mutation) ? [dehydrateMutation(mutation)] : []
30
42
  );
31
- const filterQuery = options.shouldDehydrateQuery ?? defaultShouldDehydrateQuery;
43
+ const filterQuery = options.shouldDehydrateQuery ?? ((_b = client.getDefaultOptions().dehydrate) == null ? void 0 : _b.shouldDehydrateQuery) ?? defaultShouldDehydrateQuery;
32
44
  const queries = client.getQueryCache().getAll().flatMap((query) => filterQuery(query) ? [dehydrateQuery(query)] : []);
33
45
  return { mutations, queries };
34
46
  }
@@ -41,41 +53,46 @@ function hydrate(client, dehydratedState, options) {
41
53
  const mutations = dehydratedState.mutations || [];
42
54
  const queries = dehydratedState.queries || [];
43
55
  mutations.forEach(({ state, ...mutationOptions }) => {
44
- var _a;
56
+ var _a, _b;
45
57
  mutationCache.build(
46
58
  client,
47
59
  {
48
- ...(_a = options == null ? void 0 : options.defaultOptions) == null ? void 0 : _a.mutations,
60
+ ...(_a = client.getDefaultOptions().hydrate) == null ? void 0 : _a.mutations,
61
+ ...(_b = options == null ? void 0 : options.defaultOptions) == null ? void 0 : _b.mutations,
49
62
  ...mutationOptions
50
63
  },
51
64
  state
52
65
  );
53
66
  });
54
- queries.forEach(({ queryKey, state, queryHash, meta }) => {
55
- var _a;
56
- const query = queryCache.get(queryHash);
67
+ queries.forEach(({ queryKey, state, queryHash, meta, promise }) => {
68
+ var _a, _b;
69
+ let query = queryCache.get(queryHash);
57
70
  if (query) {
58
71
  if (query.state.dataUpdatedAt < state.dataUpdatedAt) {
59
72
  const { fetchStatus: _ignored, ...dehydratedQueryState } = state;
60
73
  query.setState(dehydratedQueryState);
61
74
  }
62
- return;
75
+ } else {
76
+ query = queryCache.build(
77
+ client,
78
+ {
79
+ ...(_a = client.getDefaultOptions().hydrate) == null ? void 0 : _a.queries,
80
+ ...(_b = options == null ? void 0 : options.defaultOptions) == null ? void 0 : _b.queries,
81
+ queryKey,
82
+ queryHash,
83
+ meta
84
+ },
85
+ // Reset fetch status to idle to avoid
86
+ // query being stuck in fetching state upon hydration
87
+ {
88
+ ...state,
89
+ fetchStatus: "idle"
90
+ }
91
+ );
92
+ }
93
+ if (promise) {
94
+ void query.fetch(void 0, { initialPromise: promise });
63
95
  }
64
- queryCache.build(
65
- client,
66
- {
67
- ...(_a = options == null ? void 0 : options.defaultOptions) == null ? void 0 : _a.queries,
68
- queryKey,
69
- queryHash,
70
- meta
71
- },
72
- // Reset fetch status to idle to avoid
73
- // query being stuck in fetching state upon hydration
74
- {
75
- ...state,
76
- fetchStatus: "idle"
77
- }
78
- );
79
96
  });
80
97
  }
81
98
  export {