@hyphen/react-sdk 0.5.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 +264 -0
- package/dist/index.cjs +98 -0
- package/dist/index.d.cts +71 -0
- package/dist/index.d.ts +71 -0
- package/dist/index.js +71 -0
- package/package.json +61 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Hyphen
|
|
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
ADDED
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+

|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@hyphen/react-sdk)
|
|
4
|
+
[](https://www.npmjs.com/package/@hyphen/react-sdk)
|
|
5
|
+
[](https://github.com/hyphen/react-sdk/blob/main/LICENSE)
|
|
6
|
+
[](https://github.com/Hyphen/react-sdk/actions/workflows/tests.yaml)
|
|
7
|
+
[](https://codecov.io/gh/Hyphen/react-sdk)
|
|
8
|
+
|
|
9
|
+
# Hyphen React SDK
|
|
10
|
+
|
|
11
|
+
The Hyphen React SDK provides React components, hooks, and higher-order components (HOCs) for integrating Hyphen's feature flag and toggle service into your React applications.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @hyphen/react-sdk
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Quick Start
|
|
20
|
+
|
|
21
|
+
The SDK provides three ways to integrate Hyphen into your React application:
|
|
22
|
+
|
|
23
|
+
### 1. Higher-Order Component (HOC) Pattern
|
|
24
|
+
|
|
25
|
+
Wrap your root component with `withToggleProvider()`:
|
|
26
|
+
|
|
27
|
+
```tsx
|
|
28
|
+
import { withToggleProvider } from '@hyphen/react-sdk';
|
|
29
|
+
import App from './App';
|
|
30
|
+
|
|
31
|
+
export default withToggleProvider({
|
|
32
|
+
publicApiKey: 'public_...',
|
|
33
|
+
applicationId: 'my-app',
|
|
34
|
+
environment: 'production',
|
|
35
|
+
defaultContext: {
|
|
36
|
+
user: {
|
|
37
|
+
id: 'user-123',
|
|
38
|
+
email: 'user@example.com'
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
})(App);
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### 2. Provider Component Pattern
|
|
45
|
+
|
|
46
|
+
Use the `ToggleProvider` component directly:
|
|
47
|
+
|
|
48
|
+
```tsx
|
|
49
|
+
import { ToggleProvider } from '@hyphen/react-sdk';
|
|
50
|
+
import App from './App';
|
|
51
|
+
|
|
52
|
+
function Root() {
|
|
53
|
+
return (
|
|
54
|
+
<ToggleProvider
|
|
55
|
+
publicApiKey="public_..."
|
|
56
|
+
applicationId="my-app"
|
|
57
|
+
environment="production"
|
|
58
|
+
defaultContext={{
|
|
59
|
+
user: {
|
|
60
|
+
id: 'user-123',
|
|
61
|
+
email: 'user@example.com'
|
|
62
|
+
}
|
|
63
|
+
}}
|
|
64
|
+
>
|
|
65
|
+
<App />
|
|
66
|
+
</ToggleProvider>
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### 3. Using the `useToggle` Hook
|
|
72
|
+
|
|
73
|
+
Access feature flags in any component:
|
|
74
|
+
|
|
75
|
+
```tsx
|
|
76
|
+
import { useToggle } from '@hyphen/react-sdk';
|
|
77
|
+
|
|
78
|
+
function MyComponent() {
|
|
79
|
+
const toggle = useToggle();
|
|
80
|
+
|
|
81
|
+
// Get boolean feature flag
|
|
82
|
+
const isNewFeatureEnabled = toggle.getBoolean('new-feature', false);
|
|
83
|
+
|
|
84
|
+
// Get string feature flag
|
|
85
|
+
const theme = toggle.getString('theme', 'light');
|
|
86
|
+
|
|
87
|
+
// Get number feature flag
|
|
88
|
+
const maxItems = toggle.getNumber('max-items', 10);
|
|
89
|
+
|
|
90
|
+
// Get object feature flag
|
|
91
|
+
const config = toggle.getObject('ui-config', { layout: 'grid' });
|
|
92
|
+
|
|
93
|
+
return (
|
|
94
|
+
<div>
|
|
95
|
+
{isNewFeatureEnabled && <NewFeature />}
|
|
96
|
+
<p>Theme: {theme}</p>
|
|
97
|
+
<p>Max Items: {maxItems}</p>
|
|
98
|
+
</div>
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Configuration Options
|
|
104
|
+
|
|
105
|
+
All configuration options are passed to the Toggle instance from `@hyphen/browser-sdk`:
|
|
106
|
+
|
|
107
|
+
| Option | Type | Required | Description |
|
|
108
|
+
|--------|------|----------|-------------|
|
|
109
|
+
| `publicApiKey` | string | Yes | Your Hyphen public API key (starts with `public_`) |
|
|
110
|
+
| `applicationId` | string | No | Application identifier |
|
|
111
|
+
| `environment` | string | No | Environment name (e.g., 'development', 'production') |
|
|
112
|
+
| `defaultContext` | object | No | Default evaluation context (user, targeting, etc.) |
|
|
113
|
+
| `horizonUrls` | string[] | No | Custom Horizon endpoint URLs for load balancing |
|
|
114
|
+
| `defaultTargetKey` | string | No | Default targeting key |
|
|
115
|
+
|
|
116
|
+
### Default Context
|
|
117
|
+
|
|
118
|
+
The `defaultContext` option allows you to set default user and targeting information:
|
|
119
|
+
|
|
120
|
+
```tsx
|
|
121
|
+
{
|
|
122
|
+
defaultContext: {
|
|
123
|
+
targetingKey: 'user-123',
|
|
124
|
+
user: {
|
|
125
|
+
id: 'user-123',
|
|
126
|
+
email: 'user@example.com',
|
|
127
|
+
name: 'John Doe',
|
|
128
|
+
customAttributes: {
|
|
129
|
+
plan: 'premium',
|
|
130
|
+
region: 'us-west'
|
|
131
|
+
}
|
|
132
|
+
},
|
|
133
|
+
ipAddress: '192.168.1.1',
|
|
134
|
+
customAttributes: {
|
|
135
|
+
deviceType: 'mobile'
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## API Reference
|
|
142
|
+
|
|
143
|
+
### `ToggleProvider`
|
|
144
|
+
|
|
145
|
+
React context provider component that creates and provides a Toggle instance.
|
|
146
|
+
|
|
147
|
+
**Props:** Extends `ToggleOptions` from `@hyphen/browser-sdk` plus:
|
|
148
|
+
- `children`: ReactNode - Components to wrap with the provider
|
|
149
|
+
|
|
150
|
+
### `withToggleProvider(options)`
|
|
151
|
+
|
|
152
|
+
Higher-order component that wraps a component with `ToggleProvider`.
|
|
153
|
+
|
|
154
|
+
**Parameters:**
|
|
155
|
+
- `options`: `ToggleOptions` - Configuration for the Toggle instance
|
|
156
|
+
|
|
157
|
+
**Returns:** Function that accepts a component and returns a wrapped component
|
|
158
|
+
|
|
159
|
+
### `useToggle()`
|
|
160
|
+
|
|
161
|
+
React hook to access the Toggle instance from context.
|
|
162
|
+
|
|
163
|
+
**Returns:** `Toggle` instance from `@hyphen/browser-sdk`
|
|
164
|
+
|
|
165
|
+
**Throws:** Error if used outside of `ToggleProvider`
|
|
166
|
+
|
|
167
|
+
### Toggle Methods
|
|
168
|
+
|
|
169
|
+
The `Toggle` instance provides these methods:
|
|
170
|
+
|
|
171
|
+
- `getBoolean(key, defaultValue, options?)` - Get a boolean feature flag
|
|
172
|
+
- `getString(key, defaultValue, options?)` - Get a string feature flag
|
|
173
|
+
- `getNumber(key, defaultValue, options?)` - Get a number feature flag
|
|
174
|
+
- `getObject<T>(key, defaultValue, options?)` - Get an object feature flag
|
|
175
|
+
- `get<T>(key, defaultValue, options?)` - Generic getter for any type
|
|
176
|
+
|
|
177
|
+
All methods accept an optional `options` parameter for context overrides:
|
|
178
|
+
|
|
179
|
+
```tsx
|
|
180
|
+
const isEnabled = toggle.getBoolean('feature-key', false, {
|
|
181
|
+
context: {
|
|
182
|
+
user: { id: 'different-user' }
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## TypeScript Support
|
|
188
|
+
|
|
189
|
+
The SDK is written in TypeScript and provides full type definitions out of the box.
|
|
190
|
+
|
|
191
|
+
```tsx
|
|
192
|
+
import type { ToggleProviderProps } from '@hyphen/react-sdk';
|
|
193
|
+
|
|
194
|
+
const config: ToggleProviderProps = {
|
|
195
|
+
publicApiKey: 'public_...',
|
|
196
|
+
applicationId: 'my-app',
|
|
197
|
+
children: <App />
|
|
198
|
+
};
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
## Examples
|
|
202
|
+
|
|
203
|
+
### Conditional Rendering
|
|
204
|
+
|
|
205
|
+
```tsx
|
|
206
|
+
function FeatureFlag({ flagKey, children }) {
|
|
207
|
+
const toggle = useToggle();
|
|
208
|
+
const isEnabled = toggle.getBoolean(flagKey, false);
|
|
209
|
+
|
|
210
|
+
return isEnabled ? <>{children}</> : null;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// Usage
|
|
214
|
+
<FeatureFlag flagKey="new-dashboard">
|
|
215
|
+
<NewDashboard />
|
|
216
|
+
</FeatureFlag>
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
### A/B Testing
|
|
220
|
+
|
|
221
|
+
```tsx
|
|
222
|
+
function ABTest() {
|
|
223
|
+
const toggle = useToggle();
|
|
224
|
+
const variant = toggle.getString('homepage-variant', 'control');
|
|
225
|
+
|
|
226
|
+
switch (variant) {
|
|
227
|
+
case 'variant-a':
|
|
228
|
+
return <HomepageVariantA />;
|
|
229
|
+
case 'variant-b':
|
|
230
|
+
return <HomepageVariantB />;
|
|
231
|
+
default:
|
|
232
|
+
return <HomepageControl />;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
### Dynamic Configuration
|
|
238
|
+
|
|
239
|
+
```tsx
|
|
240
|
+
function ConfigurableComponent() {
|
|
241
|
+
const toggle = useToggle();
|
|
242
|
+
const config = toggle.getObject('component-config', {
|
|
243
|
+
maxItems: 10,
|
|
244
|
+
showImages: true,
|
|
245
|
+
layout: 'grid'
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
return (
|
|
249
|
+
<Component
|
|
250
|
+
maxItems={config.maxItems}
|
|
251
|
+
showImages={config.showImages}
|
|
252
|
+
layout={config.layout}
|
|
253
|
+
/>
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
# Contributing
|
|
259
|
+
|
|
260
|
+
We welcome contributions to the Hyphen Node.js SDK! If you have an idea for a new feature, bug fix, or improvement, please follow the [Contribution](./CONTRIBUTING.md) guidelines and our [Code of Conduct](./CODE_OF_CONDUCT.md).
|
|
261
|
+
|
|
262
|
+
# License and Copyright
|
|
263
|
+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
|
|
264
|
+
The copyright for this project is held by Hyphen, Inc. All rights reserved.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/index.ts
|
|
22
|
+
var index_exports = {};
|
|
23
|
+
__export(index_exports, {
|
|
24
|
+
Toggle: () => import_browser_sdk2.Toggle,
|
|
25
|
+
ToggleContext: () => ToggleContext,
|
|
26
|
+
ToggleProvider: () => ToggleProvider,
|
|
27
|
+
useToggle: () => useToggle,
|
|
28
|
+
withToggleProvider: () => withToggleProvider
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(index_exports);
|
|
31
|
+
var import_browser_sdk2 = require("@hyphen/browser-sdk");
|
|
32
|
+
|
|
33
|
+
// src/toggle-provider.tsx
|
|
34
|
+
var import_react = require("react");
|
|
35
|
+
var import_browser_sdk = require("@hyphen/browser-sdk");
|
|
36
|
+
var ToggleContext = /* @__PURE__ */ (0, import_react.createContext)(null);
|
|
37
|
+
function ToggleProvider({ children, publicApiKey, applicationId, environment, defaultContext, horizonUrls, defaultTargetKey }) {
|
|
38
|
+
const toggle = (0, import_react.useMemo)(() => {
|
|
39
|
+
return new import_browser_sdk.Toggle({
|
|
40
|
+
publicApiKey,
|
|
41
|
+
applicationId,
|
|
42
|
+
environment,
|
|
43
|
+
defaultContext,
|
|
44
|
+
horizonUrls,
|
|
45
|
+
defaultTargetKey
|
|
46
|
+
});
|
|
47
|
+
}, [
|
|
48
|
+
publicApiKey,
|
|
49
|
+
applicationId,
|
|
50
|
+
environment,
|
|
51
|
+
defaultContext,
|
|
52
|
+
horizonUrls,
|
|
53
|
+
defaultTargetKey
|
|
54
|
+
]);
|
|
55
|
+
(0, import_react.useEffect)(() => {
|
|
56
|
+
return () => {
|
|
57
|
+
};
|
|
58
|
+
}, [
|
|
59
|
+
toggle
|
|
60
|
+
]);
|
|
61
|
+
return /* @__PURE__ */ React.createElement(ToggleContext.Provider, {
|
|
62
|
+
value: toggle
|
|
63
|
+
}, children);
|
|
64
|
+
}
|
|
65
|
+
__name(ToggleProvider, "ToggleProvider");
|
|
66
|
+
|
|
67
|
+
// src/use-toggle.tsx
|
|
68
|
+
var import_react2 = require("react");
|
|
69
|
+
function useToggle() {
|
|
70
|
+
const toggle = (0, import_react2.useContext)(ToggleContext);
|
|
71
|
+
if (!toggle) {
|
|
72
|
+
throw new Error("useToggle must be used within a ToggleProvider. Wrap your component tree with <ToggleProvider> or use withToggleProvider().");
|
|
73
|
+
}
|
|
74
|
+
return toggle;
|
|
75
|
+
}
|
|
76
|
+
__name(useToggle, "useToggle");
|
|
77
|
+
|
|
78
|
+
// src/with-toggle-provider.tsx
|
|
79
|
+
function withToggleProvider(options) {
|
|
80
|
+
return function(Component) {
|
|
81
|
+
const WrappedComponent = /* @__PURE__ */ __name((props) => {
|
|
82
|
+
return /* @__PURE__ */ React.createElement(ToggleProvider, options, /* @__PURE__ */ React.createElement(Component, props));
|
|
83
|
+
}, "WrappedComponent");
|
|
84
|
+
const componentName = Component.displayName || Component.name || "Component";
|
|
85
|
+
WrappedComponent.displayName = `withToggleProvider(${componentName})`;
|
|
86
|
+
return WrappedComponent;
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
__name(withToggleProvider, "withToggleProvider");
|
|
90
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
91
|
+
0 && (module.exports = {
|
|
92
|
+
Toggle,
|
|
93
|
+
ToggleContext,
|
|
94
|
+
ToggleProvider,
|
|
95
|
+
useToggle,
|
|
96
|
+
withToggleProvider
|
|
97
|
+
});
|
|
98
|
+
/* v8 ignore next -- @preserve */
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { ToggleOptions, Toggle } from '@hyphen/browser-sdk';
|
|
2
|
+
export { Toggle, ToggleOptions } from '@hyphen/browser-sdk';
|
|
3
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
4
|
+
import { ReactNode, Context, ComponentType } from 'react';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Props for the ToggleProvider component
|
|
8
|
+
* Extends ToggleOptions from @hyphen/browser-sdk
|
|
9
|
+
*/
|
|
10
|
+
interface ToggleProviderProps extends ToggleOptions {
|
|
11
|
+
/**
|
|
12
|
+
* React children to be wrapped by the provider
|
|
13
|
+
*/
|
|
14
|
+
children: ReactNode;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* React Context for the Toggle instance
|
|
19
|
+
*/
|
|
20
|
+
declare const ToggleContext: Context<Toggle | null>;
|
|
21
|
+
/**
|
|
22
|
+
* Provider component that creates and provides a Toggle instance to the React tree
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```tsx
|
|
26
|
+
* <ToggleProvider publicApiKey="public_..." applicationId="my-app">
|
|
27
|
+
* <App />
|
|
28
|
+
* </ToggleProvider>
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
declare function ToggleProvider({ children, publicApiKey, applicationId, environment, defaultContext, horizonUrls, defaultTargetKey, }: ToggleProviderProps): react_jsx_runtime.JSX.Element;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Hook to access the Toggle instance from the ToggleProvider
|
|
35
|
+
*
|
|
36
|
+
* @throws {Error} If used outside of a ToggleProvider
|
|
37
|
+
* @returns {Toggle} The Toggle instance from the provider
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```tsx
|
|
41
|
+
* function MyComponent() {
|
|
42
|
+
* const toggle = useToggle();
|
|
43
|
+
* const isEnabled = toggle.getBoolean('my-feature', false);
|
|
44
|
+
*
|
|
45
|
+
* return <div>{isEnabled ? 'Feature enabled' : 'Feature disabled'}</div>;
|
|
46
|
+
* }
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
declare function useToggle(): Toggle;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Higher-order component that wraps a component with ToggleProvider
|
|
53
|
+
*
|
|
54
|
+
* @param options - Configuration options for the Toggle instance
|
|
55
|
+
* @returns A function that wraps a component with ToggleProvider
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```tsx
|
|
59
|
+
* export default withToggleProvider({
|
|
60
|
+
* publicApiKey: "public_...",
|
|
61
|
+
* applicationId: "my-app",
|
|
62
|
+
* environment: "production",
|
|
63
|
+
* defaultContext: {
|
|
64
|
+
* user: { id: "user-123" }
|
|
65
|
+
* }
|
|
66
|
+
* })(App);
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
declare function withToggleProvider(options: ToggleOptions): <P extends object>(Component: ComponentType<P>) => ComponentType<P>;
|
|
70
|
+
|
|
71
|
+
export { ToggleContext, ToggleProvider, type ToggleProviderProps, useToggle, withToggleProvider };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { ToggleOptions, Toggle } from '@hyphen/browser-sdk';
|
|
2
|
+
export { Toggle, ToggleOptions } from '@hyphen/browser-sdk';
|
|
3
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
4
|
+
import { ReactNode, Context, ComponentType } from 'react';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Props for the ToggleProvider component
|
|
8
|
+
* Extends ToggleOptions from @hyphen/browser-sdk
|
|
9
|
+
*/
|
|
10
|
+
interface ToggleProviderProps extends ToggleOptions {
|
|
11
|
+
/**
|
|
12
|
+
* React children to be wrapped by the provider
|
|
13
|
+
*/
|
|
14
|
+
children: ReactNode;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* React Context for the Toggle instance
|
|
19
|
+
*/
|
|
20
|
+
declare const ToggleContext: Context<Toggle | null>;
|
|
21
|
+
/**
|
|
22
|
+
* Provider component that creates and provides a Toggle instance to the React tree
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```tsx
|
|
26
|
+
* <ToggleProvider publicApiKey="public_..." applicationId="my-app">
|
|
27
|
+
* <App />
|
|
28
|
+
* </ToggleProvider>
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
declare function ToggleProvider({ children, publicApiKey, applicationId, environment, defaultContext, horizonUrls, defaultTargetKey, }: ToggleProviderProps): react_jsx_runtime.JSX.Element;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Hook to access the Toggle instance from the ToggleProvider
|
|
35
|
+
*
|
|
36
|
+
* @throws {Error} If used outside of a ToggleProvider
|
|
37
|
+
* @returns {Toggle} The Toggle instance from the provider
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```tsx
|
|
41
|
+
* function MyComponent() {
|
|
42
|
+
* const toggle = useToggle();
|
|
43
|
+
* const isEnabled = toggle.getBoolean('my-feature', false);
|
|
44
|
+
*
|
|
45
|
+
* return <div>{isEnabled ? 'Feature enabled' : 'Feature disabled'}</div>;
|
|
46
|
+
* }
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
declare function useToggle(): Toggle;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Higher-order component that wraps a component with ToggleProvider
|
|
53
|
+
*
|
|
54
|
+
* @param options - Configuration options for the Toggle instance
|
|
55
|
+
* @returns A function that wraps a component with ToggleProvider
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```tsx
|
|
59
|
+
* export default withToggleProvider({
|
|
60
|
+
* publicApiKey: "public_...",
|
|
61
|
+
* applicationId: "my-app",
|
|
62
|
+
* environment: "production",
|
|
63
|
+
* defaultContext: {
|
|
64
|
+
* user: { id: "user-123" }
|
|
65
|
+
* }
|
|
66
|
+
* })(App);
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
declare function withToggleProvider(options: ToggleOptions): <P extends object>(Component: ComponentType<P>) => ComponentType<P>;
|
|
70
|
+
|
|
71
|
+
export { ToggleContext, ToggleProvider, type ToggleProviderProps, useToggle, withToggleProvider };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// src/index.ts
|
|
5
|
+
import { Toggle as Toggle2 } from "@hyphen/browser-sdk";
|
|
6
|
+
|
|
7
|
+
// src/toggle-provider.tsx
|
|
8
|
+
import { createContext, useMemo, useEffect } from "react";
|
|
9
|
+
import { Toggle } from "@hyphen/browser-sdk";
|
|
10
|
+
var ToggleContext = /* @__PURE__ */ createContext(null);
|
|
11
|
+
function ToggleProvider({ children, publicApiKey, applicationId, environment, defaultContext, horizonUrls, defaultTargetKey }) {
|
|
12
|
+
const toggle = useMemo(() => {
|
|
13
|
+
return new Toggle({
|
|
14
|
+
publicApiKey,
|
|
15
|
+
applicationId,
|
|
16
|
+
environment,
|
|
17
|
+
defaultContext,
|
|
18
|
+
horizonUrls,
|
|
19
|
+
defaultTargetKey
|
|
20
|
+
});
|
|
21
|
+
}, [
|
|
22
|
+
publicApiKey,
|
|
23
|
+
applicationId,
|
|
24
|
+
environment,
|
|
25
|
+
defaultContext,
|
|
26
|
+
horizonUrls,
|
|
27
|
+
defaultTargetKey
|
|
28
|
+
]);
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
return () => {
|
|
31
|
+
};
|
|
32
|
+
}, [
|
|
33
|
+
toggle
|
|
34
|
+
]);
|
|
35
|
+
return /* @__PURE__ */ React.createElement(ToggleContext.Provider, {
|
|
36
|
+
value: toggle
|
|
37
|
+
}, children);
|
|
38
|
+
}
|
|
39
|
+
__name(ToggleProvider, "ToggleProvider");
|
|
40
|
+
|
|
41
|
+
// src/use-toggle.tsx
|
|
42
|
+
import { useContext } from "react";
|
|
43
|
+
function useToggle() {
|
|
44
|
+
const toggle = useContext(ToggleContext);
|
|
45
|
+
if (!toggle) {
|
|
46
|
+
throw new Error("useToggle must be used within a ToggleProvider. Wrap your component tree with <ToggleProvider> or use withToggleProvider().");
|
|
47
|
+
}
|
|
48
|
+
return toggle;
|
|
49
|
+
}
|
|
50
|
+
__name(useToggle, "useToggle");
|
|
51
|
+
|
|
52
|
+
// src/with-toggle-provider.tsx
|
|
53
|
+
function withToggleProvider(options) {
|
|
54
|
+
return function(Component) {
|
|
55
|
+
const WrappedComponent = /* @__PURE__ */ __name((props) => {
|
|
56
|
+
return /* @__PURE__ */ React.createElement(ToggleProvider, options, /* @__PURE__ */ React.createElement(Component, props));
|
|
57
|
+
}, "WrappedComponent");
|
|
58
|
+
const componentName = Component.displayName || Component.name || "Component";
|
|
59
|
+
WrappedComponent.displayName = `withToggleProvider(${componentName})`;
|
|
60
|
+
return WrappedComponent;
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
__name(withToggleProvider, "withToggleProvider");
|
|
64
|
+
export {
|
|
65
|
+
Toggle2 as Toggle,
|
|
66
|
+
ToggleContext,
|
|
67
|
+
ToggleProvider,
|
|
68
|
+
useToggle,
|
|
69
|
+
withToggleProvider
|
|
70
|
+
};
|
|
71
|
+
/* v8 ignore next -- @preserve */
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hyphen/react-sdk",
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "Hyphen SDK for React",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.cjs",
|
|
7
|
+
"module": "dist/index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"require": "./dist/index.cjs"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"types": "dist/index.d.ts",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"hyphen",
|
|
17
|
+
"sdk",
|
|
18
|
+
"browser",
|
|
19
|
+
"react",
|
|
20
|
+
"javascript",
|
|
21
|
+
"typescript"
|
|
22
|
+
],
|
|
23
|
+
"author": "Team Hyphen <hello@hyphen.ai>",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@biomejs/biome": "^2.3.5",
|
|
27
|
+
"@faker-js/faker": "^10.1.0",
|
|
28
|
+
"@swc/core": "^1.15.2",
|
|
29
|
+
"@testing-library/react": "^16.0.1",
|
|
30
|
+
"@types/node": "^24.10.1",
|
|
31
|
+
"@types/react": "^18.3.18",
|
|
32
|
+
"@vitest/coverage-v8": "^4.0.9",
|
|
33
|
+
"dotenv": "^17.2.3",
|
|
34
|
+
"jsdom": "^25.0.1",
|
|
35
|
+
"react": "^18.3.1",
|
|
36
|
+
"react-dom": "^18.3.1",
|
|
37
|
+
"rimraf": "^6.1.0",
|
|
38
|
+
"tsd": "^0.33.0",
|
|
39
|
+
"tsup": "^8.5.1",
|
|
40
|
+
"typescript": "^5.9.3",
|
|
41
|
+
"vitest": "^4.0.9"
|
|
42
|
+
},
|
|
43
|
+
"files": [
|
|
44
|
+
"dist",
|
|
45
|
+
"LICENSE"
|
|
46
|
+
],
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"@hyphen/browser-sdk": "^1.0.3",
|
|
49
|
+
"hookified": "^1.13.0"
|
|
50
|
+
},
|
|
51
|
+
"peerDependencies": {
|
|
52
|
+
"react": "^18.0.0"
|
|
53
|
+
},
|
|
54
|
+
"scripts": {
|
|
55
|
+
"lint": "biome check --write --error-on-warnings",
|
|
56
|
+
"test": "pnpm lint && vitest run --coverage",
|
|
57
|
+
"test:ci": "biome check --error-on-warnings && vitest run --coverage",
|
|
58
|
+
"build": "rimraf ./dist && tsup src/index.ts --format cjs,esm --dts",
|
|
59
|
+
"clean": "rimraf ./dist pnpm-lock.yaml node_modules coverage"
|
|
60
|
+
}
|
|
61
|
+
}
|