@jsontech/react 0.0.6 → 0.0.7

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.
Files changed (2) hide show
  1. package/README.md +158 -0
  2. package/package.json +4 -3
package/README.md ADDED
@@ -0,0 +1,158 @@
1
+ # @jsontech/react
2
+
3
+ React hooks and provider for ShipIt feature flags.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @jsontech/react
9
+ # or
10
+ pnpm add @jsontech/react
11
+ # or
12
+ yarn add @jsontech/react
13
+ ```
14
+
15
+ **Peer Dependencies:**
16
+
17
+ - `react >= 18`
18
+ - `react-dom >= 18`
19
+
20
+ ## Usage
21
+
22
+ ### Setup Provider
23
+
24
+ Wrap your app with `ShipItProvider`:
25
+
26
+ ```typescript
27
+ import { ShipItProvider } from '@jsontech/react';
28
+
29
+ export function App() {
30
+ return (
31
+ <ShipItProvider
32
+ config={{
33
+ // SDK automatically uses window.location.origin in browser
34
+ sdkKey: 'shipit_cli_...' // Client key for browser
35
+ }}
36
+ initialUser={{ id: 'user-123' }}
37
+ >
38
+ {/* Your app */}
39
+ </ShipItProvider>
40
+ );
41
+ }
42
+ ```
43
+
44
+ ### Read Flags
45
+
46
+ Use the `useBoolFlag` hook to read boolean flags:
47
+
48
+ ```typescript
49
+ import { useBoolFlag } from '@jsontech/react';
50
+
51
+ export function NewNav() {
52
+ const { value, loading, error } = useBoolFlag('new-nav', false);
53
+
54
+ if (loading) return <div>Loading...</div>;
55
+ if (error) return <div>Error: {error.message}</div>;
56
+
57
+ return value ? <div>New nav ON</div> : <div>New nav OFF</div>;
58
+ }
59
+ ```
60
+
61
+ ### Update User
62
+
63
+ Use the `useShipItUser` hook to get and update the current user:
64
+
65
+ ```typescript
66
+ import { useShipItUser } from '@jsontech/react';
67
+
68
+ export function UserSwitcher() {
69
+ const [user, setUser] = useShipItUser();
70
+
71
+ return (
72
+ <button
73
+ onClick={() =>
74
+ setUser({
75
+ ...(user ?? { id: 'user-123' }),
76
+ meta: { ...(user?.meta ?? {}), companyId: 'northwind' }
77
+ })
78
+ }
79
+ >
80
+ Switch user
81
+ </button>
82
+ );
83
+ }
84
+ ```
85
+
86
+ ## API Reference
87
+
88
+ ### `ShipItProvider`
89
+
90
+ Provider component that wraps your app and provides the ShipIt client context.
91
+
92
+ #### Props
93
+
94
+ - `config: ShipItReactConfig` - Configuration object
95
+ - `sdkKey?: string` - Client SDK key (recommended). If not provided, reads from `SHIPIT_CLIENT_KEY` env var.
96
+ - `projectKey?: string` - Legacy: project key (requires `envKey`). Not recommended.
97
+ - `envKey?: string` - Environment key (default: `'production'`). Only used with `projectKey`.
98
+ - `initialUser: ShipItUserPayload` - Initial user payload
99
+ - `user?: ShipItUserPayload` - Controlled user (requires `onUserChange`)
100
+ - `onUserChange?: (user: ShipItUserPayload) => void` - Callback for user changes (required if using controlled `user`)
101
+ - `children: React.ReactNode` - Your app components
102
+
103
+ ### `useBoolFlag(flagKey: string, defaultValue: boolean)`
104
+
105
+ Hook to evaluate a boolean feature flag.
106
+
107
+ **Returns:**
108
+
109
+ - `value: boolean` - The flag value
110
+ - `loading: boolean` - Whether the evaluation is in progress
111
+ - `error: Error | null` - Error if evaluation failed
112
+
113
+ **Example:**
114
+
115
+ ```typescript
116
+ const { value, loading, error } = useBoolFlag('feature-flag', false);
117
+ ```
118
+
119
+ ### `useShipItUser()`
120
+
121
+ Hook to get and update the current user.
122
+
123
+ **Returns:**
124
+
125
+ `[user: ShipItUserPayload | null, setUser: (user: ShipItUserPayload) => void]`
126
+
127
+ **Example:**
128
+
129
+ ```typescript
130
+ const [user, setUser] = useShipItUser();
131
+ ```
132
+
133
+ ## Environment Variables
134
+
135
+ The SDK automatically reads from environment variables if `sdkKey` is not provided:
136
+
137
+ - `SHIPIT_CLIENT_KEY` - Client SDK key
138
+
139
+ ```typescript
140
+ // This will use SHIPIT_CLIENT_KEY from env if sdkKey is not provided
141
+ <ShipItProvider config={{}} initialUser={{ id: 'user-123' }}>
142
+ {/* ... */}
143
+ </ShipItProvider>
144
+ ```
145
+
146
+ ## API Base URL
147
+
148
+ The SDK automatically uses `window.location.origin` in the browser (assumes API is on same origin). The API URL cannot be overridden.
149
+
150
+ ## SDK Keys
151
+
152
+ Use **client keys** (`shipit_cli_...`) for browser/mobile applications. Client keys are not secret and will be visible in your JavaScript bundle.
153
+
154
+ Get your SDK keys from the ShipIt Console → Environments.
155
+
156
+ ## License
157
+
158
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsontech/react",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -17,7 +17,8 @@
17
17
  }
18
18
  },
19
19
  "files": [
20
- "dist"
20
+ "dist",
21
+ "README.md"
21
22
  ],
22
23
  "sideEffects": false,
23
24
  "scripts": {
@@ -32,7 +33,7 @@
32
33
  "react-dom": ">=18"
33
34
  },
34
35
  "dependencies": {
35
- "@jsontech/sdk-js": "0.0.6"
36
+ "@jsontech/sdk-js": "0.0.7"
36
37
  },
37
38
  "devDependencies": {
38
39
  "@types/react": "^18.3.12",