@@ -9,36 +9,7 @@ const cache: WeakMap<any, string> | undefined = WeakMap
9
9
export const defaultSerializeQueryArgs : SerializeQueryArgs < any > = ( {
10
10
endpointName,
11
11
queryArgs,
12
- } ) => {
13
- let serialized = ''
14
-
15
- const cached = cache ?. get ( queryArgs )
16
-
17
- if ( typeof cached === 'string' ) {
18
- serialized = cached
19
- } else {
20
- const stringified = JSON . stringify ( queryArgs , ( key , value ) =>
21
- // Sort the object keys before stringifying, to prevent useQuery({ a: 1, b: 2 }) having a different cache key than useQuery({ b: 2, a: 1 })
22
- isPlainObject ( value )
23
- ? Object . keys ( value )
24
- . sort ( )
25
- . reduce < any > ( ( acc , key ) => {
26
- acc [ key ] = ( value as any ) [ key ]
27
- return acc
28
- } , { } )
29
- : value ,
30
- )
31
- if ( isPlainObject ( queryArgs ) ) {
32
- cache ?. set ( queryArgs , stringified )
33
- }
34
- serialized = stringified
35
- }
36
- return `${ endpointName } (${ serialized } )`
37
- }
38
-
39
- export const bigIntSafeSerializeQueryArgs : SerializeQueryArgs < any > = ( {
40
- endpointName,
41
- queryArgs,
12
+ replacer,
42
13
} ) => {
43
14
let serialized = ''
44
15
@@ -48,8 +19,8 @@ export const bigIntSafeSerializeQueryArgs: SerializeQueryArgs<any> = ({
48
19
serialized = cached
49
20
} else {
50
21
const stringified = JSON . stringify ( queryArgs , ( key , value ) => {
51
- // Translate bigint fields to a serializable value
52
- value = typeof value === 'bigint' ? { $bigint : value . toString ( ) } : value
22
+ // Use custom replacer first before applying key-sorting behavior:
23
+ value = replacer ? replacer ( key , value ) : value
53
24
// Sort the object keys before stringifying, to prevent useQuery({ a: 1, b: 2 }) having a different cache key than useQuery({ b: 2, a: 1 })
54
25
value = isPlainObject ( value )
55
26
? Object . keys ( value )
@@ -66,18 +37,21 @@ export const bigIntSafeSerializeQueryArgs: SerializeQueryArgs<any> = ({
66
37
}
67
38
serialized = stringified
68
39
}
69
- // Sort the object keys before stringifying, to prevent useQuery({ a: 1, b: 2 }) having a different cache key than useQuery({ b: 2, a: 1 })
70
40
return `${ endpointName } (${ serialized } )`
71
41
}
72
42
73
43
export type SerializeQueryArgs < QueryArgs , ReturnType = string > = ( _ : {
74
44
queryArgs : QueryArgs
75
45
endpointDefinition : EndpointDefinition < any , any , any , any >
76
46
endpointName : string
47
+ // Allows for a custom stringify replacer while keeping key-sorting behavior. e.g. for serializing bigint.
48
+ replacer ?: ( key : string , value : any ) => { }
77
49
} ) => ReturnType
78
50
79
51
export type InternalSerializeQueryArgs = ( _ : {
80
52
queryArgs : any
81
53
endpointDefinition : EndpointDefinition < any , any , any , any >
82
54
endpointName : string
55
+ // Allows for a custom stringify replacer while keeping key-sorting behavior. e.g. for serializing bigint.
56
+ replacer ?: ( key : string , value : any ) => { }
83
57
} ) => QueryCacheKey
0 commit comments