@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/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { HookifiedOptions, Hookified } from 'hookified';
|
|
2
|
-
import { FetchRequestInit } from '@cacheable/net';
|
|
2
|
+
import { FetchRequestInit, FetchOptions } from '@cacheable/net';
|
|
3
3
|
import { Cacheable } from 'cacheable';
|
|
4
4
|
import pino from 'pino';
|
|
5
|
-
import { EvaluationContext, Client } from '@openfeature/server-sdk';
|
|
6
5
|
|
|
7
6
|
type EnvOptions = {
|
|
8
7
|
path?: string;
|
|
@@ -398,205 +397,569 @@ declare class NetInfo extends BaseService {
|
|
|
398
397
|
getIpInfos(ips: string[]): Promise<Array<ipInfo | ipInfoError>>;
|
|
399
398
|
}
|
|
400
399
|
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
beforeGetObject = "beforeGetObject",
|
|
410
|
-
afterGetObject = "afterGetObject"
|
|
411
|
-
}
|
|
412
|
-
type ToggleCachingOptions = {
|
|
413
|
-
ttl?: number;
|
|
414
|
-
generateCacheKeyFn?: (context?: ToggleContext) => string;
|
|
415
|
-
};
|
|
416
|
-
type ToggleOptions = {
|
|
400
|
+
/**
|
|
401
|
+
* Custom attributes that can contain any key-value pairs.
|
|
402
|
+
*/
|
|
403
|
+
type CustomAttributes = Record<string, unknown>;
|
|
404
|
+
/**
|
|
405
|
+
* User information for evaluation context.
|
|
406
|
+
*/
|
|
407
|
+
type ToggleUser = {
|
|
417
408
|
/**
|
|
418
|
-
*
|
|
419
|
-
* @type {string}
|
|
409
|
+
* Unique identifier for the user.
|
|
420
410
|
*/
|
|
421
|
-
|
|
411
|
+
id: string;
|
|
422
412
|
/**
|
|
423
|
-
*
|
|
424
|
-
* @type {string}
|
|
413
|
+
* User's email address.
|
|
425
414
|
*/
|
|
426
|
-
|
|
415
|
+
email?: string;
|
|
427
416
|
/**
|
|
428
|
-
*
|
|
429
|
-
* @type {string}
|
|
430
|
-
* @example production
|
|
417
|
+
* User's display name.
|
|
431
418
|
*/
|
|
432
|
-
|
|
419
|
+
name?: string;
|
|
433
420
|
/**
|
|
434
|
-
*
|
|
435
|
-
* @type {ToggleContext}
|
|
421
|
+
* Custom attributes specific to the user.
|
|
436
422
|
*/
|
|
437
|
-
|
|
423
|
+
customAttributes?: CustomAttributes;
|
|
424
|
+
};
|
|
425
|
+
/**
|
|
426
|
+
* Evaluation context for Hyphen feature toggle evaluation.
|
|
427
|
+
*
|
|
428
|
+
* @example
|
|
429
|
+
* ```typescript
|
|
430
|
+
* const context: ToggleContext = {
|
|
431
|
+
* targetingKey: "user-123",
|
|
432
|
+
* ipAddress: "203.0.113.42",
|
|
433
|
+
* customAttributes: {
|
|
434
|
+
* subscriptionLevel: "premium",
|
|
435
|
+
* region: "us-east",
|
|
436
|
+
* },
|
|
437
|
+
* user: {
|
|
438
|
+
* id: "user-123",
|
|
439
|
+
* email: "john.doe@example.com",
|
|
440
|
+
* name: "John Doe",
|
|
441
|
+
* customAttributes: {
|
|
442
|
+
* role: "admin",
|
|
443
|
+
* },
|
|
444
|
+
* },
|
|
445
|
+
* };
|
|
446
|
+
* ```
|
|
447
|
+
*/
|
|
448
|
+
type ToggleContext = {
|
|
438
449
|
/**
|
|
439
|
-
*
|
|
440
|
-
* @type {ToggleCachingOptions}
|
|
450
|
+
* Primary key used for targeting and bucketing.
|
|
441
451
|
*/
|
|
442
|
-
|
|
452
|
+
targetingKey?: string;
|
|
443
453
|
/**
|
|
444
|
-
*
|
|
445
|
-
* @type {boolean}
|
|
446
|
-
* @default false
|
|
454
|
+
* IP address of the user making the request.
|
|
447
455
|
*/
|
|
448
|
-
|
|
456
|
+
ipAddress?: string;
|
|
449
457
|
/**
|
|
450
|
-
*
|
|
451
|
-
* the default URIs for testin or if you are using a self-hosted version.
|
|
452
|
-
* @type {Array<string>}
|
|
453
|
-
* @example ['https://toggle.hyphen.ai']
|
|
458
|
+
* Custom attributes for evaluation context.
|
|
454
459
|
*/
|
|
455
|
-
|
|
456
|
-
};
|
|
457
|
-
type ToggleGetOptions = {
|
|
460
|
+
customAttributes?: CustomAttributes;
|
|
458
461
|
/**
|
|
459
|
-
*
|
|
460
|
-
* @type {ToggleContext}
|
|
462
|
+
* User information for evaluation.
|
|
461
463
|
*/
|
|
462
|
-
|
|
464
|
+
user?: ToggleUser;
|
|
463
465
|
};
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
466
|
+
/**
|
|
467
|
+
* Internal type combining application info with toggle context for evaluation.
|
|
468
|
+
*/
|
|
469
|
+
type ToggleEvaluation = {
|
|
470
|
+
application: string;
|
|
471
|
+
environment: string;
|
|
472
|
+
} & ToggleContext;
|
|
473
|
+
/**
|
|
474
|
+
* Options for toggle getter methods.
|
|
475
|
+
*/
|
|
476
|
+
type GetOptions = {
|
|
474
477
|
/**
|
|
475
|
-
*
|
|
476
|
-
* @returns {string | undefined}
|
|
478
|
+
* Override the default context for this request.
|
|
477
479
|
*/
|
|
478
|
-
|
|
480
|
+
context?: ToggleContext;
|
|
479
481
|
/**
|
|
480
|
-
*
|
|
481
|
-
* @param {string | undefined} value
|
|
482
|
+
* Whether to use caching for this request (if caching is configured).
|
|
482
483
|
*/
|
|
483
|
-
|
|
484
|
+
cache?: boolean;
|
|
485
|
+
};
|
|
486
|
+
/**
|
|
487
|
+
* Events emitted by the Toggle class.
|
|
488
|
+
*/
|
|
489
|
+
declare enum ToggleEvents {
|
|
484
490
|
/**
|
|
485
|
-
*
|
|
486
|
-
* @returns {string}
|
|
491
|
+
* Emitted when an error occurs during toggle evaluation.
|
|
487
492
|
*/
|
|
488
|
-
|
|
493
|
+
Error = "error"
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* Configuration options for the Toggle class.
|
|
497
|
+
*/
|
|
498
|
+
type ToggleOptions = {
|
|
489
499
|
/**
|
|
490
|
-
*
|
|
491
|
-
*
|
|
500
|
+
* Public API key for authentication.
|
|
501
|
+
* Can be found in your Hyphen dashboard.
|
|
502
|
+
* Must start with "public_".
|
|
492
503
|
*/
|
|
493
|
-
|
|
504
|
+
publicApiKey?: string;
|
|
494
505
|
/**
|
|
495
|
-
*
|
|
496
|
-
*
|
|
506
|
+
* The default context to use when one is not passed to getter methods.
|
|
507
|
+
* Can be overridden per-request using the GetOptions parameter.
|
|
497
508
|
*/
|
|
498
|
-
|
|
509
|
+
defaultContext?: ToggleContext;
|
|
499
510
|
/**
|
|
500
|
-
*
|
|
501
|
-
*
|
|
511
|
+
* Horizon endpoint URLs for load balancing and failover.
|
|
512
|
+
* If provided, requests will be attempted in order. If all fail,
|
|
513
|
+
* defaults to Hyphen's hosted service.
|
|
514
|
+
* @see {@link https://hyphen.ai/horizon} for more information
|
|
502
515
|
*/
|
|
503
|
-
|
|
516
|
+
horizonUrls?: string[];
|
|
504
517
|
/**
|
|
505
|
-
*
|
|
506
|
-
*
|
|
518
|
+
* Application ID for your Hyphen project.
|
|
519
|
+
* Can be found in your Hyphen dashboard.
|
|
507
520
|
*/
|
|
508
|
-
|
|
521
|
+
applicationId?: string;
|
|
509
522
|
/**
|
|
510
|
-
*
|
|
511
|
-
*
|
|
523
|
+
* Environment name (e.g., "production", "development", "staging").
|
|
524
|
+
* Defaults to "development" if not provided.
|
|
512
525
|
*/
|
|
513
|
-
|
|
526
|
+
environment?: string;
|
|
514
527
|
/**
|
|
515
|
-
*
|
|
516
|
-
*
|
|
528
|
+
* Default targeting key to use if one cannot be derived from context.
|
|
529
|
+
* If not provided, a random key will be generated.
|
|
517
530
|
*/
|
|
518
|
-
|
|
531
|
+
defaultTargetKey?: string;
|
|
532
|
+
cache?: Cacheable;
|
|
533
|
+
};
|
|
534
|
+
/**
|
|
535
|
+
* Toggle class for feature flag management with Hyphen.
|
|
536
|
+
*
|
|
537
|
+
* This class provides methods to retrieve feature toggle values with support for
|
|
538
|
+
* multiple value types (boolean, string, number, object), context-based targeting,
|
|
539
|
+
* and load-balanced requests across multiple Horizon endpoints.
|
|
540
|
+
*
|
|
541
|
+
* @extends Hookified - Provides event and hook system capabilities
|
|
542
|
+
*
|
|
543
|
+
* @example
|
|
544
|
+
* Basic usage:
|
|
545
|
+
* ```typescript
|
|
546
|
+
* const toggle = new Toggle({
|
|
547
|
+
* publicApiKey: 'public_your-key-here',
|
|
548
|
+
* applicationId: 'app-123',
|
|
549
|
+
* environment: 'production'
|
|
550
|
+
* });
|
|
551
|
+
*
|
|
552
|
+
* const isEnabled = await toggle.getBoolean('new-feature', false);
|
|
553
|
+
* ```
|
|
554
|
+
*
|
|
555
|
+
* @example
|
|
556
|
+
* With context:
|
|
557
|
+
* ```typescript
|
|
558
|
+
* const toggle = new Toggle({
|
|
559
|
+
* publicApiKey: 'public_your-key-here',
|
|
560
|
+
* applicationId: 'app-123',
|
|
561
|
+
* defaultContext: {
|
|
562
|
+
* targetingKey: 'user-456',
|
|
563
|
+
* user: { id: 'user-456', email: 'user@example.com' }
|
|
564
|
+
* }
|
|
565
|
+
* });
|
|
566
|
+
*
|
|
567
|
+
* const message = await toggle.getString('welcome-message', 'Hello!');
|
|
568
|
+
* ```
|
|
569
|
+
*
|
|
570
|
+
* @example
|
|
571
|
+
* With error handling:
|
|
572
|
+
* ```typescript
|
|
573
|
+
* const toggle = new Toggle({ publicApiKey: 'public_key', applicationId: 'app' });
|
|
574
|
+
*
|
|
575
|
+
* toggle.on('error', (error) => {
|
|
576
|
+
* console.error('Toggle error:', error);
|
|
577
|
+
* });
|
|
578
|
+
*
|
|
579
|
+
* const value = await toggle.getNumber('max-items', 10);
|
|
580
|
+
* ```
|
|
581
|
+
*/
|
|
582
|
+
declare class Toggle extends Hookified {
|
|
583
|
+
private _publicApiKey?;
|
|
584
|
+
private _organizationId;
|
|
585
|
+
private _applicationId;
|
|
586
|
+
private _environment;
|
|
587
|
+
private _horizonUrls;
|
|
588
|
+
private _net;
|
|
589
|
+
private _defaultContext;
|
|
590
|
+
private _defaultTargetingKey;
|
|
591
|
+
/**
|
|
592
|
+
* Creates a new Toggle instance.
|
|
593
|
+
*
|
|
594
|
+
* @param options - Configuration options for the toggle client
|
|
595
|
+
*
|
|
596
|
+
* @example
|
|
597
|
+
* ```typescript
|
|
598
|
+
* // Minimal configuration
|
|
599
|
+
* const toggle = new Toggle({
|
|
600
|
+
* publicApiKey: 'public_your-key',
|
|
601
|
+
* applicationId: 'app-123'
|
|
602
|
+
* });
|
|
603
|
+
*
|
|
604
|
+
* // With full options
|
|
605
|
+
* const toggle = new Toggle({
|
|
606
|
+
* publicApiKey: 'public_your-key',
|
|
607
|
+
* applicationId: 'app-123',
|
|
608
|
+
* environment: 'production',
|
|
609
|
+
* defaultContext: { targetingKey: 'user-456' },
|
|
610
|
+
* horizonUrls: ['https://my-horizon.example.com']
|
|
611
|
+
* });
|
|
612
|
+
* ```
|
|
613
|
+
*/
|
|
614
|
+
constructor(options?: ToggleOptions);
|
|
519
615
|
/**
|
|
520
|
-
*
|
|
521
|
-
*
|
|
616
|
+
* Gets the public API key used for authentication.
|
|
617
|
+
*
|
|
618
|
+
* @returns The current public API key or undefined if not set
|
|
522
619
|
*/
|
|
523
|
-
|
|
620
|
+
get publicApiKey(): string | undefined;
|
|
524
621
|
/**
|
|
525
|
-
*
|
|
526
|
-
*
|
|
622
|
+
* Sets the public API key used for authentication.
|
|
623
|
+
*
|
|
624
|
+
* @param value - The public API key string or undefined to clear
|
|
625
|
+
* @throws {Error} If the key doesn't start with "public_"
|
|
527
626
|
*/
|
|
528
|
-
|
|
627
|
+
set publicApiKey(value: string | undefined);
|
|
529
628
|
/**
|
|
530
|
-
*
|
|
531
|
-
*
|
|
629
|
+
* Gets the default context used for toggle evaluations.
|
|
630
|
+
*
|
|
631
|
+
* @returns The current default ToggleContext
|
|
532
632
|
*/
|
|
533
|
-
|
|
633
|
+
get defaultContext(): ToggleContext | undefined;
|
|
534
634
|
/**
|
|
535
|
-
*
|
|
536
|
-
*
|
|
635
|
+
* Sets the default context used for toggle evaluations.
|
|
636
|
+
*
|
|
637
|
+
* @param value - The ToggleContext to use as default
|
|
537
638
|
*/
|
|
538
|
-
|
|
639
|
+
set defaultContext(value: ToggleContext);
|
|
539
640
|
/**
|
|
540
|
-
*
|
|
541
|
-
*
|
|
641
|
+
* Gets the organization ID extracted from the public API key.
|
|
642
|
+
*
|
|
643
|
+
* @returns The organization ID string or undefined if not available
|
|
542
644
|
*/
|
|
543
|
-
|
|
645
|
+
get organizationId(): string | undefined;
|
|
544
646
|
/**
|
|
545
|
-
*
|
|
546
|
-
*
|
|
547
|
-
*
|
|
548
|
-
*
|
|
549
|
-
*
|
|
647
|
+
* Gets the Horizon endpoint URLs used for load balancing.
|
|
648
|
+
*
|
|
649
|
+
* These URLs are used to distribute requests across multiple Horizon endpoints.
|
|
650
|
+
* If endpoints fail, the system will attempt to use the default horizon endpoint service.
|
|
651
|
+
*
|
|
652
|
+
* @returns Array of Horizon endpoint URLs
|
|
653
|
+
* @see {@link https://hyphen.ai/horizon} for more information
|
|
654
|
+
*/
|
|
655
|
+
get horizonUrls(): string[];
|
|
656
|
+
/**
|
|
657
|
+
* Sets the Horizon endpoint URLs for load balancing.
|
|
658
|
+
*
|
|
659
|
+
* Configures multiple Horizon endpoints that will be used for load balancing.
|
|
660
|
+
* When endpoints fail, the system will fall back to the default horizon endpoint service.
|
|
661
|
+
*
|
|
662
|
+
* @param value - Array of Horizon endpoint URLs or empty array to clear
|
|
663
|
+
* @see {@link https://hyphen.ai/horizon} for more information
|
|
664
|
+
*
|
|
665
|
+
* @example
|
|
666
|
+
* ```typescript
|
|
667
|
+
* const toggle = new Toggle();
|
|
668
|
+
* toggle.horizonUrls = [
|
|
669
|
+
* 'https://org1.toggle.hyphen.cloud',
|
|
670
|
+
* 'https://org2.toggle.hyphen.cloud'
|
|
671
|
+
* ];
|
|
672
|
+
* ```
|
|
673
|
+
*/
|
|
674
|
+
set horizonUrls(value: string[]);
|
|
675
|
+
/**
|
|
676
|
+
* Gets the application ID used for toggle context.
|
|
677
|
+
*
|
|
678
|
+
* @returns The current application ID or undefined if not set
|
|
550
679
|
*/
|
|
551
|
-
|
|
680
|
+
get applicationId(): string | undefined;
|
|
552
681
|
/**
|
|
553
|
-
*
|
|
554
|
-
*
|
|
682
|
+
* Sets the application ID used for toggle context.
|
|
683
|
+
*
|
|
684
|
+
* @param value - The application ID string or undefined to clear
|
|
555
685
|
*/
|
|
556
|
-
|
|
686
|
+
set applicationId(value: string | undefined);
|
|
557
687
|
/**
|
|
558
|
-
*
|
|
559
|
-
*
|
|
560
|
-
*
|
|
561
|
-
* @returns {Promise<Client>}
|
|
688
|
+
* Gets the environment used for toggle context.
|
|
689
|
+
*
|
|
690
|
+
* @returns The current environment (defaults to 'development')
|
|
562
691
|
*/
|
|
563
|
-
|
|
692
|
+
get environment(): string | undefined;
|
|
564
693
|
/**
|
|
565
|
-
*
|
|
566
|
-
*
|
|
567
|
-
* @param
|
|
568
|
-
* @param {T} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
|
|
569
|
-
* @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
|
|
570
|
-
* @returns {Promise<T>}
|
|
694
|
+
* Sets the environment used for toggle context.
|
|
695
|
+
*
|
|
696
|
+
* @param value - The environment string or undefined to clear
|
|
571
697
|
*/
|
|
572
|
-
|
|
698
|
+
set environment(value: string | undefined);
|
|
573
699
|
/**
|
|
574
|
-
*
|
|
575
|
-
*
|
|
576
|
-
* @
|
|
577
|
-
* @param {boolean} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
|
|
578
|
-
* @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
|
|
579
|
-
* @returns {Promise<boolean>} - The value of the feature flag
|
|
700
|
+
* Gets the default targeting key used for toggle evaluations.
|
|
701
|
+
*
|
|
702
|
+
* @returns The current default targeting key or undefined if not set
|
|
580
703
|
*/
|
|
581
|
-
|
|
704
|
+
get defaultTargetingKey(): string;
|
|
582
705
|
/**
|
|
583
|
-
*
|
|
584
|
-
*
|
|
585
|
-
* @param
|
|
586
|
-
* @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
|
|
587
|
-
* @returns {Promise<string>} - The value of the feature flag
|
|
706
|
+
* Sets the default targeting key used for toggle evaluations.
|
|
707
|
+
*
|
|
708
|
+
* @param value - The targeting key string or undefined to clear
|
|
588
709
|
*/
|
|
589
|
-
|
|
590
|
-
getNumber(key: string, defaultValue: number, options?: ToggleGetOptions): Promise<number>;
|
|
710
|
+
set defaultTargetingKey(value: string);
|
|
591
711
|
/**
|
|
592
|
-
*
|
|
593
|
-
*
|
|
594
|
-
* @
|
|
595
|
-
* @param {T} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
|
|
596
|
-
* @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
|
|
597
|
-
* @returns {Promise<T>} - The value of the feature flag
|
|
712
|
+
* Gets the Cacheable instance used for caching fetch operations.
|
|
713
|
+
*
|
|
714
|
+
* @returns The current Cacheable instance
|
|
598
715
|
*/
|
|
599
|
-
|
|
716
|
+
get cache(): Cacheable;
|
|
717
|
+
/**
|
|
718
|
+
* Sets the Cacheable instance for caching fetch operations.
|
|
719
|
+
*
|
|
720
|
+
* @param cache - The Cacheable instance to use for caching
|
|
721
|
+
*/
|
|
722
|
+
set cache(cache: Cacheable);
|
|
723
|
+
/**
|
|
724
|
+
* Retrieves a toggle value with generic type support.
|
|
725
|
+
*
|
|
726
|
+
* This is the core method for fetching toggle values. All convenience methods
|
|
727
|
+
* (getBoolean, getString, getNumber, getObject) delegate to this method.
|
|
728
|
+
*
|
|
729
|
+
* @template T - The expected type of the toggle value
|
|
730
|
+
* @param toggleKey - The key of the toggle to retrieve
|
|
731
|
+
* @param defaultValue - The value to return if the toggle is not found or an error occurs
|
|
732
|
+
* @param options - Optional configuration including context override
|
|
733
|
+
* @returns Promise resolving to the toggle value or defaultValue
|
|
734
|
+
*
|
|
735
|
+
* @example
|
|
736
|
+
* ```typescript
|
|
737
|
+
* const toggle = new Toggle({ publicApiKey: 'public_key', applicationId: 'app-id' });
|
|
738
|
+
*
|
|
739
|
+
* // Get a boolean
|
|
740
|
+
* const enabled = await toggle.get<boolean>('feature-flag', false);
|
|
741
|
+
*
|
|
742
|
+
* // Get an object with type safety
|
|
743
|
+
* interface Config { theme: string; }
|
|
744
|
+
* const config = await toggle.get<Config>('app-config', { theme: 'light' });
|
|
745
|
+
*
|
|
746
|
+
* // Override context for a single request
|
|
747
|
+
* const value = await toggle.get('key', 'default', {
|
|
748
|
+
* context: { targetingKey: 'user-456' }
|
|
749
|
+
* });
|
|
750
|
+
* ```
|
|
751
|
+
*/
|
|
752
|
+
get<T>(toggleKey: string, defaultValue: T, options?: GetOptions): Promise<T>;
|
|
753
|
+
/**
|
|
754
|
+
* Retrieves a boolean toggle value.
|
|
755
|
+
*
|
|
756
|
+
* This is a convenience method that wraps the generic get() method with boolean type safety.
|
|
757
|
+
*
|
|
758
|
+
* @param toggleKey - The key of the toggle to retrieve
|
|
759
|
+
* @param defaultValue - The boolean value to return if the toggle is not found or an error occurs
|
|
760
|
+
* @param options - Optional configuration including context for toggle evaluation
|
|
761
|
+
* @returns Promise resolving to the boolean toggle value or defaultValue
|
|
762
|
+
*
|
|
763
|
+
* @example
|
|
764
|
+
* ```typescript
|
|
765
|
+
* const toggle = new Toggle({ publicApiKey: 'public_key', applicationId: 'app-id' });
|
|
766
|
+
* const isFeatureEnabled = await toggle.getBoolean('feature-flag', false);
|
|
767
|
+
* console.log(isFeatureEnabled); // true or false
|
|
768
|
+
* ```
|
|
769
|
+
*/
|
|
770
|
+
getBoolean(toggleKey: string, defaultValue: boolean, options?: GetOptions): Promise<boolean>;
|
|
771
|
+
/**
|
|
772
|
+
* Retrieves a string toggle value.
|
|
773
|
+
*
|
|
774
|
+
* This is a convenience method that wraps the generic get() method with string type safety.
|
|
775
|
+
*
|
|
776
|
+
* @param toggleKey - The key of the toggle to retrieve
|
|
777
|
+
* @param defaultValue - The string value to return if the toggle is not found or an error occurs
|
|
778
|
+
* @param options - Optional configuration including context for toggle evaluation
|
|
779
|
+
* @returns Promise resolving to the string toggle value or defaultValue
|
|
780
|
+
*
|
|
781
|
+
* @example
|
|
782
|
+
* ```typescript
|
|
783
|
+
* const toggle = new Toggle({ publicApiKey: 'public_key', applicationId: 'app-id' });
|
|
784
|
+
* const message = await toggle.getString('welcome-message', 'Hello World');
|
|
785
|
+
* console.log(message); // 'Welcome to our app!' or 'Hello World'
|
|
786
|
+
* ```
|
|
787
|
+
*/
|
|
788
|
+
getString(toggleKey: string, defaultValue: string, options?: GetOptions): Promise<string>;
|
|
789
|
+
/**
|
|
790
|
+
* Retrieves an object toggle value.
|
|
791
|
+
*
|
|
792
|
+
* This is a convenience method that wraps the generic get() method with object type safety.
|
|
793
|
+
* Note that the toggle service may return JSON as a string, which should be parsed if needed.
|
|
794
|
+
*
|
|
795
|
+
* @template T - The expected object type
|
|
796
|
+
* @param toggleKey - The key of the toggle to retrieve
|
|
797
|
+
* @param defaultValue - The object value to return if the toggle is not found or an error occurs
|
|
798
|
+
* @param options - Optional configuration including context for toggle evaluation
|
|
799
|
+
* @returns Promise resolving to the object toggle value or defaultValue
|
|
800
|
+
*
|
|
801
|
+
* @example
|
|
802
|
+
* ```typescript
|
|
803
|
+
* const toggle = new Toggle({ publicApiKey: 'public_key', applicationId: 'app-id' });
|
|
804
|
+
* const config = await toggle.getObject('app-config', { theme: 'light' });
|
|
805
|
+
* console.log(config); // { theme: 'dark', features: ['a', 'b'] } or { theme: 'light' }
|
|
806
|
+
* ```
|
|
807
|
+
*/
|
|
808
|
+
getObject<T extends object>(toggleKey: string, defaultValue: T, options?: GetOptions): Promise<T>;
|
|
809
|
+
/**
|
|
810
|
+
* Retrieves a number toggle value.
|
|
811
|
+
*
|
|
812
|
+
* This is a convenience method that wraps the generic get() method with number type safety.
|
|
813
|
+
*
|
|
814
|
+
* @param toggleKey - The key of the toggle to retrieve
|
|
815
|
+
* @param defaultValue - The number value to return if the toggle is not found or an error occurs
|
|
816
|
+
* @param options - Optional configuration including context for toggle evaluation
|
|
817
|
+
* @returns Promise resolving to the number toggle value or defaultValue
|
|
818
|
+
*
|
|
819
|
+
* @example
|
|
820
|
+
* ```typescript
|
|
821
|
+
* const toggle = new Toggle({ publicApiKey: 'public_key', applicationId: 'app-id' });
|
|
822
|
+
* const maxRetries = await toggle.getNumber('max-retries', 3);
|
|
823
|
+
* console.log(maxRetries); // 5 or 3
|
|
824
|
+
* ```
|
|
825
|
+
*/
|
|
826
|
+
getNumber(toggleKey: string, defaultValue: number, options?: GetOptions): Promise<number>;
|
|
827
|
+
/**
|
|
828
|
+
* Makes an HTTP POST request to the specified URL with automatic authentication.
|
|
829
|
+
*
|
|
830
|
+
* This method uses browser-compatible fetch and automatically includes the
|
|
831
|
+
* public API key in the x-api-key header if available. It supports load
|
|
832
|
+
* balancing across multiple horizon URLs with fallback behavior.
|
|
833
|
+
*
|
|
834
|
+
* @template T - The expected response type
|
|
835
|
+
* @param path - The API path to request (e.g., '/api/toggles')
|
|
836
|
+
* @param payload - The JSON payload to send in the request body
|
|
837
|
+
* @param options - Optional fetch configuration. Cache is set at .cache
|
|
838
|
+
* @returns Promise resolving to the parsed JSON response
|
|
839
|
+
* @throws {Error} If no horizon URLs are configured or all requests fail
|
|
840
|
+
*
|
|
841
|
+
* @example
|
|
842
|
+
* ```typescript
|
|
843
|
+
* const toggle = new Toggle({
|
|
844
|
+
* publicApiKey: 'public_your-key-here',
|
|
845
|
+
* horizonUrls: ['https://api.hyphen.cloud']
|
|
846
|
+
* });
|
|
847
|
+
*
|
|
848
|
+
* interface ToggleResponse {
|
|
849
|
+
* enabled: boolean;
|
|
850
|
+
* value: string;
|
|
851
|
+
* }
|
|
852
|
+
*
|
|
853
|
+
* const result = await toggle.fetch<ToggleResponse>('/api/toggle/feature-flag', {
|
|
854
|
+
* context: { targetingKey: 'user-123' }
|
|
855
|
+
* });
|
|
856
|
+
* console.log(result.enabled); // true/false
|
|
857
|
+
* ```
|
|
858
|
+
*/
|
|
859
|
+
fetch<T>(path: string, payload?: unknown, options?: Omit<FetchOptions, "cache">): Promise<T>;
|
|
860
|
+
/**
|
|
861
|
+
* Validates and sets the public API key.
|
|
862
|
+
*
|
|
863
|
+
* This method is used internally by the publicApiKey setter to validate
|
|
864
|
+
* that the key starts with "public_" prefix. If the key is invalid,
|
|
865
|
+
* an error is thrown.
|
|
866
|
+
*
|
|
867
|
+
* @param key - The public API key string or undefined to clear
|
|
868
|
+
* @throws {Error} If the key doesn't start with "public_"
|
|
869
|
+
*
|
|
870
|
+
* @example
|
|
871
|
+
* ```typescript
|
|
872
|
+
* const toggle = new Toggle();
|
|
873
|
+
*
|
|
874
|
+
* // Valid key
|
|
875
|
+
* toggle.setPublicKey('public_abc123'); // OK
|
|
876
|
+
*
|
|
877
|
+
* // Invalid key
|
|
878
|
+
* toggle.setPublicKey('invalid_key'); // Throws error
|
|
879
|
+
*
|
|
880
|
+
* // Clear key
|
|
881
|
+
* toggle.setPublicKey(undefined); // OK
|
|
882
|
+
* ```
|
|
883
|
+
*/
|
|
884
|
+
setPublicKey(key: string | undefined): void;
|
|
885
|
+
/**
|
|
886
|
+
* Extracts the organization ID from a public API key.
|
|
887
|
+
*
|
|
888
|
+
* The public key format is: `public_<base64-encoded-data>`
|
|
889
|
+
* The base64 data contains: `orgId:secretData`
|
|
890
|
+
* Only alphanumeric characters, underscores, and hyphens are considered valid in org IDs.
|
|
891
|
+
*
|
|
892
|
+
* @param publicKey - The public API key to extract the organization ID from
|
|
893
|
+
* @returns The organization ID if valid and extractable, undefined otherwise
|
|
894
|
+
*
|
|
895
|
+
* @example
|
|
896
|
+
* ```typescript
|
|
897
|
+
* const toggle = new Toggle();
|
|
898
|
+
* const orgId = toggle.getOrgIdFromPublicKey('public_dGVzdC1vcmc6c2VjcmV0');
|
|
899
|
+
* console.log(orgId); // 'test-org'
|
|
900
|
+
* ```
|
|
901
|
+
*/
|
|
902
|
+
getOrgIdFromPublicKey(publicKey: string): string | undefined;
|
|
903
|
+
/**
|
|
904
|
+
* Builds the default Horizon API URL for the given public key.
|
|
905
|
+
*
|
|
906
|
+
* If a valid organization ID can be extracted from the public key, returns an
|
|
907
|
+
* organization-specific URL. Otherwise, returns the default fallback URL.
|
|
908
|
+
*
|
|
909
|
+
* @param publicKey - The public API key to build the URL for
|
|
910
|
+
* @returns Organization-specific URL or default fallback URL
|
|
911
|
+
*
|
|
912
|
+
* @example
|
|
913
|
+
* ```typescript
|
|
914
|
+
* const toggle = new Toggle();
|
|
915
|
+
*
|
|
916
|
+
* // With valid org ID
|
|
917
|
+
* const orgUrl = toggle.buildDefaultHorizonUrl('public_dGVzdC1vcmc6c2VjcmV0');
|
|
918
|
+
* console.log(orgUrl); // 'https://test-org.toggle.hyphen.cloud'
|
|
919
|
+
*
|
|
920
|
+
* // With invalid key
|
|
921
|
+
* const defaultUrl = toggle.buildDefaultHorizonUrl('invalid-key');
|
|
922
|
+
* console.log(defaultUrl); // 'https://toggle.hyphen.cloud'
|
|
923
|
+
* ```
|
|
924
|
+
*/
|
|
925
|
+
getDefaultHorizonUrl(publicKey?: string): string;
|
|
926
|
+
/**
|
|
927
|
+
* Gets the default Horizon URLs for load balancing and failover.
|
|
928
|
+
*
|
|
929
|
+
* If a public key is provided, returns an array with the organization-specific
|
|
930
|
+
* URL as primary and the default Hyphen URL as fallback. Without a public key,
|
|
931
|
+
* returns only the default Hyphen URL.
|
|
932
|
+
*
|
|
933
|
+
* @param publicKey - Optional public API key to derive organization-specific URL
|
|
934
|
+
* @returns Array of Horizon URLs for load balancing
|
|
935
|
+
*
|
|
936
|
+
* @example
|
|
937
|
+
* ```typescript
|
|
938
|
+
* const toggle = new Toggle();
|
|
939
|
+
*
|
|
940
|
+
* // Without public key - returns default only
|
|
941
|
+
* const defaultUrls = toggle.getDefaultHorizonUrls();
|
|
942
|
+
* // ['https://toggle.hyphen.cloud']
|
|
943
|
+
*
|
|
944
|
+
* // With public key - returns org-specific + fallback
|
|
945
|
+
* const urls = toggle.getDefaultHorizonUrls('public_dGVzdC1vcmc6c2VjcmV0');
|
|
946
|
+
* // ['https://test-org.toggle.hyphen.cloud', 'https://toggle.hyphen.cloud']
|
|
947
|
+
* ```
|
|
948
|
+
*/
|
|
949
|
+
getDefaultHorizonUrls(publicKey?: string): string[];
|
|
950
|
+
/**
|
|
951
|
+
* Generates a unique targeting key based on available context.
|
|
952
|
+
*
|
|
953
|
+
* @returns A targeting key in the format: `[app]-[env]-[random]` or simplified versions
|
|
954
|
+
*/
|
|
955
|
+
generateTargetKey(): string;
|
|
956
|
+
/**
|
|
957
|
+
* Extracts targeting key from a toggle context with fallback logic.
|
|
958
|
+
*
|
|
959
|
+
* @param context - The toggle context to extract targeting key from
|
|
960
|
+
* @returns The targeting key string
|
|
961
|
+
*/
|
|
962
|
+
private getTargetingKey;
|
|
600
963
|
}
|
|
601
964
|
|
|
602
965
|
type HyphenOptions = {
|
|
@@ -622,21 +985,21 @@ type HyphenOptions = {
|
|
|
622
985
|
* @see ToggleOptions
|
|
623
986
|
* @default {Toggle}
|
|
624
987
|
*/
|
|
625
|
-
toggle?: Omit<ToggleOptions, "publicApiKey"
|
|
988
|
+
toggle?: Omit<ToggleOptions, "publicApiKey">;
|
|
626
989
|
/**
|
|
627
990
|
* Options for the NetInfo service.
|
|
628
991
|
* Excludes apiKey and throwErrors from NetInfoOptions.
|
|
629
992
|
* @see NetInfoOptions
|
|
630
993
|
* @default {NetInfo}
|
|
631
994
|
*/
|
|
632
|
-
netInfo?: Omit<NetInfoOptions, "apiKey"
|
|
995
|
+
netInfo?: Omit<NetInfoOptions, "apiKey">;
|
|
633
996
|
/**
|
|
634
997
|
* Options for the Link service.
|
|
635
998
|
* Excludes apiKey and throwErrors from LinkOptions.
|
|
636
999
|
* @see LinkOptions
|
|
637
1000
|
* @default {Link}
|
|
638
1001
|
*/
|
|
639
|
-
link?: Omit<LinkOptions, "apiKey"
|
|
1002
|
+
link?: Omit<LinkOptions, "apiKey">;
|
|
640
1003
|
} & HookifiedOptions;
|
|
641
1004
|
declare class Hyphen extends Hookified {
|
|
642
1005
|
private readonly _netInfo;
|
|
@@ -684,18 +1047,6 @@ declare class Hyphen extends Hookified {
|
|
|
684
1047
|
* @param {string | undefined} value - The API key to set.
|
|
685
1048
|
*/
|
|
686
1049
|
set apiKey(value: string | undefined);
|
|
687
|
-
/**
|
|
688
|
-
* Get whether to throw errors or not.
|
|
689
|
-
* If set to true, errors will be thrown instead of logged.
|
|
690
|
-
* @returns {boolean} Whether to throw errors or not.
|
|
691
|
-
*/
|
|
692
|
-
get throwErrors(): boolean;
|
|
693
|
-
/**
|
|
694
|
-
* Set whether to throw errors or not. If set to true, errors will be thrown instead of logged.
|
|
695
|
-
* This will update the underlying services as well.
|
|
696
|
-
* @param {boolean} value - Whether to throw errors or not.
|
|
697
|
-
*/
|
|
698
|
-
set throwErrors(value: boolean);
|
|
699
1050
|
}
|
|
700
1051
|
|
|
701
|
-
export { type EnvOptions, Hyphen, type HyphenOptions, type LoadEnvOptions, Toggle, type
|
|
1052
|
+
export { type EnvOptions, Hyphen, type HyphenOptions, type LoadEnvOptions, Toggle, type ToggleContext, type ToggleEvaluation, ToggleEvents, type ToggleOptions, type ToggleUser, env, loadEnv };
|