react-query-kit 3.0.0-rc.1 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +37 -65
- package/build/lib/router.d.ts +1 -24
- package/build/lib/router.esm.js +15 -37
- package/build/lib/router.esm.js.map +1 -1
- package/build/lib/router.js +15 -37
- package/build/lib/router.js.map +1 -1
- package/build/lib/router.mjs +15 -37
- package/build/lib/router.mjs.map +1 -1
- package/build/lib/types.d.ts +41 -10
- package/build/lib/utils.esm.js +3 -8
- package/build/lib/utils.esm.js.map +1 -1
- package/build/lib/utils.js +3 -8
- package/build/lib/utils.js.map +1 -1
- package/build/lib/utils.mjs +3 -8
- package/build/lib/utils.mjs.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats.json +99 -99
- package/build/umd/index.development.js +18 -45
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/build/zip/zip.esm.js +1 -1
- package/package.json +1 -1
- package/src/router.ts +26 -129
- package/src/types.ts +152 -48
- package/src/utils.ts +3 -7
package/README.md
CHANGED
|
@@ -64,9 +64,9 @@ This module is distributed via [npm][npm] which is bundled with [node][node] and
|
|
|
64
64
|
should be installed as one of your project's `dependencies`:
|
|
65
65
|
|
|
66
66
|
```bash
|
|
67
|
-
$ npm i react-query-
|
|
67
|
+
$ npm i react-query-ki
|
|
68
68
|
# or
|
|
69
|
-
$ yarn add react-query-kit
|
|
69
|
+
$ yarn add react-query-kit
|
|
70
70
|
```
|
|
71
71
|
|
|
72
72
|
If you still on React Query Kit v2? Check out the v2 docs here: https://github.com/liaoliao666/react-query-kit/tree/v2#readme.
|
|
@@ -316,10 +316,6 @@ const useAddTodo = createMutation({
|
|
|
316
316
|
mutationFn: async (variables: { title: string; content: string }) =>
|
|
317
317
|
fetch('/post', {
|
|
318
318
|
method: 'POST',
|
|
319
|
-
headers: {
|
|
320
|
-
Accept: 'application/json',
|
|
321
|
-
'Content-Type': 'application/json',
|
|
322
|
-
},
|
|
323
319
|
body: JSON.stringify(variables),
|
|
324
320
|
}).then(res => res.json()),
|
|
325
321
|
onSuccess(data, variables, context) {
|
|
@@ -351,7 +347,7 @@ function App() {
|
|
|
351
347
|
mutation.mutate({ title: 'Do Laundry', content: 'content...' })
|
|
352
348
|
}}
|
|
353
349
|
>
|
|
354
|
-
|
|
350
|
+
create Todo
|
|
355
351
|
</button>
|
|
356
352
|
</>
|
|
357
353
|
)}
|
|
@@ -379,33 +375,23 @@ Returns
|
|
|
379
375
|
|
|
380
376
|
## router
|
|
381
377
|
|
|
382
|
-
`router`
|
|
378
|
+
`router` which allow you to create a shape of your entire API
|
|
383
379
|
|
|
384
380
|
### Usage
|
|
385
381
|
|
|
386
382
|
```tsx
|
|
387
383
|
import { router } from 'react-query-kit'
|
|
388
384
|
|
|
389
|
-
const
|
|
385
|
+
const post = router(`post`, {
|
|
390
386
|
byId: router.query({
|
|
391
|
-
fetcher: (variables: { id: number }) =>
|
|
392
|
-
|
|
393
|
-
},
|
|
387
|
+
fetcher: (variables: { id: number }) =>
|
|
388
|
+
fetch(`/posts/${variables.id}`).then(res => res.json()),
|
|
394
389
|
use: [myMiddleware],
|
|
395
390
|
}),
|
|
396
391
|
|
|
397
392
|
list: router.infiniteQuery({
|
|
398
|
-
fetcher: (
|
|
399
|
-
|
|
400
|
-
{ pageParam }
|
|
401
|
-
): Promise<{
|
|
402
|
-
projects: { id: string; name: string }[]
|
|
403
|
-
nextCursor: number
|
|
404
|
-
}> => {
|
|
405
|
-
return fetch(
|
|
406
|
-
`/posts/?cursor=${pageParam}?active=${variables.active}`
|
|
407
|
-
).then(res => res.json())
|
|
408
|
-
},
|
|
393
|
+
fetcher: (_variables, { pageParam }) =>
|
|
394
|
+
fetch(`/posts/?cursor=${pageParam}`).then(res => res.json()),
|
|
409
395
|
getNextPageParam: lastPage => lastPage.nextCursor,
|
|
410
396
|
initialPageParam: 0,
|
|
411
397
|
}),
|
|
@@ -414,35 +400,41 @@ const posts = router(`posts`, {
|
|
|
414
400
|
mutationFn: async (variables: { title: string; content: string }) =>
|
|
415
401
|
fetch('/posts', {
|
|
416
402
|
method: 'POST',
|
|
417
|
-
headers: {
|
|
418
|
-
Accept: 'application/json',
|
|
419
|
-
'Content-Type': 'application/json',
|
|
420
|
-
},
|
|
421
403
|
body: JSON.stringify(variables),
|
|
422
404
|
}).then(res => res.json()),
|
|
423
405
|
}),
|
|
424
406
|
})
|
|
425
407
|
|
|
426
408
|
// get root key
|
|
427
|
-
|
|
409
|
+
post.getKey() // ['post']
|
|
428
410
|
|
|
429
411
|
// hooks
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
412
|
+
post.byId.useQuery({ variables: { id: 1 } })
|
|
413
|
+
post.byId.useSuspenseQuery({ variables: { id: 1 } })
|
|
414
|
+
post.list.useInfiniteQuery()
|
|
415
|
+
post.list.useSuspenseInfiniteQuery()
|
|
416
|
+
post.add.useMutation()
|
|
435
417
|
|
|
436
418
|
// expose methods
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
419
|
+
post.byId.getKey({ id: 1 }) // ['post', 'byId', { id: 1 }]
|
|
420
|
+
post.byId.getFetchOptions({ id: 1 })
|
|
421
|
+
post.byId.getOptions({ id: 1 })
|
|
422
|
+
post.byId.fetcher({ id: 1 })
|
|
423
|
+
post.add.getKey() // ['post', 'add']
|
|
424
|
+
post.add.getOptions()
|
|
425
|
+
post.add.mutationFn({ title: 'title', content: 'content' })
|
|
426
|
+
|
|
427
|
+
// infer types
|
|
428
|
+
type Data = inferData<typeof posts.list>
|
|
429
|
+
type FnData = inferFnData<typeof posts.list>
|
|
430
|
+
type Variables = inferVariables<typeof posts.list>
|
|
431
|
+
type Error = inferError<typeof posts.list>
|
|
444
432
|
```
|
|
445
433
|
|
|
434
|
+
## API Reference
|
|
435
|
+
|
|
436
|
+
`type Router = (key: string, config: TConfig) => TRouter`
|
|
437
|
+
|
|
446
438
|
Expose Methods
|
|
447
439
|
|
|
448
440
|
- `query`
|
|
@@ -466,9 +458,9 @@ import { Middleware, MutationHook, QueryHook, getKey } from 'react-query-kit'
|
|
|
466
458
|
|
|
467
459
|
const logger: Middleware<QueryHook<Response, Variables>> = useQueryNext => {
|
|
468
460
|
return options => {
|
|
469
|
-
const
|
|
461
|
+
const log = useLogger()
|
|
470
462
|
const fetcher = (variables, context) => {
|
|
471
|
-
|
|
463
|
+
log(context.queryKey, variables)
|
|
472
464
|
return options.fetcher(variables, context)
|
|
473
465
|
}
|
|
474
466
|
|
|
@@ -480,7 +472,6 @@ const logger: Middleware<QueryHook<Response, Variables>> = useQueryNext => {
|
|
|
480
472
|
}
|
|
481
473
|
|
|
482
474
|
const useUser = createQuery<Response, Variables>({
|
|
483
|
-
// ...
|
|
484
475
|
use: [logger],
|
|
485
476
|
})
|
|
486
477
|
|
|
@@ -600,31 +591,11 @@ inferOptions<typeof useProjects> // InfiniteQueryHookOptions<...>
|
|
|
600
591
|
|
|
601
592
|
### What is the difference between `getFetchOptions` and `getOptions`?
|
|
602
593
|
|
|
603
|
-
`getFetchOptions`
|
|
604
|
-
|
|
605
|
-
```ts
|
|
606
|
-
const useTest1 = createQuery({
|
|
607
|
-
staleTime: Infinity,
|
|
608
|
-
})
|
|
609
|
-
|
|
610
|
-
// Only triggers on the first request
|
|
611
|
-
queryClient.prefetchQuery(useTest1.getOptions())
|
|
612
|
-
// Will always trigger a request
|
|
613
|
-
queryClient.prefetchQuery(useTest1.getFetchOptions())
|
|
614
|
-
|
|
615
|
-
const useTest2 = createQuery({
|
|
616
|
-
retry: 3,
|
|
617
|
-
})
|
|
618
|
-
|
|
619
|
-
// Automatically retries 3 times
|
|
620
|
-
queryClient.prefetchQuery(useTest2.getOptions())
|
|
621
|
-
// Does not automatically retry
|
|
622
|
-
queryClient.prefetchQuery(useTest2.getFetchOptions())
|
|
623
|
-
```
|
|
594
|
+
`getFetchOptions` would only return necessary options, while options like `staleTime` and `retry` would be omited
|
|
624
595
|
|
|
625
596
|
### What is the difference between `fetcher` and `queryFn`?
|
|
626
597
|
|
|
627
|
-
ReactQueryKit automatically converts fetcher to queryFn, as shown below:
|
|
598
|
+
ReactQueryKit would automatically converts fetcher to queryFn, as shown below:
|
|
628
599
|
|
|
629
600
|
```ts
|
|
630
601
|
const useTest = createQuery({
|
|
@@ -658,6 +629,7 @@ What you benefit from ReactQueryKit 3
|
|
|
658
629
|
|
|
659
630
|
- Support hierarchical key
|
|
660
631
|
- Support infer the types of fetcher, you can enjoy the preferred types automatically.
|
|
632
|
+
- Support to create a shape of your entire API
|
|
661
633
|
|
|
662
634
|
## Issues
|
|
663
635
|
|
package/build/lib/router.d.ts
CHANGED
|
@@ -1,27 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export type RouterQuery<TFnData, TVariables = void, TError = CompatibleError> = Omit<CreateQueryOptions<TFnData, TVariables, TError>, 'queryKey'> & {
|
|
3
|
-
_type: `q`;
|
|
4
|
-
};
|
|
5
|
-
export type RouterInfiniteQuery<TFnData, TVariables = void, TError = CompatibleError, TPageParam = number> = Omit<CreateInfiniteQueryOptions<TFnData, TVariables, TError, TPageParam>, 'queryKey'> & {
|
|
6
|
-
_type: `inf`;
|
|
7
|
-
};
|
|
8
|
-
export type RouterMutation<TData = unknown, TVariables = void, TError = CompatibleError, TContext = unknown> = Omit<CreateMutationOptions<TData, TVariables, TError, TContext>, 'mutationKey'> & {
|
|
9
|
-
_type: `m`;
|
|
10
|
-
};
|
|
11
|
-
export type RouterConfig = Record<string, RouterQuery<any, any, any> | RouterInfiniteQuery<any, any, any> | RouterMutation<any, any, any>>;
|
|
12
|
-
export type CreateRouter<TConfig extends RouterConfig> = {
|
|
13
|
-
[K in keyof TConfig]: TConfig[K] extends RouterMutation<infer TFnData, infer TVariables, infer TError, infer TContext> ? {
|
|
14
|
-
useMutation: MutationHook<TFnData, TVariables, TError, TContext>;
|
|
15
|
-
} & ExposeMutationMethods<TFnData, TVariables, TError, TContext> : TConfig[K] extends RouterInfiniteQuery<infer TFnData, infer TVariables, infer TError, infer TPageParam> ? {
|
|
16
|
-
useInfiniteQuery: InfiniteQueryHook<TFnData, TVariables, TError, TPageParam>;
|
|
17
|
-
useSuspenseInfiniteQuery: InfiniteQueryHook<TFnData, TVariables, TError, TPageParam>;
|
|
18
|
-
} & ExposeMethods<TFnData, TVariables, TError, TPageParam> : TConfig[K] extends Omit<RouterQuery<infer TFnData, infer TVariables, infer TError>, 'queryKey'> ? {
|
|
19
|
-
useQuery: QueryHook<TFnData, TVariables, TError>;
|
|
20
|
-
useSuspenseQuery: SuspenseQueryHook<TFnData, TVariables, TError>;
|
|
21
|
-
} & ExposeMethods<TFnData, TVariables, TError> : never;
|
|
22
|
-
} & {
|
|
23
|
-
getKey: () => [string];
|
|
24
|
-
};
|
|
1
|
+
import type { CreateInfiniteQueryOptions, CreateMutationOptions, CreateQueryOptions, CreateRouter, RouterConfig, RouterInfiniteQuery, RouterMutation, RouterQuery } from './types';
|
|
25
2
|
export declare const router: {
|
|
26
3
|
<TConfig extends RouterConfig>(scope: string, config: TConfig): CreateRouter<TConfig>;
|
|
27
4
|
query<TFnData, TVariables = void, TError = Error>(options: Omit<CreateQueryOptions<TFnData, TVariables, TError>, "queryKey">): RouterQuery<TFnData, TVariables, TError>;
|
package/build/lib/router.esm.js
CHANGED
|
@@ -6,43 +6,21 @@ import { createSuspenseQuery } from './createSuspenseQuery.esm.js';
|
|
|
6
6
|
|
|
7
7
|
const router = (scope, config) => {
|
|
8
8
|
return Object.entries(config).reduce((acc, [key, opts]) => {
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}),
|
|
25
|
-
useSuspenseQuery: createSuspenseQuery({ ...opts,
|
|
26
|
-
queryKey: keys
|
|
27
|
-
}),
|
|
28
|
-
...createQuery({ ...opts,
|
|
29
|
-
queryKey: keys
|
|
30
|
-
})
|
|
31
|
-
};
|
|
32
|
-
} else {
|
|
33
|
-
acc[key] = {
|
|
34
|
-
useInfiniteQuery: createInfiniteQuery({ ...opts,
|
|
35
|
-
queryKey: keys
|
|
36
|
-
}),
|
|
37
|
-
useSuspenseInfiniteQuery: createSuspenseInfiniteQuery({ ...opts,
|
|
38
|
-
queryKey: keys
|
|
39
|
-
}),
|
|
40
|
-
...createInfiniteQuery({ ...opts,
|
|
41
|
-
queryKey: keys
|
|
42
|
-
})
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
|
|
9
|
+
const options = { ...opts,
|
|
10
|
+
[opts._type === "m" ? "mutationKey" : "queryKey"]: [scope, key]
|
|
11
|
+
};
|
|
12
|
+
acc[key] = opts._type === "m" ? {
|
|
13
|
+
useMutation: createMutation(options),
|
|
14
|
+
...createMutation(options)
|
|
15
|
+
} : opts._type === "q" ? {
|
|
16
|
+
useQuery: createQuery(options),
|
|
17
|
+
useSuspenseQuery: createSuspenseQuery(options),
|
|
18
|
+
...createQuery(options)
|
|
19
|
+
} : {
|
|
20
|
+
useInfiniteQuery: createInfiniteQuery(options),
|
|
21
|
+
useSuspenseInfiniteQuery: createSuspenseInfiniteQuery(options),
|
|
22
|
+
...createInfiniteQuery(options)
|
|
23
|
+
};
|
|
46
24
|
return acc;
|
|
47
25
|
}, {
|
|
48
26
|
getKey: () => [scope]
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router.esm.js","sources":["../../src/router.ts"],"sourcesContent":["import { createInfiniteQuery } from './createInfiniteQuery'\nimport { createMutation } from './createMutation'\nimport { createQuery } from './createQuery'\nimport { createSuspenseInfiniteQuery } from './createSuspenseInfiniteQuery'\nimport { createSuspenseQuery } from './createSuspenseQuery'\nimport type {\n CompatibleError,\n CreateInfiniteQueryOptions,\n CreateMutationOptions,\n CreateQueryOptions,\n
|
|
1
|
+
{"version":3,"file":"router.esm.js","sources":["../../src/router.ts"],"sourcesContent":["import { createInfiniteQuery } from './createInfiniteQuery'\nimport { createMutation } from './createMutation'\nimport { createQuery } from './createQuery'\nimport { createSuspenseInfiniteQuery } from './createSuspenseInfiniteQuery'\nimport { createSuspenseQuery } from './createSuspenseQuery'\nimport type {\n CompatibleError,\n CreateInfiniteQueryOptions,\n CreateMutationOptions,\n CreateQueryOptions,\n CreateRouter,\n RouterConfig,\n RouterInfiniteQuery,\n RouterMutation,\n RouterQuery,\n} from './types'\n\nexport const router = <TConfig extends RouterConfig>(\n scope: string,\n config: TConfig\n): CreateRouter<TConfig> => {\n return Object.entries(config).reduce(\n (acc, [key, opts]) => {\n const options: any = {\n ...opts,\n [opts._type === `m` ? `mutationKey` : `queryKey`]: [scope, key],\n }\n\n acc[key] =\n opts._type === `m`\n ? {\n useMutation: createMutation(options),\n ...createMutation(options),\n }\n : opts._type === `q`\n ? {\n useQuery: createQuery(options),\n useSuspenseQuery: createSuspenseQuery(options),\n ...createQuery(options),\n }\n : {\n useInfiniteQuery: createInfiniteQuery(options),\n useSuspenseInfiniteQuery: createSuspenseInfiniteQuery(options),\n ...createInfiniteQuery(options),\n }\n\n return acc\n },\n {\n getKey: () => [scope],\n } as any\n )\n}\n\nrouter.query = <TFnData, TVariables = void, TError = CompatibleError>(\n options: Omit<CreateQueryOptions<TFnData, TVariables, TError>, 'queryKey'>\n): RouterQuery<TFnData, TVariables, TError> => {\n return {\n ...options,\n _type: 'q',\n }\n}\n\nrouter.infiniteQuery = <\n TFnData,\n TVariables = void,\n TError = CompatibleError,\n TPageParam = number\n>(\n options: Omit<\n CreateInfiniteQueryOptions<TFnData, TVariables, TError, TPageParam>,\n 'queryKey'\n >\n): RouterInfiniteQuery<TFnData, TVariables, TError, TPageParam> => {\n return { ...options, _type: 'inf' }\n}\n\nrouter.mutation = <\n TFnData = unknown,\n TVariables = void,\n TError = CompatibleError,\n TContext = unknown\n>(\n options: Omit<\n CreateMutationOptions<TFnData, TVariables, TError, TContext>,\n 'mutationKey'\n >\n): RouterMutation<TFnData, TVariables, TError, TContext> => {\n return { ...options, _type: 'm' }\n}\n"],"names":["router","scope","config","Object","entries","reduce","acc","key","opts","options","_type","useMutation","createMutation","useQuery","createQuery","useSuspenseQuery","createSuspenseQuery","useInfiniteQuery","createInfiniteQuery","useSuspenseInfiniteQuery","createSuspenseInfiniteQuery","getKey","query","infiniteQuery","mutation"],"mappings":";;;;;;MAiBaA,MAAM,GAAG,CACpBC,KADoB,EAEpBC,MAFoB,KAGM;AAC1B,EAAA,OAAOC,MAAM,CAACC,OAAP,CAAeF,MAAf,EAAuBG,MAAvB,CACL,CAACC,GAAD,EAAM,CAACC,GAAD,EAAMC,IAAN,CAAN,KAAsB;AACpB,IAAA,MAAMC,OAAY,GAAG,EACnB,GAAGD,IADgB;MAEnB,CAACA,IAAI,CAACE,KAAL,KAAA,GAAA,GAAA,aAAA,GAAA,UAAD,GAAmD,CAACT,KAAD,EAAQM,GAAR,CAAA;KAFrD,CAAA;AAKAD,IAAAA,GAAG,CAACC,GAAD,CAAH,GACEC,IAAI,CAACE,KAAL,KACI,GAAA,GAAA;AACEC,MAAAA,WAAW,EAAEC,cAAc,CAACH,OAAD,CAD7B;MAEE,GAAGG,cAAc,CAACH,OAAD,CAAA;AAFnB,KADJ,GAKID,IAAI,CAACE,KAAL,KACA,GAAA,GAAA;AACEG,MAAAA,QAAQ,EAAEC,WAAW,CAACL,OAAD,CADvB;AAEEM,MAAAA,gBAAgB,EAAEC,mBAAmB,CAACP,OAAD,CAFvC;MAGE,GAAGK,WAAW,CAACL,OAAD,CAAA;AAHhB,KADA,GAMA;AACEQ,MAAAA,gBAAgB,EAAEC,mBAAmB,CAACT,OAAD,CADvC;AAEEU,MAAAA,wBAAwB,EAAEC,2BAA2B,CAACX,OAAD,CAFvD;MAGE,GAAGS,mBAAmB,CAACT,OAAD,CAAA;KAf9B,CAAA;AAkBA,IAAA,OAAOH,GAAP,CAAA;AACD,GA1BI,EA2BL;IACEe,MAAM,EAAE,MAAM,CAACpB,KAAD,CAAA;AADhB,GA3BK,CAAP,CAAA;AA+BD,EAnCM;;AAqCPD,MAAM,CAACsB,KAAP,GACEb,OADa,IAEgC;EAC7C,OAAO,EACL,GAAGA,OADE;AAELC,IAAAA,KAAK,EAAE,GAAA;GAFT,CAAA;AAID,CAPD,CAAA;;AASAV,MAAM,CAACuB,aAAP,GAMEd,OANqB,IAU4C;EACjE,OAAO,EAAE,GAAGA,OAAL;AAAcC,IAAAA,KAAK,EAAE,KAAA;GAA5B,CAAA;AACD,CAZD,CAAA;;AAcAV,MAAM,CAACwB,QAAP,GAMEf,OANgB,IAU0C;EAC1D,OAAO,EAAE,GAAGA,OAAL;AAAcC,IAAAA,KAAK,EAAE,GAAA;GAA5B,CAAA;AACD,CAZD;;;;"}
|
package/build/lib/router.js
CHANGED
|
@@ -10,43 +10,21 @@ var createSuspenseQuery = require('./createSuspenseQuery.js');
|
|
|
10
10
|
|
|
11
11
|
const router = (scope, config) => {
|
|
12
12
|
return Object.entries(config).reduce((acc, [key, opts]) => {
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}),
|
|
29
|
-
useSuspenseQuery: createSuspenseQuery.createSuspenseQuery({ ...opts,
|
|
30
|
-
queryKey: keys
|
|
31
|
-
}),
|
|
32
|
-
...createQuery.createQuery({ ...opts,
|
|
33
|
-
queryKey: keys
|
|
34
|
-
})
|
|
35
|
-
};
|
|
36
|
-
} else {
|
|
37
|
-
acc[key] = {
|
|
38
|
-
useInfiniteQuery: createInfiniteQuery.createInfiniteQuery({ ...opts,
|
|
39
|
-
queryKey: keys
|
|
40
|
-
}),
|
|
41
|
-
useSuspenseInfiniteQuery: createSuspenseInfiniteQuery.createSuspenseInfiniteQuery({ ...opts,
|
|
42
|
-
queryKey: keys
|
|
43
|
-
}),
|
|
44
|
-
...createInfiniteQuery.createInfiniteQuery({ ...opts,
|
|
45
|
-
queryKey: keys
|
|
46
|
-
})
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
|
|
13
|
+
const options = { ...opts,
|
|
14
|
+
[opts._type === "m" ? "mutationKey" : "queryKey"]: [scope, key]
|
|
15
|
+
};
|
|
16
|
+
acc[key] = opts._type === "m" ? {
|
|
17
|
+
useMutation: createMutation.createMutation(options),
|
|
18
|
+
...createMutation.createMutation(options)
|
|
19
|
+
} : opts._type === "q" ? {
|
|
20
|
+
useQuery: createQuery.createQuery(options),
|
|
21
|
+
useSuspenseQuery: createSuspenseQuery.createSuspenseQuery(options),
|
|
22
|
+
...createQuery.createQuery(options)
|
|
23
|
+
} : {
|
|
24
|
+
useInfiniteQuery: createInfiniteQuery.createInfiniteQuery(options),
|
|
25
|
+
useSuspenseInfiniteQuery: createSuspenseInfiniteQuery.createSuspenseInfiniteQuery(options),
|
|
26
|
+
...createInfiniteQuery.createInfiniteQuery(options)
|
|
27
|
+
};
|
|
50
28
|
return acc;
|
|
51
29
|
}, {
|
|
52
30
|
getKey: () => [scope]
|
package/build/lib/router.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router.js","sources":["../../src/router.ts"],"sourcesContent":["import { createInfiniteQuery } from './createInfiniteQuery'\nimport { createMutation } from './createMutation'\nimport { createQuery } from './createQuery'\nimport { createSuspenseInfiniteQuery } from './createSuspenseInfiniteQuery'\nimport { createSuspenseQuery } from './createSuspenseQuery'\nimport type {\n CompatibleError,\n CreateInfiniteQueryOptions,\n CreateMutationOptions,\n CreateQueryOptions,\n
|
|
1
|
+
{"version":3,"file":"router.js","sources":["../../src/router.ts"],"sourcesContent":["import { createInfiniteQuery } from './createInfiniteQuery'\nimport { createMutation } from './createMutation'\nimport { createQuery } from './createQuery'\nimport { createSuspenseInfiniteQuery } from './createSuspenseInfiniteQuery'\nimport { createSuspenseQuery } from './createSuspenseQuery'\nimport type {\n CompatibleError,\n CreateInfiniteQueryOptions,\n CreateMutationOptions,\n CreateQueryOptions,\n CreateRouter,\n RouterConfig,\n RouterInfiniteQuery,\n RouterMutation,\n RouterQuery,\n} from './types'\n\nexport const router = <TConfig extends RouterConfig>(\n scope: string,\n config: TConfig\n): CreateRouter<TConfig> => {\n return Object.entries(config).reduce(\n (acc, [key, opts]) => {\n const options: any = {\n ...opts,\n [opts._type === `m` ? `mutationKey` : `queryKey`]: [scope, key],\n }\n\n acc[key] =\n opts._type === `m`\n ? {\n useMutation: createMutation(options),\n ...createMutation(options),\n }\n : opts._type === `q`\n ? {\n useQuery: createQuery(options),\n useSuspenseQuery: createSuspenseQuery(options),\n ...createQuery(options),\n }\n : {\n useInfiniteQuery: createInfiniteQuery(options),\n useSuspenseInfiniteQuery: createSuspenseInfiniteQuery(options),\n ...createInfiniteQuery(options),\n }\n\n return acc\n },\n {\n getKey: () => [scope],\n } as any\n )\n}\n\nrouter.query = <TFnData, TVariables = void, TError = CompatibleError>(\n options: Omit<CreateQueryOptions<TFnData, TVariables, TError>, 'queryKey'>\n): RouterQuery<TFnData, TVariables, TError> => {\n return {\n ...options,\n _type: 'q',\n }\n}\n\nrouter.infiniteQuery = <\n TFnData,\n TVariables = void,\n TError = CompatibleError,\n TPageParam = number\n>(\n options: Omit<\n CreateInfiniteQueryOptions<TFnData, TVariables, TError, TPageParam>,\n 'queryKey'\n >\n): RouterInfiniteQuery<TFnData, TVariables, TError, TPageParam> => {\n return { ...options, _type: 'inf' }\n}\n\nrouter.mutation = <\n TFnData = unknown,\n TVariables = void,\n TError = CompatibleError,\n TContext = unknown\n>(\n options: Omit<\n CreateMutationOptions<TFnData, TVariables, TError, TContext>,\n 'mutationKey'\n >\n): RouterMutation<TFnData, TVariables, TError, TContext> => {\n return { ...options, _type: 'm' }\n}\n"],"names":["router","scope","config","Object","entries","reduce","acc","key","opts","options","_type","useMutation","createMutation","useQuery","createQuery","useSuspenseQuery","createSuspenseQuery","useInfiniteQuery","createInfiniteQuery","useSuspenseInfiniteQuery","createSuspenseInfiniteQuery","getKey","query","infiniteQuery","mutation"],"mappings":";;;;;;;;;;MAiBaA,MAAM,GAAG,CACpBC,KADoB,EAEpBC,MAFoB,KAGM;AAC1B,EAAA,OAAOC,MAAM,CAACC,OAAP,CAAeF,MAAf,EAAuBG,MAAvB,CACL,CAACC,GAAD,EAAM,CAACC,GAAD,EAAMC,IAAN,CAAN,KAAsB;AACpB,IAAA,MAAMC,OAAY,GAAG,EACnB,GAAGD,IADgB;MAEnB,CAACA,IAAI,CAACE,KAAL,KAAA,GAAA,GAAA,aAAA,GAAA,UAAD,GAAmD,CAACT,KAAD,EAAQM,GAAR,CAAA;KAFrD,CAAA;AAKAD,IAAAA,GAAG,CAACC,GAAD,CAAH,GACEC,IAAI,CAACE,KAAL,KACI,GAAA,GAAA;AACEC,MAAAA,WAAW,EAAEC,6BAAc,CAACH,OAAD,CAD7B;MAEE,GAAGG,6BAAc,CAACH,OAAD,CAAA;AAFnB,KADJ,GAKID,IAAI,CAACE,KAAL,KACA,GAAA,GAAA;AACEG,MAAAA,QAAQ,EAAEC,uBAAW,CAACL,OAAD,CADvB;AAEEM,MAAAA,gBAAgB,EAAEC,uCAAmB,CAACP,OAAD,CAFvC;MAGE,GAAGK,uBAAW,CAACL,OAAD,CAAA;AAHhB,KADA,GAMA;AACEQ,MAAAA,gBAAgB,EAAEC,uCAAmB,CAACT,OAAD,CADvC;AAEEU,MAAAA,wBAAwB,EAAEC,uDAA2B,CAACX,OAAD,CAFvD;MAGE,GAAGS,uCAAmB,CAACT,OAAD,CAAA;KAf9B,CAAA;AAkBA,IAAA,OAAOH,GAAP,CAAA;AACD,GA1BI,EA2BL;IACEe,MAAM,EAAE,MAAM,CAACpB,KAAD,CAAA;AADhB,GA3BK,CAAP,CAAA;AA+BD,EAnCM;;AAqCPD,MAAM,CAACsB,KAAP,GACEb,OADa,IAEgC;EAC7C,OAAO,EACL,GAAGA,OADE;AAELC,IAAAA,KAAK,EAAE,GAAA;GAFT,CAAA;AAID,CAPD,CAAA;;AASAV,MAAM,CAACuB,aAAP,GAMEd,OANqB,IAU4C;EACjE,OAAO,EAAE,GAAGA,OAAL;AAAcC,IAAAA,KAAK,EAAE,KAAA;GAA5B,CAAA;AACD,CAZD,CAAA;;AAcAV,MAAM,CAACwB,QAAP,GAMEf,OANgB,IAU0C;EAC1D,OAAO,EAAE,GAAGA,OAAL;AAAcC,IAAAA,KAAK,EAAE,GAAA;GAA5B,CAAA;AACD,CAZD;;;;"}
|
package/build/lib/router.mjs
CHANGED
|
@@ -6,43 +6,21 @@ import { createSuspenseQuery } from './createSuspenseQuery.mjs';
|
|
|
6
6
|
|
|
7
7
|
const router = (scope, config) => {
|
|
8
8
|
return Object.entries(config).reduce((acc, [key, opts]) => {
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}),
|
|
25
|
-
useSuspenseQuery: createSuspenseQuery({ ...opts,
|
|
26
|
-
queryKey: keys
|
|
27
|
-
}),
|
|
28
|
-
...createQuery({ ...opts,
|
|
29
|
-
queryKey: keys
|
|
30
|
-
})
|
|
31
|
-
};
|
|
32
|
-
} else {
|
|
33
|
-
acc[key] = {
|
|
34
|
-
useInfiniteQuery: createInfiniteQuery({ ...opts,
|
|
35
|
-
queryKey: keys
|
|
36
|
-
}),
|
|
37
|
-
useSuspenseInfiniteQuery: createSuspenseInfiniteQuery({ ...opts,
|
|
38
|
-
queryKey: keys
|
|
39
|
-
}),
|
|
40
|
-
...createInfiniteQuery({ ...opts,
|
|
41
|
-
queryKey: keys
|
|
42
|
-
})
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
|
|
9
|
+
const options = { ...opts,
|
|
10
|
+
[opts._type === "m" ? "mutationKey" : "queryKey"]: [scope, key]
|
|
11
|
+
};
|
|
12
|
+
acc[key] = opts._type === "m" ? {
|
|
13
|
+
useMutation: createMutation(options),
|
|
14
|
+
...createMutation(options)
|
|
15
|
+
} : opts._type === "q" ? {
|
|
16
|
+
useQuery: createQuery(options),
|
|
17
|
+
useSuspenseQuery: createSuspenseQuery(options),
|
|
18
|
+
...createQuery(options)
|
|
19
|
+
} : {
|
|
20
|
+
useInfiniteQuery: createInfiniteQuery(options),
|
|
21
|
+
useSuspenseInfiniteQuery: createSuspenseInfiniteQuery(options),
|
|
22
|
+
...createInfiniteQuery(options)
|
|
23
|
+
};
|
|
46
24
|
return acc;
|
|
47
25
|
}, {
|
|
48
26
|
getKey: () => [scope]
|
package/build/lib/router.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router.mjs","sources":["../../src/router.ts"],"sourcesContent":["import { createInfiniteQuery } from './createInfiniteQuery'\nimport { createMutation } from './createMutation'\nimport { createQuery } from './createQuery'\nimport { createSuspenseInfiniteQuery } from './createSuspenseInfiniteQuery'\nimport { createSuspenseQuery } from './createSuspenseQuery'\nimport type {\n CompatibleError,\n CreateInfiniteQueryOptions,\n CreateMutationOptions,\n CreateQueryOptions,\n
|
|
1
|
+
{"version":3,"file":"router.mjs","sources":["../../src/router.ts"],"sourcesContent":["import { createInfiniteQuery } from './createInfiniteQuery'\nimport { createMutation } from './createMutation'\nimport { createQuery } from './createQuery'\nimport { createSuspenseInfiniteQuery } from './createSuspenseInfiniteQuery'\nimport { createSuspenseQuery } from './createSuspenseQuery'\nimport type {\n CompatibleError,\n CreateInfiniteQueryOptions,\n CreateMutationOptions,\n CreateQueryOptions,\n CreateRouter,\n RouterConfig,\n RouterInfiniteQuery,\n RouterMutation,\n RouterQuery,\n} from './types'\n\nexport const router = <TConfig extends RouterConfig>(\n scope: string,\n config: TConfig\n): CreateRouter<TConfig> => {\n return Object.entries(config).reduce(\n (acc, [key, opts]) => {\n const options: any = {\n ...opts,\n [opts._type === `m` ? `mutationKey` : `queryKey`]: [scope, key],\n }\n\n acc[key] =\n opts._type === `m`\n ? {\n useMutation: createMutation(options),\n ...createMutation(options),\n }\n : opts._type === `q`\n ? {\n useQuery: createQuery(options),\n useSuspenseQuery: createSuspenseQuery(options),\n ...createQuery(options),\n }\n : {\n useInfiniteQuery: createInfiniteQuery(options),\n useSuspenseInfiniteQuery: createSuspenseInfiniteQuery(options),\n ...createInfiniteQuery(options),\n }\n\n return acc\n },\n {\n getKey: () => [scope],\n } as any\n )\n}\n\nrouter.query = <TFnData, TVariables = void, TError = CompatibleError>(\n options: Omit<CreateQueryOptions<TFnData, TVariables, TError>, 'queryKey'>\n): RouterQuery<TFnData, TVariables, TError> => {\n return {\n ...options,\n _type: 'q',\n }\n}\n\nrouter.infiniteQuery = <\n TFnData,\n TVariables = void,\n TError = CompatibleError,\n TPageParam = number\n>(\n options: Omit<\n CreateInfiniteQueryOptions<TFnData, TVariables, TError, TPageParam>,\n 'queryKey'\n >\n): RouterInfiniteQuery<TFnData, TVariables, TError, TPageParam> => {\n return { ...options, _type: 'inf' }\n}\n\nrouter.mutation = <\n TFnData = unknown,\n TVariables = void,\n TError = CompatibleError,\n TContext = unknown\n>(\n options: Omit<\n CreateMutationOptions<TFnData, TVariables, TError, TContext>,\n 'mutationKey'\n >\n): RouterMutation<TFnData, TVariables, TError, TContext> => {\n return { ...options, _type: 'm' }\n}\n"],"names":["router","scope","config","Object","entries","reduce","acc","key","opts","options","_type","useMutation","createMutation","useQuery","createQuery","useSuspenseQuery","createSuspenseQuery","useInfiniteQuery","createInfiniteQuery","useSuspenseInfiniteQuery","createSuspenseInfiniteQuery","getKey","query","infiniteQuery","mutation"],"mappings":";;;;;;MAiBaA,MAAM,GAAG,CACpBC,KADoB,EAEpBC,MAFoB,KAGM;AAC1B,EAAA,OAAOC,MAAM,CAACC,OAAP,CAAeF,MAAf,EAAuBG,MAAvB,CACL,CAACC,GAAD,EAAM,CAACC,GAAD,EAAMC,IAAN,CAAN,KAAsB;AACpB,IAAA,MAAMC,OAAY,GAAG,EACnB,GAAGD,IADgB;MAEnB,CAACA,IAAI,CAACE,KAAL,KAAA,GAAA,GAAA,aAAA,GAAA,UAAD,GAAmD,CAACT,KAAD,EAAQM,GAAR,CAAA;KAFrD,CAAA;AAKAD,IAAAA,GAAG,CAACC,GAAD,CAAH,GACEC,IAAI,CAACE,KAAL,KACI,GAAA,GAAA;AACEC,MAAAA,WAAW,EAAEC,cAAc,CAACH,OAAD,CAD7B;MAEE,GAAGG,cAAc,CAACH,OAAD,CAAA;AAFnB,KADJ,GAKID,IAAI,CAACE,KAAL,KACA,GAAA,GAAA;AACEG,MAAAA,QAAQ,EAAEC,WAAW,CAACL,OAAD,CADvB;AAEEM,MAAAA,gBAAgB,EAAEC,mBAAmB,CAACP,OAAD,CAFvC;MAGE,GAAGK,WAAW,CAACL,OAAD,CAAA;AAHhB,KADA,GAMA;AACEQ,MAAAA,gBAAgB,EAAEC,mBAAmB,CAACT,OAAD,CADvC;AAEEU,MAAAA,wBAAwB,EAAEC,2BAA2B,CAACX,OAAD,CAFvD;MAGE,GAAGS,mBAAmB,CAACT,OAAD,CAAA;KAf9B,CAAA;AAkBA,IAAA,OAAOH,GAAP,CAAA;AACD,GA1BI,EA2BL;IACEe,MAAM,EAAE,MAAM,CAACpB,KAAD,CAAA;AADhB,GA3BK,CAAP,CAAA;AA+BD,EAnCM;;AAqCPD,MAAM,CAACsB,KAAP,GACEb,OADa,IAEgC;EAC7C,OAAO,EACL,GAAGA,OADE;AAELC,IAAAA,KAAK,EAAE,GAAA;GAFT,CAAA;AAID,CAPD,CAAA;;AASAV,MAAM,CAACuB,aAAP,GAMEd,OANqB,IAU4C;EACjE,OAAO,EAAE,GAAGA,OAAL;AAAcC,IAAAA,KAAK,EAAE,KAAA;GAA5B,CAAA;AACD,CAZD,CAAA;;AAcAV,MAAM,CAACwB,QAAP,GAMEf,OANgB,IAU0C;EAC1D,OAAO,EAAE,GAAGA,OAAL;AAAcC,IAAAA,KAAK,EAAE,GAAA;GAA5B,CAAA;AACD,CAZD;;;;"}
|
package/build/lib/types.d.ts
CHANGED
|
@@ -6,6 +6,9 @@ type NonUndefinedGuard<T> = T extends undefined ? never : T;
|
|
|
6
6
|
type WithRequired<T, K extends keyof T> = T & {
|
|
7
7
|
[_ in K]: {};
|
|
8
8
|
};
|
|
9
|
+
type DeepPartial<T> = T extends object ? {
|
|
10
|
+
[P in keyof T]?: DeepPartial<T[P]>;
|
|
11
|
+
} : T;
|
|
9
12
|
export type CompatibleError = CompatibleWithV4<DefaultError, Error>;
|
|
10
13
|
export type Fetcher<TFnData, TVariables = void, TPageParam = never> = (variables: TVariables, context: QueryFunctionContext<QueryKey, TPageParam>) => TFnData | Promise<TFnData>;
|
|
11
14
|
export type AdditionalQueryOptions<TFnData, TVariables, TPageParam = never> = {
|
|
@@ -14,12 +17,10 @@ export type AdditionalQueryOptions<TFnData, TVariables, TPageParam = never> = {
|
|
|
14
17
|
};
|
|
15
18
|
type inferMiddlewareHook<T extends (...args: any) => any> = (options: inferCreateOptions<T>, queryClient?: CompatibleWithV4<QueryClient, void>) => ReturnType<T>;
|
|
16
19
|
export type Middleware<T extends (...args: any) => any = QueryHook<any, any, any>> = (hook: inferMiddlewareHook<T>) => inferMiddlewareHook<T>;
|
|
17
|
-
type
|
|
18
|
-
[P in keyof T]?: DeepPartial<T[P]>;
|
|
19
|
-
} : T;
|
|
20
|
+
export type ExposeFetcher<TFnData, TVariables = void, TPageParam = never> = (variables: TVariables, context?: Partial<QueryFunctionContext<QueryKey, TPageParam>>) => TFnData | Promise<TFnData>;
|
|
20
21
|
export type ExposeMethods<TFnData, TVariables, TError, TPageParam = never> = {
|
|
21
|
-
fetcher:
|
|
22
|
-
getKey:
|
|
22
|
+
fetcher: ExposeFetcher<TFnData, TVariables, TPageParam>;
|
|
23
|
+
getKey: (variables?: DeepPartial<TVariables>) => CompatibleWithV4<DataTag<QueryKey, [
|
|
23
24
|
TPageParam
|
|
24
25
|
] extends [never] ? TFnData : InfiniteData<TFnData, TPageParam>>, QueryKey>;
|
|
25
26
|
getFetchOptions: (variables: TVariables extends void ? TVariables | void : TVariables) => Pick<ReturnType<ExposeMethods<TFnData, TVariables, TError, TPageParam>['getOptions']>, [
|
|
@@ -113,10 +114,40 @@ export interface ExposeMutationMethods<TData = unknown, TVariables = void, TErro
|
|
|
113
114
|
export interface MutationHook<TData = unknown, TVariables = void, TError = unknown, TDefaultContext = unknown> extends ExposeMutationMethods<TData, TVariables, TError, TDefaultContext> {
|
|
114
115
|
<TContext = TDefaultContext>(options?: MutationHookOptions<TData, TError, TVariables, TContext>, queryClient?: CompatibleWithV4<QueryClient, void>): MutationHookResult<TData, TError, TVariables, TContext>;
|
|
115
116
|
}
|
|
116
|
-
export type inferVariables<T> = T extends
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
export type
|
|
117
|
+
export type inferVariables<T> = T extends {
|
|
118
|
+
fetcher: ExposeFetcher<any, infer TVariables, any>;
|
|
119
|
+
} ? TVariables : T extends ExposeMutationMethods<any, infer TVariables, any, any> ? TVariables : never;
|
|
120
|
+
export type inferData<T> = T extends {
|
|
121
|
+
fetcher: ExposeFetcher<infer TFnData, any, infer TPageParam>;
|
|
122
|
+
} ? [TPageParam] extends [never] ? TFnData : CompatibleInfiniteData<TFnData, TPageParam> : T extends ExposeMutationMethods<infer TFnData, any, any, any> ? TFnData : never;
|
|
123
|
+
export type inferFnData<T> = T extends {
|
|
124
|
+
fetcher: ExposeFetcher<infer TFnData, any, any>;
|
|
125
|
+
} ? TFnData : T extends ExposeMutationMethods<infer TFnData, any, any, any> ? TFnData : never;
|
|
126
|
+
export type inferError<T> = T extends ExposeMethods<any, any, infer TError> ? TError : T extends ExposeMethods<any, any, infer TError, any> ? TError : T extends ExposeMutationMethods<any, any, infer TError, any> ? TError : never;
|
|
120
127
|
export type inferOptions<T> = T extends QueryHook<infer TFnData, infer TVariables, infer TError> ? QueryHookOptions<TFnData, TError, TFnData, TVariables> : T extends SuspenseQueryHook<infer TFnData, infer TVariables, infer TError> ? SuspenseQueryHookOptions<TFnData, TError, TFnData, TVariables> : T extends InfiniteQueryHook<infer TFnData, infer TVariables, infer TError, infer TPageParam> ? InfiniteQueryHookOptions<TFnData, TError, CompatibleWithV4<InfiniteData<TFnData, TPageParam>, TFnData>, TVariables, TPageParam> : T extends SuspenseInfiniteQueryHook<infer TFnData, infer TVariables, infer TError, infer TPageParam> ? SuspenseInfiniteQueryHookOptions<TFnData, TError, CompatibleWithV4<InfiniteData<TFnData, TPageParam>, TFnData>, TVariables, TPageParam> : T extends MutationHook<infer TFnData, infer TVariables, infer TError> ? MutationHookOptions<TFnData, TError, TVariables, unknown> : never;
|
|
121
|
-
export type inferCreateOptions<T> = T extends QueryHook<infer TFnData, infer TVariables, infer TError> ? CreateQueryOptions<TFnData, TVariables, TError> : T extends SuspenseQueryHook<infer TFnData, infer TVariables, infer TError> ? CreateSuspenseQueryOptions<TFnData, TVariables, TError> : T extends InfiniteQueryHook<infer TFnData, infer TVariables, infer TError, infer TPageParam> ? CreateInfiniteQueryOptions<TFnData, TVariables, TError, TPageParam> : T extends SuspenseInfiniteQueryHook<infer TFnData, infer TVariables, infer TError, infer TPageParam> ? CreateSuspenseInfiniteQueryOptions<TFnData, TVariables, TError, TPageParam> : T extends MutationHook<infer TFnData, infer TVariables, infer TError> ? CreateMutationOptions<TFnData, TError, TVariables,
|
|
128
|
+
export type inferCreateOptions<T> = T extends QueryHook<infer TFnData, infer TVariables, infer TError> ? CreateQueryOptions<TFnData, TVariables, TError> : T extends SuspenseQueryHook<infer TFnData, infer TVariables, infer TError> ? CreateSuspenseQueryOptions<TFnData, TVariables, TError> : T extends InfiniteQueryHook<infer TFnData, infer TVariables, infer TError, infer TPageParam> ? CreateInfiniteQueryOptions<TFnData, TVariables, TError, TPageParam> : T extends SuspenseInfiniteQueryHook<infer TFnData, infer TVariables, infer TError, infer TPageParam> ? CreateSuspenseInfiniteQueryOptions<TFnData, TVariables, TError, TPageParam> : T extends MutationHook<infer TFnData, infer TVariables, infer TError, infer TContext> ? CreateMutationOptions<TFnData, TError, TVariables, TContext> : never;
|
|
129
|
+
export type RouterQuery<TFnData, TVariables = void, TError = CompatibleError> = Omit<CreateQueryOptions<TFnData, TVariables, TError>, 'queryKey'> & {
|
|
130
|
+
_type: `q`;
|
|
131
|
+
};
|
|
132
|
+
export type RouterInfiniteQuery<TFnData, TVariables = void, TError = CompatibleError, TPageParam = number> = Omit<CreateInfiniteQueryOptions<TFnData, TVariables, TError, TPageParam>, 'queryKey'> & {
|
|
133
|
+
_type: `inf`;
|
|
134
|
+
};
|
|
135
|
+
export type RouterMutation<TData = unknown, TVariables = void, TError = CompatibleError, TContext = unknown> = Omit<CreateMutationOptions<TData, TVariables, TError, TContext>, 'mutationKey'> & {
|
|
136
|
+
_type: `m`;
|
|
137
|
+
};
|
|
138
|
+
type DefaultTo<T, D> = unknown extends T ? D : T;
|
|
139
|
+
export type RouterConfig = Record<string, RouterQuery<any, any, any> | RouterInfiniteQuery<any, any, any, any> | RouterMutation<any, any, any, any>>;
|
|
140
|
+
export type CreateRouter<TConfig extends RouterConfig> = {
|
|
141
|
+
[K in keyof TConfig]: TConfig[K] extends RouterMutation<infer TFnData, infer TVariables, infer TError, infer TContext> ? {
|
|
142
|
+
useMutation: MutationHook<TFnData, DefaultTo<TVariables, void>, DefaultTo<TError, CompatibleError>, TContext>;
|
|
143
|
+
} & ExposeMutationMethods<TFnData, DefaultTo<TVariables, void>, DefaultTo<TError, CompatibleError>, TContext> : TConfig[K] extends RouterInfiniteQuery<infer TFnData, infer TVariables, infer TError, infer TPageParam> ? {
|
|
144
|
+
useInfiniteQuery: InfiniteQueryHook<TFnData, DefaultTo<TVariables, void>, DefaultTo<TError, CompatibleError>, DefaultTo<TPageParam, number>>;
|
|
145
|
+
useSuspenseInfiniteQuery: SuspenseInfiniteQueryHook<TFnData, DefaultTo<TVariables, void>, DefaultTo<TError, CompatibleError>, DefaultTo<TPageParam, number>>;
|
|
146
|
+
} & ExposeMethods<TFnData, DefaultTo<TVariables, void>, DefaultTo<TError, CompatibleError>, DefaultTo<TPageParam, number>> : TConfig[K] extends Omit<RouterQuery<infer TFnData, infer TVariables, infer TError>, 'queryKey'> ? {
|
|
147
|
+
useQuery: QueryHook<TFnData, DefaultTo<TVariables, void>, DefaultTo<TError, CompatibleError>>;
|
|
148
|
+
useSuspenseQuery: SuspenseQueryHook<TFnData, DefaultTo<TVariables, void>, DefaultTo<TError, CompatibleError>>;
|
|
149
|
+
} & ExposeMethods<TFnData, DefaultTo<TVariables, void>, DefaultTo<TError, CompatibleError>> : never;
|
|
150
|
+
} & {
|
|
151
|
+
getKey: () => [string];
|
|
152
|
+
};
|
|
122
153
|
export {};
|
package/build/lib/utils.esm.js
CHANGED
|
@@ -9,20 +9,15 @@ const suspenseOptions = {
|
|
|
9
9
|
};
|
|
10
10
|
const withMiddleware = (hook, defaultOptions, type) => {
|
|
11
11
|
return function useMiddleware(options, queryClient) {
|
|
12
|
-
const [
|
|
12
|
+
const [uses, opts] = [ReactQuery.useQueryClient( // @ts-ignore Compatible with ReactQuery v4
|
|
13
13
|
isV5 ? queryClient : options).getDefaultOptions()[type], defaultOptions, options].reduce(([u1, o1], {
|
|
14
14
|
use: u2 = [],
|
|
15
15
|
...o2
|
|
16
16
|
} = {}) => [[...u1, ...u2], { ...o1,
|
|
17
17
|
...o2
|
|
18
18
|
}], [[]]);
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
for (let i = middleware.length; i--;) {
|
|
22
|
-
next = middleware[i](next);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
return next(opts, queryClient);
|
|
19
|
+
const composed = uses.reduceRight((next, use) => use(next), hook);
|
|
20
|
+
return composed(opts, queryClient);
|
|
26
21
|
};
|
|
27
22
|
};
|
|
28
23
|
const getKey = (queryKey, variables) => {
|