1 export type TQueryKey<TKey, TListQuery = any, TDetailQuery = string> = {
2 all: readonly [TKey]
3 lists: () => readonly [...TQueryKey<TKey>["all"], "list"]
4 list: (query?: TListQuery) => readonly (string | TKey | { query: TListQuery })[]
5 details: () => readonly [...TQueryKey<TKey>["all"], "detail"]
6 detail: (
7 id: TDetailQuery,
8 query?: TListQuery
9 ) => readonly (string | TKey | TDetailQuery | { query: TListQuery })[]
10 }
11
12 export const queryKeysFactory = <
13 T,
14 TListQueryType = any,
15 TDetailQueryType = string
16 >(
17 globalKey: T
18 ) => {
19 const queryKeyFactory: TQueryKey<T, TListQueryType, TDetailQueryType> = {
20 all: [globalKey],
21 lists: () => [...queryKeyFactory.all, "list"],
22 list: (query?: TListQueryType) =>
23 query !== undefined
24 ? [...queryKeyFactory.lists(), { query }]
25 : queryKeyFactory.lists(),
26 details: () => [...queryKeyFactory.all, "detail"],
27 detail: (id: TDetailQueryType, query?: TListQueryType) =>
28 query !== undefined
29 ? [...queryKeyFactory.details(), id, { query }]
30 : [...queryKeyFactory.details(), id],
31 }
32 return queryKeyFactory
33 }