@hyphen/sdk 1.0.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 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,259 @@
1
+ ![Hyphen AI](logo.svg)
2
+
3
+ [![tests](https://github.com/Hyphen/nodejs-sdk/actions/workflows/tests.yaml/badge.svg)](https://github.com/Hyphen/nodejs-sdk/actions/workflows/tests.yaml)
4
+ [![npm](https://img.shields.io/npm/v/@hyphen/sdk)](https://www.npmjs.com/package/@hyphen/sdk)
5
+ [![npm](https://img.shields.io/npm/dm/@hyphen/sdk)](https://www.npmjs.com/package/@hyphen/sdk)
6
+ [![license](https://img.shields.io/github/license/Hyphen/nodejs-sdk)](https://github.com/hyphen/nodejs-sdk/blob/main/LICENSE)
7
+
8
+ # Hyphen Node.js SDK
9
+
10
+ The Hyphen Node.js SDK is a JavaScript library that allows developers to easily integrate Hyphen's feature flagging and experimentation capabilities into their Node.js applications. With this SDK, you can manage feature flags more effectively, enabling you to control the rollout of new features and conduct A/B testing with ease.
11
+
12
+ # Installation
13
+
14
+ To install the Hyphen Node.js SDK, you can use npm or yarn. Run the following command in your terminal:
15
+
16
+ ```bash
17
+ npm install @hyphen/sdk
18
+ ```
19
+
20
+ # Usage
21
+
22
+ There are many ways to use the Hyphen Node.js SDK. Because of this we have created examples for each of the different ways in each secton of the documentation.
23
+
24
+ # Toggle
25
+
26
+ [Toggle](https://hyphen.ai/toggle) is our feature flag service that allows you to control the rollout of new features to your users. You can access your feature flags using the `Toggle` class.
27
+
28
+ ```javascript
29
+ import { Toggle, Context } from '@hyphen/sdk';
30
+
31
+ const context: Context = {
32
+ targetingKey: 'user-123',
33
+ ipAddress: '203.0.113.42',
34
+ customAttributes: {
35
+ subscriptionLevel: 'premium',
36
+ region: 'us-east',
37
+ },
38
+ user: {
39
+ id: 'user-123',
40
+ email: 'john.doe@example.com',
41
+ name: 'John Doe',
42
+ customAttributes: {
43
+ role: 'admin',
44
+ },
45
+ },
46
+ };
47
+
48
+ const toggleOptions = {
49
+ publicApiKey: 'your_public_api_key',
50
+ applicationId: 'your_application_id',
51
+ context: context,
52
+ };
53
+
54
+ const toggle = new Toggle(toggleOptions);
55
+
56
+ const result = await toggle.getBoolean('hyphen-sdk-boolean', false);
57
+
58
+ console.log('Boolean toggle value:', result); // true
59
+ ```
60
+
61
+ if you want to set the context you can do it like this:
62
+
63
+ ```javascript
64
+ import { Toggle, Context } from '@hyphen/sdk';
65
+
66
+ const context: Context = {
67
+ targetingKey: 'user-123',
68
+ ipAddress: '203.0.113.42',
69
+ customAttributes: {
70
+ subscriptionLevel: 'premium',
71
+ region: 'us-east',
72
+ },
73
+ user: {
74
+ id: 'user-123',
75
+ email: 'john.doe@example.com',
76
+ name: 'John Doe',
77
+ customAttributes: {
78
+ role: 'admin',
79
+ },
80
+ },
81
+ };
82
+
83
+ const toggleOptions = {
84
+ publicApiKey: 'your_public_api_key',
85
+ applicationId: 'your_application_id',
86
+ };
87
+
88
+ const toggle = new Toggle(toggleOptions);
89
+
90
+ toggle.setContext(context);
91
+
92
+ const result = await toggle.getBoolean('hyphen-sdk-boolean', false);
93
+
94
+ console.log('Boolean toggle value:', result); // true
95
+ ```
96
+
97
+ if you would like to override the context for a single request you can do it like this:
98
+
99
+ ```javascript
100
+ import { Toggle, Context } from '@hyphen/sdk';
101
+
102
+ const context: Context = {
103
+ targetingKey: 'user-123',
104
+ ipAddress: '203.0.113.42',
105
+ customAttributes: {
106
+ subscriptionLevel: 'premium',
107
+ region: 'us-east',
108
+ },
109
+ user: {
110
+ id: 'user-123',
111
+ email: 'john.doe@example.com',
112
+ name: 'John Doe',
113
+ customAttributes: {
114
+ role: 'admin',
115
+ },
116
+ },
117
+ };
118
+
119
+ const toggleOptions = {
120
+ publicApiKey: 'your_public_api_key',
121
+ applicationId: 'your_application_id',
122
+ context: context,
123
+ };
124
+
125
+ const overrideContext: Context = {
126
+ targetingKey: 'user-123',
127
+ ipAddress: '203.0.113.42',
128
+ customAttributes: {
129
+ subscriptionLevel: 'premium',
130
+ region: 'us-east',
131
+ },
132
+ user: {
133
+ id: 'user-123',
134
+ email: 'john.doe@example.com',
135
+ name: 'John Doe',
136
+ customAttributes: {
137
+ role: 'admin',
138
+ },
139
+ },
140
+ };
141
+
142
+ const toggle = new Toggle(toggleOptions);
143
+
144
+ const result = await toggle.getBoolean('hyphen-sdk-boolean', false, { context: overrideContext });
145
+
146
+ console.log('Boolean toggle value:', result); // true
147
+ ```
148
+
149
+ ## Toggle Options
150
+
151
+ | Option | Type | Description |
152
+ |----------------|----------------|----------------|
153
+ | *publicApiKey* | ` string` | The public API key for your Hyphen project. You can find this in the Hyphen dashboard. |
154
+ | *applicationId* | `string` | The application ID for your Hyphen project. You can find this in the Hyphen dashboard. |
155
+ | *environment?* | `string` | The environment for your Hyphen project such as `production`. Default uses `process.env.NODE_ENV` |
156
+ | *context?* | `Context` | The context object that contains the user and custom attributes. This is optional. |
157
+ | *cache?* | `{ ttl: number}` | Whether to use the cache or not. |
158
+
159
+ ## Toggle API
160
+
161
+ | Method | Parameters | Description |
162
+ |----------------|----------------|----------------|
163
+ | *setContext* | `context: Context` | Set the context for the toggle. This is optional. |
164
+ | *get<Type>* | `key: string, defaultValue: T, options?: { context?: Context }` | Get the value of a toggle. This is a generic method that can be used to get any type from toggle. |
165
+ | *getBoolean* | `key: string, defaultValue: boolean, options?: { context?: Context }` | Get the value of a boolean toggle. |
166
+ | *getNumber* | `key: string, defaultValue: number, options?: { context?: Context }` | Get the value of a number toggle. |
167
+ | *getString* | `key: string, defaultValue: string, options?: { context?: Context }` | Get the value of a string toggle. |
168
+ | *getObject<Type>* | `key: string, defaultValue: any, options?: { context?: Context }` | Get the value of a object toggle. |
169
+
170
+ ## Toggle Hooks
171
+
172
+ The following hooks are available for Toggle:
173
+ | Hook | object | Description |
174
+ |----------------|----------------|----------------|
175
+ | *beforeGetBoolean* | `{ key: string, defaultValue:boolean, options?: ToggleRequestOptions }` | Called before the boolean toggle is fetched. |
176
+ | *afterGetBoolean* | `{ key: string, defaultValue:boolean, options?: ToggleRequestOptions, result: boolean }` | Called after the boolean toggle is fetched. |
177
+ | *beforeGetNumber* | `{ key: string, defaultValue:number, options?: ToggleRequestOptions }` | Called before the number toggle is fetched. |
178
+ | *afterGetNumber* | `{ key: string, defaultValue:number, options?: ToggleRequestOptions, result: number }` | Called after the number toggle is fetched. |
179
+ | *beforeGetString* | `{ key: string, defaultValue:string, options?: ToggleRequestOptions }` | Called before the string toggle is fetched. |
180
+ | *afterGetString* | `{ key: string, defaultValue:string, options?: ToggleRequestOptions, result: string }` | Called after the string toggle is fetched. |
181
+ | *beforeGetObject* | `{ key: string, defaultValue:any, options?: ToggleRequestOptions }` | Called before the object toggle is fetched. |
182
+ | *afterGetObject* | `{ key: string, defaultValue:any, options?: ToggleRequestOptions, result: any }` | Called after the object toggle is fetched. |
183
+
184
+ You can use the hooks to modify the request or the response. For example, you can use the `beforeGetBoolean` hook to log the request before it is sent to the server.
185
+
186
+ ```javascript
187
+ import { Toggle, ToggleHooks, Context } from '@hyphen/sdk';
188
+
189
+ const context: Context = {
190
+ targetingKey: 'user-123',
191
+ ipAddress: '203.0.113.42',
192
+ customAttributes: {
193
+ subscriptionLevel: 'premium',
194
+ region: 'us-east',
195
+ },
196
+ user: {
197
+ id: 'user-123',
198
+ email: 'john.doe@example.com',
199
+ name: 'John Doe',
200
+ customAttributes: {
201
+ role: 'admin',
202
+ },
203
+ },
204
+ };
205
+
206
+ const toggleOptions = {
207
+ publicApiKey: 'your_public_api_key',
208
+ applicationId: 'your_application_id',
209
+ context: context,
210
+ };
211
+
212
+ const toggle = new Toggle(toggleOptions);
213
+
214
+ toggle.onHook(ToggleHooks.beforeGetBoolean, (data) => {
215
+ console.log('Before get boolean toggle:', data); // { key: 'hyphen-sdk-boolean', defaultValue: false }
216
+ });
217
+
218
+ const result = await toggle.getBoolean('hyphen-sdk-boolean', false);
219
+
220
+ console.log('Boolean toggle value:', result); // true
221
+ ```
222
+
223
+ # Contributing
224
+
225
+ We welcome contributions to the Hyphen Node.js SDK! If you have an idea for a new feature, bug fix, or improvement, please follow these steps:
226
+
227
+ ## Testing Your Changes
228
+
229
+ To test your changes locally you will need to create an account on [Hyphen](https://hyphen.ai) and do the following:
230
+ 1. Create a new project in the Hyphen dashboard. This will give you a project ID and an Public API key.
231
+ 2. On the project please add the following toggles:
232
+
233
+
234
+ | Name | Type | Value |
235
+ |----------------|----------------|----------------|
236
+ | hyphen-sdk-boolean | boolean | true |
237
+ | hyphen-sdk-number | number | 42 |
238
+ | hyphen-sdk-string | string | "Hyphen!" |
239
+ | hyphen-sdk-json | json | `{ "id": "Hello World!"}` |
240
+
241
+ Then, create a new application in the project. This will give you an application ID.
242
+
243
+ Once you have created the project, added the toggles, and created your application you can run the tests then create an `.env` file in the root of the project with the following content:
244
+
245
+
246
+ ```bash
247
+ HYPHEN_PUBLIC_API_KEY=your_api_key
248
+ HYPHEN_APPLICATION_ID=your_project_id
249
+ ```
250
+
251
+ Then run the tests with the following command:
252
+
253
+ ```bash
254
+ pnpm i && pnpm test
255
+ ```
256
+
257
+ # License and Copyright
258
+ This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
259
+ The copyright for this project is held by Hyphen, Inc. All rights reserved.
@@ -0,0 +1,60 @@
1
+ import { Hookified } from 'hookified';
2
+ import { EvaluationContext, Client } from '@openfeature/server-sdk';
3
+
4
+ type Context = EvaluationContext;
5
+ type ToggleOptions = {
6
+ /**
7
+ * Your application name
8
+ * @type {string}
9
+ */
10
+ applicationId: string;
11
+ /**
12
+ * Your Hyphen API key
13
+ * @type {string}
14
+ */
15
+ publicKey: string;
16
+ /**
17
+ * Your environment name such as development, production. Default is what is set at NODE_ENV
18
+ * @type {string}
19
+ * @example production
20
+ */
21
+ environment?: string;
22
+ /**
23
+ * The context to use for evaluating feature flags
24
+ * @type {Context}
25
+ */
26
+ context?: Context;
27
+ caching?: {
28
+ /**
29
+ * The time in seconds to cache the feature flag values
30
+ * @type {number} - this is in milliseconds
31
+ */
32
+ ttl?: number;
33
+ };
34
+ };
35
+ type ToggleRequestOptions = {
36
+ context?: Context;
37
+ };
38
+ declare class Toggle extends Hookified {
39
+ private _applicationId;
40
+ private _publicKey;
41
+ private _environment;
42
+ private _client;
43
+ private _context;
44
+ constructor(options: ToggleOptions);
45
+ get applicationId(): string;
46
+ set applicationId(value: string);
47
+ get publicKey(): string;
48
+ set publicKey(value: string);
49
+ get environment(): string;
50
+ set environment(value: string);
51
+ setContext(context: Context): void;
52
+ getClient(): Promise<Client>;
53
+ get<T>(key: string, defaultValue: T, options?: ToggleRequestOptions): Promise<T>;
54
+ getBoolean(key: string, defaultValue: boolean, options?: ToggleRequestOptions): Promise<boolean>;
55
+ getString(key: string, defaultValue: string, options?: ToggleRequestOptions): Promise<string>;
56
+ getNumber(key: string, defaultValue: number, options?: ToggleRequestOptions): Promise<number>;
57
+ getObject<T>(key: string, defaultValue: T, options?: ToggleRequestOptions): Promise<T>;
58
+ }
59
+
60
+ export { Toggle };
package/dist/index.js ADDED
@@ -0,0 +1,149 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // src/toggle.ts
5
+ import process from "node:process";
6
+ import { Hookified } from "hookified";
7
+ import { OpenFeature } from "@openfeature/server-sdk";
8
+ import { HyphenProvider } from "@hyphen/openfeature-server-provider";
9
+ var Toggle = class extends Hookified {
10
+ static {
11
+ __name(this, "Toggle");
12
+ }
13
+ _applicationId;
14
+ _publicKey;
15
+ _environment;
16
+ _client;
17
+ _context;
18
+ constructor(options) {
19
+ super();
20
+ this._applicationId = options.applicationId;
21
+ this._publicKey = options.publicKey;
22
+ this._environment = options.environment ?? process.env.NODE_ENV ?? "development";
23
+ this._context = options.context;
24
+ }
25
+ get applicationId() {
26
+ return this._applicationId;
27
+ }
28
+ set applicationId(value) {
29
+ this._applicationId = value;
30
+ }
31
+ get publicKey() {
32
+ return this._publicKey;
33
+ }
34
+ set publicKey(value) {
35
+ this._publicKey = value;
36
+ }
37
+ get environment() {
38
+ return this._environment;
39
+ }
40
+ set environment(value) {
41
+ this._environment = value;
42
+ }
43
+ setContext(context) {
44
+ this._context = context;
45
+ this._client = void 0;
46
+ }
47
+ async getClient() {
48
+ if (!this._client) {
49
+ const options = {
50
+ application: this._applicationId,
51
+ environment: this._environment
52
+ };
53
+ await OpenFeature.setProviderAndWait(new HyphenProvider(this._publicKey, options));
54
+ this._client = OpenFeature.getClient(this._context);
55
+ }
56
+ return this._client;
57
+ }
58
+ async get(key, defaultValue, options) {
59
+ switch (typeof defaultValue) {
60
+ case "boolean": {
61
+ return this.getBoolean(key, defaultValue, options);
62
+ }
63
+ case "string": {
64
+ return this.getString(key, defaultValue, options);
65
+ }
66
+ case "number": {
67
+ return this.getNumber(key, defaultValue, options);
68
+ }
69
+ default: {
70
+ return this.getObject(key, defaultValue, options);
71
+ }
72
+ }
73
+ }
74
+ async getBoolean(key, defaultValue, options) {
75
+ const data = {
76
+ key,
77
+ defaultValue,
78
+ options
79
+ };
80
+ await this.hook("beforeGetBoolean", data);
81
+ const client = await this.getClient();
82
+ const result = await client.getBooleanValue(data.key, data.defaultValue, data.options?.context);
83
+ const resultData = {
84
+ key,
85
+ defaultValue,
86
+ options,
87
+ result
88
+ };
89
+ await this.hook("afterGetBoolean", resultData);
90
+ return resultData.result;
91
+ }
92
+ async getString(key, defaultValue, options) {
93
+ const data = {
94
+ key,
95
+ defaultValue,
96
+ options
97
+ };
98
+ await this.hook("beforeGetString", data);
99
+ const client = await this.getClient();
100
+ const result = await client.getStringValue(data.key, data.defaultValue, data.options?.context);
101
+ const resultData = {
102
+ key,
103
+ defaultValue,
104
+ options,
105
+ result
106
+ };
107
+ await this.hook("afterGetString", resultData);
108
+ return resultData.result;
109
+ }
110
+ async getNumber(key, defaultValue, options) {
111
+ const data = {
112
+ key,
113
+ defaultValue,
114
+ options
115
+ };
116
+ await this.hook("beforeGetNumber", data);
117
+ const client = await this.getClient();
118
+ const result = await client.getNumberValue(data.key, data.defaultValue, data.options?.context);
119
+ const resultData = {
120
+ key,
121
+ defaultValue,
122
+ options,
123
+ result
124
+ };
125
+ await this.hook("afterGetNumber", resultData);
126
+ return resultData.result;
127
+ }
128
+ async getObject(key, defaultValue, options) {
129
+ const data = {
130
+ key,
131
+ defaultValue,
132
+ options
133
+ };
134
+ await this.hook("beforeGetObject", data);
135
+ const client = await this.getClient();
136
+ const result = await client.getObjectValue(key, defaultValue, data.options?.context);
137
+ const resultData = {
138
+ key,
139
+ defaultValue,
140
+ options,
141
+ result
142
+ };
143
+ await this.hook("afterGetObject", resultData);
144
+ return resultData.result;
145
+ }
146
+ };
147
+ export {
148
+ Toggle
149
+ };
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@hyphen/sdk",
3
+ "version": "1.0.0",
4
+ "description": "Hyphen SDK for Node.js",
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/node/index.d.ts",
15
+ "keywords": [
16
+ "hyphen",
17
+ "sdk",
18
+ "nodejs",
19
+ "javascript",
20
+ "typescript"
21
+ ],
22
+ "author": "Team Hyphen <hello@hyphen.ai>",
23
+ "license": "MIT",
24
+ "devDependencies": {
25
+ "@swc/core": "^1.11.24",
26
+ "@types/node": "^22.15.17",
27
+ "@vitest/coverage-v8": "^3.1.3",
28
+ "rimraf": "^6.0.1",
29
+ "tsd": "^0.32.0",
30
+ "tsup": "^8.4.0",
31
+ "typescript": "^5.8.3",
32
+ "vitest": "^3.1.3",
33
+ "xo": "^0.60.0"
34
+ },
35
+ "files": [
36
+ "dist",
37
+ "LICENSE"
38
+ ],
39
+ "dependencies": {
40
+ "@hyphen/openfeature-server-provider": "^1.0.7",
41
+ "@openfeature/server-sdk": "^1.18.0",
42
+ "dotenv": "^16.5.0",
43
+ "hookified": "^1.9.0"
44
+ },
45
+ "scripts": {
46
+ "test": "xo --fix && vitest run --coverage",
47
+ "test:ci": "xo && vitest run --coverage",
48
+ "build": "rimraf ./dist && tsup src/index.ts --format esm,cjs --dts --clean",
49
+ "clean": "rimraf ./dist"
50
+ }
51
+ }