@flagdock/react 0.0.0-stage → 0.3.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/LICENSE +21 -0
- package/README.md +91 -2
- package/dist/index.d.ts +60 -0
- package/dist/index.js +125 -0
- package/package.json +60 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Flagdock
|
|
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,92 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @flagdock/react
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[Flagdock](https://flagdock.com) feature flags for React.
|
|
4
|
+
|
|
5
|
+
Hooks over [`@flagdock/web`](https://www.npmjs.com/package/@flagdock/web). Flagdock's edge
|
|
6
|
+
evaluates the rules and the page only receives answers for the current person, so your targeting
|
|
7
|
+
rules never reach the browser. Works with server rendering (Next.js, Remix, Vite SSR) and with
|
|
8
|
+
React 18 and 19.
|
|
9
|
+
|
|
10
|
+
## Quickstart
|
|
11
|
+
|
|
12
|
+
1. Install:
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
npm install @flagdock/react @openfeature/web-sdk
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
2. Create a **browser** SDK key in the Flagdock console (SDK keys page). It is safe to ship in
|
|
19
|
+
your page: it can only fetch answers.
|
|
20
|
+
|
|
21
|
+
3. Wrap your app and read flags:
|
|
22
|
+
|
|
23
|
+
```tsx
|
|
24
|
+
import { FlagdockProvider, useFlag } from '@flagdock/react';
|
|
25
|
+
|
|
26
|
+
export function App({ user }) {
|
|
27
|
+
return (
|
|
28
|
+
<FlagdockProvider sdkKey="hbf_web_…" appName="shop-web" context={{ targetingKey: user.id, plan: user.plan }}>
|
|
29
|
+
<Checkout />
|
|
30
|
+
</FlagdockProvider>
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function Checkout() {
|
|
35
|
+
const newCheckout = useFlag('new-checkout', false); // boolean
|
|
36
|
+
const deposit = useFlag('booking-deposit-pct', 30); // number
|
|
37
|
+
return newCheckout ? <NewCheckout deposit={deposit} /> : <OldCheckout />;
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
The kind of flag comes from the default you pass: `false` reads a boolean flag, `''` a string,
|
|
42
|
+
`0` a number and `{}` a JSON flag. Until there is an answer, and for a flag that does not exist,
|
|
43
|
+
the hook returns your default.
|
|
44
|
+
|
|
45
|
+
## No flicker, even on a slow network
|
|
46
|
+
|
|
47
|
+
The first render can already show the right values:
|
|
48
|
+
|
|
49
|
+
- **Answers from your server.** If your server uses [`@flagdock/node`](https://www.npmjs.com/package/@flagdock/node),
|
|
50
|
+
compute the answers while rendering and pass them in. They contain answers only, never rules.
|
|
51
|
+
|
|
52
|
+
```tsx
|
|
53
|
+
// On the server
|
|
54
|
+
const bootstrap = flagdock.answersFor({ targetingKey: user.id, plan: user.plan });
|
|
55
|
+
// In the page
|
|
56
|
+
<FlagdockProvider sdkKey="hbf_web_…" context={{ targetingKey: user.id, plan: user.plan }} bootstrap={bootstrap}>
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
- **Answers saved on the last visit.** The browser keeps the answers for the last five people
|
|
60
|
+
(as a hash, with no names or emails), so a returning visitor starts at once.
|
|
61
|
+
|
|
62
|
+
Fresh answers replace them as soon as the edge replies. If the edge cannot be reached, the last
|
|
63
|
+
answers stay on screen and the provider tries again with growing pauses. Pass
|
|
64
|
+
`waitForServer` to wait for the edge first instead.
|
|
65
|
+
|
|
66
|
+
## Hooks
|
|
67
|
+
|
|
68
|
+
| Hook | Returns |
|
|
69
|
+
| --- | --- |
|
|
70
|
+
| `useFlag(key, default)` | The value. |
|
|
71
|
+
| `useFlagDetails(key, default)` | `{ value, variant, reason, errorCode, isDefault }`. `reason` is `CACHED` for saved or server-rendered answers and `STALE` while the edge cannot be reached. |
|
|
72
|
+
| `useFlagdock()` | `{ ready, stale, source, track, refresh, provider }`. `track('order-completed', order.total)` reports a goal event. |
|
|
73
|
+
|
|
74
|
+
When the `context` changes (someone signs in), new answers are fetched; the previous values stay
|
|
75
|
+
on screen until they arrive, so nothing flashes back to the defaults.
|
|
76
|
+
|
|
77
|
+
`FlagdockProvider` also registers with OpenFeature (pass `domain` to use a named domain), so
|
|
78
|
+
`OpenFeature.getClient()` and OpenFeature hooks work alongside these.
|
|
79
|
+
|
|
80
|
+
## Options
|
|
81
|
+
|
|
82
|
+
Everything [`@flagdock/web`](https://www.npmjs.com/package/@flagdock/web) takes, plus:
|
|
83
|
+
|
|
84
|
+
| Prop | What it does |
|
|
85
|
+
| --- | --- |
|
|
86
|
+
| `sdkKey` | A browser SDK key (`hbf_web_…`). |
|
|
87
|
+
| `context` | Who is using the page. A new object with the same content does not refetch. |
|
|
88
|
+
| `bootstrap` | Answers to start from, from `answersFor` in `@flagdock/node`. |
|
|
89
|
+
| `provider` | A `FlagdockWebProvider` you made yourself, instead of `sdkKey`. |
|
|
90
|
+
| `domain` | OpenFeature domain to register under. |
|
|
91
|
+
|
|
92
|
+
MIT licensed. Questions: info@flagdock.com
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
2
|
+
import { type EvaluationContext, type JsonValue } from '@openfeature/web-sdk';
|
|
3
|
+
import { FlagdockWebProvider, type AnswersSource, type FlagdockAnswers, type FlagdockWebOptions } from '@flagdock/web';
|
|
4
|
+
export { FlagdockWebProvider, type AnswersSource, type FlagdockAnswers, type FlagdockWebOptions };
|
|
5
|
+
export interface FlagdockProviderProps extends Omit<FlagdockWebOptions, 'key'> {
|
|
6
|
+
/** A browser SDK key (`hbf_web_…`). Called `sdkKey` because React keeps `key` for itself. */
|
|
7
|
+
sdkKey?: string;
|
|
8
|
+
/** Who is using the page: `targetingKey` plus any attributes your rules look at. Changing it fetches new answers. */
|
|
9
|
+
context?: EvaluationContext;
|
|
10
|
+
/** A provider you made yourself, instead of `sdkKey` and the options. */
|
|
11
|
+
provider?: FlagdockWebProvider;
|
|
12
|
+
/** The OpenFeature domain to register under. Leave it out to be OpenFeature's default provider. */
|
|
13
|
+
domain?: string;
|
|
14
|
+
children?: ReactNode;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Makes Flagdock available to the components inside it. Put it near the top of the app.
|
|
18
|
+
*
|
|
19
|
+
* Flags can be read in the very first render, also on the server, when the page was given
|
|
20
|
+
* `bootstrap` answers or the browser saved answers for this person on an earlier visit. The
|
|
21
|
+
* provider is also registered with OpenFeature, so `OpenFeature.getClient()` works alongside the hooks.
|
|
22
|
+
*/
|
|
23
|
+
export declare function FlagdockProvider(props: FlagdockProviderProps): ReactNode;
|
|
24
|
+
export type FlagValue = boolean | string | number | JsonValue;
|
|
25
|
+
/** Widens literal types, so `useFlag('x', false)` gives a `boolean`, not `false`. */
|
|
26
|
+
type Widen<T> = T extends boolean ? boolean : T extends string ? string : T extends number ? number : T;
|
|
27
|
+
export interface FlagDetails<T> {
|
|
28
|
+
value: T;
|
|
29
|
+
/** The value's key, such as `on` or `b`; missing when the default was used. */
|
|
30
|
+
variant?: string;
|
|
31
|
+
/** Why: TARGETING_MATCH, SPLIT, DEFAULT, DISABLED, CACHED (saved answers, the server has not replied yet), STALE (the server cannot be reached) or ERROR. */
|
|
32
|
+
reason?: string;
|
|
33
|
+
errorCode?: string;
|
|
34
|
+
errorMessage?: string;
|
|
35
|
+
/** True when this is the default you passed in, because there was no usable answer. */
|
|
36
|
+
isDefault: boolean;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* A flag's value and why. The kind of flag is taken from the default: `false` reads a boolean flag,
|
|
40
|
+
* `''` a string, `0` a number, `{}` or `[]` a JSON flag. While the person changes, the previous value
|
|
41
|
+
* stays on screen until the new answers arrive, instead of flashing the default.
|
|
42
|
+
*/
|
|
43
|
+
export declare function useFlagDetails<T extends FlagValue>(flagKey: string, defaultValue: T): FlagDetails<Widen<T>>;
|
|
44
|
+
/** A flag's value, or `defaultValue` when there is no usable answer. See `useFlagDetails`. */
|
|
45
|
+
export declare function useFlag<T extends FlagValue>(flagKey: string, defaultValue: T): Widen<T>;
|
|
46
|
+
export interface FlagdockState {
|
|
47
|
+
provider: FlagdockWebProvider;
|
|
48
|
+
/** True once there are answers to read, from the server, storage or the page. */
|
|
49
|
+
ready: boolean;
|
|
50
|
+
/** True while the server cannot be reached and the last known answers are shown. */
|
|
51
|
+
stale: boolean;
|
|
52
|
+
/** Where the answers came from. */
|
|
53
|
+
source: AnswersSource | undefined;
|
|
54
|
+
/** Reports a goal event for the current person, such as `track('order-completed', order.total)`. */
|
|
55
|
+
track: (eventName: string, value?: number) => void;
|
|
56
|
+
/** Fetches the answers again now. */
|
|
57
|
+
refresh: () => Promise<void>;
|
|
58
|
+
}
|
|
59
|
+
/** The provider and its state, for loading screens, banners and goal events. */
|
|
60
|
+
export declare function useFlagdock(): FlagdockState;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { createContext, createElement, useCallback, useContext, useEffect, useRef, useState, useSyncExternalStore, } from 'react';
|
|
3
|
+
import { NOOP_PROVIDER, OpenFeature } from '@openfeature/web-sdk';
|
|
4
|
+
import { FlagdockWebProvider } from '@flagdock/web';
|
|
5
|
+
export { FlagdockWebProvider };
|
|
6
|
+
const FlagdockContext = createContext(null);
|
|
7
|
+
/** JSON with object keys sorted, so a context rebuilt on every render does not look like a change. */
|
|
8
|
+
function stableJson(value) {
|
|
9
|
+
return JSON.stringify(value ?? {}, (_key, v) => v && typeof v === 'object' && !Array.isArray(v)
|
|
10
|
+
? Object.fromEntries(Object.entries(v).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)))
|
|
11
|
+
: v);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Makes Flagdock available to the components inside it. Put it near the top of the app.
|
|
15
|
+
*
|
|
16
|
+
* Flags can be read in the very first render, also on the server, when the page was given
|
|
17
|
+
* `bootstrap` answers or the browser saved answers for this person on an earlier visit. The
|
|
18
|
+
* provider is also registered with OpenFeature, so `OpenFeature.getClient()` works alongside the hooks.
|
|
19
|
+
*/
|
|
20
|
+
export function FlagdockProvider(props) {
|
|
21
|
+
const { children, context = {}, domain, provider: given, sdkKey, ...options } = props;
|
|
22
|
+
const contextJson = stableJson(context);
|
|
23
|
+
// Made once per mount, and primed at once, so the first render already has answers when there are some.
|
|
24
|
+
const [provider] = useState(() => {
|
|
25
|
+
if (!given && !sdkKey)
|
|
26
|
+
throw new Error('FlagdockProvider needs sdkKey (a browser SDK key) or provider');
|
|
27
|
+
const p = given ?? new FlagdockWebProvider({ key: sdkKey, ...options });
|
|
28
|
+
p.prime(context);
|
|
29
|
+
return p;
|
|
30
|
+
});
|
|
31
|
+
const [binding] = useState(() => ({ provider, domain }));
|
|
32
|
+
const mounted = useRef(false);
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
mounted.current = true;
|
|
35
|
+
const ctx = JSON.parse(contextJson);
|
|
36
|
+
const registered = domain ? OpenFeature.getProvider(domain) : OpenFeature.getProvider();
|
|
37
|
+
if (registered !== provider) {
|
|
38
|
+
// The context first, so the provider starts with the right person.
|
|
39
|
+
void (domain ? OpenFeature.setContext(domain, ctx) : OpenFeature.setContext(ctx));
|
|
40
|
+
void (domain ? OpenFeature.setProvider(domain, provider) : OpenFeature.setProvider(provider));
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
void (domain ? OpenFeature.setContext(domain, ctx) : OpenFeature.setContext(ctx));
|
|
44
|
+
}
|
|
45
|
+
return () => {
|
|
46
|
+
mounted.current = false;
|
|
47
|
+
// React may unmount and mount again straight away (Strict Mode), so only close if it stays unmounted.
|
|
48
|
+
setTimeout(() => {
|
|
49
|
+
if (mounted.current)
|
|
50
|
+
return;
|
|
51
|
+
const current = domain ? OpenFeature.getProvider(domain) : OpenFeature.getProvider();
|
|
52
|
+
if (current === provider)
|
|
53
|
+
void (domain ? OpenFeature.setProvider(domain, NOOP_PROVIDER) : OpenFeature.setProvider(NOOP_PROVIDER));
|
|
54
|
+
else
|
|
55
|
+
void provider.onClose();
|
|
56
|
+
}, 0);
|
|
57
|
+
};
|
|
58
|
+
}, [provider, domain, contextJson]);
|
|
59
|
+
return createElement(FlagdockContext.Provider, { value: binding }, children);
|
|
60
|
+
}
|
|
61
|
+
function useBinding(hook) {
|
|
62
|
+
const binding = useContext(FlagdockContext);
|
|
63
|
+
if (!binding)
|
|
64
|
+
throw new Error(`${hook} must be used inside <FlagdockProvider>`);
|
|
65
|
+
return binding;
|
|
66
|
+
}
|
|
67
|
+
/** Re-renders the component whenever the answers or the provider's state change. */
|
|
68
|
+
function useVersion(provider) {
|
|
69
|
+
const version = useRef(0);
|
|
70
|
+
const subscribe = useCallback((onChange) => provider.subscribe(() => {
|
|
71
|
+
version.current++;
|
|
72
|
+
onChange();
|
|
73
|
+
}), [provider]);
|
|
74
|
+
const read = () => version.current;
|
|
75
|
+
return useSyncExternalStore(subscribe, read, read);
|
|
76
|
+
}
|
|
77
|
+
function resolve(provider, key, fallback) {
|
|
78
|
+
switch (typeof fallback) {
|
|
79
|
+
case 'boolean':
|
|
80
|
+
return provider.resolveBooleanEvaluation(key, fallback);
|
|
81
|
+
case 'string':
|
|
82
|
+
return provider.resolveStringEvaluation(key, fallback);
|
|
83
|
+
case 'number':
|
|
84
|
+
return provider.resolveNumberEvaluation(key, fallback);
|
|
85
|
+
default:
|
|
86
|
+
return provider.resolveObjectEvaluation(key, fallback);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* A flag's value and why. The kind of flag is taken from the default: `false` reads a boolean flag,
|
|
91
|
+
* `''` a string, `0` a number, `{}` or `[]` a JSON flag. While the person changes, the previous value
|
|
92
|
+
* stays on screen until the new answers arrive, instead of flashing the default.
|
|
93
|
+
*/
|
|
94
|
+
export function useFlagDetails(flagKey, defaultValue) {
|
|
95
|
+
const { provider } = useBinding('useFlagDetails');
|
|
96
|
+
useVersion(provider);
|
|
97
|
+
const last = useRef(undefined);
|
|
98
|
+
const r = resolve(provider, flagKey, defaultValue);
|
|
99
|
+
const isDefault = r.reason === 'ERROR';
|
|
100
|
+
if (isDefault && provider.answersSource === undefined && last.current && !last.current.isDefault)
|
|
101
|
+
return last.current;
|
|
102
|
+
const details = {
|
|
103
|
+
value: r.value,
|
|
104
|
+
isDefault,
|
|
105
|
+
...(r.variant !== undefined ? { variant: r.variant } : {}),
|
|
106
|
+
...(r.reason !== undefined ? { reason: r.reason } : {}),
|
|
107
|
+
...(r.errorCode !== undefined ? { errorCode: r.errorCode } : {}),
|
|
108
|
+
...(r.errorMessage !== undefined ? { errorMessage: r.errorMessage } : {}),
|
|
109
|
+
};
|
|
110
|
+
last.current = details;
|
|
111
|
+
return details;
|
|
112
|
+
}
|
|
113
|
+
/** A flag's value, or `defaultValue` when there is no usable answer. See `useFlagDetails`. */
|
|
114
|
+
export function useFlag(flagKey, defaultValue) {
|
|
115
|
+
return useFlagDetails(flagKey, defaultValue).value;
|
|
116
|
+
}
|
|
117
|
+
/** The provider and its state, for loading screens, banners and goal events. */
|
|
118
|
+
export function useFlagdock() {
|
|
119
|
+
const { provider } = useBinding('useFlagdock');
|
|
120
|
+
useVersion(provider);
|
|
121
|
+
const track = useCallback((eventName, value) => provider.track(eventName, undefined, value !== undefined ? { value } : {}), [provider]);
|
|
122
|
+
const refresh = useCallback(() => provider.refresh(), [provider]);
|
|
123
|
+
return { provider, ready: provider.answersSource !== undefined, stale: provider.isStale, source: provider.answersSource, track, refresh };
|
|
124
|
+
}
|
|
125
|
+
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,62 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flagdock/react",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.3.1",
|
|
4
|
+
"description": "Flagdock feature flags for React: hooks over the Flagdock web provider, with server-rendered answers and no flicker",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"feature-flags",
|
|
7
|
+
"feature-toggles",
|
|
8
|
+
"react",
|
|
9
|
+
"hooks",
|
|
10
|
+
"openfeature",
|
|
11
|
+
"flagdock"
|
|
12
|
+
],
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"author": "Flagdock",
|
|
15
|
+
"homepage": "https://flagdock.com",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/paleka/flagdock-js.git",
|
|
19
|
+
"directory": "packages/react"
|
|
20
|
+
},
|
|
21
|
+
"bugs": {
|
|
22
|
+
"email": "info@flagdock.com"
|
|
23
|
+
},
|
|
24
|
+
"type": "module",
|
|
25
|
+
"main": "./dist/index.js",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"default": "./dist/index.js"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"dist",
|
|
35
|
+
"!dist/**/*.map",
|
|
36
|
+
"!dist/*.tsbuildinfo",
|
|
37
|
+
"README.md",
|
|
38
|
+
"LICENSE"
|
|
39
|
+
],
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "tsc -b"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"@flagdock/web": "0.3.1"
|
|
45
|
+
},
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"@openfeature/web-sdk": "^1.4.0",
|
|
48
|
+
"react": ">=18"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@openfeature/web-sdk": "^1.4.0",
|
|
52
|
+
"@types/react": "^19.0.0",
|
|
53
|
+
"@types/react-dom": "^19.0.0",
|
|
54
|
+
"jsdom": "^27.0.0",
|
|
55
|
+
"react": "^19.0.0",
|
|
56
|
+
"react-dom": "^19.0.0"
|
|
57
|
+
},
|
|
58
|
+
"sideEffects": false,
|
|
59
|
+
"publishConfig": {
|
|
60
|
+
"access": "public"
|
|
61
|
+
}
|
|
62
|
+
}
|