@teamkeel/client-react 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,24 @@
1
+ {
2
+ "name": "@teamkeel/client-react",
3
+ "version": "0.357.1",
4
+ "description": "React helpers 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
+ "devDependencies": {
19
+ "@types/react": "18.2.0",
20
+ "@types/react-dom": "18.2.0",
21
+ "react": "18.2.0",
22
+ "typescript": "5.1.6"
23
+ }
24
+ }
package/pnpm-lock.yaml ADDED
@@ -0,0 +1,71 @@
1
+ lockfileVersion: '6.0'
2
+
3
+ settings:
4
+ autoInstallPeers: true
5
+ excludeLinksFromLockfile: false
6
+
7
+ devDependencies:
8
+ '@types/react':
9
+ specifier: 18.2.0
10
+ version: 18.2.0
11
+ '@types/react-dom':
12
+ specifier: 18.2.0
13
+ version: 18.2.0
14
+ react:
15
+ specifier: 18.2.0
16
+ version: 18.2.0
17
+ typescript:
18
+ specifier: 5.1.6
19
+ version: 5.1.6
20
+
21
+ packages:
22
+
23
+ /@types/prop-types@15.7.5:
24
+ resolution: {integrity: sha512-JCB8C6SnDoQf0cNycqd/35A7MjcnK+ZTqE7judS6o7utxUCg6imJg3QK2qzHKszlTjcj2cn+NwMB2i96ubpj7w==}
25
+ dev: true
26
+
27
+ /@types/react-dom@18.2.0:
28
+ resolution: {integrity: sha512-8yQrvS6sMpSwIovhPOwfyNf2Wz6v/B62LFSVYQ85+Rq3tLsBIG7rP5geMxaijTUxSkrO6RzN/IRuIAADYQsleA==}
29
+ dependencies:
30
+ '@types/react': 18.2.0
31
+ dev: true
32
+
33
+ /@types/react@18.2.0:
34
+ resolution: {integrity: sha512-0FLj93y5USLHdnhIhABk83rm8XEGA7kH3cr+YUlvxoUGp1xNt/DINUMvqPxLyOQMzLmZe8i4RTHbvb8MC7NmrA==}
35
+ dependencies:
36
+ '@types/prop-types': 15.7.5
37
+ '@types/scheduler': 0.16.3
38
+ csstype: 3.1.2
39
+ dev: true
40
+
41
+ /@types/scheduler@0.16.3:
42
+ resolution: {integrity: sha512-5cJ8CB4yAx7BH1oMvdU0Jh9lrEXyPkar6F9G/ERswkCuvP4KQZfZkSjcMbAICCpQTN4OuZn8tz0HiKv9TGZgrQ==}
43
+ dev: true
44
+
45
+ /csstype@3.1.2:
46
+ resolution: {integrity: sha512-I7K1Uu0MBPzaFKg4nI5Q7Vs2t+3gWWW648spaF+Rg7pI9ds18Ugn+lvg4SHczUdKlHI5LWBXyqfS8+DufyBsgQ==}
47
+ dev: true
48
+
49
+ /js-tokens@4.0.0:
50
+ resolution: {integrity: sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==}
51
+ dev: true
52
+
53
+ /loose-envify@1.4.0:
54
+ resolution: {integrity: sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==}
55
+ hasBin: true
56
+ dependencies:
57
+ js-tokens: 4.0.0
58
+ dev: true
59
+
60
+ /react@18.2.0:
61
+ resolution: {integrity: sha512-/3IjMdb2L9QbBdWiW5e3P2/npwMBaU9mHCSCUzNln0ZCYbcfTsGbTJrU/kGemdH2IWmB2ioZ+zkxtmq6g09fGQ==}
62
+ engines: {node: '>=0.10.0'}
63
+ dependencies:
64
+ loose-envify: 1.4.0
65
+ dev: true
66
+
67
+ /typescript@5.1.6:
68
+ resolution: {integrity: sha512-zaWCozRZ6DLEWAWFrVDz1H6FVXzUSfTy5FUMWsQlU8Ym5JP9eO4xkTIROFCQvhQf61z6O/G6ugw3SgAnvvm+HA==}
69
+ engines: {node: '>=14.17'}
70
+ hasBin: true
71
+ dev: true
package/readme.md ADDED
@@ -0,0 +1,39 @@
1
+ # @teamkeel/client-react
2
+
3
+ Typed useKeel Hook from a generated Keel client
4
+
5
+ ## Install
6
+
7
+ ```
8
+ npm i @teamkeel/client-react
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ Export the `KeelProvider` and `useKeel` components based on your generated client.
14
+
15
+ N.B. [See here](https://docs.keel.so/apis/client) for documentation on generating a client
16
+
17
+ ```ts
18
+ import { APIClient } from "../keelClient";
19
+ export const { KeelProvider, useKeel } = keel(APIClient);
20
+
21
+ ```
22
+
23
+ Wrap your app with the exported `KeelProvider`
24
+
25
+ ```tsx
26
+
27
+ <KeelProvider endpoint="https://myproject.keelapps.xyz/api/">
28
+ <App />
29
+ </KeelProvider>
30
+
31
+ ```
32
+
33
+ Now you can use the typed `useKeel` hook in any component within your app
34
+
35
+ ```ts
36
+ function MyComponent() {
37
+ const keel = useKeel();
38
+ }
39
+ ```
package/src/index.tsx ADDED
@@ -0,0 +1,65 @@
1
+ import React, { createContext, useContext, useEffect, useRef } from "react";
2
+
3
+ interface KeelContextType<T> {
4
+ client: T;
5
+ }
6
+
7
+ const KeelContext = createContext<KeelContextType<any>>({
8
+ client: null,
9
+ });
10
+
11
+ interface KeelProviderProps<
12
+ T extends new (...args: any[]) => any,
13
+ U = Omit<ConstructorParameters<T>[0], "endpoint">
14
+ > {
15
+ /**
16
+ * The endpoint URL for the client.
17
+ */
18
+ endpoint: string;
19
+ /**
20
+ * Additional config options for the client.
21
+ */
22
+ config?: U;
23
+ children: React.ReactNode;
24
+ }
25
+
26
+ export const keel = <T extends new (...args: any[]) => any>(Client: T) => {
27
+ function KeelProvider<T extends new (...args: any[]) => any>({
28
+ endpoint,
29
+ config,
30
+ children,
31
+ }: KeelProviderProps<T>) {
32
+ if (typeof Client !== "function") {
33
+ throw new Error("Client must be a Keel class");
34
+ }
35
+
36
+ const clientConstructor = Client as new (args: any) => any;
37
+ const clientArgs = { endpoint, ...config };
38
+ const clientRef = useRef(new clientConstructor(clientArgs));
39
+
40
+ const client = clientRef.current;
41
+
42
+ useEffect(() => {
43
+ client._setEndpoint(endpoint);
44
+ }, [endpoint, client]);
45
+
46
+ return (
47
+ <KeelContext.Provider value={{ client }}>{children}</KeelContext.Provider>
48
+ );
49
+ }
50
+
51
+ return {
52
+ KeelProvider: KeelProvider<T>,
53
+ useKeel: useKeel<T>,
54
+ };
55
+ };
56
+
57
+ function useKeel<T extends new (...args: any) => any>() {
58
+ const keelContext = useContext<KeelContextType<InstanceType<T>>>(KeelContext);
59
+
60
+ if (!keelContext) {
61
+ throw new Error("useKeel must be used within a KeelProvider");
62
+ }
63
+
64
+ return keelContext.client;
65
+ }
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
+ }