@openfeature/web-sdk 1.1.0 → 1.2.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 +3 -3
- package/dist/cjs/index.js +220 -159
- package/dist/cjs/index.js.map +2 -2
- package/dist/esm/index.js +220 -159
- package/dist/esm/index.js.map +2 -2
- package/package.json +5 -5
package/dist/esm/index.js
CHANGED
|
@@ -1,9 +1,27 @@
|
|
|
1
1
|
var __create = Object.create;
|
|
2
2
|
var __defProp = Object.defineProperty;
|
|
3
|
+
var __defProps = Object.defineProperties;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
6
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
8
|
var __getProtoOf = Object.getPrototypeOf;
|
|
6
9
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
11
|
+
var __reflectGet = Reflect.get;
|
|
12
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
13
|
+
var __spreadValues = (a, b) => {
|
|
14
|
+
for (var prop in b || (b = {}))
|
|
15
|
+
if (__hasOwnProp.call(b, prop))
|
|
16
|
+
__defNormalProp(a, prop, b[prop]);
|
|
17
|
+
if (__getOwnPropSymbols)
|
|
18
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
19
|
+
if (__propIsEnum.call(b, prop))
|
|
20
|
+
__defNormalProp(a, prop, b[prop]);
|
|
21
|
+
}
|
|
22
|
+
return a;
|
|
23
|
+
};
|
|
24
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
7
25
|
var __commonJS = (cb, mod) => function __require() {
|
|
8
26
|
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
9
27
|
};
|
|
@@ -23,6 +41,27 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
23
41
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
24
42
|
mod
|
|
25
43
|
));
|
|
44
|
+
var __superGet = (cls, obj, key) => __reflectGet(__getProtoOf(cls), key, obj);
|
|
45
|
+
var __async = (__this, __arguments, generator) => {
|
|
46
|
+
return new Promise((resolve, reject) => {
|
|
47
|
+
var fulfilled = (value) => {
|
|
48
|
+
try {
|
|
49
|
+
step(generator.next(value));
|
|
50
|
+
} catch (e) {
|
|
51
|
+
reject(e);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
var rejected = (value) => {
|
|
55
|
+
try {
|
|
56
|
+
step(generator.throw(value));
|
|
57
|
+
} catch (e) {
|
|
58
|
+
reject(e);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
62
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
63
|
+
});
|
|
64
|
+
};
|
|
26
65
|
|
|
27
66
|
// ../../node_modules/eventemitter3/index.js
|
|
28
67
|
var require_eventemitter3 = __commonJS({
|
|
@@ -212,9 +251,11 @@ import { ClientProviderStatus } from "@openfeature/core";
|
|
|
212
251
|
// src/provider/no-op-provider.ts
|
|
213
252
|
var REASON_NO_OP = "No-op";
|
|
214
253
|
var NoopFeatureProvider = class {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
254
|
+
constructor() {
|
|
255
|
+
this.metadata = {
|
|
256
|
+
name: "No-op Provider"
|
|
257
|
+
};
|
|
258
|
+
}
|
|
218
259
|
resolveBooleanEvaluation(_, defaultValue) {
|
|
219
260
|
return this.noOp(defaultValue);
|
|
220
261
|
}
|
|
@@ -253,9 +294,9 @@ var import_index = __toESM(require_eventemitter3(), 1);
|
|
|
253
294
|
|
|
254
295
|
// src/events/open-feature-event-emitter.ts
|
|
255
296
|
var OpenFeatureEventEmitter = class extends GenericEventEmitter {
|
|
256
|
-
eventEmitter = new import_index.default();
|
|
257
297
|
constructor() {
|
|
258
298
|
super();
|
|
299
|
+
this.eventEmitter = new import_index.default();
|
|
259
300
|
}
|
|
260
301
|
};
|
|
261
302
|
|
|
@@ -265,7 +306,6 @@ import { ClientProviderEvents } from "@openfeature/core";
|
|
|
265
306
|
// src/provider/in-memory-provider/variant-not-found-error.ts
|
|
266
307
|
import { ErrorCode, OpenFeatureError } from "@openfeature/core";
|
|
267
308
|
var VariantNotFoundError = class _VariantNotFoundError extends OpenFeatureError {
|
|
268
|
-
code;
|
|
269
309
|
constructor(message) {
|
|
270
310
|
super(message);
|
|
271
311
|
Object.setPrototypeOf(this, _VariantNotFoundError.prototype);
|
|
@@ -276,40 +316,42 @@ var VariantNotFoundError = class _VariantNotFoundError extends OpenFeatureError
|
|
|
276
316
|
|
|
277
317
|
// src/provider/in-memory-provider/in-memory-provider.ts
|
|
278
318
|
var InMemoryProvider = class {
|
|
279
|
-
events = new OpenFeatureEventEmitter();
|
|
280
|
-
runsOn = "client";
|
|
281
|
-
metadata = {
|
|
282
|
-
name: "in-memory"
|
|
283
|
-
};
|
|
284
|
-
_flagConfiguration;
|
|
285
|
-
_context;
|
|
286
319
|
constructor(flagConfiguration = {}) {
|
|
287
|
-
this.
|
|
320
|
+
this.events = new OpenFeatureEventEmitter();
|
|
321
|
+
this.runsOn = "client";
|
|
322
|
+
this.metadata = {
|
|
323
|
+
name: "in-memory"
|
|
324
|
+
};
|
|
325
|
+
this._flagConfiguration = __spreadValues({}, flagConfiguration);
|
|
288
326
|
}
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
327
|
+
initialize(context) {
|
|
328
|
+
return __async(this, null, function* () {
|
|
329
|
+
try {
|
|
330
|
+
for (const key in this._flagConfiguration) {
|
|
331
|
+
this.resolveFlagWithReason(key, context);
|
|
332
|
+
}
|
|
333
|
+
this._context = context;
|
|
334
|
+
} catch (err) {
|
|
335
|
+
throw new GeneralError("initialization failure", { cause: err });
|
|
293
336
|
}
|
|
294
|
-
|
|
295
|
-
} catch (err) {
|
|
296
|
-
throw new Error("initialization failure", { cause: err });
|
|
297
|
-
}
|
|
337
|
+
});
|
|
298
338
|
}
|
|
299
339
|
/**
|
|
300
340
|
* Overwrites the configured flags.
|
|
301
341
|
* @param { FlagConfiguration } flagConfiguration new flag configuration
|
|
302
342
|
*/
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
343
|
+
putConfiguration(flagConfiguration) {
|
|
344
|
+
return __async(this, null, function* () {
|
|
345
|
+
const flagsChanged = Object.entries(flagConfiguration).filter(([key, value]) => this._flagConfiguration[key] !== value).map(([key]) => key);
|
|
346
|
+
this._flagConfiguration = __spreadValues({}, flagConfiguration);
|
|
347
|
+
try {
|
|
348
|
+
yield this.initialize(this._context);
|
|
349
|
+
this.events.emit(ClientProviderEvents.ConfigurationChanged, { flagsChanged });
|
|
350
|
+
} catch (err) {
|
|
351
|
+
this.events.emit(ClientProviderEvents.Error);
|
|
352
|
+
throw err;
|
|
353
|
+
}
|
|
354
|
+
});
|
|
313
355
|
}
|
|
314
356
|
resolveBooleanEvaluation(flagKey, defaultValue, context, logger) {
|
|
315
357
|
return this.resolveAndCheckFlag(flagKey, defaultValue, context || this._context, logger);
|
|
@@ -326,7 +368,7 @@ var InMemoryProvider = class {
|
|
|
326
368
|
resolveAndCheckFlag(flagKey, defaultValue, context, logger) {
|
|
327
369
|
if (!(flagKey in this._flagConfiguration)) {
|
|
328
370
|
const message = `no flag found with key ${flagKey}`;
|
|
329
|
-
logger
|
|
371
|
+
logger == null ? void 0 : logger.debug(message);
|
|
330
372
|
throw new FlagNotFoundError(message);
|
|
331
373
|
}
|
|
332
374
|
if (this._flagConfiguration[flagKey].disabled) {
|
|
@@ -335,7 +377,7 @@ var InMemoryProvider = class {
|
|
|
335
377
|
const resolvedFlag = this.resolveFlagWithReason(flagKey, context);
|
|
336
378
|
if (resolvedFlag.value === void 0) {
|
|
337
379
|
const message = `no value associated with variant provided for ${flagKey} found`;
|
|
338
|
-
logger
|
|
380
|
+
logger == null ? void 0 : logger.error(message);
|
|
339
381
|
throw new VariantNotFoundError(message);
|
|
340
382
|
}
|
|
341
383
|
if (typeof resolvedFlag.value != typeof defaultValue) {
|
|
@@ -349,22 +391,23 @@ var InMemoryProvider = class {
|
|
|
349
391
|
return resolutionResult;
|
|
350
392
|
} catch (error) {
|
|
351
393
|
if (!(error instanceof OpenFeatureError2)) {
|
|
352
|
-
throw new GeneralError(error
|
|
394
|
+
throw new GeneralError((error == null ? void 0 : error.message) || "unknown error");
|
|
353
395
|
}
|
|
354
396
|
throw error;
|
|
355
397
|
}
|
|
356
398
|
}
|
|
357
399
|
lookupFlagValue(flagKey, ctx) {
|
|
400
|
+
var _a;
|
|
358
401
|
const flagSpec = this._flagConfiguration[flagKey];
|
|
359
|
-
const isContextEval = ctx && flagSpec
|
|
360
|
-
const variant = isContextEval ? flagSpec.contextEvaluator
|
|
361
|
-
const value = variant && flagSpec
|
|
402
|
+
const isContextEval = ctx && (flagSpec == null ? void 0 : flagSpec.contextEvaluator);
|
|
403
|
+
const variant = isContextEval ? (_a = flagSpec.contextEvaluator) == null ? void 0 : _a.call(flagSpec, ctx) : flagSpec.defaultVariant;
|
|
404
|
+
const value = variant && (flagSpec == null ? void 0 : flagSpec.variants[variant]);
|
|
362
405
|
const reason = isContextEval ? StandardResolutionReasons.TARGETING_MATCH : StandardResolutionReasons.STATIC;
|
|
363
|
-
return {
|
|
364
|
-
value
|
|
365
|
-
|
|
406
|
+
return __spreadProps(__spreadValues({
|
|
407
|
+
value
|
|
408
|
+
}, variant && { variant }), {
|
|
366
409
|
reason
|
|
367
|
-
};
|
|
410
|
+
});
|
|
368
411
|
}
|
|
369
412
|
};
|
|
370
413
|
|
|
@@ -393,14 +436,14 @@ var OpenFeatureClient = class {
|
|
|
393
436
|
this.emitterAccessor = emitterAccessor;
|
|
394
437
|
this.globalLogger = globalLogger;
|
|
395
438
|
this.options = options;
|
|
439
|
+
this._hooks = [];
|
|
396
440
|
}
|
|
397
|
-
_hooks = [];
|
|
398
|
-
_clientLogger;
|
|
399
441
|
get metadata() {
|
|
442
|
+
var _a, _b;
|
|
400
443
|
return {
|
|
401
444
|
// Use domain if name is not provided
|
|
402
|
-
name: this.options.domain
|
|
403
|
-
domain: this.options.domain
|
|
445
|
+
name: (_a = this.options.domain) != null ? _a : this.options.name,
|
|
446
|
+
domain: (_b = this.options.domain) != null ? _b : this.options.name,
|
|
404
447
|
version: this.options.version,
|
|
405
448
|
providerMetadata: this.providerAccessor().metadata
|
|
406
449
|
};
|
|
@@ -409,6 +452,7 @@ var OpenFeatureClient = class {
|
|
|
409
452
|
return this.providerStatusAccessor();
|
|
410
453
|
}
|
|
411
454
|
addHandler(eventType, handler) {
|
|
455
|
+
var _a;
|
|
412
456
|
this.emitterAccessor().addHandler(eventType, handler);
|
|
413
457
|
const shouldRunNow = statusMatchesEvent(eventType, this.providerStatus);
|
|
414
458
|
if (shouldRunNow) {
|
|
@@ -419,7 +463,7 @@ var OpenFeatureClient = class {
|
|
|
419
463
|
providerName: this._provider.metadata.name
|
|
420
464
|
});
|
|
421
465
|
} catch (err) {
|
|
422
|
-
this._logger
|
|
466
|
+
(_a = this._logger) == null ? void 0 : _a.error("Error running event handler:", err);
|
|
423
467
|
}
|
|
424
468
|
}
|
|
425
469
|
}
|
|
@@ -483,6 +527,7 @@ var OpenFeatureClient = class {
|
|
|
483
527
|
return this.evaluate(flagKey, this._provider.resolveObjectEvaluation, defaultValue, "object", options);
|
|
484
528
|
}
|
|
485
529
|
evaluate(flagKey, resolver, defaultValue, flagType, options = {}) {
|
|
530
|
+
var _a, _b;
|
|
486
531
|
const allHooks = [
|
|
487
532
|
...OpenFeature.getHooks(),
|
|
488
533
|
...this.getHooks(),
|
|
@@ -490,9 +535,7 @@ var OpenFeatureClient = class {
|
|
|
490
535
|
...this._provider.hooks || []
|
|
491
536
|
];
|
|
492
537
|
const allHooksReversed = [...allHooks].reverse();
|
|
493
|
-
const context = {
|
|
494
|
-
...OpenFeature.getContext(this?.options?.domain)
|
|
495
|
-
};
|
|
538
|
+
const context = __spreadValues({}, OpenFeature.getContext((_a = this == null ? void 0 : this.options) == null ? void 0 : _a.domain));
|
|
496
539
|
const hookContext = {
|
|
497
540
|
flagKey,
|
|
498
541
|
defaultValue,
|
|
@@ -510,19 +553,18 @@ var OpenFeatureClient = class {
|
|
|
510
553
|
throw new ProviderFatalError("provider is in an irrecoverable error state");
|
|
511
554
|
}
|
|
512
555
|
const resolution = resolver.call(this._provider, flagKey, defaultValue, context, this._logger);
|
|
513
|
-
const evaluationDetails = {
|
|
514
|
-
|
|
515
|
-
flagMetadata: Object.freeze(resolution.flagMetadata ?? {}),
|
|
556
|
+
const evaluationDetails = __spreadProps(__spreadValues({}, resolution), {
|
|
557
|
+
flagMetadata: Object.freeze((_b = resolution.flagMetadata) != null ? _b : {}),
|
|
516
558
|
flagKey
|
|
517
|
-
};
|
|
559
|
+
});
|
|
518
560
|
if (evaluationDetails.errorCode) {
|
|
519
561
|
throw instantiateErrorByErrorCode(evaluationDetails.errorCode);
|
|
520
562
|
}
|
|
521
563
|
this.afterHooks(allHooksReversed, hookContext, evaluationDetails, options);
|
|
522
564
|
return evaluationDetails;
|
|
523
565
|
} catch (err) {
|
|
524
|
-
const errorMessage = err
|
|
525
|
-
const errorCode = err
|
|
566
|
+
const errorMessage = err == null ? void 0 : err.message;
|
|
567
|
+
const errorCode = (err == null ? void 0 : err.code) || ErrorCode2.GENERAL;
|
|
526
568
|
this.errorHooks(allHooksReversed, hookContext, err, options);
|
|
527
569
|
return {
|
|
528
570
|
errorCode,
|
|
@@ -537,40 +579,44 @@ var OpenFeatureClient = class {
|
|
|
537
579
|
}
|
|
538
580
|
}
|
|
539
581
|
beforeHooks(hooks, hookContext, options) {
|
|
582
|
+
var _a;
|
|
540
583
|
Object.freeze(hookContext);
|
|
541
584
|
Object.freeze(hookContext.context);
|
|
542
585
|
for (const hook of hooks) {
|
|
543
|
-
hook
|
|
586
|
+
(_a = hook == null ? void 0 : hook.before) == null ? void 0 : _a.call(hook, hookContext, Object.freeze(options.hookHints));
|
|
544
587
|
}
|
|
545
588
|
}
|
|
546
589
|
afterHooks(hooks, hookContext, evaluationDetails, options) {
|
|
590
|
+
var _a;
|
|
547
591
|
for (const hook of hooks) {
|
|
548
|
-
hook
|
|
592
|
+
(_a = hook == null ? void 0 : hook.after) == null ? void 0 : _a.call(hook, hookContext, evaluationDetails, options.hookHints);
|
|
549
593
|
}
|
|
550
594
|
}
|
|
551
595
|
errorHooks(hooks, hookContext, err, options) {
|
|
596
|
+
var _a;
|
|
552
597
|
for (const hook of hooks) {
|
|
553
598
|
try {
|
|
554
|
-
hook
|
|
599
|
+
(_a = hook == null ? void 0 : hook.error) == null ? void 0 : _a.call(hook, hookContext, err, options.hookHints);
|
|
555
600
|
} catch (err2) {
|
|
556
601
|
this._logger.error(`Unhandled error during 'error' hook: ${err2}`);
|
|
557
602
|
if (err2 instanceof Error) {
|
|
558
603
|
this._logger.error(err2.stack);
|
|
559
604
|
}
|
|
560
|
-
this._logger.error(err2
|
|
605
|
+
this._logger.error(err2 == null ? void 0 : err2.stack);
|
|
561
606
|
}
|
|
562
607
|
}
|
|
563
608
|
}
|
|
564
609
|
finallyHooks(hooks, hookContext, options) {
|
|
610
|
+
var _a;
|
|
565
611
|
for (const hook of hooks) {
|
|
566
612
|
try {
|
|
567
|
-
hook
|
|
613
|
+
(_a = hook == null ? void 0 : hook.finally) == null ? void 0 : _a.call(hook, hookContext, options.hookHints);
|
|
568
614
|
} catch (err) {
|
|
569
615
|
this._logger.error(`Unhandled error during 'finally' hook: ${err}`);
|
|
570
616
|
if (err instanceof Error) {
|
|
571
617
|
this._logger.error(err.stack);
|
|
572
618
|
}
|
|
573
|
-
this._logger.error(err
|
|
619
|
+
this._logger.error(err == null ? void 0 : err.stack);
|
|
574
620
|
}
|
|
575
621
|
}
|
|
576
622
|
}
|
|
@@ -586,17 +632,17 @@ var OpenFeatureClient = class {
|
|
|
586
632
|
var GLOBAL_OPENFEATURE_API_KEY = Symbol.for("@openfeature/web-sdk/api");
|
|
587
633
|
var _globalThis = globalThis;
|
|
588
634
|
var OpenFeatureAPI = class _OpenFeatureAPI extends OpenFeatureCommonAPI {
|
|
589
|
-
_statusEnumType = ClientProviderStatus;
|
|
590
|
-
_apiEmitter = new OpenFeatureEventEmitter();
|
|
591
|
-
_defaultProvider = new ProviderWrapper(
|
|
592
|
-
NOOP_PROVIDER,
|
|
593
|
-
ClientProviderStatus.NOT_READY,
|
|
594
|
-
this._statusEnumType
|
|
595
|
-
);
|
|
596
|
-
_domainScopedProviders = /* @__PURE__ */ new Map();
|
|
597
|
-
_createEventEmitter = () => new OpenFeatureEventEmitter();
|
|
598
635
|
constructor() {
|
|
599
636
|
super("client");
|
|
637
|
+
this._statusEnumType = ClientProviderStatus;
|
|
638
|
+
this._apiEmitter = new OpenFeatureEventEmitter();
|
|
639
|
+
this._defaultProvider = new ProviderWrapper(
|
|
640
|
+
NOOP_PROVIDER,
|
|
641
|
+
ClientProviderStatus.NOT_READY,
|
|
642
|
+
this._statusEnumType
|
|
643
|
+
);
|
|
644
|
+
this._domainScopedProviders = /* @__PURE__ */ new Map();
|
|
645
|
+
this._createEventEmitter = () => new OpenFeatureEventEmitter();
|
|
600
646
|
}
|
|
601
647
|
/**
|
|
602
648
|
* Gets a singleton instance of the OpenFeature API.
|
|
@@ -613,23 +659,26 @@ var OpenFeatureAPI = class _OpenFeatureAPI extends OpenFeatureCommonAPI {
|
|
|
613
659
|
return instance;
|
|
614
660
|
}
|
|
615
661
|
getProviderStatus(domain) {
|
|
662
|
+
var _a, _b;
|
|
616
663
|
if (!domain) {
|
|
617
664
|
return this._defaultProvider.status;
|
|
618
665
|
}
|
|
619
|
-
return this._domainScopedProviders.get(domain)
|
|
666
|
+
return (_b = (_a = this._domainScopedProviders.get(domain)) == null ? void 0 : _a.status) != null ? _b : this._defaultProvider.status;
|
|
620
667
|
}
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
if (
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
668
|
+
setProviderAndWait(clientOrProvider, providerContextOrUndefined, contextOrUndefined) {
|
|
669
|
+
return __async(this, null, function* () {
|
|
670
|
+
const domain = stringOrUndefined(clientOrProvider);
|
|
671
|
+
const provider = domain ? objectOrUndefined(providerContextOrUndefined) : objectOrUndefined(clientOrProvider);
|
|
672
|
+
const context = domain ? objectOrUndefined(contextOrUndefined) : objectOrUndefined(providerContextOrUndefined);
|
|
673
|
+
if (context) {
|
|
674
|
+
if (domain) {
|
|
675
|
+
this._domainScopedContext.set(domain, context);
|
|
676
|
+
} else {
|
|
677
|
+
this._context = context;
|
|
678
|
+
}
|
|
630
679
|
}
|
|
631
|
-
|
|
632
|
-
|
|
680
|
+
yield this.setAwaitableProvider(domain, provider);
|
|
681
|
+
});
|
|
633
682
|
}
|
|
634
683
|
setProvider(domainOrProvider, providerContextOrUndefined, contextOrUndefined) {
|
|
635
684
|
const domain = stringOrUndefined(domainOrProvider);
|
|
@@ -648,34 +697,37 @@ var OpenFeatureAPI = class _OpenFeatureAPI extends OpenFeatureCommonAPI {
|
|
|
648
697
|
});
|
|
649
698
|
return this;
|
|
650
699
|
}
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
const
|
|
656
|
-
if (
|
|
657
|
-
const
|
|
658
|
-
|
|
659
|
-
|
|
700
|
+
setContext(domainOrContext, contextOrUndefined) {
|
|
701
|
+
return __async(this, null, function* () {
|
|
702
|
+
var _a, _b;
|
|
703
|
+
const domain = stringOrUndefined(domainOrContext);
|
|
704
|
+
const context = (_b = (_a = objectOrUndefined(domainOrContext)) != null ? _a : objectOrUndefined(contextOrUndefined)) != null ? _b : {};
|
|
705
|
+
if (domain) {
|
|
706
|
+
const wrapper = this._domainScopedProviders.get(domain);
|
|
707
|
+
if (wrapper) {
|
|
708
|
+
const oldContext = this.getContext(domain);
|
|
709
|
+
this._domainScopedContext.set(domain, context);
|
|
710
|
+
yield this.runProviderContextChangeHandler(domain, wrapper, oldContext, context);
|
|
711
|
+
} else {
|
|
712
|
+
this._domainScopedContext.set(domain, context);
|
|
713
|
+
}
|
|
660
714
|
} else {
|
|
661
|
-
this.
|
|
715
|
+
const oldContext = this._context;
|
|
716
|
+
this._context = context;
|
|
717
|
+
const unboundProviders = Array.from(this._domainScopedProviders.entries()).filter(([domain2]) => !this._domainScopedContext.has(domain2)).reduce((acc, [domain2, wrapper]) => {
|
|
718
|
+
acc.push({ domain: domain2, wrapper });
|
|
719
|
+
return acc;
|
|
720
|
+
}, []);
|
|
721
|
+
const allDomainRecords = [
|
|
722
|
+
// add in the default (no domain)
|
|
723
|
+
{ domain: void 0, wrapper: this._defaultProvider },
|
|
724
|
+
...unboundProviders
|
|
725
|
+
];
|
|
726
|
+
yield Promise.all(
|
|
727
|
+
allDomainRecords.map((dm) => this.runProviderContextChangeHandler(dm.domain, dm.wrapper, oldContext, context))
|
|
728
|
+
);
|
|
662
729
|
}
|
|
663
|
-
}
|
|
664
|
-
const oldContext = this._context;
|
|
665
|
-
this._context = context;
|
|
666
|
-
const unboundProviders = Array.from(this._domainScopedProviders.entries()).filter(([domain2]) => !this._domainScopedContext.has(domain2)).reduce((acc, [domain2, wrapper]) => {
|
|
667
|
-
acc.push({ domain: domain2, wrapper });
|
|
668
|
-
return acc;
|
|
669
|
-
}, []);
|
|
670
|
-
const allDomainRecords = [
|
|
671
|
-
// add in the default (no domain)
|
|
672
|
-
{ domain: void 0, wrapper: this._defaultProvider },
|
|
673
|
-
...unboundProviders
|
|
674
|
-
];
|
|
675
|
-
await Promise.all(
|
|
676
|
-
allDomainRecords.map((dm) => this.runProviderContextChangeHandler(dm.domain, dm.wrapper, oldContext, context))
|
|
677
|
-
);
|
|
678
|
-
}
|
|
730
|
+
});
|
|
679
731
|
}
|
|
680
732
|
getContext(domainOrUndefined) {
|
|
681
733
|
const domain = stringOrUndefined(domainOrUndefined);
|
|
@@ -689,29 +741,33 @@ var OpenFeatureAPI = class _OpenFeatureAPI extends OpenFeatureCommonAPI {
|
|
|
689
741
|
}
|
|
690
742
|
return this._context;
|
|
691
743
|
}
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
744
|
+
clearContext(domainOrUndefined) {
|
|
745
|
+
return __async(this, null, function* () {
|
|
746
|
+
const domain = stringOrUndefined(domainOrUndefined);
|
|
747
|
+
if (domain) {
|
|
748
|
+
const wrapper = this._domainScopedProviders.get(domain);
|
|
749
|
+
if (wrapper) {
|
|
750
|
+
const oldContext = this.getContext(domain);
|
|
751
|
+
this._domainScopedContext.delete(domain);
|
|
752
|
+
const newContext = this.getContext();
|
|
753
|
+
yield this.runProviderContextChangeHandler(domain, wrapper, oldContext, newContext);
|
|
754
|
+
} else {
|
|
755
|
+
this._domainScopedContext.delete(domain);
|
|
756
|
+
}
|
|
701
757
|
} else {
|
|
702
|
-
this.
|
|
758
|
+
return this.setContext({});
|
|
703
759
|
}
|
|
704
|
-
}
|
|
705
|
-
return this.setContext({});
|
|
706
|
-
}
|
|
760
|
+
});
|
|
707
761
|
}
|
|
708
762
|
/**
|
|
709
763
|
* Resets the global evaluation context and removes the evaluation context for
|
|
710
764
|
* all domains.
|
|
711
765
|
*/
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
766
|
+
clearContexts() {
|
|
767
|
+
return __async(this, null, function* () {
|
|
768
|
+
yield this.clearContext();
|
|
769
|
+
yield Promise.allSettled(Array.from(this._domainScopedProviders.keys()).map((domain) => this.clearContext(domain)));
|
|
770
|
+
});
|
|
715
771
|
}
|
|
716
772
|
/**
|
|
717
773
|
* A factory function for creating new named OpenFeature clients. Clients can contain
|
|
@@ -739,46 +795,51 @@ var OpenFeatureAPI = class _OpenFeatureAPI extends OpenFeatureCommonAPI {
|
|
|
739
795
|
* Clears all registered providers and resets the default provider.
|
|
740
796
|
* @returns {Promise<void>}
|
|
741
797
|
*/
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
798
|
+
clearProviders() {
|
|
799
|
+
return __async(this, null, function* () {
|
|
800
|
+
yield __superGet(_OpenFeatureAPI.prototype, this, "clearProvidersAndSetDefault").call(this, NOOP_PROVIDER);
|
|
801
|
+
this._domainScopedContext.clear();
|
|
802
|
+
});
|
|
745
803
|
}
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
if (typeof
|
|
752
|
-
wrapper.
|
|
753
|
-
|
|
804
|
+
runProviderContextChangeHandler(domain, wrapper, oldContext, newContext) {
|
|
805
|
+
return __async(this, null, function* () {
|
|
806
|
+
var _a, _b, _c, _d, _e, _f;
|
|
807
|
+
const providerName = ((_b = (_a = wrapper.provider) == null ? void 0 : _a.metadata) == null ? void 0 : _b.name) || "unnamed-provider";
|
|
808
|
+
try {
|
|
809
|
+
if (typeof wrapper.provider.onContextChange === "function") {
|
|
810
|
+
const maybePromise = wrapper.provider.onContextChange(oldContext, newContext);
|
|
811
|
+
if (typeof (maybePromise == null ? void 0 : maybePromise.then) === "function") {
|
|
812
|
+
wrapper.incrementPendingContextChanges();
|
|
813
|
+
wrapper.status = this._statusEnumType.RECONCILING;
|
|
814
|
+
this.getAssociatedEventEmitters(domain).forEach((emitter) => {
|
|
815
|
+
emitter == null ? void 0 : emitter.emit(ClientProviderEvents.Reconciling, { domain, providerName });
|
|
816
|
+
});
|
|
817
|
+
(_c = this._apiEmitter) == null ? void 0 : _c.emit(ClientProviderEvents.Reconciling, { domain, providerName });
|
|
818
|
+
yield maybePromise;
|
|
819
|
+
wrapper.decrementPendingContextChanges();
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
wrapper.status = this._statusEnumType.READY;
|
|
823
|
+
if (wrapper.allContextChangesSettled) {
|
|
754
824
|
this.getAssociatedEventEmitters(domain).forEach((emitter) => {
|
|
755
|
-
emitter
|
|
825
|
+
emitter == null ? void 0 : emitter.emit(ClientProviderEvents.ContextChanged, { clientName: domain, domain, providerName });
|
|
756
826
|
});
|
|
757
|
-
this._apiEmitter
|
|
758
|
-
|
|
759
|
-
|
|
827
|
+
(_d = this._apiEmitter) == null ? void 0 : _d.emit(ClientProviderEvents.ContextChanged, { clientName: domain, domain, providerName });
|
|
828
|
+
}
|
|
829
|
+
} catch (err) {
|
|
830
|
+
wrapper.decrementPendingContextChanges();
|
|
831
|
+
wrapper.status = this._statusEnumType.ERROR;
|
|
832
|
+
if (wrapper.allContextChangesSettled) {
|
|
833
|
+
const error = err;
|
|
834
|
+
const message = `Error running ${providerName}'s context change handler: ${error == null ? void 0 : error.message}`;
|
|
835
|
+
(_e = this._logger) == null ? void 0 : _e.error(`${message}`, err);
|
|
836
|
+
this.getAssociatedEventEmitters(domain).forEach((emitter) => {
|
|
837
|
+
emitter == null ? void 0 : emitter.emit(ClientProviderEvents.Error, { clientName: domain, domain, providerName, message });
|
|
838
|
+
});
|
|
839
|
+
(_f = this._apiEmitter) == null ? void 0 : _f.emit(ClientProviderEvents.Error, { clientName: domain, domain, providerName, message });
|
|
760
840
|
}
|
|
761
841
|
}
|
|
762
|
-
|
|
763
|
-
if (wrapper.allContextChangesSettled) {
|
|
764
|
-
this.getAssociatedEventEmitters(domain).forEach((emitter) => {
|
|
765
|
-
emitter?.emit(ClientProviderEvents.ContextChanged, { clientName: domain, domain, providerName });
|
|
766
|
-
});
|
|
767
|
-
this._apiEmitter?.emit(ClientProviderEvents.ContextChanged, { clientName: domain, domain, providerName });
|
|
768
|
-
}
|
|
769
|
-
} catch (err) {
|
|
770
|
-
wrapper.decrementPendingContextChanges();
|
|
771
|
-
wrapper.status = this._statusEnumType.ERROR;
|
|
772
|
-
if (wrapper.allContextChangesSettled) {
|
|
773
|
-
const error = err;
|
|
774
|
-
const message = `Error running ${providerName}'s context change handler: ${error?.message}`;
|
|
775
|
-
this._logger?.error(`${message}`, err);
|
|
776
|
-
this.getAssociatedEventEmitters(domain).forEach((emitter) => {
|
|
777
|
-
emitter?.emit(ClientProviderEvents.Error, { clientName: domain, domain, providerName, message });
|
|
778
|
-
});
|
|
779
|
-
this._apiEmitter?.emit(ClientProviderEvents.Error, { clientName: domain, domain, providerName, message });
|
|
780
|
-
}
|
|
781
|
-
}
|
|
842
|
+
});
|
|
782
843
|
}
|
|
783
844
|
};
|
|
784
845
|
var OpenFeature = OpenFeatureAPI.getInstance();
|