UNPKG

1.07 kBPlain TextView Raw
1import type { QueryCacheKey } from './core/apiState'
2import type { EndpointDefinition } from './endpointDefinitions'
3import { isPlainObject } from '@reduxjs/toolkit'
4
5export const defaultSerializeQueryArgs: SerializeQueryArgs<any> = ({
6 endpointName,
7 queryArgs,
8}) => {
9 // Sort the object keys before stringifying, to prevent useQuery({ a: 1, b: 2 }) having a different cache key than useQuery({ b: 2, a: 1 })
10 return `${endpointName}(${JSON.stringify(queryArgs, (key, value) =>
11 isPlainObject(value)
12 ? Object.keys(value)
13 .sort()
14 .reduce<any>((acc, key) => {
15 acc[key] = (value as any)[key]
16 return acc
17 }, {})
18 : value
19 )})`
20}
21
22export type SerializeQueryArgs<QueryArgs> = (_: {
23 queryArgs: QueryArgs
24 endpointDefinition: EndpointDefinition<any, any, any, any>
25 endpointName: string
26}) => string
27
28export type InternalSerializeQueryArgs = (_: {
29 queryArgs: any
30 endpointDefinition: EndpointDefinition<any, any, any, any>
31 endpointName: string
32}) => QueryCacheKey