@openfeature/web-sdk
Advanced tools
Comparing version 0.4.2 to 0.4.3
@@ -40,2 +40,3 @@ "use strict"; | ||
}; | ||
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default")); | ||
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( | ||
@@ -98,11 +99,11 @@ // If the importer is in node compatibility mode or this is not an ESM | ||
}; | ||
function EventEmitter2() { | ||
EventEmitter2.init.call(this); | ||
function EventEmitter() { | ||
EventEmitter.init.call(this); | ||
} | ||
module2.exports = EventEmitter2; | ||
module2.exports = EventEmitter; | ||
module2.exports.once = once; | ||
EventEmitter2.EventEmitter = EventEmitter2; | ||
EventEmitter2.prototype._events = void 0; | ||
EventEmitter2.prototype._eventsCount = 0; | ||
EventEmitter2.prototype._maxListeners = void 0; | ||
EventEmitter.EventEmitter = EventEmitter; | ||
EventEmitter.prototype._events = void 0; | ||
EventEmitter.prototype._eventsCount = 0; | ||
EventEmitter.prototype._maxListeners = void 0; | ||
var defaultMaxListeners = 10; | ||
@@ -114,3 +115,3 @@ function checkListener(listener) { | ||
} | ||
Object.defineProperty(EventEmitter2, "defaultMaxListeners", { | ||
Object.defineProperty(EventEmitter, "defaultMaxListeners", { | ||
enumerable: true, | ||
@@ -127,3 +128,3 @@ get: function() { | ||
}); | ||
EventEmitter2.init = function() { | ||
EventEmitter.init = function() { | ||
if (this._events === void 0 || this._events === Object.getPrototypeOf(this)._events) { | ||
@@ -135,3 +136,3 @@ this._events = /* @__PURE__ */ Object.create(null); | ||
}; | ||
EventEmitter2.prototype.setMaxListeners = function setMaxListeners(n) { | ||
EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) { | ||
if (typeof n !== "number" || n < 0 || NumberIsNaN(n)) { | ||
@@ -145,9 +146,9 @@ throw new RangeError('The value of "n" is out of range. It must be a non-negative number. Received ' + n + "."); | ||
if (that._maxListeners === void 0) | ||
return EventEmitter2.defaultMaxListeners; | ||
return EventEmitter.defaultMaxListeners; | ||
return that._maxListeners; | ||
} | ||
EventEmitter2.prototype.getMaxListeners = function getMaxListeners() { | ||
EventEmitter.prototype.getMaxListeners = function getMaxListeners() { | ||
return _getMaxListeners(this); | ||
}; | ||
EventEmitter2.prototype.emit = function emit(type) { | ||
EventEmitter.prototype.emit = function emit(type) { | ||
var args = []; | ||
@@ -230,7 +231,7 @@ for (var i = 1; i < arguments.length; i++) | ||
} | ||
EventEmitter2.prototype.addListener = function addListener(type, listener) { | ||
EventEmitter.prototype.addListener = function addListener(type, listener) { | ||
return _addListener(this, type, listener, false); | ||
}; | ||
EventEmitter2.prototype.on = EventEmitter2.prototype.addListener; | ||
EventEmitter2.prototype.prependListener = function prependListener(type, listener) { | ||
EventEmitter.prototype.on = EventEmitter.prototype.addListener; | ||
EventEmitter.prototype.prependListener = function prependListener(type, listener) { | ||
return _addListener(this, type, listener, true); | ||
@@ -254,3 +255,3 @@ }; | ||
} | ||
EventEmitter2.prototype.once = function once2(type, listener) { | ||
EventEmitter.prototype.once = function once2(type, listener) { | ||
checkListener(listener); | ||
@@ -260,3 +261,3 @@ this.on(type, _onceWrap(this, type, listener)); | ||
}; | ||
EventEmitter2.prototype.prependOnceListener = function prependOnceListener(type, listener) { | ||
EventEmitter.prototype.prependOnceListener = function prependOnceListener(type, listener) { | ||
checkListener(listener); | ||
@@ -266,3 +267,3 @@ this.prependListener(type, _onceWrap(this, type, listener)); | ||
}; | ||
EventEmitter2.prototype.removeListener = function removeListener(type, listener) { | ||
EventEmitter.prototype.removeListener = function removeListener(type, listener) { | ||
var list, events, position, i, originalListener; | ||
@@ -307,4 +308,4 @@ checkListener(listener); | ||
}; | ||
EventEmitter2.prototype.off = EventEmitter2.prototype.removeListener; | ||
EventEmitter2.prototype.removeAllListeners = function removeAllListeners(type) { | ||
EventEmitter.prototype.off = EventEmitter.prototype.removeListener; | ||
EventEmitter.prototype.removeAllListeners = function removeAllListeners(type) { | ||
var listeners, events, i; | ||
@@ -361,9 +362,9 @@ events = this._events; | ||
} | ||
EventEmitter2.prototype.listeners = function listeners(type) { | ||
EventEmitter.prototype.listeners = function listeners(type) { | ||
return _listeners(this, type, true); | ||
}; | ||
EventEmitter2.prototype.rawListeners = function rawListeners(type) { | ||
EventEmitter.prototype.rawListeners = function rawListeners(type) { | ||
return _listeners(this, type, false); | ||
}; | ||
EventEmitter2.listenerCount = function(emitter, type) { | ||
EventEmitter.listenerCount = function(emitter, type) { | ||
if (typeof emitter.listenerCount === "function") { | ||
@@ -375,3 +376,3 @@ return emitter.listenerCount(type); | ||
}; | ||
EventEmitter2.prototype.listenerCount = listenerCount; | ||
EventEmitter.prototype.listenerCount = listenerCount; | ||
function listenerCount(type) { | ||
@@ -389,3 +390,3 @@ var events = this._events; | ||
} | ||
EventEmitter2.prototype.eventNames = function eventNames() { | ||
EventEmitter.prototype.eventNames = function eventNames() { | ||
return this._eventsCount > 0 ? ReflectOwnKeys(this._events) : []; | ||
@@ -459,550 +460,18 @@ }; | ||
__export(src_exports, { | ||
DefaultLogger: () => DefaultLogger, | ||
ErrorCode: () => ErrorCode, | ||
FlagNotFoundError: () => FlagNotFoundError, | ||
GeneralError: () => GeneralError, | ||
InternalEventEmitter: () => InternalEventEmitter, | ||
InvalidContextError: () => InvalidContextError, | ||
LOG_LEVELS: () => LOG_LEVELS, | ||
NOOP_PROVIDER: () => NOOP_PROVIDER, | ||
OpenFeature: () => OpenFeature, | ||
OpenFeatureAPI: () => OpenFeatureAPI, | ||
OpenFeatureClient: () => OpenFeatureClient, | ||
OpenFeatureCommonAPI: () => OpenFeatureCommonAPI, | ||
OpenFeatureError: () => OpenFeatureError, | ||
OpenFeatureEventEmitter: () => OpenFeatureEventEmitter, | ||
ParseError: () => ParseError, | ||
ProviderEvents: () => ProviderEvents, | ||
ProviderStatus: () => ProviderStatus, | ||
SafeLogger: () => SafeLogger, | ||
StandardResolutionReasons: () => StandardResolutionReasons, | ||
TargetingKeyMissingError: () => TargetingKeyMissingError, | ||
TypeMismatchError: () => TypeMismatchError, | ||
isObject: () => isObject, | ||
isString: () => isString, | ||
objectOrUndefined: () => objectOrUndefined, | ||
statusMatchesEvent: () => statusMatchesEvent, | ||
stringOrUndefined: () => stringOrUndefined | ||
OpenFeatureClient: () => OpenFeatureClient | ||
}); | ||
module.exports = __toCommonJS(src_exports); | ||
var import_events = __toESM(require_events()); | ||
// ../shared/src/errors/open-feature-error-abstract.ts | ||
var OpenFeatureError = class extends Error { | ||
constructor(message) { | ||
super(message); | ||
Object.setPrototypeOf(this, OpenFeatureError.prototype); | ||
this.name = "OpenFeatureError"; | ||
} | ||
}; | ||
// src/client/open-feature-client.ts | ||
var import_core3 = require("@openfeature/core"); | ||
// ../shared/src/evaluation/evaluation.ts | ||
var StandardResolutionReasons = { | ||
/** | ||
* The resolved value was the result of a dynamic evaluation, such as a rule or specific user-targeting. | ||
*/ | ||
TARGETING_MATCH: "TARGETING_MATCH", | ||
/** | ||
* The resolved value was the result of pseudorandom assignment. | ||
*/ | ||
SPLIT: "SPLIT", | ||
/** | ||
* The resolved value was the result of the flag being disabled in the management system. | ||
*/ | ||
DISABLED: "DISABLED", | ||
/** | ||
* The resolved value was configured statically, or otherwise fell back to a pre-configured value. | ||
*/ | ||
DEFAULT: "DEFAULT", | ||
/** | ||
* The reason for the resolved value could not be determined. | ||
*/ | ||
UNKNOWN: "UNKNOWN", | ||
/** | ||
* The resolved value is static (no dynamic evaluation). | ||
*/ | ||
STATIC: "STATIC", | ||
/** | ||
* The resolved value was retrieved from cache. | ||
*/ | ||
CACHED: "CACHED", | ||
/** | ||
* The resolved value was the result of an error. | ||
* | ||
* Note: The `errorCode` and `errorMessage` fields may contain additional details of this error. | ||
*/ | ||
ERROR: "ERROR" | ||
}; | ||
var ErrorCode = /* @__PURE__ */ ((ErrorCode2) => { | ||
ErrorCode2["PROVIDER_NOT_READY"] = "PROVIDER_NOT_READY"; | ||
ErrorCode2["FLAG_NOT_FOUND"] = "FLAG_NOT_FOUND"; | ||
ErrorCode2["PARSE_ERROR"] = "PARSE_ERROR"; | ||
ErrorCode2["TYPE_MISMATCH"] = "TYPE_MISMATCH"; | ||
ErrorCode2["TARGETING_KEY_MISSING"] = "TARGETING_KEY_MISSING"; | ||
ErrorCode2["INVALID_CONTEXT"] = "INVALID_CONTEXT"; | ||
ErrorCode2["GENERAL"] = "GENERAL"; | ||
return ErrorCode2; | ||
})(ErrorCode || {}); | ||
// src/open-feature.ts | ||
var import_core2 = require("@openfeature/core"); | ||
// ../shared/src/errors/general-error.ts | ||
var GeneralError = class extends OpenFeatureError { | ||
constructor(message) { | ||
super(message); | ||
Object.setPrototypeOf(this, GeneralError.prototype); | ||
this.name = "GeneralError"; | ||
this.code = "GENERAL" /* GENERAL */; | ||
} | ||
}; | ||
// ../shared/src/errors/flag-not-found-error.ts | ||
var FlagNotFoundError = class extends OpenFeatureError { | ||
constructor(message) { | ||
super(message); | ||
Object.setPrototypeOf(this, FlagNotFoundError.prototype); | ||
this.name = "FlagNotFoundError"; | ||
this.code = "FLAG_NOT_FOUND" /* FLAG_NOT_FOUND */; | ||
} | ||
}; | ||
// ../shared/src/errors/parse-error.ts | ||
var ParseError = class extends OpenFeatureError { | ||
constructor(message) { | ||
super(message); | ||
Object.setPrototypeOf(this, ParseError.prototype); | ||
this.name = "ParseError"; | ||
this.code = "PARSE_ERROR" /* PARSE_ERROR */; | ||
} | ||
}; | ||
// ../shared/src/errors/type-mismatch-error.ts | ||
var TypeMismatchError = class extends OpenFeatureError { | ||
constructor(message) { | ||
super(message); | ||
Object.setPrototypeOf(this, TypeMismatchError.prototype); | ||
this.name = "TypeMismatchError"; | ||
this.code = "TYPE_MISMATCH" /* TYPE_MISMATCH */; | ||
} | ||
}; | ||
// ../shared/src/errors/targeting-key-missing-error.ts | ||
var TargetingKeyMissingError = class extends OpenFeatureError { | ||
constructor(message) { | ||
super(message); | ||
Object.setPrototypeOf(this, TargetingKeyMissingError.prototype); | ||
this.name = "TargetingKeyMissingError"; | ||
this.code = "TARGETING_KEY_MISSING" /* TARGETING_KEY_MISSING */; | ||
} | ||
}; | ||
// ../shared/src/errors/invalid-context-error.ts | ||
var InvalidContextError = class extends OpenFeatureError { | ||
constructor(message) { | ||
super(message); | ||
Object.setPrototypeOf(this, InvalidContextError.prototype); | ||
this.name = "InvalidContextError"; | ||
this.code = "INVALID_CONTEXT" /* INVALID_CONTEXT */; | ||
} | ||
}; | ||
// ../shared/src/provider/provider.ts | ||
var ProviderStatus = /* @__PURE__ */ ((ProviderStatus2) => { | ||
ProviderStatus2["NOT_READY"] = "NOT_READY"; | ||
ProviderStatus2["READY"] = "READY"; | ||
ProviderStatus2["ERROR"] = "ERROR"; | ||
ProviderStatus2["STALE"] = "STALE"; | ||
return ProviderStatus2; | ||
})(ProviderStatus || {}); | ||
// ../shared/src/events/events.ts | ||
var ProviderEvents = /* @__PURE__ */ ((ProviderEvents2) => { | ||
ProviderEvents2["Ready"] = "PROVIDER_READY"; | ||
ProviderEvents2["Error"] = "PROVIDER_ERROR"; | ||
ProviderEvents2["ConfigurationChanged"] = "PROVIDER_CONFIGURATION_CHANGED"; | ||
ProviderEvents2["Stale"] = "PROVIDER_STALE"; | ||
return ProviderEvents2; | ||
})(ProviderEvents || {}); | ||
// ../shared/src/events/event-utils.ts | ||
var eventStatusMap = { | ||
["READY" /* READY */]: "PROVIDER_READY" /* Ready */, | ||
["ERROR" /* ERROR */]: "PROVIDER_ERROR" /* Error */, | ||
["STALE" /* STALE */]: "PROVIDER_STALE" /* Stale */, | ||
["NOT_READY" /* NOT_READY */]: void 0 | ||
}; | ||
var statusMatchesEvent = (event, status) => { | ||
return !status && event === "PROVIDER_READY" /* Ready */ || eventStatusMap[status] === event; | ||
}; | ||
// ../shared/src/events/open-feature-event-emitter.ts | ||
var import_events2 = __toESM(require_events()); | ||
// ../shared/src/logger/default-logger.ts | ||
var DefaultLogger = class { | ||
error(...args) { | ||
console.error(...args); | ||
} | ||
warn(...args) { | ||
console.warn(...args); | ||
} | ||
info() { | ||
} | ||
debug() { | ||
} | ||
}; | ||
// ../shared/src/logger/safe-logger.ts | ||
var LOG_LEVELS = ["error", "warn", "info", "debug"]; | ||
var SafeLogger = class { | ||
constructor(logger) { | ||
this.fallbackLogger = new DefaultLogger(); | ||
try { | ||
for (const level of LOG_LEVELS) { | ||
if (!logger[level] || typeof logger[level] !== "function") { | ||
throw new Error(`The provided logger is missing the ${level} method.`); | ||
} | ||
} | ||
this.logger = logger; | ||
} catch (err) { | ||
console.error(err); | ||
console.error("Falling back to the default logger."); | ||
this.logger = this.fallbackLogger; | ||
} | ||
} | ||
error(...args) { | ||
this.log("error", ...args); | ||
} | ||
warn(...args) { | ||
this.log("warn", ...args); | ||
} | ||
info(...args) { | ||
this.log("info", ...args); | ||
} | ||
debug(...args) { | ||
this.log("debug", ...args); | ||
} | ||
log(level, ...args) { | ||
try { | ||
this.logger[level](...args); | ||
} catch (error) { | ||
this.fallbackLogger[level](...args); | ||
} | ||
} | ||
}; | ||
// ../shared/src/events/open-feature-event-emitter.ts | ||
var GenericEventEmitter = class { | ||
constructor(globalLogger) { | ||
this.globalLogger = globalLogger; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
this._handlers = /* @__PURE__ */ new WeakMap(); | ||
this.eventEmitter = new import_events2.default({ captureRejections: true }); | ||
this.eventEmitter.on("error", (err) => { | ||
var _a; | ||
(_a = this._logger) == null ? void 0 : _a.error("Error running event handler:", err); | ||
}); | ||
} | ||
emit(eventType, context) { | ||
this.eventEmitter.emit(eventType, context); | ||
} | ||
addHandler(eventType, handler) { | ||
const asyncHandler = (details) => __async(this, null, function* () { | ||
yield handler(details); | ||
}); | ||
this._handlers.set(handler, asyncHandler); | ||
this.eventEmitter.on(eventType, asyncHandler); | ||
} | ||
removeHandler(eventType, handler) { | ||
const asyncHandler = this._handlers.get(handler); | ||
if (!asyncHandler) { | ||
return; | ||
} | ||
this.eventEmitter.removeListener(eventType, asyncHandler); | ||
} | ||
removeAllHandlers(eventType) { | ||
if (eventType) { | ||
this.eventEmitter.removeAllListeners(eventType); | ||
} else { | ||
this.eventEmitter.removeAllListeners(); | ||
} | ||
} | ||
getHandlers(eventType) { | ||
return this.eventEmitter.listeners(eventType); | ||
} | ||
setLogger(logger) { | ||
this._eventLogger = new SafeLogger(logger); | ||
return this; | ||
} | ||
get _logger() { | ||
var _a, _b; | ||
return (_b = this._eventLogger) != null ? _b : (_a = this.globalLogger) == null ? void 0 : _a.call(this); | ||
} | ||
}; | ||
var OpenFeatureEventEmitter = class extends GenericEventEmitter { | ||
}; | ||
var InternalEventEmitter = class extends GenericEventEmitter { | ||
}; | ||
// ../shared/src/type-guards.ts | ||
function isString(value) { | ||
return typeof value === "string"; | ||
} | ||
function stringOrUndefined(value) { | ||
return isString(value) ? value : void 0; | ||
} | ||
function isObject(value) { | ||
return typeof value === "object"; | ||
} | ||
function objectOrUndefined(value) { | ||
return isObject(value) ? value : void 0; | ||
} | ||
// ../shared/src/filter.ts | ||
function isDefined(input) { | ||
return typeof input !== "undefined" && input !== null; | ||
} | ||
// ../shared/src/open-feature.ts | ||
var OpenFeatureCommonAPI = class { | ||
constructor(category) { | ||
this._hooks = []; | ||
this._context = {}; | ||
this._logger = new DefaultLogger(); | ||
this._events = new InternalEventEmitter(() => this._logger); | ||
this._clientEventHandlers = /* @__PURE__ */ new Map(); | ||
this._clientProviders = /* @__PURE__ */ new Map(); | ||
this._clientEvents = /* @__PURE__ */ new Map(); | ||
this._runsOn = category; | ||
} | ||
addHooks(...hooks) { | ||
this._hooks = [...this._hooks, ...hooks]; | ||
return this; | ||
} | ||
getHooks() { | ||
return this._hooks; | ||
} | ||
clearHooks() { | ||
this._hooks = []; | ||
return this; | ||
} | ||
setLogger(logger) { | ||
this._logger = new SafeLogger(logger); | ||
return this; | ||
} | ||
/** | ||
* Get metadata about registered provider. | ||
* @returns {ProviderMetadata} Provider Metadata | ||
*/ | ||
get providerMetadata() { | ||
return this._defaultProvider.metadata; | ||
} | ||
/** | ||
* Adds a handler for the given provider event type. | ||
* The handlers are called in the order they have been added. | ||
* API (global) events run for all providers. | ||
* @param {ProviderEvents} eventType The provider event type to listen to | ||
* @param {EventHandler} handler The handler to run on occurrence of the event type | ||
*/ | ||
addHandler(eventType, handler) { | ||
[.../* @__PURE__ */ new Map([[void 0, this._defaultProvider]]), ...this._clientProviders].forEach((keyProviderTuple) => { | ||
var _a; | ||
const clientName = keyProviderTuple[0]; | ||
const provider = keyProviderTuple[1]; | ||
const shouldRunNow = statusMatchesEvent(eventType, keyProviderTuple[1].status); | ||
if (shouldRunNow) { | ||
try { | ||
handler({ clientName, providerName: provider.metadata.name }); | ||
} catch (err) { | ||
(_a = this._logger) == null ? void 0 : _a.error("Error running event handler:", err); | ||
} | ||
} | ||
}); | ||
this._events.addHandler(eventType, handler); | ||
} | ||
/** | ||
* Removes a handler for the given provider event type. | ||
* @param {ProviderEvents} eventType The provider event type to remove the listener for | ||
* @param {EventHandler} handler The handler to remove for the provider event type | ||
*/ | ||
removeHandler(eventType, handler) { | ||
this._events.removeHandler(eventType, handler); | ||
} | ||
/** | ||
* Gets the current handlers for the given provider event type. | ||
* @param {ProviderEvents} eventType The provider event type to get the current handlers for | ||
* @returns {EventHandler[]} The handlers currently attached to the given provider event type | ||
*/ | ||
getHandlers(eventType) { | ||
return this._events.getHandlers(eventType); | ||
} | ||
setProviderAndWait(clientOrProvider, providerOrUndefined) { | ||
return __async(this, null, function* () { | ||
yield this.setAwaitableProvider(clientOrProvider, providerOrUndefined); | ||
}); | ||
} | ||
setProvider(clientOrProvider, providerOrUndefined) { | ||
const maybePromise = this.setAwaitableProvider(clientOrProvider, providerOrUndefined); | ||
if (maybePromise) { | ||
maybePromise.catch(() => { | ||
}); | ||
} | ||
return this; | ||
} | ||
setAwaitableProvider(clientOrProvider, providerOrUndefined) { | ||
var _a, _b, _c, _d, _e, _f; | ||
const clientName = stringOrUndefined(clientOrProvider); | ||
const provider = (_a = objectOrUndefined(clientOrProvider)) != null ? _a : objectOrUndefined(providerOrUndefined); | ||
if (!provider) { | ||
this._logger.debug("No provider defined, ignoring setProvider call"); | ||
return; | ||
} | ||
const oldProvider = this.getProviderForClient(clientName); | ||
const providerName = provider.metadata.name; | ||
if (oldProvider === provider) { | ||
this._logger.debug("Provider is already set, ignoring setProvider call"); | ||
return; | ||
} | ||
if (!provider.runsOn) { | ||
this._logger.debug(`Provider '${provider.metadata.name}' has not defined its intended use.`); | ||
} else if (provider.runsOn !== this._runsOn) { | ||
throw new GeneralError(`Provider '${provider.metadata.name}' is intended for use on the ${provider.runsOn}.`); | ||
} | ||
const emitters = this.getAssociatedEventEmitters(clientName); | ||
if (typeof provider.initialize === "function" && provider.status === void 0) { | ||
const activeLogger = this._logger || console; | ||
activeLogger.warn( | ||
`Provider ${providerName} implements 'initialize' but not 'status'. Please implement 'status'.` | ||
); | ||
} | ||
let initializationPromise = void 0; | ||
if ((provider == null ? void 0 : provider.status) === "NOT_READY" /* NOT_READY */ && typeof provider.initialize === "function") { | ||
initializationPromise = (_d = (_c = (_b = provider.initialize) == null ? void 0 : _b.call(provider, this._context)) == null ? void 0 : _c.then(() => { | ||
var _a2; | ||
this.getAssociatedEventEmitters(clientName).forEach((emitter) => { | ||
emitter == null ? void 0 : emitter.emit("PROVIDER_READY" /* Ready */, { clientName, providerName }); | ||
}); | ||
(_a2 = this._events) == null ? void 0 : _a2.emit("PROVIDER_READY" /* Ready */, { clientName, providerName }); | ||
})) == null ? void 0 : _d.catch((error) => { | ||
var _a2; | ||
this.getAssociatedEventEmitters(clientName).forEach((emitter) => { | ||
emitter == null ? void 0 : emitter.emit("PROVIDER_ERROR" /* Error */, { clientName, providerName, message: error.message }); | ||
}); | ||
(_a2 = this._events) == null ? void 0 : _a2.emit("PROVIDER_ERROR" /* Error */, { clientName, providerName, message: error.message }); | ||
throw error; | ||
}); | ||
} else { | ||
emitters.forEach((emitter) => { | ||
emitter == null ? void 0 : emitter.emit("PROVIDER_READY" /* Ready */, { clientName, providerName }); | ||
}); | ||
(_e = this._events) == null ? void 0 : _e.emit("PROVIDER_READY" /* Ready */, { clientName, providerName }); | ||
} | ||
if (clientName) { | ||
this._clientProviders.set(clientName, provider); | ||
} else { | ||
this._defaultProvider = provider; | ||
} | ||
this.transferListeners(oldProvider, provider, clientName, emitters); | ||
if (![...this._clientProviders.values(), this._defaultProvider].includes(oldProvider)) { | ||
(_f = oldProvider == null ? void 0 : oldProvider.onClose) == null ? void 0 : _f.call(oldProvider); | ||
} | ||
return initializationPromise; | ||
} | ||
getProviderForClient(name) { | ||
var _a; | ||
if (!name) { | ||
return this._defaultProvider; | ||
} | ||
return (_a = this._clientProviders.get(name)) != null ? _a : this._defaultProvider; | ||
} | ||
buildAndCacheEventEmitterForClient(name) { | ||
const emitter = this._clientEvents.get(name); | ||
if (emitter) { | ||
return emitter; | ||
} | ||
const newEmitter = new InternalEventEmitter(() => this._logger); | ||
this._clientEvents.set(name, newEmitter); | ||
const clientProvider = this.getProviderForClient(name); | ||
Object.values(ProviderEvents).forEach( | ||
(eventType) => { | ||
var _a; | ||
return (_a = clientProvider.events) == null ? void 0 : _a.addHandler(eventType, (details) => __async(this, null, function* () { | ||
newEmitter.emit(eventType, __spreadProps(__spreadValues({}, details), { clientName: name, providerName: clientProvider.metadata.name })); | ||
})); | ||
} | ||
); | ||
return newEmitter; | ||
} | ||
getUnboundEmitters() { | ||
const namedProviders = [...this._clientProviders.keys()]; | ||
const eventEmitterNames = [...this._clientEvents.keys()].filter(isDefined); | ||
const unboundEmitterNames = eventEmitterNames.filter((name) => !namedProviders.includes(name)); | ||
return [ | ||
// all unbound, named emitters | ||
...unboundEmitterNames.map((name) => this._clientEvents.get(name)), | ||
// the default emitter | ||
this._clientEvents.get(void 0) | ||
].filter(isDefined); | ||
} | ||
getAssociatedEventEmitters(clientName) { | ||
return clientName ? [this.buildAndCacheEventEmitterForClient(clientName)] : this.getUnboundEmitters(); | ||
} | ||
transferListeners(oldProvider, newProvider, clientName, emitters) { | ||
var _a; | ||
(_a = this._clientEventHandlers.get(clientName)) == null ? void 0 : _a.forEach((eventHandler) => { | ||
var _a2; | ||
return (_a2 = oldProvider.events) == null ? void 0 : _a2.removeHandler(...eventHandler); | ||
}); | ||
const newClientHandlers = Object.values(ProviderEvents).map((eventType) => { | ||
const handler = (details) => __async(this, null, function* () { | ||
emitters.forEach((emitter) => { | ||
emitter == null ? void 0 : emitter.emit(eventType, __spreadProps(__spreadValues({}, details), { clientName, providerName: newProvider.metadata.name })); | ||
}); | ||
this._events.emit(eventType, __spreadProps(__spreadValues({}, details), { clientName, providerName: newProvider.metadata.name })); | ||
}); | ||
return [eventType, handler]; | ||
}); | ||
this._clientEventHandlers.set(clientName, newClientHandlers); | ||
newClientHandlers.forEach((eventHandler) => { | ||
var _a2; | ||
return (_a2 = newProvider.events) == null ? void 0 : _a2.addHandler(...eventHandler); | ||
}); | ||
} | ||
close() { | ||
return __async(this, null, function* () { | ||
var _a, _b; | ||
try { | ||
yield (_b = (_a = this == null ? void 0 : this._defaultProvider) == null ? void 0 : _a.onClose) == null ? void 0 : _b.call(_a); | ||
} catch (err) { | ||
this.handleShutdownError(this._defaultProvider, err); | ||
} | ||
const providers = Array.from(this._clientProviders); | ||
yield Promise.all( | ||
providers.map((_0) => __async(this, [_0], function* ([, provider]) { | ||
var _a2; | ||
try { | ||
yield (_a2 = provider.onClose) == null ? void 0 : _a2.call(provider); | ||
} catch (err) { | ||
this.handleShutdownError(provider, err); | ||
} | ||
})) | ||
); | ||
}); | ||
} | ||
clearProvidersAndSetDefault(defaultProvider) { | ||
return __async(this, null, function* () { | ||
try { | ||
yield this.close(); | ||
} catch (err) { | ||
this._logger.error("Unable to cleanly close providers. Resetting to the default configuration."); | ||
} finally { | ||
this._clientProviders.clear(); | ||
this._defaultProvider = defaultProvider; | ||
} | ||
}); | ||
} | ||
handleShutdownError(provider, err) { | ||
this._logger.error(`Error during shutdown of provider ${provider.metadata.name}: ${err}`); | ||
this._logger.error(err == null ? void 0 : err.stack); | ||
} | ||
}; | ||
// src/provider/no-op-provider.ts | ||
var import_core = require("@openfeature/core"); | ||
var REASON_NO_OP = "No-op"; | ||
@@ -1016,3 +485,3 @@ var NoopFeatureProvider = class { | ||
get status() { | ||
return "NOT_READY" /* NOT_READY */; | ||
return import_core.ProviderStatus.NOT_READY; | ||
} | ||
@@ -1043,3 +512,3 @@ resolveBooleanEvaluation(_, defaultValue) { | ||
var _globalThis = globalThis; | ||
var OpenFeatureAPI = class extends OpenFeatureCommonAPI { | ||
var OpenFeatureAPI = class extends import_core2.OpenFeatureCommonAPI { | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
@@ -1134,4 +603,4 @@ constructor() { | ||
this.emitterAccessor().addHandler(eventType, handler); | ||
const providerReady = !this._provider.status || this._provider.status === "READY" /* READY */; | ||
if (eventType === "PROVIDER_READY" /* Ready */ && providerReady) { | ||
const providerReady = !this._provider.status || this._provider.status === import_core3.ProviderStatus.READY; | ||
if (eventType === import_core3.ProviderEvents.Ready && providerReady) { | ||
try { | ||
@@ -1151,3 +620,3 @@ handler({ clientName: this.metadata.name, providerName: this._provider.metadata.name }); | ||
setLogger(logger) { | ||
this._clientLogger = new SafeLogger(logger); | ||
this._clientLogger = new import_core3.SafeLogger(logger); | ||
return this; | ||
@@ -1234,3 +703,3 @@ } | ||
const errorMessage = err == null ? void 0 : err.message; | ||
const errorCode = (err == null ? void 0 : err.code) || "GENERAL" /* GENERAL */; | ||
const errorCode = (err == null ? void 0 : err.code) || import_core3.ErrorCode.GENERAL; | ||
this.errorHooks(allHooksReversed, hookContext, err, options); | ||
@@ -1241,3 +710,3 @@ return { | ||
value: defaultValue, | ||
reason: StandardResolutionReasons.ERROR, | ||
reason: import_core3.StandardResolutionReasons.ERROR, | ||
flagMetadata: Object.freeze({}), | ||
@@ -1299,2 +768,5 @@ flagKey | ||
}; | ||
// src/index.ts | ||
__reExport(src_exports, require("@openfeature/core"), module.exports); | ||
//# sourceMappingURL=index.js.map |
@@ -91,11 +91,11 @@ var __create = Object.create; | ||
}; | ||
function EventEmitter2() { | ||
EventEmitter2.init.call(this); | ||
function EventEmitter() { | ||
EventEmitter.init.call(this); | ||
} | ||
module.exports = EventEmitter2; | ||
module.exports = EventEmitter; | ||
module.exports.once = once; | ||
EventEmitter2.EventEmitter = EventEmitter2; | ||
EventEmitter2.prototype._events = void 0; | ||
EventEmitter2.prototype._eventsCount = 0; | ||
EventEmitter2.prototype._maxListeners = void 0; | ||
EventEmitter.EventEmitter = EventEmitter; | ||
EventEmitter.prototype._events = void 0; | ||
EventEmitter.prototype._eventsCount = 0; | ||
EventEmitter.prototype._maxListeners = void 0; | ||
var defaultMaxListeners = 10; | ||
@@ -107,3 +107,3 @@ function checkListener(listener) { | ||
} | ||
Object.defineProperty(EventEmitter2, "defaultMaxListeners", { | ||
Object.defineProperty(EventEmitter, "defaultMaxListeners", { | ||
enumerable: true, | ||
@@ -120,3 +120,3 @@ get: function() { | ||
}); | ||
EventEmitter2.init = function() { | ||
EventEmitter.init = function() { | ||
if (this._events === void 0 || this._events === Object.getPrototypeOf(this)._events) { | ||
@@ -128,3 +128,3 @@ this._events = /* @__PURE__ */ Object.create(null); | ||
}; | ||
EventEmitter2.prototype.setMaxListeners = function setMaxListeners(n) { | ||
EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) { | ||
if (typeof n !== "number" || n < 0 || NumberIsNaN(n)) { | ||
@@ -138,9 +138,9 @@ throw new RangeError('The value of "n" is out of range. It must be a non-negative number. Received ' + n + "."); | ||
if (that._maxListeners === void 0) | ||
return EventEmitter2.defaultMaxListeners; | ||
return EventEmitter.defaultMaxListeners; | ||
return that._maxListeners; | ||
} | ||
EventEmitter2.prototype.getMaxListeners = function getMaxListeners() { | ||
EventEmitter.prototype.getMaxListeners = function getMaxListeners() { | ||
return _getMaxListeners(this); | ||
}; | ||
EventEmitter2.prototype.emit = function emit(type) { | ||
EventEmitter.prototype.emit = function emit(type) { | ||
var args = []; | ||
@@ -223,7 +223,7 @@ for (var i = 1; i < arguments.length; i++) | ||
} | ||
EventEmitter2.prototype.addListener = function addListener(type, listener) { | ||
EventEmitter.prototype.addListener = function addListener(type, listener) { | ||
return _addListener(this, type, listener, false); | ||
}; | ||
EventEmitter2.prototype.on = EventEmitter2.prototype.addListener; | ||
EventEmitter2.prototype.prependListener = function prependListener(type, listener) { | ||
EventEmitter.prototype.on = EventEmitter.prototype.addListener; | ||
EventEmitter.prototype.prependListener = function prependListener(type, listener) { | ||
return _addListener(this, type, listener, true); | ||
@@ -247,3 +247,3 @@ }; | ||
} | ||
EventEmitter2.prototype.once = function once2(type, listener) { | ||
EventEmitter.prototype.once = function once2(type, listener) { | ||
checkListener(listener); | ||
@@ -253,3 +253,3 @@ this.on(type, _onceWrap(this, type, listener)); | ||
}; | ||
EventEmitter2.prototype.prependOnceListener = function prependOnceListener(type, listener) { | ||
EventEmitter.prototype.prependOnceListener = function prependOnceListener(type, listener) { | ||
checkListener(listener); | ||
@@ -259,3 +259,3 @@ this.prependListener(type, _onceWrap(this, type, listener)); | ||
}; | ||
EventEmitter2.prototype.removeListener = function removeListener(type, listener) { | ||
EventEmitter.prototype.removeListener = function removeListener(type, listener) { | ||
var list, events, position, i, originalListener; | ||
@@ -300,4 +300,4 @@ checkListener(listener); | ||
}; | ||
EventEmitter2.prototype.off = EventEmitter2.prototype.removeListener; | ||
EventEmitter2.prototype.removeAllListeners = function removeAllListeners(type) { | ||
EventEmitter.prototype.off = EventEmitter.prototype.removeListener; | ||
EventEmitter.prototype.removeAllListeners = function removeAllListeners(type) { | ||
var listeners, events, i; | ||
@@ -354,9 +354,9 @@ events = this._events; | ||
} | ||
EventEmitter2.prototype.listeners = function listeners(type) { | ||
EventEmitter.prototype.listeners = function listeners(type) { | ||
return _listeners(this, type, true); | ||
}; | ||
EventEmitter2.prototype.rawListeners = function rawListeners(type) { | ||
EventEmitter.prototype.rawListeners = function rawListeners(type) { | ||
return _listeners(this, type, false); | ||
}; | ||
EventEmitter2.listenerCount = function(emitter, type) { | ||
EventEmitter.listenerCount = function(emitter, type) { | ||
if (typeof emitter.listenerCount === "function") { | ||
@@ -368,3 +368,3 @@ return emitter.listenerCount(type); | ||
}; | ||
EventEmitter2.prototype.listenerCount = listenerCount; | ||
EventEmitter.prototype.listenerCount = listenerCount; | ||
function listenerCount(type) { | ||
@@ -382,3 +382,3 @@ var events = this._events; | ||
} | ||
EventEmitter2.prototype.eventNames = function eventNames() { | ||
EventEmitter.prototype.eventNames = function eventNames() { | ||
return this._eventsCount > 0 ? ReflectOwnKeys(this._events) : []; | ||
@@ -449,521 +449,19 @@ }; | ||
// ../shared/src/errors/open-feature-error-abstract.ts | ||
var OpenFeatureError = class extends Error { | ||
constructor(message) { | ||
super(message); | ||
Object.setPrototypeOf(this, OpenFeatureError.prototype); | ||
this.name = "OpenFeatureError"; | ||
} | ||
}; | ||
// src/index.ts | ||
var import_events = __toESM(require_events()); | ||
// ../shared/src/evaluation/evaluation.ts | ||
var StandardResolutionReasons = { | ||
/** | ||
* The resolved value was the result of a dynamic evaluation, such as a rule or specific user-targeting. | ||
*/ | ||
TARGETING_MATCH: "TARGETING_MATCH", | ||
/** | ||
* The resolved value was the result of pseudorandom assignment. | ||
*/ | ||
SPLIT: "SPLIT", | ||
/** | ||
* The resolved value was the result of the flag being disabled in the management system. | ||
*/ | ||
DISABLED: "DISABLED", | ||
/** | ||
* The resolved value was configured statically, or otherwise fell back to a pre-configured value. | ||
*/ | ||
DEFAULT: "DEFAULT", | ||
/** | ||
* The reason for the resolved value could not be determined. | ||
*/ | ||
UNKNOWN: "UNKNOWN", | ||
/** | ||
* The resolved value is static (no dynamic evaluation). | ||
*/ | ||
STATIC: "STATIC", | ||
/** | ||
* The resolved value was retrieved from cache. | ||
*/ | ||
CACHED: "CACHED", | ||
/** | ||
* The resolved value was the result of an error. | ||
* | ||
* Note: The `errorCode` and `errorMessage` fields may contain additional details of this error. | ||
*/ | ||
ERROR: "ERROR" | ||
}; | ||
var ErrorCode = /* @__PURE__ */ ((ErrorCode2) => { | ||
ErrorCode2["PROVIDER_NOT_READY"] = "PROVIDER_NOT_READY"; | ||
ErrorCode2["FLAG_NOT_FOUND"] = "FLAG_NOT_FOUND"; | ||
ErrorCode2["PARSE_ERROR"] = "PARSE_ERROR"; | ||
ErrorCode2["TYPE_MISMATCH"] = "TYPE_MISMATCH"; | ||
ErrorCode2["TARGETING_KEY_MISSING"] = "TARGETING_KEY_MISSING"; | ||
ErrorCode2["INVALID_CONTEXT"] = "INVALID_CONTEXT"; | ||
ErrorCode2["GENERAL"] = "GENERAL"; | ||
return ErrorCode2; | ||
})(ErrorCode || {}); | ||
// src/client/open-feature-client.ts | ||
import { | ||
ErrorCode, | ||
ProviderEvents, | ||
ProviderStatus as ProviderStatus2, | ||
SafeLogger, | ||
StandardResolutionReasons | ||
} from "@openfeature/core"; | ||
// ../shared/src/errors/general-error.ts | ||
var GeneralError = class extends OpenFeatureError { | ||
constructor(message) { | ||
super(message); | ||
Object.setPrototypeOf(this, GeneralError.prototype); | ||
this.name = "GeneralError"; | ||
this.code = "GENERAL" /* GENERAL */; | ||
} | ||
}; | ||
// src/open-feature.ts | ||
import { OpenFeatureCommonAPI } from "@openfeature/core"; | ||
// ../shared/src/errors/flag-not-found-error.ts | ||
var FlagNotFoundError = class extends OpenFeatureError { | ||
constructor(message) { | ||
super(message); | ||
Object.setPrototypeOf(this, FlagNotFoundError.prototype); | ||
this.name = "FlagNotFoundError"; | ||
this.code = "FLAG_NOT_FOUND" /* FLAG_NOT_FOUND */; | ||
} | ||
}; | ||
// ../shared/src/errors/parse-error.ts | ||
var ParseError = class extends OpenFeatureError { | ||
constructor(message) { | ||
super(message); | ||
Object.setPrototypeOf(this, ParseError.prototype); | ||
this.name = "ParseError"; | ||
this.code = "PARSE_ERROR" /* PARSE_ERROR */; | ||
} | ||
}; | ||
// ../shared/src/errors/type-mismatch-error.ts | ||
var TypeMismatchError = class extends OpenFeatureError { | ||
constructor(message) { | ||
super(message); | ||
Object.setPrototypeOf(this, TypeMismatchError.prototype); | ||
this.name = "TypeMismatchError"; | ||
this.code = "TYPE_MISMATCH" /* TYPE_MISMATCH */; | ||
} | ||
}; | ||
// ../shared/src/errors/targeting-key-missing-error.ts | ||
var TargetingKeyMissingError = class extends OpenFeatureError { | ||
constructor(message) { | ||
super(message); | ||
Object.setPrototypeOf(this, TargetingKeyMissingError.prototype); | ||
this.name = "TargetingKeyMissingError"; | ||
this.code = "TARGETING_KEY_MISSING" /* TARGETING_KEY_MISSING */; | ||
} | ||
}; | ||
// ../shared/src/errors/invalid-context-error.ts | ||
var InvalidContextError = class extends OpenFeatureError { | ||
constructor(message) { | ||
super(message); | ||
Object.setPrototypeOf(this, InvalidContextError.prototype); | ||
this.name = "InvalidContextError"; | ||
this.code = "INVALID_CONTEXT" /* INVALID_CONTEXT */; | ||
} | ||
}; | ||
// ../shared/src/provider/provider.ts | ||
var ProviderStatus = /* @__PURE__ */ ((ProviderStatus2) => { | ||
ProviderStatus2["NOT_READY"] = "NOT_READY"; | ||
ProviderStatus2["READY"] = "READY"; | ||
ProviderStatus2["ERROR"] = "ERROR"; | ||
ProviderStatus2["STALE"] = "STALE"; | ||
return ProviderStatus2; | ||
})(ProviderStatus || {}); | ||
// ../shared/src/events/events.ts | ||
var ProviderEvents = /* @__PURE__ */ ((ProviderEvents2) => { | ||
ProviderEvents2["Ready"] = "PROVIDER_READY"; | ||
ProviderEvents2["Error"] = "PROVIDER_ERROR"; | ||
ProviderEvents2["ConfigurationChanged"] = "PROVIDER_CONFIGURATION_CHANGED"; | ||
ProviderEvents2["Stale"] = "PROVIDER_STALE"; | ||
return ProviderEvents2; | ||
})(ProviderEvents || {}); | ||
// ../shared/src/events/event-utils.ts | ||
var eventStatusMap = { | ||
["READY" /* READY */]: "PROVIDER_READY" /* Ready */, | ||
["ERROR" /* ERROR */]: "PROVIDER_ERROR" /* Error */, | ||
["STALE" /* STALE */]: "PROVIDER_STALE" /* Stale */, | ||
["NOT_READY" /* NOT_READY */]: void 0 | ||
}; | ||
var statusMatchesEvent = (event, status) => { | ||
return !status && event === "PROVIDER_READY" /* Ready */ || eventStatusMap[status] === event; | ||
}; | ||
// ../shared/src/events/open-feature-event-emitter.ts | ||
var import_events2 = __toESM(require_events()); | ||
// ../shared/src/logger/default-logger.ts | ||
var DefaultLogger = class { | ||
error(...args) { | ||
console.error(...args); | ||
} | ||
warn(...args) { | ||
console.warn(...args); | ||
} | ||
info() { | ||
} | ||
debug() { | ||
} | ||
}; | ||
// ../shared/src/logger/safe-logger.ts | ||
var LOG_LEVELS = ["error", "warn", "info", "debug"]; | ||
var SafeLogger = class { | ||
constructor(logger) { | ||
this.fallbackLogger = new DefaultLogger(); | ||
try { | ||
for (const level of LOG_LEVELS) { | ||
if (!logger[level] || typeof logger[level] !== "function") { | ||
throw new Error(`The provided logger is missing the ${level} method.`); | ||
} | ||
} | ||
this.logger = logger; | ||
} catch (err) { | ||
console.error(err); | ||
console.error("Falling back to the default logger."); | ||
this.logger = this.fallbackLogger; | ||
} | ||
} | ||
error(...args) { | ||
this.log("error", ...args); | ||
} | ||
warn(...args) { | ||
this.log("warn", ...args); | ||
} | ||
info(...args) { | ||
this.log("info", ...args); | ||
} | ||
debug(...args) { | ||
this.log("debug", ...args); | ||
} | ||
log(level, ...args) { | ||
try { | ||
this.logger[level](...args); | ||
} catch (error) { | ||
this.fallbackLogger[level](...args); | ||
} | ||
} | ||
}; | ||
// ../shared/src/events/open-feature-event-emitter.ts | ||
var GenericEventEmitter = class { | ||
constructor(globalLogger) { | ||
this.globalLogger = globalLogger; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
this._handlers = /* @__PURE__ */ new WeakMap(); | ||
this.eventEmitter = new import_events2.default({ captureRejections: true }); | ||
this.eventEmitter.on("error", (err) => { | ||
var _a; | ||
(_a = this._logger) == null ? void 0 : _a.error("Error running event handler:", err); | ||
}); | ||
} | ||
emit(eventType, context) { | ||
this.eventEmitter.emit(eventType, context); | ||
} | ||
addHandler(eventType, handler) { | ||
const asyncHandler = (details) => __async(this, null, function* () { | ||
yield handler(details); | ||
}); | ||
this._handlers.set(handler, asyncHandler); | ||
this.eventEmitter.on(eventType, asyncHandler); | ||
} | ||
removeHandler(eventType, handler) { | ||
const asyncHandler = this._handlers.get(handler); | ||
if (!asyncHandler) { | ||
return; | ||
} | ||
this.eventEmitter.removeListener(eventType, asyncHandler); | ||
} | ||
removeAllHandlers(eventType) { | ||
if (eventType) { | ||
this.eventEmitter.removeAllListeners(eventType); | ||
} else { | ||
this.eventEmitter.removeAllListeners(); | ||
} | ||
} | ||
getHandlers(eventType) { | ||
return this.eventEmitter.listeners(eventType); | ||
} | ||
setLogger(logger) { | ||
this._eventLogger = new SafeLogger(logger); | ||
return this; | ||
} | ||
get _logger() { | ||
var _a, _b; | ||
return (_b = this._eventLogger) != null ? _b : (_a = this.globalLogger) == null ? void 0 : _a.call(this); | ||
} | ||
}; | ||
var OpenFeatureEventEmitter = class extends GenericEventEmitter { | ||
}; | ||
var InternalEventEmitter = class extends GenericEventEmitter { | ||
}; | ||
// ../shared/src/type-guards.ts | ||
function isString(value) { | ||
return typeof value === "string"; | ||
} | ||
function stringOrUndefined(value) { | ||
return isString(value) ? value : void 0; | ||
} | ||
function isObject(value) { | ||
return typeof value === "object"; | ||
} | ||
function objectOrUndefined(value) { | ||
return isObject(value) ? value : void 0; | ||
} | ||
// ../shared/src/filter.ts | ||
function isDefined(input) { | ||
return typeof input !== "undefined" && input !== null; | ||
} | ||
// ../shared/src/open-feature.ts | ||
var OpenFeatureCommonAPI = class { | ||
constructor(category) { | ||
this._hooks = []; | ||
this._context = {}; | ||
this._logger = new DefaultLogger(); | ||
this._events = new InternalEventEmitter(() => this._logger); | ||
this._clientEventHandlers = /* @__PURE__ */ new Map(); | ||
this._clientProviders = /* @__PURE__ */ new Map(); | ||
this._clientEvents = /* @__PURE__ */ new Map(); | ||
this._runsOn = category; | ||
} | ||
addHooks(...hooks) { | ||
this._hooks = [...this._hooks, ...hooks]; | ||
return this; | ||
} | ||
getHooks() { | ||
return this._hooks; | ||
} | ||
clearHooks() { | ||
this._hooks = []; | ||
return this; | ||
} | ||
setLogger(logger) { | ||
this._logger = new SafeLogger(logger); | ||
return this; | ||
} | ||
/** | ||
* Get metadata about registered provider. | ||
* @returns {ProviderMetadata} Provider Metadata | ||
*/ | ||
get providerMetadata() { | ||
return this._defaultProvider.metadata; | ||
} | ||
/** | ||
* Adds a handler for the given provider event type. | ||
* The handlers are called in the order they have been added. | ||
* API (global) events run for all providers. | ||
* @param {ProviderEvents} eventType The provider event type to listen to | ||
* @param {EventHandler} handler The handler to run on occurrence of the event type | ||
*/ | ||
addHandler(eventType, handler) { | ||
[.../* @__PURE__ */ new Map([[void 0, this._defaultProvider]]), ...this._clientProviders].forEach((keyProviderTuple) => { | ||
var _a; | ||
const clientName = keyProviderTuple[0]; | ||
const provider = keyProviderTuple[1]; | ||
const shouldRunNow = statusMatchesEvent(eventType, keyProviderTuple[1].status); | ||
if (shouldRunNow) { | ||
try { | ||
handler({ clientName, providerName: provider.metadata.name }); | ||
} catch (err) { | ||
(_a = this._logger) == null ? void 0 : _a.error("Error running event handler:", err); | ||
} | ||
} | ||
}); | ||
this._events.addHandler(eventType, handler); | ||
} | ||
/** | ||
* Removes a handler for the given provider event type. | ||
* @param {ProviderEvents} eventType The provider event type to remove the listener for | ||
* @param {EventHandler} handler The handler to remove for the provider event type | ||
*/ | ||
removeHandler(eventType, handler) { | ||
this._events.removeHandler(eventType, handler); | ||
} | ||
/** | ||
* Gets the current handlers for the given provider event type. | ||
* @param {ProviderEvents} eventType The provider event type to get the current handlers for | ||
* @returns {EventHandler[]} The handlers currently attached to the given provider event type | ||
*/ | ||
getHandlers(eventType) { | ||
return this._events.getHandlers(eventType); | ||
} | ||
setProviderAndWait(clientOrProvider, providerOrUndefined) { | ||
return __async(this, null, function* () { | ||
yield this.setAwaitableProvider(clientOrProvider, providerOrUndefined); | ||
}); | ||
} | ||
setProvider(clientOrProvider, providerOrUndefined) { | ||
const maybePromise = this.setAwaitableProvider(clientOrProvider, providerOrUndefined); | ||
if (maybePromise) { | ||
maybePromise.catch(() => { | ||
}); | ||
} | ||
return this; | ||
} | ||
setAwaitableProvider(clientOrProvider, providerOrUndefined) { | ||
var _a, _b, _c, _d, _e, _f; | ||
const clientName = stringOrUndefined(clientOrProvider); | ||
const provider = (_a = objectOrUndefined(clientOrProvider)) != null ? _a : objectOrUndefined(providerOrUndefined); | ||
if (!provider) { | ||
this._logger.debug("No provider defined, ignoring setProvider call"); | ||
return; | ||
} | ||
const oldProvider = this.getProviderForClient(clientName); | ||
const providerName = provider.metadata.name; | ||
if (oldProvider === provider) { | ||
this._logger.debug("Provider is already set, ignoring setProvider call"); | ||
return; | ||
} | ||
if (!provider.runsOn) { | ||
this._logger.debug(`Provider '${provider.metadata.name}' has not defined its intended use.`); | ||
} else if (provider.runsOn !== this._runsOn) { | ||
throw new GeneralError(`Provider '${provider.metadata.name}' is intended for use on the ${provider.runsOn}.`); | ||
} | ||
const emitters = this.getAssociatedEventEmitters(clientName); | ||
if (typeof provider.initialize === "function" && provider.status === void 0) { | ||
const activeLogger = this._logger || console; | ||
activeLogger.warn( | ||
`Provider ${providerName} implements 'initialize' but not 'status'. Please implement 'status'.` | ||
); | ||
} | ||
let initializationPromise = void 0; | ||
if ((provider == null ? void 0 : provider.status) === "NOT_READY" /* NOT_READY */ && typeof provider.initialize === "function") { | ||
initializationPromise = (_d = (_c = (_b = provider.initialize) == null ? void 0 : _b.call(provider, this._context)) == null ? void 0 : _c.then(() => { | ||
var _a2; | ||
this.getAssociatedEventEmitters(clientName).forEach((emitter) => { | ||
emitter == null ? void 0 : emitter.emit("PROVIDER_READY" /* Ready */, { clientName, providerName }); | ||
}); | ||
(_a2 = this._events) == null ? void 0 : _a2.emit("PROVIDER_READY" /* Ready */, { clientName, providerName }); | ||
})) == null ? void 0 : _d.catch((error) => { | ||
var _a2; | ||
this.getAssociatedEventEmitters(clientName).forEach((emitter) => { | ||
emitter == null ? void 0 : emitter.emit("PROVIDER_ERROR" /* Error */, { clientName, providerName, message: error.message }); | ||
}); | ||
(_a2 = this._events) == null ? void 0 : _a2.emit("PROVIDER_ERROR" /* Error */, { clientName, providerName, message: error.message }); | ||
throw error; | ||
}); | ||
} else { | ||
emitters.forEach((emitter) => { | ||
emitter == null ? void 0 : emitter.emit("PROVIDER_READY" /* Ready */, { clientName, providerName }); | ||
}); | ||
(_e = this._events) == null ? void 0 : _e.emit("PROVIDER_READY" /* Ready */, { clientName, providerName }); | ||
} | ||
if (clientName) { | ||
this._clientProviders.set(clientName, provider); | ||
} else { | ||
this._defaultProvider = provider; | ||
} | ||
this.transferListeners(oldProvider, provider, clientName, emitters); | ||
if (![...this._clientProviders.values(), this._defaultProvider].includes(oldProvider)) { | ||
(_f = oldProvider == null ? void 0 : oldProvider.onClose) == null ? void 0 : _f.call(oldProvider); | ||
} | ||
return initializationPromise; | ||
} | ||
getProviderForClient(name) { | ||
var _a; | ||
if (!name) { | ||
return this._defaultProvider; | ||
} | ||
return (_a = this._clientProviders.get(name)) != null ? _a : this._defaultProvider; | ||
} | ||
buildAndCacheEventEmitterForClient(name) { | ||
const emitter = this._clientEvents.get(name); | ||
if (emitter) { | ||
return emitter; | ||
} | ||
const newEmitter = new InternalEventEmitter(() => this._logger); | ||
this._clientEvents.set(name, newEmitter); | ||
const clientProvider = this.getProviderForClient(name); | ||
Object.values(ProviderEvents).forEach( | ||
(eventType) => { | ||
var _a; | ||
return (_a = clientProvider.events) == null ? void 0 : _a.addHandler(eventType, (details) => __async(this, null, function* () { | ||
newEmitter.emit(eventType, __spreadProps(__spreadValues({}, details), { clientName: name, providerName: clientProvider.metadata.name })); | ||
})); | ||
} | ||
); | ||
return newEmitter; | ||
} | ||
getUnboundEmitters() { | ||
const namedProviders = [...this._clientProviders.keys()]; | ||
const eventEmitterNames = [...this._clientEvents.keys()].filter(isDefined); | ||
const unboundEmitterNames = eventEmitterNames.filter((name) => !namedProviders.includes(name)); | ||
return [ | ||
// all unbound, named emitters | ||
...unboundEmitterNames.map((name) => this._clientEvents.get(name)), | ||
// the default emitter | ||
this._clientEvents.get(void 0) | ||
].filter(isDefined); | ||
} | ||
getAssociatedEventEmitters(clientName) { | ||
return clientName ? [this.buildAndCacheEventEmitterForClient(clientName)] : this.getUnboundEmitters(); | ||
} | ||
transferListeners(oldProvider, newProvider, clientName, emitters) { | ||
var _a; | ||
(_a = this._clientEventHandlers.get(clientName)) == null ? void 0 : _a.forEach((eventHandler) => { | ||
var _a2; | ||
return (_a2 = oldProvider.events) == null ? void 0 : _a2.removeHandler(...eventHandler); | ||
}); | ||
const newClientHandlers = Object.values(ProviderEvents).map((eventType) => { | ||
const handler = (details) => __async(this, null, function* () { | ||
emitters.forEach((emitter) => { | ||
emitter == null ? void 0 : emitter.emit(eventType, __spreadProps(__spreadValues({}, details), { clientName, providerName: newProvider.metadata.name })); | ||
}); | ||
this._events.emit(eventType, __spreadProps(__spreadValues({}, details), { clientName, providerName: newProvider.metadata.name })); | ||
}); | ||
return [eventType, handler]; | ||
}); | ||
this._clientEventHandlers.set(clientName, newClientHandlers); | ||
newClientHandlers.forEach((eventHandler) => { | ||
var _a2; | ||
return (_a2 = newProvider.events) == null ? void 0 : _a2.addHandler(...eventHandler); | ||
}); | ||
} | ||
close() { | ||
return __async(this, null, function* () { | ||
var _a, _b; | ||
try { | ||
yield (_b = (_a = this == null ? void 0 : this._defaultProvider) == null ? void 0 : _a.onClose) == null ? void 0 : _b.call(_a); | ||
} catch (err) { | ||
this.handleShutdownError(this._defaultProvider, err); | ||
} | ||
const providers = Array.from(this._clientProviders); | ||
yield Promise.all( | ||
providers.map((_0) => __async(this, [_0], function* ([, provider]) { | ||
var _a2; | ||
try { | ||
yield (_a2 = provider.onClose) == null ? void 0 : _a2.call(provider); | ||
} catch (err) { | ||
this.handleShutdownError(provider, err); | ||
} | ||
})) | ||
); | ||
}); | ||
} | ||
clearProvidersAndSetDefault(defaultProvider) { | ||
return __async(this, null, function* () { | ||
try { | ||
yield this.close(); | ||
} catch (err) { | ||
this._logger.error("Unable to cleanly close providers. Resetting to the default configuration."); | ||
} finally { | ||
this._clientProviders.clear(); | ||
this._defaultProvider = defaultProvider; | ||
} | ||
}); | ||
} | ||
handleShutdownError(provider, err) { | ||
this._logger.error(`Error during shutdown of provider ${provider.metadata.name}: ${err}`); | ||
this._logger.error(err == null ? void 0 : err.stack); | ||
} | ||
}; | ||
// src/provider/no-op-provider.ts | ||
import { ProviderStatus } from "@openfeature/core"; | ||
var REASON_NO_OP = "No-op"; | ||
@@ -977,3 +475,3 @@ var NoopFeatureProvider = class { | ||
get status() { | ||
return "NOT_READY" /* NOT_READY */; | ||
return ProviderStatus.NOT_READY; | ||
} | ||
@@ -1094,4 +592,4 @@ resolveBooleanEvaluation(_, defaultValue) { | ||
this.emitterAccessor().addHandler(eventType, handler); | ||
const providerReady = !this._provider.status || this._provider.status === "READY" /* READY */; | ||
if (eventType === "PROVIDER_READY" /* Ready */ && providerReady) { | ||
const providerReady = !this._provider.status || this._provider.status === ProviderStatus2.READY; | ||
if (eventType === ProviderEvents.Ready && providerReady) { | ||
try { | ||
@@ -1193,3 +691,3 @@ handler({ clientName: this.metadata.name, providerName: this._provider.metadata.name }); | ||
const errorMessage = err == null ? void 0 : err.message; | ||
const errorCode = (err == null ? void 0 : err.code) || "GENERAL" /* GENERAL */; | ||
const errorCode = (err == null ? void 0 : err.code) || ErrorCode.GENERAL; | ||
this.errorHooks(allHooksReversed, hookContext, err, options); | ||
@@ -1257,30 +755,11 @@ return { | ||
}; | ||
// src/index.ts | ||
export * from "@openfeature/core"; | ||
export { | ||
DefaultLogger, | ||
ErrorCode, | ||
FlagNotFoundError, | ||
GeneralError, | ||
InternalEventEmitter, | ||
InvalidContextError, | ||
LOG_LEVELS, | ||
NOOP_PROVIDER, | ||
OpenFeature, | ||
OpenFeatureAPI, | ||
OpenFeatureClient, | ||
OpenFeatureCommonAPI, | ||
OpenFeatureError, | ||
OpenFeatureEventEmitter, | ||
ParseError, | ||
ProviderEvents, | ||
ProviderStatus, | ||
SafeLogger, | ||
StandardResolutionReasons, | ||
TargetingKeyMissingError, | ||
TypeMismatchError, | ||
isObject, | ||
isString, | ||
objectOrUndefined, | ||
statusMatchesEvent, | ||
stringOrUndefined | ||
OpenFeatureClient | ||
}; | ||
//# sourceMappingURL=index.js.map |
@@ -1,574 +0,4 @@ | ||
type FlagValueType = 'boolean' | 'string' | 'number' | 'object'; | ||
type PrimitiveValue = null | boolean | string | number; | ||
type JsonObject = { | ||
[key: string]: JsonValue; | ||
}; | ||
type JsonArray = JsonValue[]; | ||
/** | ||
* Represents a JSON node value. | ||
*/ | ||
type JsonValue = PrimitiveValue | JsonObject | JsonArray; | ||
/** | ||
* Represents a JSON node value, or Date. | ||
*/ | ||
type FlagValue = boolean | string | number | JsonValue; | ||
type ResolutionReason = keyof typeof StandardResolutionReasons | (string & Record<never, never>); | ||
/** | ||
* A structure which supports definition of arbitrary properties, with keys of type string, and values of type boolean, string, or number. | ||
* | ||
* This structure is populated by a provider for use by an Application Author (via the Evaluation API) or an Application Integrator (via hooks). | ||
*/ | ||
type FlagMetadata = Record<string, string | number | boolean>; | ||
type ResolutionDetails<U> = { | ||
value: U; | ||
variant?: string; | ||
flagMetadata?: FlagMetadata; | ||
reason?: ResolutionReason; | ||
errorCode?: ErrorCode; | ||
errorMessage?: string; | ||
}; | ||
type EvaluationDetails<T extends FlagValue> = { | ||
flagKey: string; | ||
flagMetadata: Readonly<FlagMetadata>; | ||
} & ResolutionDetails<T>; | ||
declare const StandardResolutionReasons: { | ||
/** | ||
* The resolved value was the result of a dynamic evaluation, such as a rule or specific user-targeting. | ||
*/ | ||
readonly TARGETING_MATCH: "TARGETING_MATCH"; | ||
/** | ||
* The resolved value was the result of pseudorandom assignment. | ||
*/ | ||
readonly SPLIT: "SPLIT"; | ||
/** | ||
* The resolved value was the result of the flag being disabled in the management system. | ||
*/ | ||
readonly DISABLED: "DISABLED"; | ||
/** | ||
* The resolved value was configured statically, or otherwise fell back to a pre-configured value. | ||
*/ | ||
readonly DEFAULT: "DEFAULT"; | ||
/** | ||
* The reason for the resolved value could not be determined. | ||
*/ | ||
readonly UNKNOWN: "UNKNOWN"; | ||
/** | ||
* The resolved value is static (no dynamic evaluation). | ||
*/ | ||
readonly STATIC: "STATIC"; | ||
/** | ||
* The resolved value was retrieved from cache. | ||
*/ | ||
readonly CACHED: "CACHED"; | ||
/** | ||
* The resolved value was the result of an error. | ||
* | ||
* Note: The `errorCode` and `errorMessage` fields may contain additional details of this error. | ||
*/ | ||
readonly ERROR: "ERROR"; | ||
}; | ||
declare enum ErrorCode { | ||
/** | ||
* The value was resolved before the provider was ready. | ||
*/ | ||
PROVIDER_NOT_READY = "PROVIDER_NOT_READY", | ||
/** | ||
* The flag could not be found. | ||
*/ | ||
FLAG_NOT_FOUND = "FLAG_NOT_FOUND", | ||
/** | ||
* An error was encountered parsing data, such as a flag configuration. | ||
*/ | ||
PARSE_ERROR = "PARSE_ERROR", | ||
/** | ||
* The type of the flag value does not match the expected type. | ||
*/ | ||
TYPE_MISMATCH = "TYPE_MISMATCH", | ||
/** | ||
* The provider requires a targeting key and one was not provided in the evaluation context. | ||
*/ | ||
TARGETING_KEY_MISSING = "TARGETING_KEY_MISSING", | ||
/** | ||
* The evaluation context does not meet provider requirements. | ||
*/ | ||
INVALID_CONTEXT = "INVALID_CONTEXT", | ||
/** | ||
* An error with an unspecified code. | ||
*/ | ||
GENERAL = "GENERAL" | ||
} | ||
import { Hook, HookHints, EvaluationDetails, JsonValue, EvaluationLifeCycle, ManageLogger, Eventing, ClientMetadata, CommonProvider, EvaluationContext, Logger, ResolutionDetails, ProviderStatus, OpenFeatureEventEmitter, ProviderEvents, EventHandler, FlagValue, OpenFeatureCommonAPI, ManageContext } from '@openfeature/core'; | ||
export * from '@openfeature/core'; | ||
type EvaluationContextValue = PrimitiveValue | Date | { | ||
[key: string]: EvaluationContextValue; | ||
} | EvaluationContextValue[]; | ||
/** | ||
* A container for arbitrary contextual data that can be used as a basis for dynamic evaluation | ||
*/ | ||
type EvaluationContext = { | ||
/** | ||
* A string uniquely identifying the subject (end-user, or client service) of a flag evaluation. | ||
* Providers may require this field for fractional flag evaluation, rules, or overrides targeting specific users. | ||
* Such providers may behave unpredictably if a targeting key is not specified at flag resolution. | ||
*/ | ||
targetingKey?: string; | ||
} & Record<string, EvaluationContextValue>; | ||
interface ManageContext<T> { | ||
/** | ||
* Access the evaluation context set on the receiver. | ||
* @returns {EvaluationContext} Evaluation context | ||
*/ | ||
getContext(): EvaluationContext; | ||
/** | ||
* Sets evaluation context that will be used during flag evaluations | ||
* on this receiver. | ||
* @template T The type of the receiver | ||
* @param {EvaluationContext} context Evaluation context | ||
* @returns {T} The receiver (this object) | ||
*/ | ||
setContext(context: EvaluationContext): T; | ||
} | ||
declare enum ProviderEvents { | ||
/** | ||
* The provider is ready to evaluate flags. | ||
*/ | ||
Ready = "PROVIDER_READY", | ||
/** | ||
* The provider is in an error state. | ||
*/ | ||
Error = "PROVIDER_ERROR", | ||
/** | ||
* The flag configuration in the source-of-truth has changed. | ||
*/ | ||
ConfigurationChanged = "PROVIDER_CONFIGURATION_CHANGED", | ||
/** | ||
* The provider's cached state is no longer valid and may not be up-to-date with the source of truth. | ||
*/ | ||
Stale = "PROVIDER_STALE" | ||
} | ||
/** | ||
* Returns true if the provider's status corresponds to the event. | ||
* If the provider's status is not defined, it matches READY. | ||
* @param {ProviderEvents} event event to match | ||
* @param {ProviderStatus} status status of provider | ||
* @returns {boolean} boolean indicating if the provider status corresponds to the event. | ||
*/ | ||
declare const statusMatchesEvent: (event: ProviderEvents, status?: ProviderStatus) => boolean; | ||
type EventMetadata = { | ||
[key: string]: string | boolean | number; | ||
}; | ||
type CommonEventDetails = { | ||
providerName: string; | ||
clientName?: string; | ||
}; | ||
type CommonEventProps = { | ||
message?: string; | ||
metadata?: EventMetadata; | ||
}; | ||
type ReadyEvent = CommonEventProps; | ||
type ErrorEvent = CommonEventProps; | ||
type StaleEvent = CommonEventProps; | ||
type ConfigChangeEvent = CommonEventProps & { | ||
flagsChanged?: string[]; | ||
}; | ||
type EventMap = { | ||
[ProviderEvents.Ready]: ReadyEvent; | ||
[ProviderEvents.Error]: ErrorEvent; | ||
[ProviderEvents.Stale]: StaleEvent; | ||
[ProviderEvents.ConfigurationChanged]: ConfigChangeEvent; | ||
}; | ||
type EventContext<T extends ProviderEvents, U extends Record<string, unknown> = Record<string, unknown>> = EventMap[T] & U; | ||
type EventDetails<T extends ProviderEvents> = EventContext<T> & CommonEventDetails; | ||
type EventHandler<T extends ProviderEvents> = (eventDetails?: EventDetails<T>) => Promise<unknown> | unknown; | ||
interface Eventing { | ||
/** | ||
* Adds a handler for the given provider event type. | ||
* The handlers are called in the order they have been added. | ||
* @param {ProviderEvents} eventType The provider event type to listen to | ||
* @param {EventHandler} handler The handler to run on occurrence of the event type | ||
*/ | ||
addHandler<T extends ProviderEvents>(eventType: T, handler: EventHandler<T>): void; | ||
/** | ||
* Removes a handler for the given provider event type. | ||
* @param {ProviderEvents} eventType The provider event type to remove the listener for | ||
* @param {EventHandler} handler The handler to remove for the provider event type | ||
*/ | ||
removeHandler<T extends ProviderEvents>(eventType: T, handler: EventHandler<T>): void; | ||
/** | ||
* Gets the current handlers for the given provider event type. | ||
* @param {ProviderEvents} eventType The provider event type to get the current handlers for | ||
* @returns {EventHandler[]} The handlers currently attached to the given provider event type | ||
*/ | ||
getHandlers<T extends ProviderEvents>(eventType: T): EventHandler<T>[]; | ||
} | ||
interface Logger { | ||
error(...args: unknown[]): void; | ||
warn(...args: unknown[]): void; | ||
info(...args: unknown[]): void; | ||
debug(...args: unknown[]): void; | ||
} | ||
interface ManageLogger<T> { | ||
/** | ||
* Sets a logger on this receiver. This logger supersedes to the global logger | ||
* and is passed to various components in the SDK. | ||
* The logger configured on the global API object will be used for all evaluations, | ||
* unless overridden in a particular client. | ||
* @template T The type of the receiver | ||
* @param {Logger} logger The logger to be used | ||
* @returns {T} The receiver (this object) | ||
*/ | ||
setLogger(logger: Logger): T; | ||
} | ||
declare class DefaultLogger implements Logger { | ||
error(...args: unknown[]): void; | ||
warn(...args: unknown[]): void; | ||
info(): void; | ||
debug(): void; | ||
} | ||
declare const LOG_LEVELS: Array<keyof Logger>; | ||
declare class SafeLogger implements Logger { | ||
private readonly logger; | ||
private readonly fallbackLogger; | ||
constructor(logger: Logger); | ||
error(...args: unknown[]): void; | ||
warn(...args: unknown[]): void; | ||
info(...args: unknown[]): void; | ||
debug(...args: unknown[]): void; | ||
private log; | ||
} | ||
declare abstract class GenericEventEmitter<AdditionalContext extends Record<string, unknown> = Record<string, unknown>> implements ManageLogger<GenericEventEmitter<AdditionalContext>> { | ||
private readonly globalLogger?; | ||
private readonly _handlers; | ||
private readonly eventEmitter; | ||
private _eventLogger?; | ||
constructor(globalLogger?: (() => Logger) | undefined); | ||
emit<T extends ProviderEvents>(eventType: T, context?: EventContext<T, AdditionalContext>): void; | ||
addHandler<T extends ProviderEvents>(eventType: T, handler: EventHandler<T>): void; | ||
removeHandler<T extends ProviderEvents>(eventType: T, handler: EventHandler<T>): void; | ||
removeAllHandlers(eventType?: ProviderEvents): void; | ||
getHandlers<T extends ProviderEvents>(eventType: T): EventHandler<T>[]; | ||
setLogger(logger: Logger): this; | ||
private get _logger(); | ||
} | ||
/** | ||
* The OpenFeatureEventEmitter can be used by provider developers to emit | ||
* events at various parts of the provider lifecycle. | ||
* | ||
* NOTE: Ready and error events are automatically emitted by the SDK based on | ||
* the result of the initialize method. | ||
*/ | ||
declare class OpenFeatureEventEmitter extends GenericEventEmitter { | ||
} | ||
/** | ||
* The InternalEventEmitter should only be used within the SDK. It extends the | ||
* OpenFeatureEventEmitter to include additional properties that can be included | ||
* in the event details. | ||
*/ | ||
declare class InternalEventEmitter extends GenericEventEmitter<CommonEventDetails> { | ||
} | ||
interface Metadata { | ||
} | ||
/** | ||
* Defines where the library is intended to be run. | ||
*/ | ||
type Paradigm = 'server' | 'client'; | ||
/** | ||
* The state of the provider. | ||
*/ | ||
declare enum ProviderStatus { | ||
/** | ||
* The provider has not been initialized and cannot yet evaluate flags. | ||
*/ | ||
NOT_READY = "NOT_READY", | ||
/** | ||
* The provider is ready to resolve flags. | ||
*/ | ||
READY = "READY", | ||
/** | ||
* The provider is in an error state and unable to evaluate flags. | ||
*/ | ||
ERROR = "ERROR", | ||
/** | ||
* The provider's cached state is no longer valid and may not be up-to-date with the source of truth. | ||
*/ | ||
STALE = "STALE" | ||
} | ||
/** | ||
* Static data about the provider. | ||
*/ | ||
interface ProviderMetadata extends Metadata { | ||
readonly name: string; | ||
} | ||
interface CommonProvider { | ||
readonly metadata: ProviderMetadata; | ||
/** | ||
* Represents where the provider is intended to be run. If defined, | ||
* the SDK will enforce that the defined paradigm at runtime. | ||
*/ | ||
readonly runsOn?: Paradigm; | ||
/** | ||
* Returns a representation of the current readiness of the provider. | ||
* If the provider needs to be initialized, it should return {@link ProviderStatus.READY}. | ||
* If the provider is in an error state, it should return {@link ProviderStatus.ERROR}. | ||
* If the provider is functioning normally, it should return {@link ProviderStatus.NOT_READY}. | ||
* | ||
* _Providers which do not implement this method are assumed to be ready immediately._ | ||
*/ | ||
readonly status?: ProviderStatus; | ||
/** | ||
* An event emitter for ProviderEvents. | ||
* @see ProviderEvents | ||
*/ | ||
events?: OpenFeatureEventEmitter; | ||
/** | ||
* A function used to shut down the provider. | ||
* Called when this provider is replaced with a new one, or when the OpenFeature is shut down. | ||
*/ | ||
onClose?(): Promise<void>; | ||
/** | ||
* A function used to setup the provider. | ||
* Called by the SDK after the provider is set if the provider's status is {@link ProviderStatus.NOT_READY}. | ||
* When the returned promise resolves, the SDK fires the ProviderEvents.Ready event. | ||
* If the returned promise rejects, the SDK fires the ProviderEvents.Error event. | ||
* Use this function to perform any context-dependent setup within the provider. | ||
* @param context | ||
*/ | ||
initialize?(context?: EvaluationContext): Promise<void>; | ||
} | ||
interface ClientMetadata extends Metadata { | ||
readonly version?: string; | ||
readonly name?: string; | ||
readonly providerMetadata: ProviderMetadata; | ||
} | ||
type HookHints = Readonly<Record<string, unknown>>; | ||
interface HookContext<T extends FlagValue = FlagValue> { | ||
readonly flagKey: string; | ||
readonly defaultValue: T; | ||
readonly flagValueType: FlagValueType; | ||
readonly context: Readonly<EvaluationContext>; | ||
readonly clientMetadata: ClientMetadata; | ||
readonly providerMetadata: ProviderMetadata; | ||
readonly logger: Logger; | ||
} | ||
interface BeforeHookContext extends HookContext { | ||
context: EvaluationContext; | ||
} | ||
interface Hook<T extends FlagValue = FlagValue> { | ||
/** | ||
* Runs before flag values are resolved from the provider. | ||
* If an EvaluationContext is returned, it will be merged with the pre-existing EvaluationContext. | ||
* @param hookContext | ||
* @param hookHints | ||
*/ | ||
before?(hookContext: BeforeHookContext, hookHints?: HookHints): Promise<EvaluationContext | void> | EvaluationContext | void; | ||
/** | ||
* Runs after flag values are successfully resolved from the provider. | ||
* @param hookContext | ||
* @param evaluationDetails | ||
* @param hookHints | ||
*/ | ||
after?(hookContext: Readonly<HookContext<T>>, evaluationDetails: EvaluationDetails<T>, hookHints?: HookHints): Promise<void> | void; | ||
/** | ||
* Runs in the event of an unhandled error or promise rejection during flag resolution, or any attached hooks. | ||
* @param hookContext | ||
* @param error | ||
* @param hookHints | ||
*/ | ||
error?(hookContext: Readonly<HookContext<T>>, error: unknown, hookHints?: HookHints): Promise<void> | void; | ||
/** | ||
* Runs after all other hook stages, regardless of success or error. | ||
* Errors thrown here are unhandled by the client and will surface in application code. | ||
* @param hookContext | ||
* @param hookHints | ||
*/ | ||
finally?(hookContext: Readonly<HookContext<T>>, hookHints?: HookHints): Promise<void> | void; | ||
} | ||
interface EvaluationLifeCycle<T> { | ||
/** | ||
* Adds hooks that will run during flag evaluations on this receiver. | ||
* Hooks are executed in the order they were registered. Adding additional hooks | ||
* will not remove existing hooks. | ||
* Hooks registered on the global API object run with all evaluations. | ||
* Hooks registered on the client run with all evaluations on that client. | ||
* @template T The type of the receiver | ||
* @param {Hook<FlagValue>[]} hooks A list of hooks that should always run | ||
* @returns {T} The receiver (this object) | ||
*/ | ||
addHooks(...hooks: Hook<FlagValue>[]): T; | ||
/** | ||
* Access all the hooks that are registered on this receiver. | ||
* @returns {Hook<FlagValue>[]} A list of the client hooks | ||
*/ | ||
getHooks(): Hook<FlagValue>[]; | ||
/** | ||
* Clears all the hooks that are registered on this receiver. | ||
* @template T The type of the receiver | ||
* @returns {T} The receiver (this object) | ||
*/ | ||
clearHooks(): T; | ||
} | ||
declare abstract class OpenFeatureError extends Error { | ||
abstract code: ErrorCode; | ||
constructor(message?: string); | ||
} | ||
declare class GeneralError extends OpenFeatureError { | ||
code: ErrorCode; | ||
constructor(message?: string); | ||
} | ||
declare class FlagNotFoundError extends OpenFeatureError { | ||
code: ErrorCode; | ||
constructor(message?: string); | ||
} | ||
declare class ParseError extends OpenFeatureError { | ||
code: ErrorCode; | ||
constructor(message?: string); | ||
} | ||
declare class TypeMismatchError extends OpenFeatureError { | ||
code: ErrorCode; | ||
constructor(message?: string); | ||
} | ||
declare class TargetingKeyMissingError extends OpenFeatureError { | ||
code: ErrorCode; | ||
constructor(message?: string); | ||
} | ||
declare class InvalidContextError extends OpenFeatureError { | ||
code: ErrorCode; | ||
constructor(message?: string); | ||
} | ||
/** | ||
* Checks whether the parameter is a string. | ||
* @param {unknown} value The value to check | ||
* @returns {value is string} True if the value is a string | ||
*/ | ||
declare function isString(value: unknown): value is string; | ||
/** | ||
* Returns the parameter if it is a string, otherwise returns undefined. | ||
* @param {unknown} value The value to check | ||
* @returns {string|undefined} The parameter if it is a string, otherwise undefined | ||
*/ | ||
declare function stringOrUndefined(value: unknown): string | undefined; | ||
/** | ||
* Checks whether the parameter is an object. | ||
* @param {unknown} value The value to check | ||
* @returns {value is string} True if the value is an object | ||
*/ | ||
declare function isObject<T extends object>(value: unknown): value is T; | ||
/** | ||
* Returns the parameter if it is an object, otherwise returns undefined. | ||
* @param {unknown} value The value to check | ||
* @returns {object|undefined} The parameter if it is an object, otherwise undefined | ||
*/ | ||
declare function objectOrUndefined<T extends object>(value: unknown): T | undefined; | ||
declare abstract class OpenFeatureCommonAPI<P extends CommonProvider = CommonProvider> implements Eventing, EvaluationLifeCycle<OpenFeatureCommonAPI<P>>, ManageLogger<OpenFeatureCommonAPI<P>> { | ||
protected _hooks: Hook[]; | ||
protected _context: EvaluationContext; | ||
protected _logger: Logger; | ||
protected abstract _defaultProvider: P; | ||
private readonly _events; | ||
private readonly _clientEventHandlers; | ||
protected _clientProviders: Map<string, P>; | ||
protected _clientEvents: Map<string | undefined, InternalEventEmitter>; | ||
protected _runsOn: Paradigm; | ||
constructor(category: Paradigm); | ||
addHooks(...hooks: Hook<FlagValue>[]): this; | ||
getHooks(): Hook<FlagValue>[]; | ||
clearHooks(): this; | ||
setLogger(logger: Logger): this; | ||
/** | ||
* Get metadata about registered provider. | ||
* @returns {ProviderMetadata} Provider Metadata | ||
*/ | ||
get providerMetadata(): ProviderMetadata; | ||
/** | ||
* Adds a handler for the given provider event type. | ||
* The handlers are called in the order they have been added. | ||
* API (global) events run for all providers. | ||
* @param {ProviderEvents} eventType The provider event type to listen to | ||
* @param {EventHandler} handler The handler to run on occurrence of the event type | ||
*/ | ||
addHandler<T extends ProviderEvents>(eventType: T, handler: EventHandler<T>): void; | ||
/** | ||
* Removes a handler for the given provider event type. | ||
* @param {ProviderEvents} eventType The provider event type to remove the listener for | ||
* @param {EventHandler} handler The handler to remove for the provider event type | ||
*/ | ||
removeHandler<T extends ProviderEvents>(eventType: T, handler: EventHandler<T>): void; | ||
/** | ||
* Gets the current handlers for the given provider event type. | ||
* @param {ProviderEvents} eventType The provider event type to get the current handlers for | ||
* @returns {EventHandler[]} The handlers currently attached to the given provider event type | ||
*/ | ||
getHandlers<T extends ProviderEvents>(eventType: T): EventHandler<T>[]; | ||
/** | ||
* Sets the default provider for flag evaluations and returns a promise that resolves when the provider is ready. | ||
* This provider will be used by unnamed clients and named clients to which no provider is bound. | ||
* Setting a provider supersedes the current provider used in new and existing clients without a name. | ||
* @template P | ||
* @param {P} provider The provider responsible for flag evaluations. | ||
* @returns {Promise<void>} | ||
* @throws Uncaught exceptions thrown by the provider during initialization. | ||
*/ | ||
setProviderAndWait(provider: P): Promise<void>; | ||
/** | ||
* Sets the provider that OpenFeature will use for flag evaluations of providers with the given name. | ||
* A promise is returned that resolves when the provider is ready. | ||
* Setting a provider supersedes the current provider used in new and existing clients with that name. | ||
* @template P | ||
* @param {string} clientName The name to identify the client | ||
* @param {P} provider The provider responsible for flag evaluations. | ||
* @returns {Promise<void>} | ||
* @throws Uncaught exceptions thrown by the provider during initialization. | ||
*/ | ||
setProviderAndWait(clientName: string, provider: P): Promise<void>; | ||
/** | ||
* Sets the default provider for flag evaluations. | ||
* This provider will be used by unnamed clients and named clients to which no provider is bound. | ||
* Setting a provider supersedes the current provider used in new and existing clients without a name. | ||
* @template P | ||
* @param {P} provider The provider responsible for flag evaluations. | ||
* @returns {this} OpenFeature API | ||
*/ | ||
setProvider(provider: P): this; | ||
/** | ||
* Sets the provider that OpenFeature will use for flag evaluations of providers with the given name. | ||
* Setting a provider supersedes the current provider used in new and existing clients with that name. | ||
* @template P | ||
* @param {string} clientName The name to identify the client | ||
* @param {P} provider The provider responsible for flag evaluations. | ||
* @returns {this} OpenFeature API | ||
*/ | ||
setProvider(clientName: string, provider: P): this; | ||
private setAwaitableProvider; | ||
protected getProviderForClient(name?: string): P; | ||
protected buildAndCacheEventEmitterForClient(name?: string): InternalEventEmitter; | ||
private getUnboundEmitters; | ||
private getAssociatedEventEmitters; | ||
private transferListeners; | ||
close(): Promise<void>; | ||
protected clearProvidersAndSetDefault(defaultProvider: P): Promise<void>; | ||
private handleShutdownError; | ||
} | ||
interface FlagEvaluationOptions { | ||
@@ -788,2 +218,2 @@ hooks?: Hook[]; | ||
export { BeforeHookContext, Client, ClientMetadata, CommonEventDetails, CommonProvider, ConfigChangeEvent, DefaultLogger, ErrorCode, ErrorEvent, EvaluationContext, EvaluationContextValue, EvaluationDetails, EvaluationLifeCycle, EventContext, EventDetails, EventHandler, EventMetadata, Eventing, Features, FlagEvaluationOptions, FlagMetadata, FlagNotFoundError, FlagValue, FlagValueType, GeneralError, Hook, HookContext, HookHints, InternalEventEmitter, InvalidContextError, JsonArray, JsonObject, JsonValue, LOG_LEVELS, Logger, ManageContext, ManageLogger, Metadata, NOOP_PROVIDER, OpenFeature, OpenFeatureAPI, OpenFeatureClient, OpenFeatureCommonAPI, OpenFeatureError, OpenFeatureEventEmitter, Paradigm, ParseError, PrimitiveValue, Provider, ProviderEvents, ProviderMetadata, ProviderStatus, ReadyEvent, ResolutionDetails, ResolutionReason, SafeLogger, StaleEvent, StandardResolutionReasons, TargetingKeyMissingError, TypeMismatchError, isObject, isString, objectOrUndefined, statusMatchesEvent, stringOrUndefined }; | ||
export { Client, Features, FlagEvaluationOptions, NOOP_PROVIDER, OpenFeature, OpenFeatureAPI, OpenFeatureClient, Provider }; |
{ | ||
"name": "@openfeature/web-sdk", | ||
"version": "0.4.2", | ||
"version": "0.4.3", | ||
"description": "OpenFeature SDK for Web", | ||
@@ -20,4 +20,4 @@ "main": "./dist/cjs/index.js", | ||
"clean": "shx rm -rf ./dist", | ||
"build:web-esm": "esbuild src/index.ts --bundle --sourcemap --target=es2016 --platform=browser --format=esm --outfile=./dist/esm/index.js --analyze", | ||
"build:web-cjs": "esbuild src/index.ts --bundle --sourcemap --target=es2016 --platform=browser --format=cjs --outfile=./dist/cjs/index.js --analyze", | ||
"build:web-esm": "esbuild src/index.ts --bundle --external:@openfeature/core --sourcemap --target=es2016 --platform=browser --format=esm --outfile=./dist/esm/index.js --analyze", | ||
"build:web-cjs": "esbuild src/index.ts --bundle --external:@openfeature/core --sourcemap --target=es2016 --platform=browser --format=cjs --outfile=./dist/cjs/index.js --analyze", | ||
"build:rollup-types": "rollup -c ../../rollup.config.mjs", | ||
@@ -29,3 +29,3 @@ "build": "npm run clean && npm run build:web-esm && npm run build:web-cjs && npm run build:rollup-types", | ||
"publish-if-not-exists": "cp $NPM_CONFIG_USERCONFIG .npmrc && if [ \"$(npm show $npm_package_name@$npm_package_version version)\" = \"$(npm run current-version -s)\" ]; then echo 'already published, skipping'; else npm publish --access public; fi", | ||
"docs": "typedoc" | ||
"update-core-peer": "npm install --save-peer --save-exact @openfeature/core@$OPENFEATURE_CORE_VERSION" | ||
}, | ||
@@ -50,9 +50,8 @@ "repository": { | ||
"homepage": "https://github.com/open-feature/js-sdk#readme", | ||
"peerDependencies": { | ||
"@openfeature/core": "0.0.16" | ||
}, | ||
"devDependencies": { | ||
"@openfeature/shared": "*" | ||
}, | ||
"typedoc": { | ||
"displayName": "OpenFeature Web SDK", | ||
"entryPoint": "./src/index.ts" | ||
"@openfeature/core": "0.0.16" | ||
} | ||
} |
@@ -19,4 +19,4 @@ <!-- markdownlint-disable MD033 --> | ||
<!-- x-release-please-start-version --> | ||
<a href="https://github.com/open-feature/js-sdk/releases/tag/web-sdk-v0.4.2"> | ||
<img alt="Release" src="https://img.shields.io/static/v1?label=release&message=v0.4.2&color=blue&style=for-the-badge" /> | ||
<a href="https://github.com/open-feature/js-sdk/releases/tag/web-sdk-v0.4.3"> | ||
<img alt="Release" src="https://img.shields.io/static/v1?label=release&message=v0.4.3&color=blue&style=for-the-badge" /> | ||
</a> | ||
@@ -28,3 +28,3 @@ <!-- x-release-please-end --> | ||
</a> | ||
<a href="https://open-feature.github.io/js-sdk/modules/OpenFeature_Web_SDK.html"> | ||
<a href="https://open-feature.github.io/js-sdk/modules/_openfeature_web_sdk.html"> | ||
<img alt="API Reference" src="https://img.shields.io/badge/reference-teal?logo=javascript&logoColor=white" /> | ||
@@ -89,3 +89,3 @@ </a> | ||
See [here](https://open-feature.github.io/js-sdk/modules/OpenFeature_Web_SDK.html) for the complete API documentation. | ||
See [here](https://open-feature.github.io/js-sdk/modules/_openfeature_web_sdk.html) for the complete API documentation. | ||
@@ -92,0 +92,0 @@ ## 🌟 Features |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
180202
1
1693