@hyphen/sdk 1.13.0 → 2.0.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/README.md +110 -99
- package/dist/index.cjs +522 -314
- package/dist/index.d.cts +509 -158
- package/dist/index.d.ts +509 -158
- package/dist/index.js +522 -313
- package/package.json +8 -11
package/README.md
CHANGED
|
@@ -139,6 +139,39 @@ The rest of the examples for each service show you accessing the service instanc
|
|
|
139
139
|
|
|
140
140
|
[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.
|
|
141
141
|
|
|
142
|
+
## Breaking Changes (v2.0)
|
|
143
|
+
|
|
144
|
+
**⚠️ The Toggle API has been simplified in v2.0. Please review the changes below:**
|
|
145
|
+
|
|
146
|
+
### Removed Methods
|
|
147
|
+
- ❌ `setContext(context)` - Use property setter instead: `toggle.defaultContext = context`
|
|
148
|
+
- ❌ `setPublicApiKey(key)` - Use property setter instead: `toggle.publicApiKey = key`
|
|
149
|
+
- ❌ `getClient()` - No longer needed, use the Toggle instance directly
|
|
150
|
+
|
|
151
|
+
### Removed Options
|
|
152
|
+
- ❌ `throwErrors` - Errors are now always emitted via events and default values are returned
|
|
153
|
+
|
|
154
|
+
### Changed Behavior
|
|
155
|
+
- Hooks removed from convenience methods (`getBoolean`, `getString`, etc.)
|
|
156
|
+
- Hooks now only available via event system (`toggle.on('error', ...)`)
|
|
157
|
+
- All toggle methods now return default values on error instead of throwing
|
|
158
|
+
|
|
159
|
+
### Migration Guide
|
|
160
|
+
|
|
161
|
+
**Before (v1.x):**
|
|
162
|
+
```javascript
|
|
163
|
+
const toggle = new Toggle(options);
|
|
164
|
+
toggle.setContext(context);
|
|
165
|
+
const client = await toggle.getClient();
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
**After (v2.0):**
|
|
169
|
+
```javascript
|
|
170
|
+
const toggle = new Toggle(options);
|
|
171
|
+
toggle.defaultContext = context;
|
|
172
|
+
// Use toggle directly - no getClient() needed
|
|
173
|
+
```
|
|
174
|
+
|
|
142
175
|
```javascript
|
|
143
176
|
import { Toggle, ToggleContext } from '@hyphen/sdk';
|
|
144
177
|
|
|
@@ -172,7 +205,7 @@ const result = await toggle.getBoolean('hyphen-sdk-boolean', false);
|
|
|
172
205
|
console.log('Boolean toggle value:', result); // true
|
|
173
206
|
```
|
|
174
207
|
|
|
175
|
-
|
|
208
|
+
You can also set or update the context after initialization using the `defaultContext` property:
|
|
176
209
|
|
|
177
210
|
```javascript
|
|
178
211
|
import { Toggle, ToggleContext } from '@hyphen/sdk';
|
|
@@ -201,14 +234,15 @@ const toggleOptions = {
|
|
|
201
234
|
|
|
202
235
|
const toggle = new Toggle(toggleOptions);
|
|
203
236
|
|
|
204
|
-
|
|
237
|
+
// Set the default context
|
|
238
|
+
toggle.defaultContext = context;
|
|
205
239
|
|
|
206
240
|
const result = await toggle.getBoolean('hyphen-sdk-boolean', false);
|
|
207
241
|
|
|
208
242
|
console.log('Boolean toggle value:', result); // true
|
|
209
243
|
```
|
|
210
244
|
|
|
211
|
-
|
|
245
|
+
If you would like to override the context for a single request you can do it like this:
|
|
212
246
|
|
|
213
247
|
```javascript
|
|
214
248
|
import { Toggle, ToggleContext } from '@hyphen/sdk';
|
|
@@ -264,116 +298,100 @@ console.log('Boolean toggle value:', result); // true
|
|
|
264
298
|
|
|
265
299
|
| Option | Type | Description |
|
|
266
300
|
|----------------|----------------|----------------|
|
|
267
|
-
| *publicApiKey* | `
|
|
301
|
+
| *publicApiKey* | `string` | The public API key for your Hyphen project. You can find this in the Hyphen dashboard. Must start with "public_". |
|
|
268
302
|
| *applicationId* | `string` | The application ID for your Hyphen project. You can find this in the Hyphen dashboard. |
|
|
269
|
-
| *environment?* | `string` | The environment for your Hyphen project such as `production`.
|
|
270
|
-
| *
|
|
271
|
-
| *
|
|
303
|
+
| *environment?* | `string` | The environment for your Hyphen project such as `production`. Defaults to `development` if not provided. |
|
|
304
|
+
| *defaultContext?* | `ToggleContext` | The default context to use when one is not passed to getter methods. Can be overridden per-request using the GetOptions parameter. |
|
|
305
|
+
| *horizonUrls?* | `string[]` | Array of Horizon endpoint URLs for load balancing and failover. If not provided, defaults to Hyphen's hosted service. |
|
|
306
|
+
| *defaultTargetKey?* | `string` | Default targeting key to use if one cannot be derived from context. If not provided, a random key will be generated. |
|
|
307
|
+
| *cache?* | `Cacheable` | Cacheable instance for caching fetch operations. See [@cacheable/cacheable](https://github.com/jaredwray/cacheable) for more information. |
|
|
272
308
|
|
|
273
309
|
## Toggle API
|
|
274
310
|
|
|
311
|
+
### Methods
|
|
312
|
+
|
|
275
313
|
| Method | Parameters | Description |
|
|
276
314
|
|----------------|----------------|----------------|
|
|
277
|
-
| *
|
|
278
|
-
| *
|
|
279
|
-
| *
|
|
280
|
-
| *
|
|
281
|
-
| *
|
|
282
|
-
| *
|
|
315
|
+
| *get<T>* | `key: string, defaultValue: T, options?: GetOptions` | Get the value of a toggle. This is a generic method that can be used to get any type from toggle. |
|
|
316
|
+
| *getBoolean* | `key: string, defaultValue: boolean, options?: GetOptions` | Get the value of a boolean toggle. |
|
|
317
|
+
| *getNumber* | `key: string, defaultValue: number, options?: GetOptions` | Get the value of a number toggle. |
|
|
318
|
+
| *getString* | `key: string, defaultValue: string, options?: GetOptions` | Get the value of a string toggle. |
|
|
319
|
+
| *getObject<T>* | `key: string, defaultValue: T, options?: GetOptions` | Get the value of an object toggle. |
|
|
320
|
+
| *fetch<T>* | `path: string, payload?: unknown, options?: FetchOptions` | Make a raw HTTP POST request to Horizon endpoints with automatic authentication and load balancing. |
|
|
283
321
|
|
|
284
|
-
|
|
322
|
+
### Properties (Getters/Setters)
|
|
285
323
|
|
|
286
|
-
|
|
287
|
-
| Hook | object | Description |
|
|
324
|
+
| Property | Type | Description |
|
|
288
325
|
|----------------|----------------|----------------|
|
|
289
|
-
| *
|
|
290
|
-
| *
|
|
291
|
-
| *
|
|
292
|
-
| *
|
|
293
|
-
| *
|
|
294
|
-
| *
|
|
295
|
-
| *
|
|
296
|
-
| *
|
|
326
|
+
| *publicApiKey* | `string \| undefined` | Get or set the public API key. |
|
|
327
|
+
| *defaultContext* | `ToggleContext \| undefined` | Get or set the default context for toggle evaluations. |
|
|
328
|
+
| *applicationId* | `string \| undefined` | Get or set the application ID. |
|
|
329
|
+
| *environment* | `string \| undefined` | Get or set the environment. |
|
|
330
|
+
| *horizonUrls* | `string[]` | Get or set the Horizon endpoint URLs for load balancing. |
|
|
331
|
+
| *defaultTargetingKey* | `string` | Get or set the default targeting key. |
|
|
332
|
+
| *organizationId* | `string \| undefined` | Get the organization ID (read-only, extracted from public key). |
|
|
333
|
+
| *cache* | `Cacheable` | Get or set the Cacheable instance used for caching fetch operations. |
|
|
297
334
|
|
|
298
|
-
|
|
335
|
+
### GetOptions
|
|
299
336
|
|
|
300
|
-
|
|
301
|
-
import { Toggle, ToggleHooks, ToggleContext } from '@hyphen/sdk';
|
|
337
|
+
The `GetOptions` type is used in toggle getter methods:
|
|
302
338
|
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
subscriptionLevel: 'premium',
|
|
308
|
-
region: 'us-east',
|
|
309
|
-
},
|
|
310
|
-
user: {
|
|
311
|
-
id: 'user-123',
|
|
312
|
-
email: 'john.doe@example.com',
|
|
313
|
-
name: 'John Doe',
|
|
314
|
-
customAttributes: {
|
|
315
|
-
role: 'admin',
|
|
316
|
-
},
|
|
317
|
-
},
|
|
339
|
+
```typescript
|
|
340
|
+
type GetOptions = {
|
|
341
|
+
context?: ToggleContext; // Override the default context for this request
|
|
342
|
+
cache?: boolean; // Whether to use caching (if configured)
|
|
318
343
|
};
|
|
344
|
+
```
|
|
319
345
|
|
|
320
|
-
|
|
321
|
-
publicApiKey: 'your_public_api_key',
|
|
322
|
-
applicationId: 'your_application_id',
|
|
323
|
-
context: context,
|
|
324
|
-
};
|
|
346
|
+
## Toggle Hooks
|
|
325
347
|
|
|
326
|
-
|
|
348
|
+
Toggle extends the Hookified class, which provides a flexible hook system for intercepting and modifying behavior. You can register hooks using the `onHook()` method.
|
|
349
|
+
|
|
350
|
+
**Note:** Hooks are currently only implemented on the base `get<T>()` method and event emitters.
|
|
351
|
+
|
|
352
|
+
### Available Hooks
|
|
353
|
+
|
|
354
|
+
You can listen to errors using the `error` event:
|
|
355
|
+
|
|
356
|
+
```javascript
|
|
357
|
+
import { Toggle } from '@hyphen/sdk';
|
|
327
358
|
|
|
328
|
-
toggle
|
|
329
|
-
|
|
359
|
+
const toggle = new Toggle({
|
|
360
|
+
publicApiKey: 'your_public_api_key',
|
|
361
|
+
applicationId: 'your_application_id',
|
|
330
362
|
});
|
|
331
363
|
|
|
332
|
-
|
|
364
|
+
toggle.on('error', (error) => {
|
|
365
|
+
console.error('Toggle error:', error);
|
|
366
|
+
});
|
|
333
367
|
|
|
334
|
-
|
|
368
|
+
const result = await toggle.getBoolean('my-feature', false);
|
|
335
369
|
```
|
|
336
370
|
|
|
337
|
-
|
|
371
|
+
### Custom Hooks (Advanced)
|
|
338
372
|
|
|
339
|
-
|
|
373
|
+
Since Toggle extends Hookified, you can create custom hooks for advanced use cases:
|
|
340
374
|
|
|
341
375
|
```javascript
|
|
342
|
-
import { Toggle
|
|
343
|
-
|
|
344
|
-
const context: ToggleContext = {
|
|
345
|
-
targetingKey: 'user-123',
|
|
346
|
-
ipAddress: '203.0.113.42',
|
|
347
|
-
customAttributes: {
|
|
348
|
-
subscriptionLevel: 'premium',
|
|
349
|
-
region: 'us-east',
|
|
350
|
-
},
|
|
351
|
-
user: {
|
|
352
|
-
id: 'user-123',
|
|
353
|
-
email: 'john.doe@example.com',
|
|
354
|
-
name: 'John Doe',
|
|
355
|
-
customAttributes: {
|
|
356
|
-
role: 'admin',
|
|
357
|
-
},
|
|
358
|
-
},
|
|
359
|
-
};
|
|
376
|
+
import { Toggle } from '@hyphen/sdk';
|
|
360
377
|
|
|
361
|
-
const
|
|
378
|
+
const toggle = new Toggle({
|
|
362
379
|
publicApiKey: 'your_public_api_key',
|
|
363
380
|
applicationId: 'your_application_id',
|
|
364
|
-
|
|
365
|
-
};
|
|
381
|
+
});
|
|
366
382
|
|
|
367
|
-
|
|
368
|
-
toggle.
|
|
369
|
-
console.
|
|
383
|
+
// Register a custom hook
|
|
384
|
+
toggle.onHook('myCustomHook', (data) => {
|
|
385
|
+
console.log('Custom hook called:', data);
|
|
370
386
|
});
|
|
371
387
|
|
|
372
|
-
|
|
373
|
-
|
|
388
|
+
// Trigger the hook (in your own code)
|
|
389
|
+
await toggle.hook('myCustomHook', { someData: 'value' });
|
|
374
390
|
```
|
|
375
391
|
|
|
376
|
-
|
|
392
|
+
## Toggle Error Handling
|
|
393
|
+
|
|
394
|
+
The SDK provides a way to handle errors that occur during toggle requests. You can use the `.on` method to listen for errors globally. When an error occurs, the toggle methods will return the default value provided.
|
|
377
395
|
|
|
378
396
|
```javascript
|
|
379
397
|
import { Toggle, ToggleContext } from '@hyphen/sdk';
|
|
@@ -399,17 +417,15 @@ const toggleOptions = {
|
|
|
399
417
|
publicApiKey: 'your_public_api_key',
|
|
400
418
|
applicationId: 'your_application_id',
|
|
401
419
|
context: context,
|
|
402
|
-
throwErrors: true,
|
|
403
420
|
};
|
|
404
421
|
|
|
405
422
|
const toggle = new Toggle(toggleOptions);
|
|
406
|
-
|
|
407
|
-
try {
|
|
408
|
-
const result = await toggle.getBoolean('hyphen-sdk-boolean', false);
|
|
409
|
-
console.log('Boolean toggle value:', result); // true
|
|
410
|
-
} catch (error) {
|
|
423
|
+
toggle.on('error', (error) => {
|
|
411
424
|
console.error('Error fetching toggle:', error);
|
|
412
|
-
}
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
const result = await toggle.getBoolean('hyphen-sdk-boolean', false);
|
|
428
|
+
console.log('Boolean toggle value:', result); // Returns false (default) if error occurs
|
|
413
429
|
```
|
|
414
430
|
|
|
415
431
|
## Toggle Caching
|
|
@@ -439,11 +455,9 @@ const toggleOptions = {
|
|
|
439
455
|
publicApiKey: 'your_public_api_key',
|
|
440
456
|
applicationId: 'your_application_id',
|
|
441
457
|
context: context,
|
|
442
|
-
uris: [
|
|
443
|
-
'https://your-self-hosted-horizon-url',
|
|
444
|
-
],
|
|
445
458
|
caching: {
|
|
446
|
-
ttl:
|
|
459
|
+
ttl: 60000, // Cache for 60 seconds (in milliseconds)
|
|
460
|
+
},
|
|
447
461
|
};
|
|
448
462
|
|
|
449
463
|
const toggle = new Toggle(toggleOptions);
|
|
@@ -478,11 +492,8 @@ const toggleOptions = {
|
|
|
478
492
|
publicApiKey: 'your_public_api_key',
|
|
479
493
|
applicationId: 'your_application_id',
|
|
480
494
|
context: context,
|
|
481
|
-
uris: [
|
|
482
|
-
'https://your-self-hosted-horizon-url',
|
|
483
|
-
],
|
|
484
495
|
caching: {
|
|
485
|
-
ttl:
|
|
496
|
+
ttl: 60000, // Cache for 60 seconds (in milliseconds)
|
|
486
497
|
generateCacheKeyFn: (context) => {
|
|
487
498
|
return `toggle-${context?.targetingKey || 'default'}-hyphen-sdk-boolean`;
|
|
488
499
|
},
|
|
@@ -508,7 +519,7 @@ On initialization of the `Toggle` class, the SDK will automatically check for th
|
|
|
508
519
|
|
|
509
520
|
## Toggle Self-Hosted
|
|
510
521
|
|
|
511
|
-
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
|
|
522
|
+
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 `horizonUrls` (or `uris` for backward compatibility) option in the constructor to set the URL of your self-hosted version:
|
|
512
523
|
|
|
513
524
|
```javascript
|
|
514
525
|
import { Toggle, ToggleContext } from '@hyphen/sdk';
|
|
@@ -534,7 +545,7 @@ const toggleOptions = {
|
|
|
534
545
|
publicApiKey: 'your_public_api_key',
|
|
535
546
|
applicationId: 'your_application_id',
|
|
536
547
|
context: context,
|
|
537
|
-
|
|
548
|
+
horizonUrls: [
|
|
538
549
|
'https://your-self-hosted-horizon-url',
|
|
539
550
|
],
|
|
540
551
|
};
|
|
@@ -571,7 +582,7 @@ const toggleOptions = {
|
|
|
571
582
|
publicApiKey: 'your_public_api_key',
|
|
572
583
|
applicationId: 'your_application_id',
|
|
573
584
|
context: context,
|
|
574
|
-
|
|
585
|
+
horizonUrls: [
|
|
575
586
|
'https://your-self-hosted-horizon-url',
|
|
576
587
|
'https://toggle.hyphen.cloud',
|
|
577
588
|
],
|