@hyphen/sdk 1.12.2 → 2.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/README.md +110 -99
- package/dist/index.cjs +599 -302
- package/dist/index.d.cts +527 -164
- package/dist/index.d.ts +527 -164
- package/dist/index.js +599 -301
- package/package.json +10 -13
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { HookifiedOptions, Hookified } from 'hookified';
|
|
2
|
-
import
|
|
3
|
-
import { AxiosRequestConfig } from 'axios';
|
|
2
|
+
import { FetchRequestInit, FetchOptions } from '@cacheable/net';
|
|
4
3
|
import { Cacheable } from 'cacheable';
|
|
5
4
|
import pino from 'pino';
|
|
6
|
-
import { EvaluationContext, Client } from '@openfeature/server-sdk';
|
|
7
5
|
|
|
8
6
|
type EnvOptions = {
|
|
9
7
|
path?: string;
|
|
@@ -23,6 +21,14 @@ declare function env(options?: EnvOptions): void;
|
|
|
23
21
|
declare const loadEnv: typeof env;
|
|
24
22
|
type LoadEnvOptions = EnvOptions;
|
|
25
23
|
|
|
24
|
+
interface HttpResponse<T = any> {
|
|
25
|
+
data: T;
|
|
26
|
+
status: number;
|
|
27
|
+
statusText: string;
|
|
28
|
+
headers: any;
|
|
29
|
+
config: any;
|
|
30
|
+
request?: any;
|
|
31
|
+
}
|
|
26
32
|
type BaseServiceOptions = {
|
|
27
33
|
throwErrors?: boolean;
|
|
28
34
|
} & HookifiedOptions;
|
|
@@ -30,6 +36,7 @@ declare class BaseService extends Hookified {
|
|
|
30
36
|
private _log;
|
|
31
37
|
private _cache;
|
|
32
38
|
private _throwErrors;
|
|
39
|
+
private _net;
|
|
33
40
|
constructor(options?: BaseServiceOptions);
|
|
34
41
|
get log(): pino.Logger;
|
|
35
42
|
set log(value: pino.Logger);
|
|
@@ -40,11 +47,15 @@ declare class BaseService extends Hookified {
|
|
|
40
47
|
error(message: string, ...args: any[]): void;
|
|
41
48
|
warn(message: string, ...args: any[]): void;
|
|
42
49
|
info(message: string, ...args: any[]): void;
|
|
43
|
-
get<T>(url: string, config?:
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
50
|
+
get<T>(url: string, config?: FetchRequestInit & {
|
|
51
|
+
params?: any;
|
|
52
|
+
}): Promise<HttpResponse<T>>;
|
|
53
|
+
post<T>(url: string, data: any, config?: FetchRequestInit): Promise<HttpResponse<T>>;
|
|
54
|
+
put<T>(url: string, data: any, config?: FetchRequestInit): Promise<HttpResponse<T>>;
|
|
55
|
+
delete<T>(url: string, config?: FetchRequestInit & {
|
|
56
|
+
data?: any;
|
|
57
|
+
}): Promise<HttpResponse<T>>;
|
|
58
|
+
patch<T>(url: string, data: any, config?: FetchRequestInit): Promise<HttpResponse<T>>;
|
|
48
59
|
createHeaders(apiKey?: string): Record<string, string>;
|
|
49
60
|
}
|
|
50
61
|
|
|
@@ -386,205 +397,569 @@ declare class NetInfo extends BaseService {
|
|
|
386
397
|
getIpInfos(ips: string[]): Promise<Array<ipInfo | ipInfoError>>;
|
|
387
398
|
}
|
|
388
399
|
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
beforeGetObject = "beforeGetObject",
|
|
398
|
-
afterGetObject = "afterGetObject"
|
|
399
|
-
}
|
|
400
|
-
type ToggleCachingOptions = {
|
|
401
|
-
ttl?: number;
|
|
402
|
-
generateCacheKeyFn?: (context?: ToggleContext) => string;
|
|
403
|
-
};
|
|
404
|
-
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 = {
|
|
405
408
|
/**
|
|
406
|
-
*
|
|
407
|
-
* @type {string}
|
|
409
|
+
* Unique identifier for the user.
|
|
408
410
|
*/
|
|
409
|
-
|
|
411
|
+
id: string;
|
|
410
412
|
/**
|
|
411
|
-
*
|
|
412
|
-
* @type {string}
|
|
413
|
+
* User's email address.
|
|
413
414
|
*/
|
|
414
|
-
|
|
415
|
+
email?: string;
|
|
415
416
|
/**
|
|
416
|
-
*
|
|
417
|
-
* @type {string}
|
|
418
|
-
* @example production
|
|
417
|
+
* User's display name.
|
|
419
418
|
*/
|
|
420
|
-
|
|
419
|
+
name?: string;
|
|
421
420
|
/**
|
|
422
|
-
*
|
|
423
|
-
* @type {ToggleContext}
|
|
421
|
+
* Custom attributes specific to the user.
|
|
424
422
|
*/
|
|
425
|
-
|
|
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 = {
|
|
426
449
|
/**
|
|
427
|
-
*
|
|
428
|
-
* @type {ToggleCachingOptions}
|
|
450
|
+
* Primary key used for targeting and bucketing.
|
|
429
451
|
*/
|
|
430
|
-
|
|
452
|
+
targetingKey?: string;
|
|
431
453
|
/**
|
|
432
|
-
*
|
|
433
|
-
* @type {boolean}
|
|
434
|
-
* @default false
|
|
454
|
+
* IP address of the user making the request.
|
|
435
455
|
*/
|
|
436
|
-
|
|
456
|
+
ipAddress?: string;
|
|
437
457
|
/**
|
|
438
|
-
*
|
|
439
|
-
* the default URIs for testin or if you are using a self-hosted version.
|
|
440
|
-
* @type {Array<string>}
|
|
441
|
-
* @example ['https://toggle.hyphen.ai']
|
|
458
|
+
* Custom attributes for evaluation context.
|
|
442
459
|
*/
|
|
443
|
-
|
|
444
|
-
};
|
|
445
|
-
type ToggleGetOptions = {
|
|
460
|
+
customAttributes?: CustomAttributes;
|
|
446
461
|
/**
|
|
447
|
-
*
|
|
448
|
-
* @type {ToggleContext}
|
|
462
|
+
* User information for evaluation.
|
|
449
463
|
*/
|
|
450
|
-
|
|
464
|
+
user?: ToggleUser;
|
|
451
465
|
};
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
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 = {
|
|
462
477
|
/**
|
|
463
|
-
*
|
|
464
|
-
* @returns {string | undefined}
|
|
478
|
+
* Override the default context for this request.
|
|
465
479
|
*/
|
|
466
|
-
|
|
480
|
+
context?: ToggleContext;
|
|
467
481
|
/**
|
|
468
|
-
*
|
|
469
|
-
* @param {string | undefined} value
|
|
482
|
+
* Whether to use caching for this request (if caching is configured).
|
|
470
483
|
*/
|
|
471
|
-
|
|
484
|
+
cache?: boolean;
|
|
485
|
+
};
|
|
486
|
+
/**
|
|
487
|
+
* Events emitted by the Toggle class.
|
|
488
|
+
*/
|
|
489
|
+
declare enum ToggleEvents {
|
|
472
490
|
/**
|
|
473
|
-
*
|
|
474
|
-
* @returns {string}
|
|
491
|
+
* Emitted when an error occurs during toggle evaluation.
|
|
475
492
|
*/
|
|
476
|
-
|
|
493
|
+
Error = "error"
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* Configuration options for the Toggle class.
|
|
497
|
+
*/
|
|
498
|
+
type ToggleOptions = {
|
|
477
499
|
/**
|
|
478
|
-
*
|
|
479
|
-
*
|
|
500
|
+
* Public API key for authentication.
|
|
501
|
+
* Can be found in your Hyphen dashboard.
|
|
502
|
+
* Must start with "public_".
|
|
480
503
|
*/
|
|
481
|
-
|
|
504
|
+
publicApiKey?: string;
|
|
482
505
|
/**
|
|
483
|
-
*
|
|
484
|
-
*
|
|
506
|
+
* The default context to use when one is not passed to getter methods.
|
|
507
|
+
* Can be overridden per-request using the GetOptions parameter.
|
|
485
508
|
*/
|
|
486
|
-
|
|
509
|
+
defaultContext?: ToggleContext;
|
|
487
510
|
/**
|
|
488
|
-
*
|
|
489
|
-
*
|
|
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
|
|
490
515
|
*/
|
|
491
|
-
|
|
516
|
+
horizonUrls?: string[];
|
|
492
517
|
/**
|
|
493
|
-
*
|
|
494
|
-
*
|
|
518
|
+
* Application ID for your Hyphen project.
|
|
519
|
+
* Can be found in your Hyphen dashboard.
|
|
495
520
|
*/
|
|
496
|
-
|
|
521
|
+
applicationId?: string;
|
|
497
522
|
/**
|
|
498
|
-
*
|
|
499
|
-
*
|
|
523
|
+
* Environment name (e.g., "production", "development", "staging").
|
|
524
|
+
* Defaults to "development" if not provided.
|
|
500
525
|
*/
|
|
501
|
-
|
|
526
|
+
environment?: string;
|
|
502
527
|
/**
|
|
503
|
-
*
|
|
504
|
-
*
|
|
528
|
+
* Default targeting key to use if one cannot be derived from context.
|
|
529
|
+
* If not provided, a random key will be generated.
|
|
505
530
|
*/
|
|
506
|
-
|
|
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);
|
|
507
615
|
/**
|
|
508
|
-
*
|
|
509
|
-
*
|
|
616
|
+
* Gets the public API key used for authentication.
|
|
617
|
+
*
|
|
618
|
+
* @returns The current public API key or undefined if not set
|
|
510
619
|
*/
|
|
511
|
-
|
|
620
|
+
get publicApiKey(): string | undefined;
|
|
512
621
|
/**
|
|
513
|
-
*
|
|
514
|
-
*
|
|
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_"
|
|
515
626
|
*/
|
|
516
|
-
|
|
627
|
+
set publicApiKey(value: string | undefined);
|
|
517
628
|
/**
|
|
518
|
-
*
|
|
519
|
-
*
|
|
629
|
+
* Gets the default context used for toggle evaluations.
|
|
630
|
+
*
|
|
631
|
+
* @returns The current default ToggleContext
|
|
520
632
|
*/
|
|
521
|
-
|
|
633
|
+
get defaultContext(): ToggleContext | undefined;
|
|
522
634
|
/**
|
|
523
|
-
*
|
|
524
|
-
*
|
|
635
|
+
* Sets the default context used for toggle evaluations.
|
|
636
|
+
*
|
|
637
|
+
* @param value - The ToggleContext to use as default
|
|
525
638
|
*/
|
|
526
|
-
|
|
639
|
+
set defaultContext(value: ToggleContext);
|
|
527
640
|
/**
|
|
528
|
-
*
|
|
529
|
-
*
|
|
641
|
+
* Gets the organization ID extracted from the public API key.
|
|
642
|
+
*
|
|
643
|
+
* @returns The organization ID string or undefined if not available
|
|
530
644
|
*/
|
|
531
|
-
|
|
645
|
+
get organizationId(): string | undefined;
|
|
532
646
|
/**
|
|
533
|
-
*
|
|
534
|
-
*
|
|
535
|
-
*
|
|
536
|
-
*
|
|
537
|
-
*
|
|
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
|
|
538
679
|
*/
|
|
539
|
-
|
|
680
|
+
get applicationId(): string | undefined;
|
|
540
681
|
/**
|
|
541
|
-
*
|
|
542
|
-
*
|
|
682
|
+
* Sets the application ID used for toggle context.
|
|
683
|
+
*
|
|
684
|
+
* @param value - The application ID string or undefined to clear
|
|
543
685
|
*/
|
|
544
|
-
|
|
686
|
+
set applicationId(value: string | undefined);
|
|
545
687
|
/**
|
|
546
|
-
*
|
|
547
|
-
*
|
|
548
|
-
*
|
|
549
|
-
* @returns {Promise<Client>}
|
|
688
|
+
* Gets the environment used for toggle context.
|
|
689
|
+
*
|
|
690
|
+
* @returns The current environment (defaults to 'development')
|
|
550
691
|
*/
|
|
551
|
-
|
|
692
|
+
get environment(): string | undefined;
|
|
552
693
|
/**
|
|
553
|
-
*
|
|
554
|
-
*
|
|
555
|
-
* @param
|
|
556
|
-
* @param {T} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
|
|
557
|
-
* @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
|
|
558
|
-
* @returns {Promise<T>}
|
|
694
|
+
* Sets the environment used for toggle context.
|
|
695
|
+
*
|
|
696
|
+
* @param value - The environment string or undefined to clear
|
|
559
697
|
*/
|
|
560
|
-
|
|
698
|
+
set environment(value: string | undefined);
|
|
561
699
|
/**
|
|
562
|
-
*
|
|
563
|
-
*
|
|
564
|
-
* @
|
|
565
|
-
* @param {boolean} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
|
|
566
|
-
* @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
|
|
567
|
-
* @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
|
|
568
703
|
*/
|
|
569
|
-
|
|
704
|
+
get defaultTargetingKey(): string;
|
|
570
705
|
/**
|
|
571
|
-
*
|
|
572
|
-
*
|
|
573
|
-
* @param
|
|
574
|
-
* @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
|
|
575
|
-
* @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
|
|
576
709
|
*/
|
|
577
|
-
|
|
578
|
-
getNumber(key: string, defaultValue: number, options?: ToggleGetOptions): Promise<number>;
|
|
710
|
+
set defaultTargetingKey(value: string);
|
|
579
711
|
/**
|
|
580
|
-
*
|
|
581
|
-
*
|
|
582
|
-
* @
|
|
583
|
-
* @param {T} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
|
|
584
|
-
* @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
|
|
585
|
-
* @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
|
|
586
715
|
*/
|
|
587
|
-
|
|
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;
|
|
588
963
|
}
|
|
589
964
|
|
|
590
965
|
type HyphenOptions = {
|
|
@@ -610,21 +985,21 @@ type HyphenOptions = {
|
|
|
610
985
|
* @see ToggleOptions
|
|
611
986
|
* @default {Toggle}
|
|
612
987
|
*/
|
|
613
|
-
toggle?: Omit<ToggleOptions, "publicApiKey"
|
|
988
|
+
toggle?: Omit<ToggleOptions, "publicApiKey">;
|
|
614
989
|
/**
|
|
615
990
|
* Options for the NetInfo service.
|
|
616
991
|
* Excludes apiKey and throwErrors from NetInfoOptions.
|
|
617
992
|
* @see NetInfoOptions
|
|
618
993
|
* @default {NetInfo}
|
|
619
994
|
*/
|
|
620
|
-
netInfo?: Omit<NetInfoOptions, "apiKey"
|
|
995
|
+
netInfo?: Omit<NetInfoOptions, "apiKey">;
|
|
621
996
|
/**
|
|
622
997
|
* Options for the Link service.
|
|
623
998
|
* Excludes apiKey and throwErrors from LinkOptions.
|
|
624
999
|
* @see LinkOptions
|
|
625
1000
|
* @default {Link}
|
|
626
1001
|
*/
|
|
627
|
-
link?: Omit<LinkOptions, "apiKey"
|
|
1002
|
+
link?: Omit<LinkOptions, "apiKey">;
|
|
628
1003
|
} & HookifiedOptions;
|
|
629
1004
|
declare class Hyphen extends Hookified {
|
|
630
1005
|
private readonly _netInfo;
|
|
@@ -672,18 +1047,6 @@ declare class Hyphen extends Hookified {
|
|
|
672
1047
|
* @param {string | undefined} value - The API key to set.
|
|
673
1048
|
*/
|
|
674
1049
|
set apiKey(value: string | undefined);
|
|
675
|
-
/**
|
|
676
|
-
* Get whether to throw errors or not.
|
|
677
|
-
* If set to true, errors will be thrown instead of logged.
|
|
678
|
-
* @returns {boolean} Whether to throw errors or not.
|
|
679
|
-
*/
|
|
680
|
-
get throwErrors(): boolean;
|
|
681
|
-
/**
|
|
682
|
-
* Set whether to throw errors or not. If set to true, errors will be thrown instead of logged.
|
|
683
|
-
* This will update the underlying services as well.
|
|
684
|
-
* @param {boolean} value - Whether to throw errors or not.
|
|
685
|
-
*/
|
|
686
|
-
set throwErrors(value: boolean);
|
|
687
1050
|
}
|
|
688
1051
|
|
|
689
|
-
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 };
|