@dipswitch/sdk-react 0.0.0-stage → 0.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/LICENSE +21 -0
- package/README.md +75 -2
- package/dist/index.d.ts +13 -0
- package/dist/index.js +58 -0
- package/package.json +22 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Engineers Space LLC
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,76 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @dipswitch/sdk-react
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Read Dipswitch flags from React 19.3.1 or later in the React 19 series.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
For local use, build `sdk-react` and `sdk-js`, then run `pnpm pack` in each package and copy the two tarballs to your React app. In the app, install both together.
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install ./dipswitch-sdk-js-0.1.0.tgz ./dipswitch-sdk-react-0.1.0.tgz
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
After the packages are published, install from npm.
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
npm install @dipswitch/sdk-react
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## First use
|
|
20
|
+
|
|
21
|
+
```tsx
|
|
22
|
+
import { DipswitchProvider, useDipswitch, useFlag } from '@dipswitch/sdk-react'
|
|
23
|
+
|
|
24
|
+
function Billing() {
|
|
25
|
+
const newBilling = useFlag('new-billing', false)
|
|
26
|
+
const client = useDipswitch()
|
|
27
|
+
return (
|
|
28
|
+
<>
|
|
29
|
+
{newBilling ? <p>New billing</p> : <p>Billing</p>}
|
|
30
|
+
<button disabled={!client} onClick={() => { void client?.identify(undefined) }}>
|
|
31
|
+
Clear flag user
|
|
32
|
+
</button>
|
|
33
|
+
</>
|
|
34
|
+
)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function App() {
|
|
38
|
+
return (
|
|
39
|
+
<DipswitchProvider options={{ clientKey: 'your-public-client-key', user: { id: 'user-123' } }}>
|
|
40
|
+
<Billing />
|
|
41
|
+
</DipswitchProvider>
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
The provider reads `options` on mount. Pass `edgeUrl` to use a local Dipswitch edge. Use a React `key` to mount a new provider if the client key or edge address changes.
|
|
47
|
+
|
|
48
|
+
`useFlag` follows flag and user changes. Server-rendered pages and their first render in the browser show the supplied defaults. The browser then reads the current user's cached or loaded flags. `useDipswitch` returns `undefined` until the provider starts the client in the browser. Use the client’s `ready` promise to wait for the initial load and `identify(user)` to switch users. Pass `undefined` for signed-out values. While the new user’s request is pending, flag reads use that user's cache or defaults. A provider hidden and shown again by React Activity resumes with the last identified user.
|
|
49
|
+
|
|
50
|
+
## Identity verification
|
|
51
|
+
|
|
52
|
+
For an environment that verifies identity, pass the signature your backend issued for the current user. Give a new user and their signature to the client returned by `useDipswitch`.
|
|
53
|
+
|
|
54
|
+
```tsx
|
|
55
|
+
import { DipswitchProvider, useDipswitch, useFlag } from '@dipswitch/sdk-react'
|
|
56
|
+
|
|
57
|
+
type SignedUser = { id: string; signature: string }
|
|
58
|
+
|
|
59
|
+
function Billing({ next }: { next: SignedUser }) {
|
|
60
|
+
const client = useDipswitch()
|
|
61
|
+
const enabled = useFlag('new-billing', false)
|
|
62
|
+
return (
|
|
63
|
+
<button onClick={() => void client?.identify({ id: next.id }, next.signature)}>
|
|
64
|
+
{enabled ? 'New billing' : 'Billing'}
|
|
65
|
+
</button>
|
|
66
|
+
)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function App({ clientKey, first, next }: { clientKey: string; first: SignedUser; next: SignedUser }) {
|
|
70
|
+
return (
|
|
71
|
+
<DipswitchProvider options={{ clientKey, user: { id: first.id }, signature: first.signature }}>
|
|
72
|
+
<Billing next={next} />
|
|
73
|
+
</DipswitchProvider>
|
|
74
|
+
)
|
|
75
|
+
}
|
|
76
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ClientOptions, ClientUser, DipswitchClient } from "@dipswitch/sdk-js";
|
|
2
|
+
import { ReactNode } from "react";
|
|
3
|
+
export interface DipswitchProviderProps {
|
|
4
|
+
/** Browser SDK settings read when this provider mounts. Switch users with `identify`. */
|
|
5
|
+
options: Omit<ClientOptions, 'refetchDelayMaxMs'>;
|
|
6
|
+
children?: ReactNode;
|
|
7
|
+
}
|
|
8
|
+
export declare function DipswitchProvider({ options, children }: DipswitchProviderProps): import("react").FunctionComponentElement<import("react").ProviderProps<DipswitchClient | null | undefined>>;
|
|
9
|
+
/** The native client, once the provider has started it in the browser. */
|
|
10
|
+
export declare function useDipswitch(): DipswitchClient | undefined;
|
|
11
|
+
/** Re-renders when the current user's flag changes. The server renders `defaultValue`. */
|
|
12
|
+
export declare function useFlag(flagKey: string, defaultValue: boolean): boolean;
|
|
13
|
+
export type { ClientOptions, ClientUser, DipswitchClient };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { ADAPTER_REFETCH_DELAY_MAX_MS, createClient } from "@dipswitch/sdk-js";
|
|
2
|
+
import { createContext, createElement, useContext, useEffect, useRef, useState, useSyncExternalStore } from "react";
|
|
3
|
+
// src/index.ts
|
|
4
|
+
const ClientContext = createContext(null);
|
|
5
|
+
const noSubscription = () => () => void 0;
|
|
6
|
+
function copyUser(user) {
|
|
7
|
+
if (typeof user !== "object" || user === null) return user;
|
|
8
|
+
return {
|
|
9
|
+
id: user.id,
|
|
10
|
+
name: user.name
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
function DipswitchProvider({ options, children }) {
|
|
14
|
+
const [initialOptions] = useState(() => ({
|
|
15
|
+
...options,
|
|
16
|
+
user: copyUser(options.user),
|
|
17
|
+
refetchDelayMaxMs: ADAPTER_REFETCH_DELAY_MAX_MS
|
|
18
|
+
}));
|
|
19
|
+
const current = useRef({
|
|
20
|
+
user: initialOptions.user,
|
|
21
|
+
signature: initialOptions.signature
|
|
22
|
+
});
|
|
23
|
+
const [client, setClient] = useState();
|
|
24
|
+
useEffect(() => {
|
|
25
|
+
const sdk = createClient({
|
|
26
|
+
...initialOptions,
|
|
27
|
+
...current.current
|
|
28
|
+
});
|
|
29
|
+
const identify = sdk.identify;
|
|
30
|
+
let active = true;
|
|
31
|
+
sdk.identify = (user, signature) => {
|
|
32
|
+
if (!active) return Promise.resolve();
|
|
33
|
+
current.current = {
|
|
34
|
+
user: copyUser(user),
|
|
35
|
+
signature
|
|
36
|
+
};
|
|
37
|
+
return identify(current.current.user, signature);
|
|
38
|
+
};
|
|
39
|
+
setClient(sdk);
|
|
40
|
+
return () => {
|
|
41
|
+
active = false;
|
|
42
|
+
sdk.close();
|
|
43
|
+
};
|
|
44
|
+
}, [initialOptions]);
|
|
45
|
+
return createElement(ClientContext.Provider, { value: client }, children);
|
|
46
|
+
}
|
|
47
|
+
/** The native client, once the provider has started it in the browser. */
|
|
48
|
+
function useDipswitch() {
|
|
49
|
+
const client = useContext(ClientContext);
|
|
50
|
+
if (client === null) throw new Error("useFlag and useDipswitch need a DipswitchProvider.");
|
|
51
|
+
return client;
|
|
52
|
+
}
|
|
53
|
+
/** Re-renders when the current user's flag changes. The server renders `defaultValue`. */
|
|
54
|
+
function useFlag(flagKey, defaultValue) {
|
|
55
|
+
const client = useDipswitch();
|
|
56
|
+
return useSyncExternalStore(client?.onChange ?? noSubscription, () => client?.isEnabled(flagKey, defaultValue) ?? defaultValue, () => defaultValue);
|
|
57
|
+
}
|
|
58
|
+
export { DipswitchProvider, useDipswitch, useFlag };
|
package/package.json
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dipswitch/sdk-react",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Dipswitch feature flags for React.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"default": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"module": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"sideEffects": false,
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@dipswitch/sdk-js": "0.1.0"
|
|
21
|
+
},
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"react": "^19.3.1"
|
|
24
|
+
}
|
|
6
25
|
}
|