1
1
import { Query } from '../core/query'
2
2
import { QueryCache } from '../core/queryCache'
3
- import { QueryKey } from '../core/types'
3
+ import { QueryKey , QueryOptions } from '../core/types'
4
4
5
- export interface DehydratedQueryConfig {
5
+ // TYPES
6
+
7
+ export interface DehydrateOptions {
8
+ shouldDehydrate ?: ShouldDehydrateFunction
9
+ }
10
+
11
+ export interface HydrateOptions {
12
+ defaultOptions ?: QueryOptions
13
+ }
14
+
15
+ interface DehydratedQueryConfig {
6
16
cacheTime : number
7
17
}
8
18
9
- export interface DehydratedQuery {
19
+ interface DehydratedQuery {
10
20
queryKey : QueryKey
11
21
queryHash : string
12
22
data ?: unknown
@@ -20,9 +30,7 @@ export interface DehydratedState {
20
30
21
31
export type ShouldDehydrateFunction = ( query : Query ) => boolean
22
32
23
- export interface DehydrateConfig {
24
- shouldDehydrate ?: ShouldDehydrateFunction
25
- }
33
+ // FUNCTIONS
26
34
27
35
function serializePositiveNumber ( value : number ) : number {
28
36
return value === Infinity ? - 1 : value
@@ -54,10 +62,11 @@ function defaultShouldDehydrate(query: Query) {
54
62
55
63
export function dehydrate (
56
64
cache : QueryCache ,
57
- dehydrateConfig ?: DehydrateConfig
65
+ options ?: DehydrateOptions
58
66
) : DehydratedState {
59
- const config = dehydrateConfig || { }
60
- const shouldDehydrate = config . shouldDehydrate || defaultShouldDehydrate
67
+ options = options || { }
68
+
69
+ const shouldDehydrate = options . shouldDehydrate || defaultShouldDehydrate
61
70
const queries : DehydratedQuery [ ] = [ ]
62
71
63
72
cache . getAll ( ) . forEach ( query => {
@@ -69,11 +78,16 @@ export function dehydrate(
69
78
return { queries }
70
79
}
71
80
72
- export function hydrate ( cache : QueryCache , dehydratedState : unknown ) : void {
81
+ export function hydrate (
82
+ cache : QueryCache ,
83
+ dehydratedState : unknown ,
84
+ options ?: HydrateOptions
85
+ ) : void {
73
86
if ( typeof dehydratedState !== 'object' || dehydratedState === null ) {
74
87
return
75
88
}
76
89
90
+ const defaultOptions = options ?. defaultOptions || { }
77
91
const queries = ( dehydratedState as DehydratedState ) . queries || [ ]
78
92
79
93
queries . forEach ( dehydratedQuery => {
@@ -90,6 +104,7 @@ export function hydrate(cache: QueryCache, dehydratedState: unknown): void {
90
104
queryKey : dehydratedQuery . queryKey ,
91
105
queryHash : dehydratedQuery . queryHash ,
92
106
options : {
107
+ ...defaultOptions ,
93
108
cacheTime : deserializePositiveNumber (
94
109
dehydratedQuery . config . cacheTime
95
110
) ,
0 commit comments