@@ -9,36 +9,7 @@ const cache: WeakMap<any, string> | undefined = WeakMap
99export const defaultSerializeQueryArgs : SerializeQueryArgs < any > = ( {
1010 endpointName,
1111 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,
4213} ) => {
4314 let serialized = ''
4415
@@ -48,8 +19,8 @@ export const bigIntSafeSerializeQueryArgs: SerializeQueryArgs<any> = ({
4819 serialized = cached
4920 } else {
5021 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
5324 // Sort the object keys before stringifying, to prevent useQuery({ a: 1, b: 2 }) having a different cache key than useQuery({ b: 2, a: 1 })
5425 value = isPlainObject ( value )
5526 ? Object . keys ( value )
@@ -66,18 +37,21 @@ export const bigIntSafeSerializeQueryArgs: SerializeQueryArgs<any> = ({
6637 }
6738 serialized = stringified
6839 }
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 })
7040 return `${ endpointName } (${ serialized } )`
7141}
7242
7343export type SerializeQueryArgs < QueryArgs , ReturnType = string > = ( _ : {
7444 queryArgs : QueryArgs
7545 endpointDefinition : EndpointDefinition < any , any , any , any >
7646 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 ) => { }
7749} ) => ReturnType
7850
7951export type InternalSerializeQueryArgs = ( _ : {
8052 queryArgs : any
8153 endpointDefinition : EndpointDefinition < any , any , any , any >
8254 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 ) => { }
8357} ) => QueryCacheKey
0 commit comments