react-query-kit 3.0.0-rc.2 → 3.1.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 +48 -44
- package/build/lib/router.esm.js +26 -17
- package/build/lib/router.esm.js.map +1 -1
- package/build/lib/router.js +26 -17
- package/build/lib/router.js.map +1 -1
- package/build/lib/router.mjs +26 -17
- package/build/lib/router.mjs.map +1 -1
- package/build/lib/types.d.ts +6 -8
- package/build/lib/utils.esm.js +5 -10
- package/build/lib/utils.esm.js.map +1 -1
- package/build/lib/utils.js +5 -10
- package/build/lib/utils.js.map +1 -1
- package/build/lib/utils.mjs +5 -10
- 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 +31 -27
- 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 +34 -26
- package/src/types.ts +38 -6
- package/src/utils.ts +5 -9
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-kit
|
|
67
|
+
$ npm i react-query-kit
|
|
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.
|
|
@@ -347,7 +347,7 @@ function App() {
|
|
|
347
347
|
mutation.mutate({ title: 'Do Laundry', content: 'content...' })
|
|
348
348
|
}}
|
|
349
349
|
>
|
|
350
|
-
|
|
350
|
+
create Todo
|
|
351
351
|
</button>
|
|
352
352
|
</>
|
|
353
353
|
)}
|
|
@@ -367,7 +367,7 @@ Options
|
|
|
367
367
|
- Optional
|
|
368
368
|
- array of middleware functions [(details)](#middleware)
|
|
369
369
|
|
|
370
|
-
|
|
370
|
+
Expose Methods
|
|
371
371
|
|
|
372
372
|
- `getKey: () => MutationKey`
|
|
373
373
|
- `getOptions: () => UseMutationOptions`
|
|
@@ -382,7 +382,7 @@ Returns
|
|
|
382
382
|
```tsx
|
|
383
383
|
import { router } from 'react-query-kit'
|
|
384
384
|
|
|
385
|
-
const
|
|
385
|
+
const post = router(`post`, {
|
|
386
386
|
byId: router.query({
|
|
387
387
|
fetcher: (variables: { id: number }) =>
|
|
388
388
|
fetch(`/posts/${variables.id}`).then(res => res.json()),
|
|
@@ -403,34 +403,57 @@ const posts = router(`posts`, {
|
|
|
403
403
|
body: JSON.stringify(variables),
|
|
404
404
|
}).then(res => res.json()),
|
|
405
405
|
}),
|
|
406
|
+
|
|
407
|
+
// nest router
|
|
408
|
+
command: {
|
|
409
|
+
report: router.mutation({ mutationFn }),
|
|
410
|
+
|
|
411
|
+
promote: router.mutation({ mutationFn }),
|
|
412
|
+
},
|
|
406
413
|
})
|
|
407
414
|
|
|
408
415
|
// get root key
|
|
409
|
-
|
|
416
|
+
post.getKey() // ['post']
|
|
410
417
|
|
|
411
418
|
// hooks
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
419
|
+
post.byId.useQuery({ variables: { id: 1 } })
|
|
420
|
+
post.byId.useSuspenseQuery({ variables: { id: 1 } })
|
|
421
|
+
post.list.useInfiniteQuery()
|
|
422
|
+
post.list.useSuspenseInfiniteQuery()
|
|
423
|
+
post.add.useMutation()
|
|
424
|
+
post.command.report.useMutation()
|
|
417
425
|
|
|
418
426
|
// expose methods
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
427
|
+
post.byId.getKey({ id: 1 }) // ['post', 'byId', { id: 1 }]
|
|
428
|
+
post.byId.getFetchOptions({ id: 1 })
|
|
429
|
+
post.byId.getOptions({ id: 1 })
|
|
430
|
+
post.byId.fetcher({ id: 1 })
|
|
431
|
+
post.add.getKey() // ['post', 'add']
|
|
432
|
+
post.add.getOptions()
|
|
433
|
+
post.add.mutationFn({ title: 'title', content: 'content' })
|
|
426
434
|
|
|
427
435
|
// infer types
|
|
428
|
-
type Data = inferData<typeof
|
|
429
|
-
type FnData = inferFnData<typeof
|
|
430
|
-
type Variables = inferVariables<typeof
|
|
436
|
+
type Data = inferData<typeof post.list>
|
|
437
|
+
type FnData = inferFnData<typeof post.list>
|
|
438
|
+
type Variables = inferVariables<typeof post.list>
|
|
439
|
+
type Error = inferError<typeof post.list>
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
### Merging Routers
|
|
443
|
+
|
|
444
|
+
```ts
|
|
445
|
+
import { router } from 'react-query-kit'
|
|
446
|
+
|
|
447
|
+
const user = router(`user`, {})
|
|
448
|
+
const post = router(`post`, {})
|
|
449
|
+
|
|
450
|
+
const k = {
|
|
451
|
+
user,
|
|
452
|
+
post,
|
|
453
|
+
}
|
|
431
454
|
```
|
|
432
455
|
|
|
433
|
-
|
|
456
|
+
### API Reference
|
|
434
457
|
|
|
435
458
|
`type Router = (key: string, config: TConfig) => TRouter`
|
|
436
459
|
|
|
@@ -590,31 +613,11 @@ inferOptions<typeof useProjects> // InfiniteQueryHookOptions<...>
|
|
|
590
613
|
|
|
591
614
|
### What is the difference between `getFetchOptions` and `getOptions`?
|
|
592
615
|
|
|
593
|
-
`getFetchOptions`
|
|
594
|
-
|
|
595
|
-
```ts
|
|
596
|
-
const useTest1 = createQuery({
|
|
597
|
-
staleTime: Infinity,
|
|
598
|
-
})
|
|
599
|
-
|
|
600
|
-
// Only triggers on the first request
|
|
601
|
-
queryClient.prefetchQuery(useTest1.getOptions())
|
|
602
|
-
// Will always trigger a request
|
|
603
|
-
queryClient.prefetchQuery(useTest1.getFetchOptions())
|
|
604
|
-
|
|
605
|
-
const useTest2 = createQuery({
|
|
606
|
-
retry: 3,
|
|
607
|
-
})
|
|
608
|
-
|
|
609
|
-
// Automatically retries 3 times
|
|
610
|
-
queryClient.prefetchQuery(useTest2.getOptions())
|
|
611
|
-
// Does not automatically retry
|
|
612
|
-
queryClient.prefetchQuery(useTest2.getFetchOptions())
|
|
613
|
-
```
|
|
616
|
+
`getFetchOptions` would only return necessary options, while options like `staleTime` and `retry` would be omited
|
|
614
617
|
|
|
615
618
|
### What is the difference between `fetcher` and `queryFn`?
|
|
616
619
|
|
|
617
|
-
ReactQueryKit automatically converts fetcher to queryFn, as shown below:
|
|
620
|
+
ReactQueryKit would automatically converts fetcher to queryFn, as shown below:
|
|
618
621
|
|
|
619
622
|
```ts
|
|
620
623
|
const useTest = createQuery({
|
|
@@ -648,6 +651,7 @@ What you benefit from ReactQueryKit 3
|
|
|
648
651
|
|
|
649
652
|
- Support hierarchical key
|
|
650
653
|
- Support infer the types of fetcher, you can enjoy the preferred types automatically.
|
|
654
|
+
- Support to create a shape of your entire API
|
|
651
655
|
|
|
652
656
|
## Issues
|
|
653
657
|
|
package/build/lib/router.esm.js
CHANGED
|
@@ -4,29 +4,38 @@ import { createQuery } from './createQuery.esm.js';
|
|
|
4
4
|
import { createSuspenseInfiniteQuery } from './createSuspenseInfiniteQuery.esm.js';
|
|
5
5
|
import { createSuspenseQuery } from './createSuspenseQuery.esm.js';
|
|
6
6
|
|
|
7
|
-
const
|
|
7
|
+
const buildRouter = (keys, config) => {
|
|
8
8
|
return Object.entries(config).reduce((acc, [key, opts]) => {
|
|
9
|
-
|
|
10
|
-
[
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
9
|
+
if (!opts._type) {
|
|
10
|
+
acc[key] = buildRouter([...keys, key], opts);
|
|
11
|
+
} else {
|
|
12
|
+
const options = { ...opts,
|
|
13
|
+
[opts._type === "m" ? "mutationKey" : "queryKey"]: [...keys, key]
|
|
14
|
+
};
|
|
15
|
+
acc[key] = opts._type === "m" ? {
|
|
16
|
+
useMutation: createMutation(options),
|
|
17
|
+
...createMutation(options)
|
|
18
|
+
} : opts._type === "q" ? {
|
|
19
|
+
useQuery: createQuery(options),
|
|
20
|
+
useSuspenseQuery: createSuspenseQuery(options),
|
|
21
|
+
...createQuery(options)
|
|
22
|
+
} : {
|
|
23
|
+
useInfiniteQuery: createInfiniteQuery(options),
|
|
24
|
+
useSuspenseInfiniteQuery: createSuspenseInfiniteQuery(options),
|
|
25
|
+
...createInfiniteQuery(options)
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
24
29
|
return acc;
|
|
25
30
|
}, {
|
|
26
|
-
getKey: () =>
|
|
31
|
+
getKey: () => keys
|
|
27
32
|
});
|
|
28
33
|
};
|
|
29
34
|
|
|
35
|
+
const router = (scope, config) => {
|
|
36
|
+
return buildRouter([scope], config);
|
|
37
|
+
};
|
|
38
|
+
|
|
30
39
|
router.query = options => {
|
|
31
40
|
return { ...options,
|
|
32
41
|
_type: 'q'
|
|
@@ -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 CreateRouter,\n RouterConfig,\n RouterInfiniteQuery,\n RouterMutation,\n RouterQuery,\n} from './types'\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\nconst buildRouter = (keys: string[], config: RouterConfig) => {\n return Object.entries(config).reduce(\n (acc, [key, opts]) => {\n if (!opts._type) {\n acc[key] = buildRouter([...keys, key], opts)\n } else {\n const options: any = {\n ...opts,\n [opts._type === `m` ? `mutationKey` : `queryKey`]: [...keys, 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\n return acc\n },\n {\n getKey: () => keys,\n } as any\n )\n}\n\nexport const router = <TConfig extends RouterConfig>(\n scope: string,\n config: TConfig\n): CreateRouter<TConfig> => {\n return buildRouter([scope], config)\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":["buildRouter","keys","config","Object","entries","reduce","acc","key","opts","_type","options","useMutation","createMutation","useQuery","createQuery","useSuspenseQuery","createSuspenseQuery","useInfiniteQuery","createInfiniteQuery","useSuspenseInfiniteQuery","createSuspenseInfiniteQuery","getKey","router","scope","query","infiniteQuery","mutation"],"mappings":";;;;;;AAiBA,MAAMA,WAAW,GAAG,CAACC,IAAD,EAAiBC,MAAjB,KAA0C;AAC5D,EAAA,OAAOC,MAAM,CAACC,OAAP,CAAeF,MAAf,EAAuBG,MAAvB,CACL,CAACC,GAAD,EAAM,CAACC,GAAD,EAAMC,IAAN,CAAN,KAAsB;AACpB,IAAA,IAAI,CAACA,IAAI,CAACC,KAAV,EAAiB;AACfH,MAAAA,GAAG,CAACC,GAAD,CAAH,GAAWP,WAAW,CAAC,CAAC,GAAGC,IAAJ,EAAUM,GAAV,CAAD,EAAiBC,IAAjB,CAAtB,CAAA;AACD,KAFD,MAEO;AACL,MAAA,MAAME,OAAY,GAAG,EACnB,GAAGF,IADgB;QAEnB,CAACA,IAAI,CAACC,KAAL,KAAA,GAAA,GAAA,aAAA,GAAA,UAAD,GAAmD,CAAC,GAAGR,IAAJ,EAAUM,GAAV,CAAA;OAFrD,CAAA;AAKAD,MAAAA,GAAG,CAACC,GAAD,CAAH,GACEC,IAAI,CAACC,KAAL,KACI,GAAA,GAAA;AACEE,QAAAA,WAAW,EAAEC,cAAc,CAACF,OAAD,CAD7B;QAEE,GAAGE,cAAc,CAACF,OAAD,CAAA;AAFnB,OADJ,GAKIF,IAAI,CAACC,KAAL,KACA,GAAA,GAAA;AACEI,QAAAA,QAAQ,EAAEC,WAAW,CAACJ,OAAD,CADvB;AAEEK,QAAAA,gBAAgB,EAAEC,mBAAmB,CAACN,OAAD,CAFvC;QAGE,GAAGI,WAAW,CAACJ,OAAD,CAAA;AAHhB,OADA,GAMA;AACEO,QAAAA,gBAAgB,EAAEC,mBAAmB,CAACR,OAAD,CADvC;AAEES,QAAAA,wBAAwB,EAAEC,2BAA2B,CAACV,OAAD,CAFvD;QAGE,GAAGQ,mBAAmB,CAACR,OAAD,CAAA;OAf9B,CAAA;AAiBD,KAAA;;AAED,IAAA,OAAOJ,GAAP,CAAA;AACD,GA9BI,EA+BL;AACEe,IAAAA,MAAM,EAAE,MAAMpB,IAAAA;AADhB,GA/BK,CAAP,CAAA;AAmCD,CApCD,CAAA;;MAsCaqB,MAAM,GAAG,CACpBC,KADoB,EAEpBrB,MAFoB,KAGM;AAC1B,EAAA,OAAOF,WAAW,CAAC,CAACuB,KAAD,CAAD,EAAUrB,MAAV,CAAlB,CAAA;AACD,EALM;;AAOPoB,MAAM,CAACE,KAAP,GACEd,OADa,IAEgC;EAC7C,OAAO,EACL,GAAGA,OADE;AAELD,IAAAA,KAAK,EAAE,GAAA;GAFT,CAAA;AAID,CAPD,CAAA;;AASAa,MAAM,CAACG,aAAP,GAMEf,OANqB,IAU4C;EACjE,OAAO,EAAE,GAAGA,OAAL;AAAcD,IAAAA,KAAK,EAAE,KAAA;GAA5B,CAAA;AACD,CAZD,CAAA;;AAcAa,MAAM,CAACI,QAAP,GAMEhB,OANgB,IAU0C;EAC1D,OAAO,EAAE,GAAGA,OAAL;AAAcD,IAAAA,KAAK,EAAE,GAAA;GAA5B,CAAA;AACD,CAZD;;;;"}
|
package/build/lib/router.js
CHANGED
|
@@ -8,29 +8,38 @@ var createQuery = require('./createQuery.js');
|
|
|
8
8
|
var createSuspenseInfiniteQuery = require('./createSuspenseInfiniteQuery.js');
|
|
9
9
|
var createSuspenseQuery = require('./createSuspenseQuery.js');
|
|
10
10
|
|
|
11
|
-
const
|
|
11
|
+
const buildRouter = (keys, config) => {
|
|
12
12
|
return Object.entries(config).reduce((acc, [key, opts]) => {
|
|
13
|
-
|
|
14
|
-
[
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
13
|
+
if (!opts._type) {
|
|
14
|
+
acc[key] = buildRouter([...keys, key], opts);
|
|
15
|
+
} else {
|
|
16
|
+
const options = { ...opts,
|
|
17
|
+
[opts._type === "m" ? "mutationKey" : "queryKey"]: [...keys, key]
|
|
18
|
+
};
|
|
19
|
+
acc[key] = opts._type === "m" ? {
|
|
20
|
+
useMutation: createMutation.createMutation(options),
|
|
21
|
+
...createMutation.createMutation(options)
|
|
22
|
+
} : opts._type === "q" ? {
|
|
23
|
+
useQuery: createQuery.createQuery(options),
|
|
24
|
+
useSuspenseQuery: createSuspenseQuery.createSuspenseQuery(options),
|
|
25
|
+
...createQuery.createQuery(options)
|
|
26
|
+
} : {
|
|
27
|
+
useInfiniteQuery: createInfiniteQuery.createInfiniteQuery(options),
|
|
28
|
+
useSuspenseInfiniteQuery: createSuspenseInfiniteQuery.createSuspenseInfiniteQuery(options),
|
|
29
|
+
...createInfiniteQuery.createInfiniteQuery(options)
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
28
33
|
return acc;
|
|
29
34
|
}, {
|
|
30
|
-
getKey: () =>
|
|
35
|
+
getKey: () => keys
|
|
31
36
|
});
|
|
32
37
|
};
|
|
33
38
|
|
|
39
|
+
const router = (scope, config) => {
|
|
40
|
+
return buildRouter([scope], config);
|
|
41
|
+
};
|
|
42
|
+
|
|
34
43
|
router.query = options => {
|
|
35
44
|
return { ...options,
|
|
36
45
|
_type: 'q'
|
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 CreateRouter,\n RouterConfig,\n RouterInfiniteQuery,\n RouterMutation,\n RouterQuery,\n} from './types'\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\nconst buildRouter = (keys: string[], config: RouterConfig) => {\n return Object.entries(config).reduce(\n (acc, [key, opts]) => {\n if (!opts._type) {\n acc[key] = buildRouter([...keys, key], opts)\n } else {\n const options: any = {\n ...opts,\n [opts._type === `m` ? `mutationKey` : `queryKey`]: [...keys, 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\n return acc\n },\n {\n getKey: () => keys,\n } as any\n )\n}\n\nexport const router = <TConfig extends RouterConfig>(\n scope: string,\n config: TConfig\n): CreateRouter<TConfig> => {\n return buildRouter([scope], config)\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":["buildRouter","keys","config","Object","entries","reduce","acc","key","opts","_type","options","useMutation","createMutation","useQuery","createQuery","useSuspenseQuery","createSuspenseQuery","useInfiniteQuery","createInfiniteQuery","useSuspenseInfiniteQuery","createSuspenseInfiniteQuery","getKey","router","scope","query","infiniteQuery","mutation"],"mappings":";;;;;;;;;;AAiBA,MAAMA,WAAW,GAAG,CAACC,IAAD,EAAiBC,MAAjB,KAA0C;AAC5D,EAAA,OAAOC,MAAM,CAACC,OAAP,CAAeF,MAAf,EAAuBG,MAAvB,CACL,CAACC,GAAD,EAAM,CAACC,GAAD,EAAMC,IAAN,CAAN,KAAsB;AACpB,IAAA,IAAI,CAACA,IAAI,CAACC,KAAV,EAAiB;AACfH,MAAAA,GAAG,CAACC,GAAD,CAAH,GAAWP,WAAW,CAAC,CAAC,GAAGC,IAAJ,EAAUM,GAAV,CAAD,EAAiBC,IAAjB,CAAtB,CAAA;AACD,KAFD,MAEO;AACL,MAAA,MAAME,OAAY,GAAG,EACnB,GAAGF,IADgB;QAEnB,CAACA,IAAI,CAACC,KAAL,KAAA,GAAA,GAAA,aAAA,GAAA,UAAD,GAAmD,CAAC,GAAGR,IAAJ,EAAUM,GAAV,CAAA;OAFrD,CAAA;AAKAD,MAAAA,GAAG,CAACC,GAAD,CAAH,GACEC,IAAI,CAACC,KAAL,KACI,GAAA,GAAA;AACEE,QAAAA,WAAW,EAAEC,6BAAc,CAACF,OAAD,CAD7B;QAEE,GAAGE,6BAAc,CAACF,OAAD,CAAA;AAFnB,OADJ,GAKIF,IAAI,CAACC,KAAL,KACA,GAAA,GAAA;AACEI,QAAAA,QAAQ,EAAEC,uBAAW,CAACJ,OAAD,CADvB;AAEEK,QAAAA,gBAAgB,EAAEC,uCAAmB,CAACN,OAAD,CAFvC;QAGE,GAAGI,uBAAW,CAACJ,OAAD,CAAA;AAHhB,OADA,GAMA;AACEO,QAAAA,gBAAgB,EAAEC,uCAAmB,CAACR,OAAD,CADvC;AAEES,QAAAA,wBAAwB,EAAEC,uDAA2B,CAACV,OAAD,CAFvD;QAGE,GAAGQ,uCAAmB,CAACR,OAAD,CAAA;OAf9B,CAAA;AAiBD,KAAA;;AAED,IAAA,OAAOJ,GAAP,CAAA;AACD,GA9BI,EA+BL;AACEe,IAAAA,MAAM,EAAE,MAAMpB,IAAAA;AADhB,GA/BK,CAAP,CAAA;AAmCD,CApCD,CAAA;;MAsCaqB,MAAM,GAAG,CACpBC,KADoB,EAEpBrB,MAFoB,KAGM;AAC1B,EAAA,OAAOF,WAAW,CAAC,CAACuB,KAAD,CAAD,EAAUrB,MAAV,CAAlB,CAAA;AACD,EALM;;AAOPoB,MAAM,CAACE,KAAP,GACEd,OADa,IAEgC;EAC7C,OAAO,EACL,GAAGA,OADE;AAELD,IAAAA,KAAK,EAAE,GAAA;GAFT,CAAA;AAID,CAPD,CAAA;;AASAa,MAAM,CAACG,aAAP,GAMEf,OANqB,IAU4C;EACjE,OAAO,EAAE,GAAGA,OAAL;AAAcD,IAAAA,KAAK,EAAE,KAAA;GAA5B,CAAA;AACD,CAZD,CAAA;;AAcAa,MAAM,CAACI,QAAP,GAMEhB,OANgB,IAU0C;EAC1D,OAAO,EAAE,GAAGA,OAAL;AAAcD,IAAAA,KAAK,EAAE,GAAA;GAA5B,CAAA;AACD,CAZD;;;;"}
|
package/build/lib/router.mjs
CHANGED
|
@@ -4,29 +4,38 @@ import { createQuery } from './createQuery.mjs';
|
|
|
4
4
|
import { createSuspenseInfiniteQuery } from './createSuspenseInfiniteQuery.mjs';
|
|
5
5
|
import { createSuspenseQuery } from './createSuspenseQuery.mjs';
|
|
6
6
|
|
|
7
|
-
const
|
|
7
|
+
const buildRouter = (keys, config) => {
|
|
8
8
|
return Object.entries(config).reduce((acc, [key, opts]) => {
|
|
9
|
-
|
|
10
|
-
[
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
9
|
+
if (!opts._type) {
|
|
10
|
+
acc[key] = buildRouter([...keys, key], opts);
|
|
11
|
+
} else {
|
|
12
|
+
const options = { ...opts,
|
|
13
|
+
[opts._type === "m" ? "mutationKey" : "queryKey"]: [...keys, key]
|
|
14
|
+
};
|
|
15
|
+
acc[key] = opts._type === "m" ? {
|
|
16
|
+
useMutation: createMutation(options),
|
|
17
|
+
...createMutation(options)
|
|
18
|
+
} : opts._type === "q" ? {
|
|
19
|
+
useQuery: createQuery(options),
|
|
20
|
+
useSuspenseQuery: createSuspenseQuery(options),
|
|
21
|
+
...createQuery(options)
|
|
22
|
+
} : {
|
|
23
|
+
useInfiniteQuery: createInfiniteQuery(options),
|
|
24
|
+
useSuspenseInfiniteQuery: createSuspenseInfiniteQuery(options),
|
|
25
|
+
...createInfiniteQuery(options)
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
24
29
|
return acc;
|
|
25
30
|
}, {
|
|
26
|
-
getKey: () =>
|
|
31
|
+
getKey: () => keys
|
|
27
32
|
});
|
|
28
33
|
};
|
|
29
34
|
|
|
35
|
+
const router = (scope, config) => {
|
|
36
|
+
return buildRouter([scope], config);
|
|
37
|
+
};
|
|
38
|
+
|
|
30
39
|
router.query = options => {
|
|
31
40
|
return { ...options,
|
|
32
41
|
_type: 'q'
|
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 CreateRouter,\n RouterConfig,\n RouterInfiniteQuery,\n RouterMutation,\n RouterQuery,\n} from './types'\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\nconst buildRouter = (keys: string[], config: RouterConfig) => {\n return Object.entries(config).reduce(\n (acc, [key, opts]) => {\n if (!opts._type) {\n acc[key] = buildRouter([...keys, key], opts)\n } else {\n const options: any = {\n ...opts,\n [opts._type === `m` ? `mutationKey` : `queryKey`]: [...keys, 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\n return acc\n },\n {\n getKey: () => keys,\n } as any\n )\n}\n\nexport const router = <TConfig extends RouterConfig>(\n scope: string,\n config: TConfig\n): CreateRouter<TConfig> => {\n return buildRouter([scope], config)\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":["buildRouter","keys","config","Object","entries","reduce","acc","key","opts","_type","options","useMutation","createMutation","useQuery","createQuery","useSuspenseQuery","createSuspenseQuery","useInfiniteQuery","createInfiniteQuery","useSuspenseInfiniteQuery","createSuspenseInfiniteQuery","getKey","router","scope","query","infiniteQuery","mutation"],"mappings":";;;;;;AAiBA,MAAMA,WAAW,GAAG,CAACC,IAAD,EAAiBC,MAAjB,KAA0C;AAC5D,EAAA,OAAOC,MAAM,CAACC,OAAP,CAAeF,MAAf,EAAuBG,MAAvB,CACL,CAACC,GAAD,EAAM,CAACC,GAAD,EAAMC,IAAN,CAAN,KAAsB;AACpB,IAAA,IAAI,CAACA,IAAI,CAACC,KAAV,EAAiB;AACfH,MAAAA,GAAG,CAACC,GAAD,CAAH,GAAWP,WAAW,CAAC,CAAC,GAAGC,IAAJ,EAAUM,GAAV,CAAD,EAAiBC,IAAjB,CAAtB,CAAA;AACD,KAFD,MAEO;AACL,MAAA,MAAME,OAAY,GAAG,EACnB,GAAGF,IADgB;QAEnB,CAACA,IAAI,CAACC,KAAL,KAAA,GAAA,GAAA,aAAA,GAAA,UAAD,GAAmD,CAAC,GAAGR,IAAJ,EAAUM,GAAV,CAAA;OAFrD,CAAA;AAKAD,MAAAA,GAAG,CAACC,GAAD,CAAH,GACEC,IAAI,CAACC,KAAL,KACI,GAAA,GAAA;AACEE,QAAAA,WAAW,EAAEC,cAAc,CAACF,OAAD,CAD7B;QAEE,GAAGE,cAAc,CAACF,OAAD,CAAA;AAFnB,OADJ,GAKIF,IAAI,CAACC,KAAL,KACA,GAAA,GAAA;AACEI,QAAAA,QAAQ,EAAEC,WAAW,CAACJ,OAAD,CADvB;AAEEK,QAAAA,gBAAgB,EAAEC,mBAAmB,CAACN,OAAD,CAFvC;QAGE,GAAGI,WAAW,CAACJ,OAAD,CAAA;AAHhB,OADA,GAMA;AACEO,QAAAA,gBAAgB,EAAEC,mBAAmB,CAACR,OAAD,CADvC;AAEES,QAAAA,wBAAwB,EAAEC,2BAA2B,CAACV,OAAD,CAFvD;QAGE,GAAGQ,mBAAmB,CAACR,OAAD,CAAA;OAf9B,CAAA;AAiBD,KAAA;;AAED,IAAA,OAAOJ,GAAP,CAAA;AACD,GA9BI,EA+BL;AACEe,IAAAA,MAAM,EAAE,MAAMpB,IAAAA;AADhB,GA/BK,CAAP,CAAA;AAmCD,CApCD,CAAA;;MAsCaqB,MAAM,GAAG,CACpBC,KADoB,EAEpBrB,MAFoB,KAGM;AAC1B,EAAA,OAAOF,WAAW,CAAC,CAACuB,KAAD,CAAD,EAAUrB,MAAV,CAAlB,CAAA;AACD,EALM;;AAOPoB,MAAM,CAACE,KAAP,GACEd,OADa,IAEgC;EAC7C,OAAO,EACL,GAAGA,OADE;AAELD,IAAAA,KAAK,EAAE,GAAA;GAFT,CAAA;AAID,CAPD,CAAA;;AASAa,MAAM,CAACG,aAAP,GAMEf,OANqB,IAU4C;EACjE,OAAO,EAAE,GAAGA,OAAL;AAAcD,IAAAA,KAAK,EAAE,KAAA;GAA5B,CAAA;AACD,CAZD,CAAA;;AAcAa,MAAM,CAACI,QAAP,GAMEhB,OANgB,IAU0C;EAC1D,OAAO,EAAE,GAAGA,OAAL;AAAcD,IAAAA,KAAK,EAAE,GAAA;GAA5B,CAAA;AACD,CAZD;;;;"}
|
package/build/lib/types.d.ts
CHANGED
|
@@ -123,9 +123,7 @@ export type inferData<T> = T extends {
|
|
|
123
123
|
export type inferFnData<T> = T extends {
|
|
124
124
|
fetcher: ExposeFetcher<infer TFnData, any, any>;
|
|
125
125
|
} ? TFnData : T extends ExposeMutationMethods<infer TFnData, any, any, any> ? TFnData : never;
|
|
126
|
-
export type inferError<T> = T extends
|
|
127
|
-
fetcher: ExposeFetcher<any, infer TError, any>;
|
|
128
|
-
} ? TError : T extends ExposeMutationMethods<any, any, infer TError, any> ? TError : 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;
|
|
129
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;
|
|
130
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;
|
|
131
129
|
export type RouterQuery<TFnData, TVariables = void, TError = CompatibleError> = Omit<CreateQueryOptions<TFnData, TVariables, TError>, 'queryKey'> & {
|
|
@@ -138,17 +136,17 @@ export type RouterMutation<TData = unknown, TVariables = void, TError = Compatib
|
|
|
138
136
|
_type: `m`;
|
|
139
137
|
};
|
|
140
138
|
type DefaultTo<T, D> = unknown extends T ? D : T;
|
|
141
|
-
export type RouterConfig = Record<string, RouterQuery<any, any, any> | RouterInfiniteQuery<any, any, any, any> | RouterMutation<any, any, any, any
|
|
139
|
+
export type RouterConfig = Record<string, RouterQuery<any, any, any> | RouterInfiniteQuery<any, any, any, any> | RouterMutation<any, any, any, any> | Record<string, RouterQuery<any, any, any> | RouterInfiniteQuery<any, any, any, any> | RouterMutation<any, any, any, any> | Record<string, RouterQuery<any, any, any> | RouterInfiniteQuery<any, any, any, any> | RouterMutation<any, any, any, any> | Record<string, RouterQuery<any, any, any> | RouterInfiniteQuery<any, any, any, any> | RouterMutation<any, any, any, any> | Record<string, RouterQuery<any, any, any> | RouterInfiniteQuery<any, any, any, any> | RouterMutation<any, any, any, any> | Record<string, RouterQuery<any, any, any> | RouterInfiniteQuery<any, any, any, any> | RouterMutation<any, any, any, any>>>>>>>;
|
|
142
140
|
export type CreateRouter<TConfig extends RouterConfig> = {
|
|
143
141
|
[K in keyof TConfig]: TConfig[K] extends RouterMutation<infer TFnData, infer TVariables, infer TError, infer TContext> ? {
|
|
144
142
|
useMutation: MutationHook<TFnData, DefaultTo<TVariables, void>, DefaultTo<TError, CompatibleError>, TContext>;
|
|
145
143
|
} & ExposeMutationMethods<TFnData, DefaultTo<TVariables, void>, DefaultTo<TError, CompatibleError>, TContext> : TConfig[K] extends RouterInfiniteQuery<infer TFnData, infer TVariables, infer TError, infer TPageParam> ? {
|
|
146
|
-
useInfiniteQuery: InfiniteQueryHook<TFnData, DefaultTo<TVariables, void>, DefaultTo<TError, CompatibleError>, TPageParam
|
|
147
|
-
useSuspenseInfiniteQuery: SuspenseInfiniteQueryHook<TFnData, DefaultTo<TVariables, void>, DefaultTo<TError, CompatibleError>, TPageParam
|
|
148
|
-
} & ExposeMethods<TFnData, DefaultTo<TVariables, void>, DefaultTo<TError, CompatibleError>, 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'> ? {
|
|
149
147
|
useQuery: QueryHook<TFnData, DefaultTo<TVariables, void>, DefaultTo<TError, CompatibleError>>;
|
|
150
148
|
useSuspenseQuery: SuspenseQueryHook<TFnData, DefaultTo<TVariables, void>, DefaultTo<TError, CompatibleError>>;
|
|
151
|
-
} & ExposeMethods<TFnData, DefaultTo<TVariables, void>, DefaultTo<TError, CompatibleError>> : never;
|
|
149
|
+
} & ExposeMethods<TFnData, DefaultTo<TVariables, void>, DefaultTo<TError, CompatibleError>> : TConfig[K] extends RouterConfig ? CreateRouter<TConfig[K]> : never;
|
|
152
150
|
} & {
|
|
153
151
|
getKey: () => [string];
|
|
154
152
|
};
|
package/build/lib/utils.esm.js
CHANGED
|
@@ -5,28 +5,23 @@ const isV5 = !!ReactQuery.useSuspenseQuery;
|
|
|
5
5
|
const suspenseOptions = {
|
|
6
6
|
enabled: true,
|
|
7
7
|
suspense: true,
|
|
8
|
-
useErrorBoundary: (_error, query) => query.state.data === undefined
|
|
8
|
+
useErrorBoundary: (_error, query) => typeof query.state.data === 'undefined'
|
|
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 composedHook = uses.reduceRight((next, use) => use(next), hook);
|
|
20
|
+
return composedHook(opts, queryClient);
|
|
26
21
|
};
|
|
27
22
|
};
|
|
28
23
|
const getKey = (queryKey, variables) => {
|
|
29
|
-
return variables === undefined ?
|
|
24
|
+
return typeof variables === 'undefined' ? queryKey : [...queryKey, variables];
|
|
30
25
|
};
|
|
31
26
|
|
|
32
27
|
export { ReactQuery, getKey, isV5, suspenseOptions, withMiddleware };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.esm.js","sources":["../../src/utils.ts"],"sourcesContent":["import * as TanstackReactQuery from '@tanstack/react-query'\nimport type { Query, QueryClient, QueryKey } from '@tanstack/react-query'\n\nimport type { Middleware } from './types'\n\nexport const ReactQuery = TanstackReactQuery\n\nexport const isV5 = !!ReactQuery.useSuspenseQuery\n\nexport const suspenseOptions = {\n enabled: true,\n suspense: true,\n useErrorBoundary: (_error: unknown, query: Query) =>\n query.state.data === undefined,\n}\n\nexport const withMiddleware = (\n hook: any,\n defaultOptions: any,\n type: 'queries' | 'mutations'\n) => {\n return function useMiddleware(\n options?: { client?: QueryClient; use?: Middleware[] },\n queryClient?: QueryClient\n ) {\n const [
|
|
1
|
+
{"version":3,"file":"utils.esm.js","sources":["../../src/utils.ts"],"sourcesContent":["import * as TanstackReactQuery from '@tanstack/react-query'\nimport type { Query, QueryClient, QueryKey } from '@tanstack/react-query'\n\nimport type { Middleware } from './types'\n\nexport const ReactQuery = TanstackReactQuery\n\nexport const isV5 = !!ReactQuery.useSuspenseQuery\n\nexport const suspenseOptions = {\n enabled: true,\n suspense: true,\n useErrorBoundary: (_error: unknown, query: Query) =>\n typeof query.state.data === 'undefined',\n}\n\nexport const withMiddleware = (\n hook: any,\n defaultOptions: any,\n type: 'queries' | 'mutations'\n) => {\n return function useMiddleware(\n options?: { client?: QueryClient; use?: Middleware[] },\n queryClient?: QueryClient\n ) {\n const [uses, opts]: [Middleware[], any] = [\n ReactQuery.useQueryClient(\n // @ts-ignore Compatible with ReactQuery v4\n isV5 ? queryClient : options\n ).getDefaultOptions()[type],\n defaultOptions,\n options,\n ].reduce(\n ([u1, o1], { use: u2 = [], ...o2 } = {}) => [\n [...u1, ...u2],\n { ...o1, ...o2 },\n ],\n [[]]\n )\n const composedHook = uses.reduceRight((next, use) => use(next), hook)\n\n return composedHook(opts, queryClient)\n }\n}\n\nexport const getKey = (queryKey: QueryKey, variables?: any): QueryKey => {\n return typeof variables === 'undefined' ? queryKey : [...queryKey, variables]\n}\n"],"names":["ReactQuery","TanstackReactQuery","isV5","useSuspenseQuery","suspenseOptions","enabled","suspense","useErrorBoundary","_error","query","state","data","withMiddleware","hook","defaultOptions","type","useMiddleware","options","queryClient","uses","opts","useQueryClient","getDefaultOptions","reduce","u1","o1","use","u2","o2","composedHook","reduceRight","next","getKey","queryKey","variables"],"mappings":";;AAKO,MAAMA,UAAU,GAAGC,mBAAnB;MAEMC,IAAI,GAAG,CAAC,CAACF,UAAU,CAACG,iBAA1B;AAEA,MAAMC,eAAe,GAAG;AAC7BC,EAAAA,OAAO,EAAE,IADoB;AAE7BC,EAAAA,QAAQ,EAAE,IAFmB;AAG7BC,EAAAA,gBAAgB,EAAE,CAACC,MAAD,EAAkBC,KAAlB,KAChB,OAAOA,KAAK,CAACC,KAAN,CAAYC,IAAnB,KAA4B,WAAA;AAJD,EAAxB;AAOA,MAAMC,cAAc,GAAG,CAC5BC,IAD4B,EAE5BC,cAF4B,EAG5BC,IAH4B,KAIzB;AACH,EAAA,OAAO,SAASC,aAAT,CACLC,OADK,EAELC,WAFK,EAGL;IACA,MAAM,CAACC,IAAD,EAAOC,IAAP,IAAoC,CACxCpB,UAAU,CAACqB,cAAX;IAEEnB,IAAI,GAAGgB,WAAH,GAAiBD,OAFvB,EAGEK,iBAHF,EAAA,CAGsBP,IAHtB,CADwC,EAKxCD,cALwC,EAMxCG,OANwC,CAAA,CAOxCM,MAPwC,CAQxC,CAAC,CAACC,EAAD,EAAKC,EAAL,CAAD,EAAW;MAAEC,GAAG,EAAEC,EAAE,GAAG,EAAZ;MAAgB,GAAGC,EAAAA;AAAnB,KAAA,GAA0B,EAArC,KAA4C,CAC1C,CAAC,GAAGJ,EAAJ,EAAQ,GAAGG,EAAX,CAD0C,EAE1C,EAAE,GAAGF,EAAL;MAAS,GAAGG,EAAAA;AAAZ,KAF0C,CARJ,EAYxC,CAAC,EAAD,CAZwC,CAA1C,CAAA;AAcA,IAAA,MAAMC,YAAY,GAAGV,IAAI,CAACW,WAAL,CAAiB,CAACC,IAAD,EAAOL,GAAP,KAAeA,GAAG,CAACK,IAAD,CAAnC,EAA2ClB,IAA3C,CAArB,CAAA;AAEA,IAAA,OAAOgB,YAAY,CAACT,IAAD,EAAOF,WAAP,CAAnB,CAAA;GApBF,CAAA;AAsBD,EA3BM;MA6BMc,MAAM,GAAG,CAACC,QAAD,EAAqBC,SAArB,KAAmD;AACvE,EAAA,OAAO,OAAOA,SAAP,KAAqB,WAArB,GAAmCD,QAAnC,GAA8C,CAAC,GAAGA,QAAJ,EAAcC,SAAd,CAArD,CAAA;AACD;;;;"}
|
package/build/lib/utils.js
CHANGED
|
@@ -29,28 +29,23 @@ const isV5 = !!ReactQuery.useSuspenseQuery;
|
|
|
29
29
|
const suspenseOptions = {
|
|
30
30
|
enabled: true,
|
|
31
31
|
suspense: true,
|
|
32
|
-
useErrorBoundary: (_error, query) => query.state.data === undefined
|
|
32
|
+
useErrorBoundary: (_error, query) => typeof query.state.data === 'undefined'
|
|
33
33
|
};
|
|
34
34
|
const withMiddleware = (hook, defaultOptions, type) => {
|
|
35
35
|
return function useMiddleware(options, queryClient) {
|
|
36
|
-
const [
|
|
36
|
+
const [uses, opts] = [ReactQuery.useQueryClient( // @ts-ignore Compatible with ReactQuery v4
|
|
37
37
|
isV5 ? queryClient : options).getDefaultOptions()[type], defaultOptions, options].reduce(([u1, o1], {
|
|
38
38
|
use: u2 = [],
|
|
39
39
|
...o2
|
|
40
40
|
} = {}) => [[...u1, ...u2], { ...o1,
|
|
41
41
|
...o2
|
|
42
42
|
}], [[]]);
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
for (let i = middleware.length; i--;) {
|
|
46
|
-
next = middleware[i](next);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return next(opts, queryClient);
|
|
43
|
+
const composedHook = uses.reduceRight((next, use) => use(next), hook);
|
|
44
|
+
return composedHook(opts, queryClient);
|
|
50
45
|
};
|
|
51
46
|
};
|
|
52
47
|
const getKey = (queryKey, variables) => {
|
|
53
|
-
return variables === undefined ?
|
|
48
|
+
return typeof variables === 'undefined' ? queryKey : [...queryKey, variables];
|
|
54
49
|
};
|
|
55
50
|
|
|
56
51
|
exports.ReactQuery = ReactQuery;
|
package/build/lib/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sources":["../../src/utils.ts"],"sourcesContent":["import * as TanstackReactQuery from '@tanstack/react-query'\nimport type { Query, QueryClient, QueryKey } from '@tanstack/react-query'\n\nimport type { Middleware } from './types'\n\nexport const ReactQuery = TanstackReactQuery\n\nexport const isV5 = !!ReactQuery.useSuspenseQuery\n\nexport const suspenseOptions = {\n enabled: true,\n suspense: true,\n useErrorBoundary: (_error: unknown, query: Query) =>\n query.state.data === undefined,\n}\n\nexport const withMiddleware = (\n hook: any,\n defaultOptions: any,\n type: 'queries' | 'mutations'\n) => {\n return function useMiddleware(\n options?: { client?: QueryClient; use?: Middleware[] },\n queryClient?: QueryClient\n ) {\n const [
|
|
1
|
+
{"version":3,"file":"utils.js","sources":["../../src/utils.ts"],"sourcesContent":["import * as TanstackReactQuery from '@tanstack/react-query'\nimport type { Query, QueryClient, QueryKey } from '@tanstack/react-query'\n\nimport type { Middleware } from './types'\n\nexport const ReactQuery = TanstackReactQuery\n\nexport const isV5 = !!ReactQuery.useSuspenseQuery\n\nexport const suspenseOptions = {\n enabled: true,\n suspense: true,\n useErrorBoundary: (_error: unknown, query: Query) =>\n typeof query.state.data === 'undefined',\n}\n\nexport const withMiddleware = (\n hook: any,\n defaultOptions: any,\n type: 'queries' | 'mutations'\n) => {\n return function useMiddleware(\n options?: { client?: QueryClient; use?: Middleware[] },\n queryClient?: QueryClient\n ) {\n const [uses, opts]: [Middleware[], any] = [\n ReactQuery.useQueryClient(\n // @ts-ignore Compatible with ReactQuery v4\n isV5 ? queryClient : options\n ).getDefaultOptions()[type],\n defaultOptions,\n options,\n ].reduce(\n ([u1, o1], { use: u2 = [], ...o2 } = {}) => [\n [...u1, ...u2],\n { ...o1, ...o2 },\n ],\n [[]]\n )\n const composedHook = uses.reduceRight((next, use) => use(next), hook)\n\n return composedHook(opts, queryClient)\n }\n}\n\nexport const getKey = (queryKey: QueryKey, variables?: any): QueryKey => {\n return typeof variables === 'undefined' ? queryKey : [...queryKey, variables]\n}\n"],"names":["ReactQuery","TanstackReactQuery","isV5","useSuspenseQuery","suspenseOptions","enabled","suspense","useErrorBoundary","_error","query","state","data","withMiddleware","hook","defaultOptions","type","useMiddleware","options","queryClient","uses","opts","useQueryClient","getDefaultOptions","reduce","u1","o1","use","u2","o2","composedHook","reduceRight","next","getKey","queryKey","variables"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAKO,MAAMA,UAAU,GAAGC,8BAAnB;MAEMC,IAAI,GAAG,CAAC,CAACF,UAAU,CAACG,iBAA1B;AAEA,MAAMC,eAAe,GAAG;AAC7BC,EAAAA,OAAO,EAAE,IADoB;AAE7BC,EAAAA,QAAQ,EAAE,IAFmB;AAG7BC,EAAAA,gBAAgB,EAAE,CAACC,MAAD,EAAkBC,KAAlB,KAChB,OAAOA,KAAK,CAACC,KAAN,CAAYC,IAAnB,KAA4B,WAAA;AAJD,EAAxB;AAOA,MAAMC,cAAc,GAAG,CAC5BC,IAD4B,EAE5BC,cAF4B,EAG5BC,IAH4B,KAIzB;AACH,EAAA,OAAO,SAASC,aAAT,CACLC,OADK,EAELC,WAFK,EAGL;IACA,MAAM,CAACC,IAAD,EAAOC,IAAP,IAAoC,CACxCpB,UAAU,CAACqB,cAAX;IAEEnB,IAAI,GAAGgB,WAAH,GAAiBD,OAFvB,EAGEK,iBAHF,EAAA,CAGsBP,IAHtB,CADwC,EAKxCD,cALwC,EAMxCG,OANwC,CAAA,CAOxCM,MAPwC,CAQxC,CAAC,CAACC,EAAD,EAAKC,EAAL,CAAD,EAAW;MAAEC,GAAG,EAAEC,EAAE,GAAG,EAAZ;MAAgB,GAAGC,EAAAA;AAAnB,KAAA,GAA0B,EAArC,KAA4C,CAC1C,CAAC,GAAGJ,EAAJ,EAAQ,GAAGG,EAAX,CAD0C,EAE1C,EAAE,GAAGF,EAAL;MAAS,GAAGG,EAAAA;AAAZ,KAF0C,CARJ,EAYxC,CAAC,EAAD,CAZwC,CAA1C,CAAA;AAcA,IAAA,MAAMC,YAAY,GAAGV,IAAI,CAACW,WAAL,CAAiB,CAACC,IAAD,EAAOL,GAAP,KAAeA,GAAG,CAACK,IAAD,CAAnC,EAA2ClB,IAA3C,CAArB,CAAA;AAEA,IAAA,OAAOgB,YAAY,CAACT,IAAD,EAAOF,WAAP,CAAnB,CAAA;GApBF,CAAA;AAsBD,EA3BM;MA6BMc,MAAM,GAAG,CAACC,QAAD,EAAqBC,SAArB,KAAmD;AACvE,EAAA,OAAO,OAAOA,SAAP,KAAqB,WAArB,GAAmCD,QAAnC,GAA8C,CAAC,GAAGA,QAAJ,EAAcC,SAAd,CAArD,CAAA;AACD;;;;;;;;"}
|
package/build/lib/utils.mjs
CHANGED
|
@@ -5,28 +5,23 @@ const isV5 = !!ReactQuery.useSuspenseQuery;
|
|
|
5
5
|
const suspenseOptions = {
|
|
6
6
|
enabled: true,
|
|
7
7
|
suspense: true,
|
|
8
|
-
useErrorBoundary: (_error, query) => query.state.data === undefined
|
|
8
|
+
useErrorBoundary: (_error, query) => typeof query.state.data === 'undefined'
|
|
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 composedHook = uses.reduceRight((next, use) => use(next), hook);
|
|
20
|
+
return composedHook(opts, queryClient);
|
|
26
21
|
};
|
|
27
22
|
};
|
|
28
23
|
const getKey = (queryKey, variables) => {
|
|
29
|
-
return variables === undefined ?
|
|
24
|
+
return typeof variables === 'undefined' ? queryKey : [...queryKey, variables];
|
|
30
25
|
};
|
|
31
26
|
|
|
32
27
|
export { ReactQuery, getKey, isV5, suspenseOptions, withMiddleware };
|