@openfeature/web-sdk 0.4.8 → 0.4.10
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 +2 -2
- package/dist/cjs/index.js +82 -61
- package/dist/cjs/index.js.map +4 -4
- package/dist/esm/index.js +53 -33
- package/dist/esm/index.js.map +4 -4
- package/dist/types.d.ts +105 -54
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
<img alt="Specification" src="https://img.shields.io/static/v1?label=specification&message=v0.7.0&color=yellow&style=for-the-badge" />
|
|
17
17
|
</a>
|
|
18
18
|
<!-- x-release-please-start-version -->
|
|
19
|
-
<a href="https://github.com/open-feature/js-sdk/releases/tag/web-sdk-v0.4.
|
|
20
|
-
<img alt="Release" src="https://img.shields.io/static/v1?label=release&message=v0.4.
|
|
19
|
+
<a href="https://github.com/open-feature/js-sdk/releases/tag/web-sdk-v0.4.10">
|
|
20
|
+
<img alt="Release" src="https://img.shields.io/static/v1?label=release&message=v0.4.10&color=blue&style=for-the-badge" />
|
|
21
21
|
</a>
|
|
22
22
|
<!-- x-release-please-end -->
|
|
23
23
|
<br/>
|
package/dist/cjs/index.js
CHANGED
|
@@ -450,18 +450,36 @@ __export(src_exports, {
|
|
|
450
450
|
OpenFeature: () => OpenFeature,
|
|
451
451
|
OpenFeatureAPI: () => OpenFeatureAPI,
|
|
452
452
|
OpenFeatureClient: () => OpenFeatureClient,
|
|
453
|
-
OpenFeatureEventEmitter: () => OpenFeatureEventEmitter
|
|
453
|
+
OpenFeatureEventEmitter: () => OpenFeatureEventEmitter,
|
|
454
|
+
ProviderEvents: () => import_core2.ClientProviderEvents
|
|
454
455
|
});
|
|
455
456
|
module.exports = __toCommonJS(src_exports);
|
|
456
457
|
|
|
457
458
|
// src/client/open-feature-client.ts
|
|
458
|
-
var
|
|
459
|
+
var import_core7 = require("@openfeature/core");
|
|
459
460
|
|
|
460
461
|
// src/open-feature.ts
|
|
461
|
-
var
|
|
462
|
+
var import_core6 = require("@openfeature/core");
|
|
462
463
|
|
|
463
|
-
// src/
|
|
464
|
+
// src/events/open-feature-event-emitter.ts
|
|
464
465
|
var import_core = require("@openfeature/core");
|
|
466
|
+
var import_events = __toESM(require_events());
|
|
467
|
+
var OpenFeatureEventEmitter = class extends import_core.GenericEventEmitter {
|
|
468
|
+
constructor() {
|
|
469
|
+
super();
|
|
470
|
+
this.eventEmitter = new import_events.default({ captureRejections: true });
|
|
471
|
+
this.eventEmitter.on("error", (err) => {
|
|
472
|
+
var _a;
|
|
473
|
+
(_a = this._logger) == null ? void 0 : _a.error("Error running event handler:", err);
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
};
|
|
477
|
+
|
|
478
|
+
// src/events/events.ts
|
|
479
|
+
var import_core2 = require("@openfeature/core");
|
|
480
|
+
|
|
481
|
+
// src/provider/no-op-provider.ts
|
|
482
|
+
var import_core3 = require("@openfeature/core");
|
|
465
483
|
var REASON_NO_OP = "No-op";
|
|
466
484
|
var NoopFeatureProvider = class {
|
|
467
485
|
constructor() {
|
|
@@ -470,7 +488,7 @@ var NoopFeatureProvider = class {
|
|
|
470
488
|
};
|
|
471
489
|
}
|
|
472
490
|
get status() {
|
|
473
|
-
return
|
|
491
|
+
return import_core3.ProviderStatus.NOT_READY;
|
|
474
492
|
}
|
|
475
493
|
resolveBooleanEvaluation(_, defaultValue) {
|
|
476
494
|
return this.noOp(defaultValue);
|
|
@@ -494,30 +512,16 @@ var NoopFeatureProvider = class {
|
|
|
494
512
|
var NOOP_PROVIDER = new NoopFeatureProvider();
|
|
495
513
|
|
|
496
514
|
// src/provider/in-memory-provider/in-memory-provider.ts
|
|
497
|
-
var
|
|
498
|
-
|
|
499
|
-
// src/events/open-feature-event-emitter.ts
|
|
500
|
-
var import_core2 = require("@openfeature/core");
|
|
501
|
-
var import_events = __toESM(require_events());
|
|
502
|
-
var OpenFeatureEventEmitter = class extends import_core2.GenericEventEmitter {
|
|
503
|
-
constructor() {
|
|
504
|
-
super();
|
|
505
|
-
this.eventEmitter = new import_events.default({ captureRejections: true });
|
|
506
|
-
this.eventEmitter.on("error", (err) => {
|
|
507
|
-
var _a;
|
|
508
|
-
(_a = this._logger) == null ? void 0 : _a.error("Error running event handler:", err);
|
|
509
|
-
});
|
|
510
|
-
}
|
|
511
|
-
};
|
|
515
|
+
var import_core5 = require("@openfeature/core");
|
|
512
516
|
|
|
513
517
|
// src/provider/in-memory-provider/variant-not-found-error.ts
|
|
514
|
-
var
|
|
515
|
-
var VariantNotFoundError = class extends
|
|
518
|
+
var import_core4 = require("@openfeature/core");
|
|
519
|
+
var VariantNotFoundError = class extends import_core4.OpenFeatureError {
|
|
516
520
|
constructor(message) {
|
|
517
521
|
super(message);
|
|
518
522
|
Object.setPrototypeOf(this, VariantNotFoundError.prototype);
|
|
519
523
|
this.name = "VariantNotFoundError";
|
|
520
|
-
this.code =
|
|
524
|
+
this.code = import_core4.ErrorCode.GENERAL;
|
|
521
525
|
}
|
|
522
526
|
};
|
|
523
527
|
|
|
@@ -526,7 +530,7 @@ var InMemoryProvider = class {
|
|
|
526
530
|
constructor(flagConfiguration = {}) {
|
|
527
531
|
this.events = new OpenFeatureEventEmitter();
|
|
528
532
|
this.runsOn = "client";
|
|
529
|
-
this.status =
|
|
533
|
+
this.status = import_core5.ProviderStatus.NOT_READY;
|
|
530
534
|
this.metadata = {
|
|
531
535
|
name: "in-memory"
|
|
532
536
|
};
|
|
@@ -539,9 +543,9 @@ var InMemoryProvider = class {
|
|
|
539
543
|
this.resolveFlagWithReason(key, context);
|
|
540
544
|
}
|
|
541
545
|
this._context = context;
|
|
542
|
-
this.status =
|
|
546
|
+
this.status = import_core5.ProviderStatus.READY;
|
|
543
547
|
} catch (error) {
|
|
544
|
-
this.status =
|
|
548
|
+
this.status = import_core5.ProviderStatus.ERROR;
|
|
545
549
|
throw error;
|
|
546
550
|
}
|
|
547
551
|
});
|
|
@@ -553,15 +557,15 @@ var InMemoryProvider = class {
|
|
|
553
557
|
putConfiguration(flagConfiguration) {
|
|
554
558
|
return __async(this, null, function* () {
|
|
555
559
|
const flagsChanged = Object.entries(flagConfiguration).filter(([key, value]) => this._flagConfiguration[key] !== value).map(([key]) => key);
|
|
556
|
-
this.status =
|
|
557
|
-
this.events.emit(
|
|
560
|
+
this.status = import_core5.ProviderStatus.STALE;
|
|
561
|
+
this.events.emit(import_core2.ClientProviderEvents.Stale);
|
|
558
562
|
this._flagConfiguration = __spreadValues({}, flagConfiguration);
|
|
559
|
-
this.events.emit(
|
|
563
|
+
this.events.emit(import_core2.ClientProviderEvents.ConfigurationChanged, { flagsChanged });
|
|
560
564
|
try {
|
|
561
565
|
yield this.initialize(this._context);
|
|
562
|
-
this.events.emit(
|
|
566
|
+
this.events.emit(import_core2.ClientProviderEvents.Ready);
|
|
563
567
|
} catch (err) {
|
|
564
|
-
this.events.emit(
|
|
568
|
+
this.events.emit(import_core2.ClientProviderEvents.Error);
|
|
565
569
|
throw err;
|
|
566
570
|
}
|
|
567
571
|
});
|
|
@@ -582,10 +586,10 @@ var InMemoryProvider = class {
|
|
|
582
586
|
if (!(flagKey in this._flagConfiguration)) {
|
|
583
587
|
const message = `no flag found with key ${flagKey}`;
|
|
584
588
|
logger == null ? void 0 : logger.debug(message);
|
|
585
|
-
throw new
|
|
589
|
+
throw new import_core5.FlagNotFoundError(message);
|
|
586
590
|
}
|
|
587
591
|
if (this._flagConfiguration[flagKey].disabled) {
|
|
588
|
-
return { value: defaultValue, reason:
|
|
592
|
+
return { value: defaultValue, reason: import_core5.StandardResolutionReasons.DISABLED };
|
|
589
593
|
}
|
|
590
594
|
const resolvedFlag = this.resolveFlagWithReason(flagKey, context);
|
|
591
595
|
if (resolvedFlag.value === void 0) {
|
|
@@ -594,7 +598,7 @@ var InMemoryProvider = class {
|
|
|
594
598
|
throw new VariantNotFoundError(message);
|
|
595
599
|
}
|
|
596
600
|
if (typeof resolvedFlag.value != typeof defaultValue) {
|
|
597
|
-
throw new
|
|
601
|
+
throw new import_core5.TypeMismatchError();
|
|
598
602
|
}
|
|
599
603
|
return resolvedFlag;
|
|
600
604
|
}
|
|
@@ -603,8 +607,8 @@ var InMemoryProvider = class {
|
|
|
603
607
|
const resolutionResult = this.lookupFlagValue(flagKey, ctx);
|
|
604
608
|
return resolutionResult;
|
|
605
609
|
} catch (error) {
|
|
606
|
-
if (!(error instanceof
|
|
607
|
-
throw new
|
|
610
|
+
if (!(error instanceof import_core5.OpenFeatureError)) {
|
|
611
|
+
throw new import_core5.GeneralError((error == null ? void 0 : error.message) || "unknown error");
|
|
608
612
|
}
|
|
609
613
|
throw error;
|
|
610
614
|
}
|
|
@@ -615,8 +619,8 @@ var InMemoryProvider = class {
|
|
|
615
619
|
const isContextEval = ctx && (flagSpec == null ? void 0 : flagSpec.contextEvaluator);
|
|
616
620
|
const variant = isContextEval ? (_a = flagSpec.contextEvaluator) == null ? void 0 : _a.call(flagSpec, ctx) : flagSpec.defaultVariant;
|
|
617
621
|
const value = variant && (flagSpec == null ? void 0 : flagSpec.variants[variant]);
|
|
618
|
-
const evalReason = isContextEval ?
|
|
619
|
-
const reason = this.status ===
|
|
622
|
+
const evalReason = isContextEval ? import_core5.StandardResolutionReasons.TARGETING_MATCH : import_core5.StandardResolutionReasons.STATIC;
|
|
623
|
+
const reason = this.status === import_core5.ProviderStatus.STALE ? import_core5.StandardResolutionReasons.CACHED : evalReason;
|
|
620
624
|
return __spreadProps(__spreadValues({
|
|
621
625
|
value
|
|
622
626
|
}, variant && { variant }), {
|
|
@@ -628,7 +632,7 @@ var InMemoryProvider = class {
|
|
|
628
632
|
// src/open-feature.ts
|
|
629
633
|
var GLOBAL_OPENFEATURE_API_KEY = Symbol.for("@openfeature/web-sdk/api");
|
|
630
634
|
var _globalThis = globalThis;
|
|
631
|
-
var OpenFeatureAPI = class extends
|
|
635
|
+
var OpenFeatureAPI = class extends import_core6.OpenFeatureCommonAPI {
|
|
632
636
|
constructor() {
|
|
633
637
|
super("client");
|
|
634
638
|
this._events = new OpenFeatureEventEmitter();
|
|
@@ -653,33 +657,39 @@ var OpenFeatureAPI = class extends import_core5.OpenFeatureCommonAPI {
|
|
|
653
657
|
setContext(nameOrContext, contextOrUndefined) {
|
|
654
658
|
return __async(this, null, function* () {
|
|
655
659
|
var _a, _b;
|
|
656
|
-
const clientName = (0,
|
|
657
|
-
const context = (_b = (_a = (0,
|
|
660
|
+
const clientName = (0, import_core6.stringOrUndefined)(nameOrContext);
|
|
661
|
+
const context = (_b = (_a = (0, import_core6.objectOrUndefined)(nameOrContext)) != null ? _a : (0, import_core6.objectOrUndefined)(contextOrUndefined)) != null ? _b : {};
|
|
658
662
|
if (clientName) {
|
|
659
663
|
const provider = this._clientProviders.get(clientName);
|
|
660
664
|
if (provider) {
|
|
661
665
|
const oldContext = this.getContext(clientName);
|
|
662
666
|
this._namedProviderContext.set(clientName, context);
|
|
663
|
-
yield this.runProviderContextChangeHandler(provider, oldContext, context);
|
|
667
|
+
yield this.runProviderContextChangeHandler(clientName, provider, oldContext, context);
|
|
664
668
|
} else {
|
|
665
669
|
this._namedProviderContext.set(clientName, context);
|
|
666
670
|
}
|
|
667
671
|
} else {
|
|
668
672
|
const oldContext = this._context;
|
|
669
673
|
this._context = context;
|
|
670
|
-
const
|
|
671
|
-
acc.push(provider);
|
|
674
|
+
const defaultContextNameProviders = Array.from(this._clientProviders.entries()).filter(([name]) => !this._namedProviderContext.has(name)).reduce((acc, [name, provider]) => {
|
|
675
|
+
acc.push({ name, provider });
|
|
672
676
|
return acc;
|
|
673
677
|
}, []);
|
|
674
|
-
const allProviders = [
|
|
678
|
+
const allProviders = [
|
|
679
|
+
// add in the default (no name)
|
|
680
|
+
{ name: void 0, provider: this._defaultProvider },
|
|
681
|
+
...defaultContextNameProviders
|
|
682
|
+
];
|
|
675
683
|
yield Promise.all(
|
|
676
|
-
allProviders.map(
|
|
684
|
+
allProviders.map(
|
|
685
|
+
(tuple) => this.runProviderContextChangeHandler(tuple.name, tuple.provider, oldContext, context)
|
|
686
|
+
)
|
|
677
687
|
);
|
|
678
688
|
}
|
|
679
689
|
});
|
|
680
690
|
}
|
|
681
691
|
getContext(nameOrUndefined) {
|
|
682
|
-
const clientName = (0,
|
|
692
|
+
const clientName = (0, import_core6.stringOrUndefined)(nameOrUndefined);
|
|
683
693
|
if (clientName) {
|
|
684
694
|
const context = this._namedProviderContext.get(clientName);
|
|
685
695
|
if (context) {
|
|
@@ -692,14 +702,14 @@ var OpenFeatureAPI = class extends import_core5.OpenFeatureCommonAPI {
|
|
|
692
702
|
}
|
|
693
703
|
clearContext(nameOrUndefined) {
|
|
694
704
|
return __async(this, null, function* () {
|
|
695
|
-
const clientName = (0,
|
|
705
|
+
const clientName = (0, import_core6.stringOrUndefined)(nameOrUndefined);
|
|
696
706
|
if (clientName) {
|
|
697
707
|
const provider = this._clientProviders.get(clientName);
|
|
698
708
|
if (provider) {
|
|
699
709
|
const oldContext = this.getContext(clientName);
|
|
700
710
|
this._namedProviderContext.delete(clientName);
|
|
701
711
|
const newContext = this.getContext();
|
|
702
|
-
yield this.runProviderContextChangeHandler(provider, oldContext, newContext);
|
|
712
|
+
yield this.runProviderContextChangeHandler(clientName, provider, oldContext, newContext);
|
|
703
713
|
} else {
|
|
704
714
|
this._namedProviderContext.delete(clientName);
|
|
705
715
|
}
|
|
@@ -749,13 +759,24 @@ var OpenFeatureAPI = class extends import_core5.OpenFeatureCommonAPI {
|
|
|
749
759
|
this._namedProviderContext.clear();
|
|
750
760
|
});
|
|
751
761
|
}
|
|
752
|
-
runProviderContextChangeHandler(provider, oldContext, newContext) {
|
|
762
|
+
runProviderContextChangeHandler(clientName, provider, oldContext, newContext) {
|
|
753
763
|
return __async(this, null, function* () {
|
|
754
|
-
var _a, _b;
|
|
764
|
+
var _a, _b, _c, _d, _e;
|
|
765
|
+
const providerName = provider.metadata.name;
|
|
755
766
|
try {
|
|
756
|
-
|
|
767
|
+
yield (_a = provider.onContextChange) == null ? void 0 : _a.call(provider, oldContext, newContext);
|
|
768
|
+
this.getAssociatedEventEmitters(clientName).forEach((emitter) => {
|
|
769
|
+
emitter == null ? void 0 : emitter.emit(import_core2.ClientProviderEvents.ContextChanged, { clientName, providerName });
|
|
770
|
+
});
|
|
771
|
+
(_b = this._events) == null ? void 0 : _b.emit(import_core2.ClientProviderEvents.ContextChanged, { clientName, providerName });
|
|
757
772
|
} catch (err) {
|
|
758
|
-
|
|
773
|
+
const error = err;
|
|
774
|
+
const message = `Error running ${(_c = provider == null ? void 0 : provider.metadata) == null ? void 0 : _c.name}'s context change handler: ${error == null ? void 0 : error.message}`;
|
|
775
|
+
(_d = this._logger) == null ? void 0 : _d.error(`${message}`, err);
|
|
776
|
+
this.getAssociatedEventEmitters(clientName).forEach((emitter) => {
|
|
777
|
+
emitter == null ? void 0 : emitter.emit(import_core2.ClientProviderEvents.Error, { clientName, providerName, message });
|
|
778
|
+
});
|
|
779
|
+
(_e = this._events) == null ? void 0 : _e.emit(import_core2.ClientProviderEvents.Error, { clientName, providerName, message });
|
|
759
780
|
}
|
|
760
781
|
});
|
|
761
782
|
}
|
|
@@ -780,12 +801,12 @@ var OpenFeatureClient = class {
|
|
|
780
801
|
}
|
|
781
802
|
get providerStatus() {
|
|
782
803
|
var _a;
|
|
783
|
-
return ((_a = this.providerAccessor()) == null ? void 0 : _a.status) ||
|
|
804
|
+
return ((_a = this.providerAccessor()) == null ? void 0 : _a.status) || import_core7.ProviderStatus.READY;
|
|
784
805
|
}
|
|
785
806
|
addHandler(eventType, handler) {
|
|
786
807
|
var _a;
|
|
787
808
|
this.emitterAccessor().addHandler(eventType, handler);
|
|
788
|
-
const shouldRunNow = (0,
|
|
809
|
+
const shouldRunNow = (0, import_core7.statusMatchesEvent)(eventType, this._provider.status);
|
|
789
810
|
if (shouldRunNow) {
|
|
790
811
|
try {
|
|
791
812
|
handler({ clientName: this.metadata.name, providerName: this._provider.metadata.name });
|
|
@@ -801,7 +822,7 @@ var OpenFeatureClient = class {
|
|
|
801
822
|
return this.emitterAccessor().getHandlers(eventType);
|
|
802
823
|
}
|
|
803
824
|
setLogger(logger) {
|
|
804
|
-
this._clientLogger = new
|
|
825
|
+
this._clientLogger = new import_core7.SafeLogger(logger);
|
|
805
826
|
return this;
|
|
806
827
|
}
|
|
807
828
|
addHooks(...hooks) {
|
|
@@ -854,7 +875,7 @@ var OpenFeatureClient = class {
|
|
|
854
875
|
return this.evaluate(flagKey, this._provider.resolveObjectEvaluation, defaultValue, "object", options);
|
|
855
876
|
}
|
|
856
877
|
evaluate(flagKey, resolver, defaultValue, flagType, options = {}) {
|
|
857
|
-
var _a;
|
|
878
|
+
var _a, _b;
|
|
858
879
|
const allHooks = [
|
|
859
880
|
...OpenFeature.getHooks(),
|
|
860
881
|
...this.getHooks(),
|
|
@@ -862,7 +883,7 @@ var OpenFeatureClient = class {
|
|
|
862
883
|
...this._provider.hooks || []
|
|
863
884
|
];
|
|
864
885
|
const allHooksReversed = [...allHooks].reverse();
|
|
865
|
-
const context = __spreadValues({}, OpenFeature.getContext());
|
|
886
|
+
const context = __spreadValues({}, OpenFeature.getContext((_a = this == null ? void 0 : this.options) == null ? void 0 : _a.name));
|
|
866
887
|
const hookContext = {
|
|
867
888
|
flagKey,
|
|
868
889
|
defaultValue,
|
|
@@ -876,20 +897,20 @@ var OpenFeatureClient = class {
|
|
|
876
897
|
this.beforeHooks(allHooks, hookContext, options);
|
|
877
898
|
const resolution = resolver.call(this._provider, flagKey, defaultValue, context, this._logger);
|
|
878
899
|
const evaluationDetails = __spreadProps(__spreadValues({}, resolution), {
|
|
879
|
-
flagMetadata: Object.freeze((
|
|
900
|
+
flagMetadata: Object.freeze((_b = resolution.flagMetadata) != null ? _b : {}),
|
|
880
901
|
flagKey
|
|
881
902
|
});
|
|
882
903
|
this.afterHooks(allHooksReversed, hookContext, evaluationDetails, options);
|
|
883
904
|
return evaluationDetails;
|
|
884
905
|
} catch (err) {
|
|
885
906
|
const errorMessage = err == null ? void 0 : err.message;
|
|
886
|
-
const errorCode = (err == null ? void 0 : err.code) ||
|
|
907
|
+
const errorCode = (err == null ? void 0 : err.code) || import_core7.ErrorCode.GENERAL;
|
|
887
908
|
this.errorHooks(allHooksReversed, hookContext, err, options);
|
|
888
909
|
return {
|
|
889
910
|
errorCode,
|
|
890
911
|
errorMessage,
|
|
891
912
|
value: defaultValue,
|
|
892
|
-
reason:
|
|
913
|
+
reason: import_core7.StandardResolutionReasons.ERROR,
|
|
893
914
|
flagMetadata: Object.freeze({}),
|
|
894
915
|
flagKey
|
|
895
916
|
};
|