@teamkeel/client-react-query 0.357.1

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/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@teamkeel/client-react-query",
3
+ "version": "0.357.1",
4
+ "description": "Typed React-Query Hooks for Keel clients",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc",
9
+ "prepublish": "npm run build"
10
+ },
11
+ "keywords": [],
12
+ "author": "Keel (www.keel.so)",
13
+ "license": "ASL (Apache 2.0)",
14
+ "publishConfig": {
15
+ "access": "public"
16
+ },
17
+ "dependencies": {
18
+ "@tanstack/react-query": "^4.0.0"
19
+ },
20
+ "devDependencies": {
21
+ "@types/react": "18.2.0",
22
+ "@types/react-dom": "18.2.0",
23
+ "react": "18.2.0",
24
+ "typescript": "5.1.6"
25
+ }
26
+ }
package/pnpm-lock.yaml ADDED
@@ -0,0 +1,102 @@
1
+ lockfileVersion: '6.0'
2
+
3
+ settings:
4
+ autoInstallPeers: true
5
+ excludeLinksFromLockfile: false
6
+
7
+ dependencies:
8
+ '@tanstack/react-query':
9
+ specifier: ^4.0.0
10
+ version: 4.29.25(react@18.2.0)
11
+
12
+ devDependencies:
13
+ '@types/react':
14
+ specifier: 18.2.0
15
+ version: 18.2.0
16
+ '@types/react-dom':
17
+ specifier: 18.2.0
18
+ version: 18.2.0
19
+ react:
20
+ specifier: 18.2.0
21
+ version: 18.2.0
22
+ typescript:
23
+ specifier: 5.1.6
24
+ version: 5.1.6
25
+
26
+ packages:
27
+
28
+ /@tanstack/query-core@4.29.25:
29
+ resolution: {integrity: sha512-DI4y4VC6Uw4wlTpOocEXDky69xeOScME1ezLKsj+hOk7DguC9fkqXtp6Hn39BVb9y0b5IBrY67q6kIX623Zj4Q==}
30
+ dev: false
31
+
32
+ /@tanstack/react-query@4.29.25(react@18.2.0):
33
+ resolution: {integrity: sha512-c1+Ezu+XboYrdAMdusK2fTdRqXPMgPAnyoTrzHOZQqr8Hqz6PNvV9DSKl8agUo6nXX4np7fdWabIprt+838dLg==}
34
+ peerDependencies:
35
+ react: ^16.8.0 || ^17.0.0 || ^18.0.0
36
+ react-dom: ^16.8.0 || ^17.0.0 || ^18.0.0
37
+ react-native: '*'
38
+ peerDependenciesMeta:
39
+ react-dom:
40
+ optional: true
41
+ react-native:
42
+ optional: true
43
+ dependencies:
44
+ '@tanstack/query-core': 4.29.25
45
+ react: 18.2.0
46
+ use-sync-external-store: 1.2.0(react@18.2.0)
47
+ dev: false
48
+
49
+ /@types/prop-types@15.7.5:
50
+ resolution: {integrity: sha512-JCB8C6SnDoQf0cNycqd/35A7MjcnK+ZTqE7judS6o7utxUCg6imJg3QK2qzHKszlTjcj2cn+NwMB2i96ubpj7w==}
51
+ dev: true
52
+
53
+ /@types/react-dom@18.2.0:
54
+ resolution: {integrity: sha512-8yQrvS6sMpSwIovhPOwfyNf2Wz6v/B62LFSVYQ85+Rq3tLsBIG7rP5geMxaijTUxSkrO6RzN/IRuIAADYQsleA==}
55
+ dependencies:
56
+ '@types/react': 18.2.0
57
+ dev: true
58
+
59
+ /@types/react@18.2.0:
60
+ resolution: {integrity: sha512-0FLj93y5USLHdnhIhABk83rm8XEGA7kH3cr+YUlvxoUGp1xNt/DINUMvqPxLyOQMzLmZe8i4RTHbvb8MC7NmrA==}
61
+ dependencies:
62
+ '@types/prop-types': 15.7.5
63
+ '@types/scheduler': 0.16.3
64
+ csstype: 3.1.2
65
+ dev: true
66
+
67
+ /@types/scheduler@0.16.3:
68
+ resolution: {integrity: sha512-5cJ8CB4yAx7BH1oMvdU0Jh9lrEXyPkar6F9G/ERswkCuvP4KQZfZkSjcMbAICCpQTN4OuZn8tz0HiKv9TGZgrQ==}
69
+ dev: true
70
+
71
+ /csstype@3.1.2:
72
+ resolution: {integrity: sha512-I7K1Uu0MBPzaFKg4nI5Q7Vs2t+3gWWW648spaF+Rg7pI9ds18Ugn+lvg4SHczUdKlHI5LWBXyqfS8+DufyBsgQ==}
73
+ dev: true
74
+
75
+ /js-tokens@4.0.0:
76
+ resolution: {integrity: sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==}
77
+
78
+ /loose-envify@1.4.0:
79
+ resolution: {integrity: sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==}
80
+ hasBin: true
81
+ dependencies:
82
+ js-tokens: 4.0.0
83
+
84
+ /react@18.2.0:
85
+ resolution: {integrity: sha512-/3IjMdb2L9QbBdWiW5e3P2/npwMBaU9mHCSCUzNln0ZCYbcfTsGbTJrU/kGemdH2IWmB2ioZ+zkxtmq6g09fGQ==}
86
+ engines: {node: '>=0.10.0'}
87
+ dependencies:
88
+ loose-envify: 1.4.0
89
+
90
+ /typescript@5.1.6:
91
+ resolution: {integrity: sha512-zaWCozRZ6DLEWAWFrVDz1H6FVXzUSfTy5FUMWsQlU8Ym5JP9eO4xkTIROFCQvhQf61z6O/G6ugw3SgAnvvm+HA==}
92
+ engines: {node: '>=14.17'}
93
+ hasBin: true
94
+ dev: true
95
+
96
+ /use-sync-external-store@1.2.0(react@18.2.0):
97
+ resolution: {integrity: sha512-eEgnFxGQ1Ife9bzYs6VLi8/4X6CObHMw9Qr9tPY43iKwsPw8xE8+EFsf/2cFZ5S3esXgpWgtSCtLNS41F+sKPA==}
98
+ peerDependencies:
99
+ react: ^16.8.0 || ^17.0.0 || ^18.0.0
100
+ dependencies:
101
+ react: 18.2.0
102
+ dev: false
package/readme.md ADDED
@@ -0,0 +1,34 @@
1
+ # @teamkeel/client-react-query
2
+
3
+ Typed React-Query Hook from a generated Keel client
4
+
5
+ ## Install
6
+
7
+ N.B. Requires `@teamtkeel/client-react` to be installed and setup first.
8
+
9
+ ```
10
+ npm i @teamkeel/client-react-query
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ Export `useKeelQuery` and `useKeelMutation` by passing in a `useKeel` hook based on your api client.
16
+
17
+ ```ts
18
+ // Follow @teamtkeel/client-react setup instructions
19
+ import { APIClient } from "../keelClient";
20
+ export const { KeelProvider, useKeel } = keel(APIClient);
21
+
22
+ export const { useKeelQuery, useKeelMutation } = keelQuery(useKeel);
23
+
24
+ ```
25
+
26
+ You can then use these hooks in your components. Inputs are fully typed based on the action name.
27
+
28
+ ```tsx
29
+
30
+ const query = useKeelQuery("actionName", actionInputs, queryOptions);
31
+
32
+ const mutation = useKeelMutation("actionName", mutationOptions);
33
+
34
+ ```
package/src/index.ts ADDED
@@ -0,0 +1,108 @@
1
+ import {
2
+ QueryKey,
3
+ UseInfiniteQueryOptions,
4
+ UseMutationOptions,
5
+ UseQueryOptions,
6
+ useInfiniteQuery,
7
+ useMutation,
8
+ useQuery,
9
+ } from "@tanstack/react-query";
10
+
11
+ type FunctionTypes = "queries" | "mutations";
12
+
13
+ export const keelQuery = <T extends (...args: any) => any>(useKeel: T) => {
14
+ type KeelType = ReturnType<typeof useKeel>;
15
+
16
+ type QueryKeys<F extends FunctionTypes> = keyof KeelType[F];
17
+ type QueryArgs<F extends FunctionTypes, K extends QueryKeys<F>> = Parameters<
18
+ KeelType[F][K]
19
+ >[0];
20
+ type QueryResult<
21
+ F extends FunctionTypes,
22
+ K extends QueryKeys<F>
23
+ > = ReturnType<KeelType[F][K]>;
24
+
25
+ type Result<F extends FunctionTypes, K extends QueryKeys<F>> = Exclude<
26
+ Awaited<QueryResult<F, K>>["data"],
27
+ undefined
28
+ >;
29
+ type Error<F extends FunctionTypes, K extends QueryKeys<F>> = Exclude<
30
+ Awaited<QueryResult<F, K>>["error"],
31
+ undefined
32
+ >;
33
+
34
+ return {
35
+ useKeelQuery: <F extends "queries", K extends QueryKeys<"queries">>(
36
+ key: K,
37
+ args: QueryArgs<F, K>,
38
+ options?: Omit<UseQueryOptions<Result<F, K>, Error<F, K>>, "queryFn">
39
+ ) => {
40
+ const keel = useKeel();
41
+ return useQuery<Result<F, K>, Error<F, K>>(
42
+ queryKeys(key, args),
43
+ async () => {
44
+ const res = await keel[key](args);
45
+ if (res.error) {
46
+ return Promise.reject(res.error);
47
+ }
48
+ return res.data;
49
+ },
50
+ options
51
+ );
52
+ },
53
+ useInfiniteKeelQuery: <F extends "queries", K extends QueryKeys<"queries">>(
54
+ key: K,
55
+ args: QueryArgs<F, K>,
56
+ options?: Omit<
57
+ UseInfiniteQueryOptions<Result<F, K>, Error<F, K>>,
58
+ "queryFn"
59
+ >
60
+ ) => {
61
+ const keel = useKeel();
62
+ return useInfiniteQuery<Result<F, K>, Error<F, K>>(
63
+ queryKeys(key, args),
64
+ async () => {
65
+ const res = await keel[key](args);
66
+ if (res.error) {
67
+ return Promise.reject(res.error);
68
+ }
69
+ return res.data;
70
+ },
71
+ options
72
+ );
73
+ },
74
+ useKeelMutation: <F extends "mutations", K extends QueryKeys<"mutations">>(
75
+ key: K,
76
+ options?: Omit<
77
+ UseMutationOptions<Result<F, K>, Error<F, K>, QueryArgs<F, K>>,
78
+ "queryFn"
79
+ >
80
+ ) => {
81
+ const keel = useKeel();
82
+ return useMutation<Result<F, K>, Error<F, K>, QueryArgs<F, K>>(
83
+ [key],
84
+ async (args) => {
85
+ const res = await keel[key](args);
86
+ if (res.error) {
87
+ return Promise.reject(res.error);
88
+ }
89
+ return res.data;
90
+ },
91
+ options
92
+ );
93
+ },
94
+ };
95
+ };
96
+
97
+ const queryKeys = (key: any, args?: any): QueryKey => {
98
+ // Query key is either ["action name", {args}] or ["action name", "id", {args}]
99
+ const queryKey = [key];
100
+ if (args && args.id) {
101
+ queryKey.push(args.id);
102
+ }
103
+ if (args) {
104
+ queryKey.push(args);
105
+ }
106
+
107
+ return queryKey;
108
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ESNext",
4
+ "module": "commonjs",
5
+ "declaration": true,
6
+ "outDir": "./dist",
7
+ "strict": true,
8
+ "jsx": "react",
9
+ "lib": ["ESNext", "dom"],
10
+ "esModuleInterop": true,
11
+ "sourceMap": true
12
+ },
13
+ "include": ["src"],
14
+ "exclude": ["node_modules", "**/__tests__/*"]
15
+ }