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