MCPcopy Create free account
hub / github.com/HuolalaTech/react-query-kit / buildRouter

Function buildRouter

src/router.ts:29–69  ·  view source on GitHub ↗
(keys: QueryKey, config: RouterConfig)

Source from the content-addressed store, hash-verified

27}
28
29const buildRouter = (keys: QueryKey, config: RouterConfig) => {
30 return Object.entries(config).reduce(
31 (acc, [key, opts]) => {
32 if (!isRouterLeaf(opts)) {
33 acc[key] = buildRouter([...keys, key], opts)
34 } else {
35 const type = opts._routerType
36 const options: any = {
37 ...opts,
38 [type === `m` ? `mutationKey` : `queryKey`]: [...keys, key],
39 }
40
41 if (type === `m`) {
42 acc[key] = {
43 useMutation: createMutation(options),
44 ...createMutation(options),
45 }
46 return acc
47 }
48
49 acc[key] =
50 type === `q`
51 ? {
52 useQuery: createQuery(options),
53 useSuspenseQuery: createSuspenseQuery(options),
54 ...createQuery(options),
55 }
56 : {
57 useInfiniteQuery: createInfiniteQuery(options),
58 useSuspenseInfiniteQuery: createSuspenseInfiniteQuery(options),
59 ...createInfiniteQuery(options),
60 }
61 }
62
63 return acc
64 },
65 {
66 getKey: () => keys,
67 } as any
68 )
69}
70
71export const router = <TConfig extends RouterConfig>(
72 key: string | QueryKey,

Callers 1

routerFunction · 0.85

Calls 6

createMutationFunction · 0.90
createQueryFunction · 0.90
createSuspenseQueryFunction · 0.90
createInfiniteQueryFunction · 0.90
isRouterLeafFunction · 0.85

Tested by

no test coverage detected