@hyphen/sdk 1.0.3 → 1.2.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/README.md CHANGED
@@ -1,4 +1,4 @@
1
- ![Hyphen AI](https://github.com/Hyphen/nodejs-sdk/raw/main/logo.svg)
1
+ ![Hyphen AI](https://github.com/Hyphen/nodejs-sdk/raw/main/logo.png)
2
2
 
3
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
4
  [![npm](https://img.shields.io/npm/v/@hyphen/sdk)](https://www.npmjs.com/package/@hyphen/sdk)
@@ -10,7 +10,6 @@
10
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
11
 
12
12
  # Table of Contents
13
- - [Hyphen Node.js SDK](#hyphen-nodejs-sdk)
14
13
  - [Installation](#installation)
15
14
  - [Basic Usage](#basic-usage)
16
15
  - [Toggle](#toggle)
@@ -18,8 +17,9 @@ The Hyphen Node.js SDK is a JavaScript library that allows developers to easily
18
17
  - [Toggle API](#toggle-api)
19
18
  - [Toggle Hooks](#toggle-hooks)
20
19
  - [Toggle Error Handling](#toggle-error-handling)
20
+ - [Toggle Self-Hosted](#toggle-self-hosted)
21
21
  - [Contributing](#contributing)
22
- - [Testing Your Changes](#testing-your-changes)
22
+ - [Testing Your Changes](#testing-your-changes)
23
23
  - [License and Copyright](#license-and-copyright)
24
24
 
25
25
  # Installation
@@ -311,10 +311,100 @@ try {
311
311
  }
312
312
  ```
313
313
 
314
+ ## Toggle Self-Hosted
315
+
316
+ Toggle uses [Horizon](https://hyphen.ai/horizon) to fetch the feature flags. If you are using a self-hosted version of Hyphen you can use the `uris` option in the constructor to set the url of your self-hosted version:
317
+
318
+ ```javascript
319
+ import { Toggle, ToggleContext } from '@hyphen/sdk';
320
+
321
+ const context: ToggleContext = {
322
+ targetingKey: 'user-123',
323
+ ipAddress: '203.0.113.42',
324
+ customAttributes: {
325
+ subscriptionLevel: 'premium',
326
+ region: 'us-east',
327
+ },
328
+ user: {
329
+ id: 'user-123',
330
+ email: 'john.doe@example.com',
331
+ name: 'John Doe',
332
+ customAttributes: {
333
+ role: 'admin',
334
+ },
335
+ },
336
+ };
337
+
338
+ const toggleOptions = {
339
+ publicApiKey: 'your_public_api_key',
340
+ applicationId: 'your_application_id',
341
+ context: context,
342
+ uris: [
343
+ 'https://your-self-hosted-horizon-url',
344
+ ],
345
+ };
346
+
347
+ const toggle = new Toggle(toggleOptions);
348
+
349
+ const result = await toggle.getBoolean('hyphen-sdk-boolean', false);
350
+ console.log('Boolean toggle value:', result); // true
351
+ ```
352
+
353
+ If you want to use your self-hosted version of [Horizon](https://hyphen.ai/horizon) as a primary and fallback to our hosted version you can do it like this:
354
+
355
+ ```javascript
356
+ import { Toggle, ToggleContext } from '@hyphen/sdk';
357
+
358
+ const context: ToggleContext = {
359
+ targetingKey: 'user-123',
360
+ ipAddress: '203.0.113.42',
361
+ customAttributes: {
362
+ subscriptionLevel: 'premium',
363
+ region: 'us-east',
364
+ },
365
+ user: {
366
+ id: 'user-123',
367
+ email: 'john.doe@example.com',
368
+ name: 'John Doe',
369
+ customAttributes: {
370
+ role: 'admin',
371
+ },
372
+ },
373
+ };
374
+
375
+ const toggleOptions = {
376
+ publicApiKey: 'your_public_api_key',
377
+ applicationId: 'your_application_id',
378
+ context: context,
379
+ uris: [
380
+ 'https://your-self-hosted-horizon-url',
381
+ 'https://toggle.hyphen.cloud',
382
+ ],
383
+ };
384
+
385
+ const toggle = new Toggle(toggleOptions);
386
+
387
+ const result = await toggle.getBoolean('hyphen-sdk-boolean', false);
388
+ console.log('Boolean toggle value:', result); // true
389
+ ```
390
+
314
391
  # Contributing
315
392
 
316
393
  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:
317
394
 
395
+ 1. Fork the repository.
396
+ 2. Create a new branch for your feature or bug fix.
397
+ 3. Install `pnpm` by running `npm install -g pnpm` if you don't have it already.
398
+ 3. Run the installation and tests to ensure everything is working correctly `pnpm i && pnpm test`.
399
+ 4. Make your changes and commit them with a clear message. In the following format:
400
+ - `feat: <describe the feature>`
401
+ - `fix: <describe the bug fix>`
402
+ - `chore: updgrading xxx to version x.x.x`
403
+
404
+ 5. Push your changes to your forked repository.
405
+ 6. Create a pull request to the main repository.
406
+ 7. Describe your changes and why they are necessary.
407
+
318
408
  ## Testing Your Changes
319
409
 
320
410
  To test your changes locally you will need to create an account on [Hyphen](https://hyphen.ai) and do the following:
package/dist/index.cjs CHANGED
@@ -57,64 +57,161 @@ var Toggle = class extends import_hookified.Hookified {
57
57
  __name(this, "Toggle");
58
58
  }
59
59
  _applicationId;
60
- _publicKey;
60
+ _publicApiKey = "";
61
61
  _environment;
62
62
  _client;
63
63
  _context;
64
64
  _throwErrors = false;
65
+ _uris;
66
+ /*
67
+ * Create a new Toggle instance. This will create a new client and set the options.
68
+ * @param {ToggleOptions}
69
+ */
65
70
  constructor(options) {
66
71
  super();
67
72
  this._applicationId = options.applicationId;
68
- this._publicKey = options.publicKey;
73
+ this.setPublicApiKey(options.publicApiKey);
69
74
  this._environment = options.environment ?? import_node_process.default.env.NODE_ENV ?? "development";
70
75
  this._context = options.context;
71
76
  this._throwErrors = options.throwErrors ?? false;
77
+ this._uris = options.uris;
72
78
  }
79
+ /**
80
+ * Get the application ID
81
+ * @returns {string}
82
+ */
73
83
  get applicationId() {
74
84
  return this._applicationId;
75
85
  }
86
+ /**
87
+ * Set the application ID
88
+ * @param {string} value
89
+ */
76
90
  set applicationId(value) {
77
91
  this._applicationId = value;
78
92
  }
79
- get publicKey() {
80
- return this._publicKey;
93
+ /**
94
+ * Get the public API key
95
+ * @returns {string}
96
+ */
97
+ get publicApiKey() {
98
+ return this._publicApiKey;
81
99
  }
82
- set publicKey(value) {
83
- this._publicKey = value;
100
+ /**
101
+ * Set the public API key
102
+ * @param {string} value
103
+ */
104
+ set publicApiKey(value) {
105
+ this.setPublicApiKey(value);
84
106
  }
107
+ /**
108
+ * Get the environment
109
+ * @returns {string}
110
+ */
85
111
  get environment() {
86
112
  return this._environment;
87
113
  }
114
+ /**
115
+ * Set the environment
116
+ * @param {string} value
117
+ */
88
118
  set environment(value) {
89
119
  this._environment = value;
90
120
  }
121
+ /**
122
+ * Get the throwErrors. If true, errors will be thrown in addition to being emitted.
123
+ * @returns {boolean}
124
+ */
91
125
  get throwErrors() {
92
126
  return this._throwErrors;
93
127
  }
128
+ /**
129
+ * Set the throwErrors. If true, errors will be thrown in addition to being emitted.
130
+ * @param {boolean} value
131
+ */
94
132
  set throwErrors(value) {
95
133
  this._throwErrors = value;
96
134
  }
135
+ /**
136
+ * Get the current context. This is the default context used. You can override this at the get function level.
137
+ * @returns {ToggleContext}
138
+ */
97
139
  get context() {
98
140
  return this._context;
99
141
  }
142
+ /**
143
+ * Set the context. This is the default context used. You can override this at the get function level.
144
+ * @param {ToggleContext} value
145
+ */
100
146
  set context(value) {
101
147
  this._context = value;
102
148
  }
149
+ /**
150
+ * Get the URIs. This is used to override the default URIs for testing or if you are using a self-hosted version.
151
+ * @returns {Array<string>}
152
+ */
153
+ get uris() {
154
+ return this._uris;
155
+ }
156
+ /**
157
+ * Set the URIs. This is used to override the default URIs for testing or if you are using a self-hosted version.
158
+ * @param {Array<string>} value
159
+ */
160
+ set uris(value) {
161
+ this._uris = value;
162
+ }
163
+ /**
164
+ * This is a helper function to set the public API key. It will check if the key starts with public_ and set it. If it
165
+ * does set it will also set the client to undefined to force a new one to be created. If it does not,
166
+ * it will emit an error and console warning and not set the key. Used by the constructor and publicApiKey setter.
167
+ * @param key
168
+ * @returns
169
+ */
170
+ setPublicApiKey(key) {
171
+ if (!key.startsWith("public_")) {
172
+ this.emit("error", new Error("Public API key should start with public_"));
173
+ if (import_node_process.default.env.NODE_ENV !== "production") {
174
+ console.error("Public API key should start with public_");
175
+ }
176
+ return;
177
+ }
178
+ this._publicApiKey = key;
179
+ this._client = void 0;
180
+ }
181
+ /**
182
+ * Set the context. This is the default context used. You can override this at the get function level.
183
+ * @param {ToggleContext} context
184
+ */
103
185
  setContext(context) {
104
186
  this._context = context;
105
187
  this._client = void 0;
106
188
  }
189
+ /**
190
+ * Helper function to get the client. This will create a new client if one does not exist. It will also set the
191
+ * application ID, environment, and URIs if they are not set. This is used by the get function to get the client.
192
+ * This is normally only used internally.
193
+ * @returns {Promise<Client>}
194
+ */
107
195
  async getClient() {
108
196
  if (!this._client) {
109
197
  const options = {
110
198
  application: this._applicationId,
111
- environment: this._environment
199
+ environment: this._environment,
200
+ horizonUrls: this._uris
112
201
  };
113
- await import_server_sdk.OpenFeature.setProviderAndWait(new import_openfeature_server_provider.HyphenProvider(this._publicKey, options));
202
+ await import_server_sdk.OpenFeature.setProviderAndWait(new import_openfeature_server_provider.HyphenProvider(this._publicApiKey, options));
114
203
  this._client = import_server_sdk.OpenFeature.getClient(this._context);
115
204
  }
116
205
  return this._client;
117
206
  }
207
+ /**
208
+ * This is the main function to get a feature flag value. It will check the type of the default value and call the
209
+ * appropriate function. It will also set the context if it is not set.
210
+ * @param {string} key - The key of the feature flag
211
+ * @param {T} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
212
+ * @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
213
+ * @returns {Promise<T>}
214
+ */
118
215
  async get(key, defaultValue, options) {
119
216
  switch (typeof defaultValue) {
120
217
  case "boolean": {
@@ -131,6 +228,14 @@ var Toggle = class extends import_hookified.Hookified {
131
228
  }
132
229
  }
133
230
  }
231
+ /**
232
+ * Get a boolean value from the feature flag. This will check the type of the default value and call the
233
+ * appropriate function. It will also set the context if it is not set.
234
+ * @param {string} key - The key of the feature flag
235
+ * @param {boolean} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
236
+ * @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
237
+ * @returns {Promise<boolean>} - The value of the feature flag
238
+ */
134
239
  async getBoolean(key, defaultValue, options) {
135
240
  try {
136
241
  const data = {
@@ -157,6 +262,13 @@ var Toggle = class extends import_hookified.Hookified {
157
262
  }
158
263
  return defaultValue;
159
264
  }
265
+ /**
266
+ * Get a string value from the feature flag.
267
+ * @param {string} key - The key of the feature flag
268
+ * @param {string} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
269
+ * @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
270
+ * @returns {Promise<string>} - The value of the feature flag
271
+ */
160
272
  async getString(key, defaultValue, options) {
161
273
  try {
162
274
  const data = {
@@ -209,6 +321,14 @@ var Toggle = class extends import_hookified.Hookified {
209
321
  }
210
322
  return defaultValue;
211
323
  }
324
+ /**
325
+ * Get an object value from the feature flag. This will check the type of the default value and call the
326
+ * appropriate function. It will also set the context if it is not set.
327
+ * @param {string} key - The key of the feature flag
328
+ * @param {T} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
329
+ * @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
330
+ * @returns {Promise<T>} - The value of the feature flag
331
+ */
212
332
  async getObject(key, defaultValue, options) {
213
333
  try {
214
334
  const data = {
package/dist/index.d.cts CHANGED
@@ -19,10 +19,10 @@ type ToggleOptions = {
19
19
  */
20
20
  applicationId: string;
21
21
  /**
22
- * Your Hyphen API key
22
+ * Your Hyphen Public API key
23
23
  * @type {string}
24
24
  */
25
- publicKey: string;
25
+ publicApiKey: string;
26
26
  /**
27
27
  * Your environment name such as development, production. Default is what is set at NODE_ENV
28
28
  * @type {string}
@@ -47,34 +47,145 @@ type ToggleOptions = {
47
47
  * @default false
48
48
  */
49
49
  throwErrors?: boolean;
50
+ /**
51
+ * Horizon URIs to use for talking to Toggle. You can use this to override
52
+ * the default URIs for testin or if you are using a self-hosted version.
53
+ * @type {Array<string>}
54
+ * @example ['https://toggle.hyphen.ai']
55
+ */
56
+ uris?: string[];
50
57
  };
51
58
  type ToggleRequestOptions = {
59
+ /**
60
+ * The context to use for evaluating feature flags
61
+ * @type {ToggleContext}
62
+ */
52
63
  context?: ToggleContext;
53
64
  };
54
65
  declare class Toggle extends Hookified {
55
66
  private _applicationId;
56
- private _publicKey;
67
+ private _publicApiKey;
57
68
  private _environment;
58
69
  private _client;
59
70
  private _context;
60
71
  private _throwErrors;
72
+ private _uris?;
61
73
  constructor(options: ToggleOptions);
74
+ /**
75
+ * Get the application ID
76
+ * @returns {string}
77
+ */
62
78
  get applicationId(): string;
79
+ /**
80
+ * Set the application ID
81
+ * @param {string} value
82
+ */
63
83
  set applicationId(value: string);
64
- get publicKey(): string;
65
- set publicKey(value: string);
84
+ /**
85
+ * Get the public API key
86
+ * @returns {string}
87
+ */
88
+ get publicApiKey(): string;
89
+ /**
90
+ * Set the public API key
91
+ * @param {string} value
92
+ */
93
+ set publicApiKey(value: string);
94
+ /**
95
+ * Get the environment
96
+ * @returns {string}
97
+ */
66
98
  get environment(): string;
99
+ /**
100
+ * Set the environment
101
+ * @param {string} value
102
+ */
67
103
  set environment(value: string);
104
+ /**
105
+ * Get the throwErrors. If true, errors will be thrown in addition to being emitted.
106
+ * @returns {boolean}
107
+ */
68
108
  get throwErrors(): boolean;
109
+ /**
110
+ * Set the throwErrors. If true, errors will be thrown in addition to being emitted.
111
+ * @param {boolean} value
112
+ */
69
113
  set throwErrors(value: boolean);
114
+ /**
115
+ * Get the current context. This is the default context used. You can override this at the get function level.
116
+ * @returns {ToggleContext}
117
+ */
70
118
  get context(): ToggleContext | undefined;
119
+ /**
120
+ * Set the context. This is the default context used. You can override this at the get function level.
121
+ * @param {ToggleContext} value
122
+ */
71
123
  set context(value: ToggleContext | undefined);
124
+ /**
125
+ * Get the URIs. This is used to override the default URIs for testing or if you are using a self-hosted version.
126
+ * @returns {Array<string>}
127
+ */
128
+ get uris(): string[] | undefined;
129
+ /**
130
+ * Set the URIs. This is used to override the default URIs for testing or if you are using a self-hosted version.
131
+ * @param {Array<string>} value
132
+ */
133
+ set uris(value: string[] | undefined);
134
+ /**
135
+ * This is a helper function to set the public API key. It will check if the key starts with public_ and set it. If it
136
+ * does set it will also set the client to undefined to force a new one to be created. If it does not,
137
+ * it will emit an error and console warning and not set the key. Used by the constructor and publicApiKey setter.
138
+ * @param key
139
+ * @returns
140
+ */
141
+ setPublicApiKey(key: string): void;
142
+ /**
143
+ * Set the context. This is the default context used. You can override this at the get function level.
144
+ * @param {ToggleContext} context
145
+ */
72
146
  setContext(context: ToggleContext): void;
147
+ /**
148
+ * Helper function to get the client. This will create a new client if one does not exist. It will also set the
149
+ * application ID, environment, and URIs if they are not set. This is used by the get function to get the client.
150
+ * This is normally only used internally.
151
+ * @returns {Promise<Client>}
152
+ */
73
153
  getClient(): Promise<Client>;
154
+ /**
155
+ * This is the main function to get a feature flag value. It will check the type of the default value and call the
156
+ * appropriate function. It will also set the context if it is not set.
157
+ * @param {string} key - The key of the feature flag
158
+ * @param {T} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
159
+ * @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
160
+ * @returns {Promise<T>}
161
+ */
74
162
  get<T>(key: string, defaultValue: T, options?: ToggleRequestOptions): Promise<T>;
163
+ /**
164
+ * Get a boolean value from the feature flag. This will check the type of the default value and call the
165
+ * appropriate function. It will also set the context if it is not set.
166
+ * @param {string} key - The key of the feature flag
167
+ * @param {boolean} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
168
+ * @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
169
+ * @returns {Promise<boolean>} - The value of the feature flag
170
+ */
75
171
  getBoolean(key: string, defaultValue: boolean, options?: ToggleRequestOptions): Promise<boolean>;
172
+ /**
173
+ * Get a string value from the feature flag.
174
+ * @param {string} key - The key of the feature flag
175
+ * @param {string} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
176
+ * @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
177
+ * @returns {Promise<string>} - The value of the feature flag
178
+ */
76
179
  getString(key: string, defaultValue: string, options?: ToggleRequestOptions): Promise<string>;
77
180
  getNumber(key: string, defaultValue: number, options?: ToggleRequestOptions): Promise<number>;
181
+ /**
182
+ * Get an object value from the feature flag. This will check the type of the default value and call the
183
+ * appropriate function. It will also set the context if it is not set.
184
+ * @param {string} key - The key of the feature flag
185
+ * @param {T} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
186
+ * @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
187
+ * @returns {Promise<T>} - The value of the feature flag
188
+ */
78
189
  getObject<T>(key: string, defaultValue: T, options?: ToggleRequestOptions): Promise<T>;
79
190
  }
80
191
 
package/dist/index.d.ts CHANGED
@@ -19,10 +19,10 @@ type ToggleOptions = {
19
19
  */
20
20
  applicationId: string;
21
21
  /**
22
- * Your Hyphen API key
22
+ * Your Hyphen Public API key
23
23
  * @type {string}
24
24
  */
25
- publicKey: string;
25
+ publicApiKey: string;
26
26
  /**
27
27
  * Your environment name such as development, production. Default is what is set at NODE_ENV
28
28
  * @type {string}
@@ -47,34 +47,145 @@ type ToggleOptions = {
47
47
  * @default false
48
48
  */
49
49
  throwErrors?: boolean;
50
+ /**
51
+ * Horizon URIs to use for talking to Toggle. You can use this to override
52
+ * the default URIs for testin or if you are using a self-hosted version.
53
+ * @type {Array<string>}
54
+ * @example ['https://toggle.hyphen.ai']
55
+ */
56
+ uris?: string[];
50
57
  };
51
58
  type ToggleRequestOptions = {
59
+ /**
60
+ * The context to use for evaluating feature flags
61
+ * @type {ToggleContext}
62
+ */
52
63
  context?: ToggleContext;
53
64
  };
54
65
  declare class Toggle extends Hookified {
55
66
  private _applicationId;
56
- private _publicKey;
67
+ private _publicApiKey;
57
68
  private _environment;
58
69
  private _client;
59
70
  private _context;
60
71
  private _throwErrors;
72
+ private _uris?;
61
73
  constructor(options: ToggleOptions);
74
+ /**
75
+ * Get the application ID
76
+ * @returns {string}
77
+ */
62
78
  get applicationId(): string;
79
+ /**
80
+ * Set the application ID
81
+ * @param {string} value
82
+ */
63
83
  set applicationId(value: string);
64
- get publicKey(): string;
65
- set publicKey(value: string);
84
+ /**
85
+ * Get the public API key
86
+ * @returns {string}
87
+ */
88
+ get publicApiKey(): string;
89
+ /**
90
+ * Set the public API key
91
+ * @param {string} value
92
+ */
93
+ set publicApiKey(value: string);
94
+ /**
95
+ * Get the environment
96
+ * @returns {string}
97
+ */
66
98
  get environment(): string;
99
+ /**
100
+ * Set the environment
101
+ * @param {string} value
102
+ */
67
103
  set environment(value: string);
104
+ /**
105
+ * Get the throwErrors. If true, errors will be thrown in addition to being emitted.
106
+ * @returns {boolean}
107
+ */
68
108
  get throwErrors(): boolean;
109
+ /**
110
+ * Set the throwErrors. If true, errors will be thrown in addition to being emitted.
111
+ * @param {boolean} value
112
+ */
69
113
  set throwErrors(value: boolean);
114
+ /**
115
+ * Get the current context. This is the default context used. You can override this at the get function level.
116
+ * @returns {ToggleContext}
117
+ */
70
118
  get context(): ToggleContext | undefined;
119
+ /**
120
+ * Set the context. This is the default context used. You can override this at the get function level.
121
+ * @param {ToggleContext} value
122
+ */
71
123
  set context(value: ToggleContext | undefined);
124
+ /**
125
+ * Get the URIs. This is used to override the default URIs for testing or if you are using a self-hosted version.
126
+ * @returns {Array<string>}
127
+ */
128
+ get uris(): string[] | undefined;
129
+ /**
130
+ * Set the URIs. This is used to override the default URIs for testing or if you are using a self-hosted version.
131
+ * @param {Array<string>} value
132
+ */
133
+ set uris(value: string[] | undefined);
134
+ /**
135
+ * This is a helper function to set the public API key. It will check if the key starts with public_ and set it. If it
136
+ * does set it will also set the client to undefined to force a new one to be created. If it does not,
137
+ * it will emit an error and console warning and not set the key. Used by the constructor and publicApiKey setter.
138
+ * @param key
139
+ * @returns
140
+ */
141
+ setPublicApiKey(key: string): void;
142
+ /**
143
+ * Set the context. This is the default context used. You can override this at the get function level.
144
+ * @param {ToggleContext} context
145
+ */
72
146
  setContext(context: ToggleContext): void;
147
+ /**
148
+ * Helper function to get the client. This will create a new client if one does not exist. It will also set the
149
+ * application ID, environment, and URIs if they are not set. This is used by the get function to get the client.
150
+ * This is normally only used internally.
151
+ * @returns {Promise<Client>}
152
+ */
73
153
  getClient(): Promise<Client>;
154
+ /**
155
+ * This is the main function to get a feature flag value. It will check the type of the default value and call the
156
+ * appropriate function. It will also set the context if it is not set.
157
+ * @param {string} key - The key of the feature flag
158
+ * @param {T} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
159
+ * @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
160
+ * @returns {Promise<T>}
161
+ */
74
162
  get<T>(key: string, defaultValue: T, options?: ToggleRequestOptions): Promise<T>;
163
+ /**
164
+ * Get a boolean value from the feature flag. This will check the type of the default value and call the
165
+ * appropriate function. It will also set the context if it is not set.
166
+ * @param {string} key - The key of the feature flag
167
+ * @param {boolean} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
168
+ * @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
169
+ * @returns {Promise<boolean>} - The value of the feature flag
170
+ */
75
171
  getBoolean(key: string, defaultValue: boolean, options?: ToggleRequestOptions): Promise<boolean>;
172
+ /**
173
+ * Get a string value from the feature flag.
174
+ * @param {string} key - The key of the feature flag
175
+ * @param {string} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
176
+ * @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
177
+ * @returns {Promise<string>} - The value of the feature flag
178
+ */
76
179
  getString(key: string, defaultValue: string, options?: ToggleRequestOptions): Promise<string>;
77
180
  getNumber(key: string, defaultValue: number, options?: ToggleRequestOptions): Promise<number>;
181
+ /**
182
+ * Get an object value from the feature flag. This will check the type of the default value and call the
183
+ * appropriate function. It will also set the context if it is not set.
184
+ * @param {string} key - The key of the feature flag
185
+ * @param {T} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
186
+ * @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
187
+ * @returns {Promise<T>} - The value of the feature flag
188
+ */
78
189
  getObject<T>(key: string, defaultValue: T, options?: ToggleRequestOptions): Promise<T>;
79
190
  }
80
191
 
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@ var __defProp = Object.defineProperty;
2
2
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
3
 
4
4
  // src/toggle.ts
5
- import process from "node:process";
5
+ import process from "process";
6
6
  import { Hookified } from "hookified";
7
7
  import { OpenFeature } from "@openfeature/server-sdk";
8
8
  import { HyphenProvider } from "@hyphen/openfeature-server-provider";
@@ -22,64 +22,161 @@ var Toggle = class extends Hookified {
22
22
  __name(this, "Toggle");
23
23
  }
24
24
  _applicationId;
25
- _publicKey;
25
+ _publicApiKey = "";
26
26
  _environment;
27
27
  _client;
28
28
  _context;
29
29
  _throwErrors = false;
30
+ _uris;
31
+ /*
32
+ * Create a new Toggle instance. This will create a new client and set the options.
33
+ * @param {ToggleOptions}
34
+ */
30
35
  constructor(options) {
31
36
  super();
32
37
  this._applicationId = options.applicationId;
33
- this._publicKey = options.publicKey;
38
+ this.setPublicApiKey(options.publicApiKey);
34
39
  this._environment = options.environment ?? process.env.NODE_ENV ?? "development";
35
40
  this._context = options.context;
36
41
  this._throwErrors = options.throwErrors ?? false;
42
+ this._uris = options.uris;
37
43
  }
44
+ /**
45
+ * Get the application ID
46
+ * @returns {string}
47
+ */
38
48
  get applicationId() {
39
49
  return this._applicationId;
40
50
  }
51
+ /**
52
+ * Set the application ID
53
+ * @param {string} value
54
+ */
41
55
  set applicationId(value) {
42
56
  this._applicationId = value;
43
57
  }
44
- get publicKey() {
45
- return this._publicKey;
58
+ /**
59
+ * Get the public API key
60
+ * @returns {string}
61
+ */
62
+ get publicApiKey() {
63
+ return this._publicApiKey;
46
64
  }
47
- set publicKey(value) {
48
- this._publicKey = value;
65
+ /**
66
+ * Set the public API key
67
+ * @param {string} value
68
+ */
69
+ set publicApiKey(value) {
70
+ this.setPublicApiKey(value);
49
71
  }
72
+ /**
73
+ * Get the environment
74
+ * @returns {string}
75
+ */
50
76
  get environment() {
51
77
  return this._environment;
52
78
  }
79
+ /**
80
+ * Set the environment
81
+ * @param {string} value
82
+ */
53
83
  set environment(value) {
54
84
  this._environment = value;
55
85
  }
86
+ /**
87
+ * Get the throwErrors. If true, errors will be thrown in addition to being emitted.
88
+ * @returns {boolean}
89
+ */
56
90
  get throwErrors() {
57
91
  return this._throwErrors;
58
92
  }
93
+ /**
94
+ * Set the throwErrors. If true, errors will be thrown in addition to being emitted.
95
+ * @param {boolean} value
96
+ */
59
97
  set throwErrors(value) {
60
98
  this._throwErrors = value;
61
99
  }
100
+ /**
101
+ * Get the current context. This is the default context used. You can override this at the get function level.
102
+ * @returns {ToggleContext}
103
+ */
62
104
  get context() {
63
105
  return this._context;
64
106
  }
107
+ /**
108
+ * Set the context. This is the default context used. You can override this at the get function level.
109
+ * @param {ToggleContext} value
110
+ */
65
111
  set context(value) {
66
112
  this._context = value;
67
113
  }
114
+ /**
115
+ * Get the URIs. This is used to override the default URIs for testing or if you are using a self-hosted version.
116
+ * @returns {Array<string>}
117
+ */
118
+ get uris() {
119
+ return this._uris;
120
+ }
121
+ /**
122
+ * Set the URIs. This is used to override the default URIs for testing or if you are using a self-hosted version.
123
+ * @param {Array<string>} value
124
+ */
125
+ set uris(value) {
126
+ this._uris = value;
127
+ }
128
+ /**
129
+ * This is a helper function to set the public API key. It will check if the key starts with public_ and set it. If it
130
+ * does set it will also set the client to undefined to force a new one to be created. If it does not,
131
+ * it will emit an error and console warning and not set the key. Used by the constructor and publicApiKey setter.
132
+ * @param key
133
+ * @returns
134
+ */
135
+ setPublicApiKey(key) {
136
+ if (!key.startsWith("public_")) {
137
+ this.emit("error", new Error("Public API key should start with public_"));
138
+ if (process.env.NODE_ENV !== "production") {
139
+ console.error("Public API key should start with public_");
140
+ }
141
+ return;
142
+ }
143
+ this._publicApiKey = key;
144
+ this._client = void 0;
145
+ }
146
+ /**
147
+ * Set the context. This is the default context used. You can override this at the get function level.
148
+ * @param {ToggleContext} context
149
+ */
68
150
  setContext(context) {
69
151
  this._context = context;
70
152
  this._client = void 0;
71
153
  }
154
+ /**
155
+ * Helper function to get the client. This will create a new client if one does not exist. It will also set the
156
+ * application ID, environment, and URIs if they are not set. This is used by the get function to get the client.
157
+ * This is normally only used internally.
158
+ * @returns {Promise<Client>}
159
+ */
72
160
  async getClient() {
73
161
  if (!this._client) {
74
162
  const options = {
75
163
  application: this._applicationId,
76
- environment: this._environment
164
+ environment: this._environment,
165
+ horizonUrls: this._uris
77
166
  };
78
- await OpenFeature.setProviderAndWait(new HyphenProvider(this._publicKey, options));
167
+ await OpenFeature.setProviderAndWait(new HyphenProvider(this._publicApiKey, options));
79
168
  this._client = OpenFeature.getClient(this._context);
80
169
  }
81
170
  return this._client;
82
171
  }
172
+ /**
173
+ * This is the main function to get a feature flag value. It will check the type of the default value and call the
174
+ * appropriate function. It will also set the context if it is not set.
175
+ * @param {string} key - The key of the feature flag
176
+ * @param {T} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
177
+ * @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
178
+ * @returns {Promise<T>}
179
+ */
83
180
  async get(key, defaultValue, options) {
84
181
  switch (typeof defaultValue) {
85
182
  case "boolean": {
@@ -96,6 +193,14 @@ var Toggle = class extends Hookified {
96
193
  }
97
194
  }
98
195
  }
196
+ /**
197
+ * Get a boolean value from the feature flag. This will check the type of the default value and call the
198
+ * appropriate function. It will also set the context if it is not set.
199
+ * @param {string} key - The key of the feature flag
200
+ * @param {boolean} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
201
+ * @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
202
+ * @returns {Promise<boolean>} - The value of the feature flag
203
+ */
99
204
  async getBoolean(key, defaultValue, options) {
100
205
  try {
101
206
  const data = {
@@ -122,6 +227,13 @@ var Toggle = class extends Hookified {
122
227
  }
123
228
  return defaultValue;
124
229
  }
230
+ /**
231
+ * Get a string value from the feature flag.
232
+ * @param {string} key - The key of the feature flag
233
+ * @param {string} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
234
+ * @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
235
+ * @returns {Promise<string>} - The value of the feature flag
236
+ */
125
237
  async getString(key, defaultValue, options) {
126
238
  try {
127
239
  const data = {
@@ -174,6 +286,14 @@ var Toggle = class extends Hookified {
174
286
  }
175
287
  return defaultValue;
176
288
  }
289
+ /**
290
+ * Get an object value from the feature flag. This will check the type of the default value and call the
291
+ * appropriate function. It will also set the context if it is not set.
292
+ * @param {string} key - The key of the feature flag
293
+ * @param {T} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
294
+ * @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
295
+ * @returns {Promise<T>} - The value of the feature flag
296
+ */
177
297
  async getObject(key, defaultValue, options) {
178
298
  try {
179
299
  const data = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyphen/sdk",
3
- "version": "1.0.3",
3
+ "version": "1.2.0",
4
4
  "description": "Hyphen SDK for Node.js",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",