@benchdog-ts/react 0.0.0-stage → 0.3.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 +16 -2
- package/dist/index.d.ts +11 -0
- package/dist/index.js +30 -0
- package/dist/index.js.map +1 -0
- package/package.json +54 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Joe Gaudet
|
|
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,17 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @benchdog-ts/react
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[Benchdog](https://joegaudet.com/benchdog/) for React 18 and later.
|
|
4
|
+
|
|
5
|
+
```tsx
|
|
6
|
+
import { createClient, readPayload } from '@benchdog-ts/client'
|
|
7
|
+
import { BenchdogProvider, useEnabled, useFlagValue } from '@benchdog-ts/react'
|
|
8
|
+
|
|
9
|
+
const client = createClient({ flags, payload: readPayload(), live: true })
|
|
10
|
+
|
|
11
|
+
<BenchdogProvider client={client}><App /></BenchdogProvider>
|
|
12
|
+
|
|
13
|
+
const on = useEnabled<typeof flags>('new-checkout')
|
|
14
|
+
const theme = useFlagValue<typeof flags, 'theme'>('theme', 'classic')
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
The hooks use `useSyncExternalStore`, so a component re-renders when its value changes.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { BooleanKey, BrowserClient, FlagDeclarations, ValueOf } from '@benchdog-ts/client';
|
|
2
|
+
import type { ReactNode } from 'react';
|
|
3
|
+
/** Gives the client to the components below it. */
|
|
4
|
+
export declare function BenchdogProvider<D extends FlagDeclarations>(props: {
|
|
5
|
+
client: BrowserClient<D>;
|
|
6
|
+
children?: ReactNode;
|
|
7
|
+
}): import("react").FunctionComponentElement<import("react").ProviderProps<unknown>>;
|
|
8
|
+
/** Is a boolean flag on? The component re-renders when the value changes. */
|
|
9
|
+
export declare function useEnabled<D extends FlagDeclarations>(key: BooleanKey<D>): boolean;
|
|
10
|
+
/** The value of a flag, or the default. The component re-renders when the value changes. */
|
|
11
|
+
export declare function useFlagValue<D extends FlagDeclarations, K extends keyof D & string>(key: K, defaultValue: ValueOf<D[K]>): ValueOf<D[K]>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { createContext, createElement, useSyncExternalStore, useContext } from 'react';
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
var Context = createContext(null);
|
|
5
|
+
function BenchdogProvider(props) {
|
|
6
|
+
return createElement(Context.Provider, { value: props.client }, props.children);
|
|
7
|
+
}
|
|
8
|
+
function useClient() {
|
|
9
|
+
const client = useContext(Context);
|
|
10
|
+
if (!client)
|
|
11
|
+
throw new Error("Benchdog: put a <BenchdogProvider client={...}> above this component");
|
|
12
|
+
return client;
|
|
13
|
+
}
|
|
14
|
+
function useEnabled(key) {
|
|
15
|
+
const client = useClient();
|
|
16
|
+
return useSyncExternalStore(
|
|
17
|
+
(listener) => client.subscribe(listener),
|
|
18
|
+
() => client.enabled(key),
|
|
19
|
+
() => client.enabled(key)
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
function useFlagValue(key, defaultValue) {
|
|
23
|
+
const client = useClient();
|
|
24
|
+
const read = () => client.value(key, defaultValue);
|
|
25
|
+
return useSyncExternalStore((listener) => client.subscribe(listener), read, read);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export { BenchdogProvider, useEnabled, useFlagValue };
|
|
29
|
+
//# sourceMappingURL=index.js.map
|
|
30
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;AAKA,IAAM,OAAA,GAAU,cAAuB,IAAI,CAAA;AAGpC,SAAS,iBAA6C,KAAA,EAG1D;AACD,EAAA,OAAO,aAAA,CAAc,QAAQ,QAAA,EAAU,EAAE,OAAO,KAAA,CAAM,MAAA,EAAO,EAAG,KAAA,CAAM,QAAQ,CAAA;AAChF;AAEA,SAAS,SAAA,GAA0D;AACjE,EAAA,MAAM,MAAA,GAAS,WAAW,OAAO,CAAA;AACjC,EAAA,IAAI,CAAC,MAAA;AACH,IAAA,MAAM,IAAI,MAAM,sEAAsE,CAAA;AACxF,EAAA,OAAO,MAAA;AACT;AAGO,SAAS,WAAuC,GAAA,EAA6B;AAClF,EAAA,MAAM,SAAS,SAAA,EAAa;AAC5B,EAAA,OAAO,oBAAA;AAAA,IACL,CAAA,QAAA,KAAY,MAAA,CAAO,SAAA,CAAU,QAAQ,CAAA;AAAA,IACrC,MAAM,MAAA,CAAO,OAAA,CAAQ,GAAG,CAAA;AAAA,IACxB,MAAM,MAAA,CAAO,OAAA,CAAQ,GAAG;AAAA,GAC1B;AACF;AAGO,SAAS,YAAA,CACd,KACA,YAAA,EACe;AACf,EAAA,MAAM,SAAS,SAAA,EAAa;AAC5B,EAAA,MAAM,IAAA,GAAO,MAAM,MAAA,CAAO,KAAA,CAAM,KAAK,YAAY,CAAA;AACjD,EAAA,OAAO,qBAAqB,CAAA,QAAA,KAAY,MAAA,CAAO,UAAU,QAAQ,CAAA,EAAG,MAAM,IAAI,CAAA;AAChF","file":"index.js","sourcesContent":["import type { BooleanKey, BrowserClient, FlagDeclarations, ValueOf } from '@benchdog-ts/client'\nimport { createContext, createElement, useContext, useSyncExternalStore } from 'react'\nimport type { ReactNode } from 'react'\n\n// Each app types its own client; the context holds it without the flag types.\nconst Context = createContext<unknown>(null)\n\n/** Gives the client to the components below it. */\nexport function BenchdogProvider<D extends FlagDeclarations>(props: {\n client: BrowserClient<D>\n children?: ReactNode\n}) {\n return createElement(Context.Provider, { value: props.client }, props.children)\n}\n\nfunction useClient<D extends FlagDeclarations>(): BrowserClient<D> {\n const client = useContext(Context)\n if (!client)\n throw new Error('Benchdog: put a <BenchdogProvider client={...}> above this component')\n return client as BrowserClient<D>\n}\n\n/** Is a boolean flag on? The component re-renders when the value changes. */\nexport function useEnabled<D extends FlagDeclarations>(key: BooleanKey<D>): boolean {\n const client = useClient<D>()\n return useSyncExternalStore(\n listener => client.subscribe(listener),\n () => client.enabled(key),\n () => client.enabled(key),\n )\n}\n\n/** The value of a flag, or the default. The component re-renders when the value changes. */\nexport function useFlagValue<D extends FlagDeclarations, K extends keyof D & string>(\n key: K,\n defaultValue: ValueOf<D[K]>,\n): ValueOf<D[K]> {\n const client = useClient<D>()\n const read = () => client.value(key, defaultValue)\n return useSyncExternalStore(listener => client.subscribe(listener), read, read)\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@benchdog-ts/react",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Benchdog for React: a provider and hooks that re-render when a flag changes.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"feature-flags",
|
|
7
|
+
"benchdog",
|
|
8
|
+
"react"
|
|
9
|
+
],
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"author": "Joe Gaudet joe@joegaudet.com",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/joegaudet/benchdog.git",
|
|
15
|
+
"directory": "packages/react"
|
|
16
|
+
},
|
|
17
|
+
"homepage": "https://joegaudet.com/benchdog/",
|
|
18
|
+
"bugs": "https://github.com/joegaudet/benchdog/issues",
|
|
19
|
+
"type": "module",
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"default": "./dist/index.js"
|
|
25
|
+
},
|
|
26
|
+
"./package.json": "./package.json"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist",
|
|
30
|
+
"README.md",
|
|
31
|
+
"LICENSE"
|
|
32
|
+
],
|
|
33
|
+
"sideEffects": false,
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"access": "public"
|
|
36
|
+
},
|
|
37
|
+
"engines": {
|
|
38
|
+
"node": ">=22.12.0"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@types/react": "^19.3.3",
|
|
42
|
+
"@types/react-dom": "^19.3.0",
|
|
43
|
+
"jsdom": "^29.1.1",
|
|
44
|
+
"react": "^19.3.1",
|
|
45
|
+
"react-dom": "^19.3.1"
|
|
46
|
+
},
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"@benchdog-ts/client": "^0.3.0"
|
|
49
|
+
},
|
|
50
|
+
"peerDependencies": {
|
|
51
|
+
"react": ">=18"
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"build": "tsup",
|
|
55
|
+
"test": "vitest run"
|
|
56
|
+
}
|
|
6
57
|
}
|