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

Function withMiddleware

src/utils.ts:18–44  ·  view source on GitHub ↗
(
  hook: any,
  defaultOptions: any,
  type: 'queries' | 'mutations'
)

Source from the content-addressed store, hash-verified

16}
17
18export const withMiddleware = (
19 hook: any,
20 defaultOptions: any,
21 type: 'queries' | 'mutations'
22) => {
23 return function useMiddleware(
24 options?: { client?: QueryClient; use?: Middleware[] },
25 queryClient?: QueryClient
26 ) {
27 const [uses, opts]: [Middleware[], any] = [
28 ReactQuery.useQueryClient(
29 // @ts-ignore Compatible with ReactQuery v4
30 isV5 ? queryClient : options
31 ).getDefaultOptions()[type],
32 defaultOptions,
33 options,
34 ].reduce(
35 ([u1, o1], { use: u2 = [], ...o2 } = {}) => [
36 [...u1, ...u2],
37 { ...o1, ...o2 },
38 ],
39 [[]]
40 )
41
42 return uses.reduceRight((next, use) => use(next), hook)(opts, queryClient)
43 }
44}
45
46export const getKey = (queryKey: QueryKey, variables?: any): QueryKey => {
47 return variables === undefined ? queryKey : [...queryKey, variables]

Callers 2

createMutationFunction · 0.90
createBaseQueryFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected