@openfeature/server-sdk
Advanced tools
Comparing version 1.9.1 to 1.10.0
"use strict"; | ||
var __create = Object.create; | ||
var __defProp = Object.defineProperty; | ||
var __defProps = Object.defineProperties; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropDescs = Object.getOwnPropertyDescriptors; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __getOwnPropSymbols = Object.getOwnPropertySymbols; | ||
var __getProtoOf = Object.getPrototypeOf; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __propIsEnum = Object.prototype.propertyIsEnumerable; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __spreadValues = (a, b) => { | ||
for (var prop in b || (b = {})) | ||
if (__hasOwnProp.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
if (__getOwnPropSymbols) | ||
for (var prop of __getOwnPropSymbols(b)) { | ||
if (__propIsEnum.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
} | ||
return a; | ||
}; | ||
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); | ||
var __export = (target, all) => { | ||
@@ -47,22 +30,2 @@ for (var name in all) | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
var __async = (__this, __arguments, generator) => { | ||
return new Promise((resolve, reject) => { | ||
var fulfilled = (value) => { | ||
try { | ||
step(generator.next(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}; | ||
var rejected = (value) => { | ||
try { | ||
step(generator.throw(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}; | ||
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); | ||
step((generator = generator.apply(__this, __arguments)).next()); | ||
}); | ||
}; | ||
@@ -90,7 +53,5 @@ // src/index.ts | ||
var NoopFeatureProvider = class { | ||
constructor() { | ||
this.metadata = { | ||
name: "No-op Provider" | ||
}; | ||
} | ||
metadata = { | ||
name: "No-op Provider" | ||
}; | ||
get status() { | ||
@@ -126,2 +87,3 @@ return import_core.ProviderStatus.NOT_READY; | ||
var VariantFoundError = class extends import_core2.OpenFeatureError { | ||
code; | ||
constructor(message) { | ||
@@ -137,9 +99,10 @@ super(message); | ||
var InMemoryProvider = class { | ||
events = new OpenFeatureEventEmitter(); | ||
runsOn = "server"; | ||
metadata = { | ||
name: "in-memory" | ||
}; | ||
_flagConfiguration; | ||
constructor(flagConfiguration = {}) { | ||
this.events = new OpenFeatureEventEmitter(); | ||
this.runsOn = "server"; | ||
this.metadata = { | ||
name: "in-memory" | ||
}; | ||
this._flagConfiguration = __spreadValues({}, flagConfiguration); | ||
this._flagConfiguration = { ...flagConfiguration }; | ||
} | ||
@@ -152,3 +115,3 @@ /** | ||
const flagsChanged = Object.entries(flagConfiguration).filter(([key, value]) => this._flagConfiguration[key] !== value).map(([key]) => key); | ||
this._flagConfiguration = __spreadValues({}, flagConfiguration); | ||
this._flagConfiguration = { ...flagConfiguration }; | ||
this.events.emit(import_core5.ServerProviderEvents.ConfigurationChanged, { flagsChanged }); | ||
@@ -162,33 +125,26 @@ } | ||
} | ||
resolveStringEvaluation(flagKey, defaultValue, context, logger) { | ||
return __async(this, null, function* () { | ||
return this.resolveFlagWithReason(flagKey, defaultValue, context, logger); | ||
}); | ||
async resolveStringEvaluation(flagKey, defaultValue, context, logger) { | ||
return this.resolveFlagWithReason(flagKey, defaultValue, context, logger); | ||
} | ||
resolveObjectEvaluation(flagKey, defaultValue, context, logger) { | ||
return __async(this, null, function* () { | ||
return this.resolveFlagWithReason(flagKey, defaultValue, context, logger); | ||
}); | ||
async resolveObjectEvaluation(flagKey, defaultValue, context, logger) { | ||
return this.resolveFlagWithReason(flagKey, defaultValue, context, logger); | ||
} | ||
resolveFlagWithReason(flagKey, defaultValue, ctx, logger) { | ||
return __async(this, null, function* () { | ||
try { | ||
const resolutionResult = this.lookupFlagValue(flagKey, defaultValue, ctx, logger); | ||
if (typeof (resolutionResult == null ? void 0 : resolutionResult.value) != typeof defaultValue) { | ||
throw new import_core3.TypeMismatchError(); | ||
} | ||
return resolutionResult; | ||
} catch (error) { | ||
if (!(error instanceof import_core3.OpenFeatureError)) { | ||
throw new import_core3.GeneralError((error == null ? void 0 : error.message) || "unknown error"); | ||
} | ||
throw error; | ||
async resolveFlagWithReason(flagKey, defaultValue, ctx, logger) { | ||
try { | ||
const resolutionResult = this.lookupFlagValue(flagKey, defaultValue, ctx, logger); | ||
if (typeof resolutionResult?.value != typeof defaultValue) { | ||
throw new import_core3.TypeMismatchError(); | ||
} | ||
}); | ||
return resolutionResult; | ||
} catch (error) { | ||
if (!(error instanceof import_core3.OpenFeatureError)) { | ||
throw new import_core3.GeneralError(error?.message || "unknown error"); | ||
} | ||
throw error; | ||
} | ||
} | ||
lookupFlagValue(flagKey, defaultValue, ctx, logger) { | ||
var _a; | ||
if (!(flagKey in this._flagConfiguration)) { | ||
const message = `no flag found with key ${flagKey}`; | ||
logger == null ? void 0 : logger.debug(message); | ||
logger?.debug(message); | ||
throw new import_core3.FlagNotFoundError(message); | ||
@@ -200,15 +156,15 @@ } | ||
} | ||
const isContextEval = ctx && (flagSpec == null ? void 0 : flagSpec.contextEvaluator); | ||
const variant = isContextEval ? (_a = flagSpec.contextEvaluator) == null ? void 0 : _a.call(flagSpec, ctx) : flagSpec.defaultVariant; | ||
const value = variant && (flagSpec == null ? void 0 : flagSpec.variants[variant]); | ||
const isContextEval = ctx && flagSpec?.contextEvaluator; | ||
const variant = isContextEval ? flagSpec.contextEvaluator?.(ctx) : flagSpec.defaultVariant; | ||
const value = variant && flagSpec?.variants[variant]; | ||
if (value === void 0) { | ||
const message = `no value associated with variant ${variant}`; | ||
logger == null ? void 0 : logger.error(message); | ||
logger?.error(message); | ||
throw new VariantFoundError(message); | ||
} | ||
return __spreadProps(__spreadValues({ | ||
value | ||
}, variant && { variant }), { | ||
return { | ||
value, | ||
...variant && { variant }, | ||
reason: isContextEval ? import_core3.StandardResolutionReasons.TARGETING_MATCH : import_core3.StandardResolutionReasons.STATIC | ||
}); | ||
}; | ||
} | ||
@@ -235,8 +191,7 @@ }; | ||
var OpenFeatureEventEmitter = class extends import_core4.GenericEventEmitter { | ||
eventEmitter = new import_events.default({ captureRejections: true }); | ||
constructor() { | ||
super(); | ||
this.eventEmitter = new import_events.default({ captureRejections: true }); | ||
this.eventEmitter.on("error", (err) => { | ||
var _a; | ||
(_a = this._logger) == null ? void 0 : _a.error("Error running event handler:", err); | ||
this._logger?.error("Error running event handler:", err); | ||
}); | ||
@@ -253,8 +208,8 @@ } | ||
var OpenFeatureAPI = class extends import_core6.OpenFeatureCommonAPI { | ||
_events = new OpenFeatureEventEmitter(); | ||
_defaultProvider = NOOP_PROVIDER; | ||
_createEventEmitter = () => new OpenFeatureEventEmitter(); | ||
_transactionContextPropagator = NOOP_TRANSACTION_CONTEXT_PROPAGATOR; | ||
constructor() { | ||
super("server"); | ||
this._events = new OpenFeatureEventEmitter(); | ||
this._defaultProvider = NOOP_PROVIDER; | ||
this._createEventEmitter = () => new OpenFeatureEventEmitter(); | ||
this._transactionContextPropagator = NOOP_TRANSACTION_CONTEXT_PROPAGATOR; | ||
} | ||
@@ -283,6 +238,5 @@ /** | ||
getClient(nameOrContext, versionOrContext, contextOrUndefined) { | ||
var _a, _b; | ||
const name = (0, import_core6.stringOrUndefined)(nameOrContext); | ||
const version = (0, import_core6.stringOrUndefined)(versionOrContext); | ||
const context = (_b = (_a = (0, import_core6.objectOrUndefined)(nameOrContext)) != null ? _a : (0, import_core6.objectOrUndefined)(versionOrContext)) != null ? _b : (0, import_core6.objectOrUndefined)(contextOrUndefined); | ||
const context = (0, import_core6.objectOrUndefined)(nameOrContext) ?? (0, import_core6.objectOrUndefined)(versionOrContext) ?? (0, import_core6.objectOrUndefined)(contextOrUndefined); | ||
return new OpenFeatureClient( | ||
@@ -305,5 +259,5 @@ () => this.getProviderForClient(name), | ||
const baseMessage = "Invalid TransactionContextPropagator, will not be set: "; | ||
if (typeof (transactionContextPropagator == null ? void 0 : transactionContextPropagator.getTransactionContext) !== "function") { | ||
if (typeof transactionContextPropagator?.getTransactionContext !== "function") { | ||
this._logger.error(`${baseMessage}: getTransactionContext is not a function.`); | ||
} else if (typeof (transactionContextPropagator == null ? void 0 : transactionContextPropagator.setTransactionContext) !== "function") { | ||
} else if (typeof transactionContextPropagator?.setTransactionContext !== "function") { | ||
this._logger.error(`${baseMessage}: setTransactionContext is not a function.`); | ||
@@ -323,4 +277,4 @@ } else { | ||
const error = err; | ||
this._logger.error(`Error getting transaction context: ${error == null ? void 0 : error.message}, returning empty context.`); | ||
this._logger.error(error == null ? void 0 : error.stack); | ||
this._logger.error(`Error getting transaction context: ${error?.message}, returning empty context.`); | ||
this._logger.error(error?.stack); | ||
return {}; | ||
@@ -339,5 +293,7 @@ } | ||
this.options = options; | ||
this._hooks = []; | ||
this._context = context; | ||
} | ||
_context; | ||
_hooks = []; | ||
_clientLogger; | ||
get metadata() { | ||
@@ -351,3 +307,2 @@ return { | ||
addHandler(eventType, handler) { | ||
var _a; | ||
this.emitterAccessor().addHandler(eventType, handler); | ||
@@ -359,3 +314,3 @@ const shouldRunNow = (0, import_core7.statusMatchesEvent)(eventType, this._provider.status); | ||
} catch (err) { | ||
(_a = this._logger) == null ? void 0 : _a.error("Error running event handler:", err); | ||
this._logger?.error("Error running event handler:", err); | ||
} | ||
@@ -392,6 +347,4 @@ } | ||
} | ||
getBooleanValue(flagKey, defaultValue, context, options) { | ||
return __async(this, null, function* () { | ||
return (yield this.getBooleanDetails(flagKey, defaultValue, context, options)).value; | ||
}); | ||
async getBooleanValue(flagKey, defaultValue, context, options) { | ||
return (await this.getBooleanDetails(flagKey, defaultValue, context, options)).value; | ||
} | ||
@@ -408,6 +361,4 @@ getBooleanDetails(flagKey, defaultValue, context, options) { | ||
} | ||
getStringValue(flagKey, defaultValue, context, options) { | ||
return __async(this, null, function* () { | ||
return (yield this.getStringDetails(flagKey, defaultValue, context, options)).value; | ||
}); | ||
async getStringValue(flagKey, defaultValue, context, options) { | ||
return (await this.getStringDetails(flagKey, defaultValue, context, options)).value; | ||
} | ||
@@ -425,6 +376,4 @@ getStringDetails(flagKey, defaultValue, context, options) { | ||
} | ||
getNumberValue(flagKey, defaultValue, context, options) { | ||
return __async(this, null, function* () { | ||
return (yield this.getNumberDetails(flagKey, defaultValue, context, options)).value; | ||
}); | ||
async getNumberValue(flagKey, defaultValue, context, options) { | ||
return (await this.getNumberDetails(flagKey, defaultValue, context, options)).value; | ||
} | ||
@@ -442,6 +391,4 @@ getNumberDetails(flagKey, defaultValue, context, options) { | ||
} | ||
getObjectValue(flagKey, defaultValue, context, options) { | ||
return __async(this, null, function* () { | ||
return (yield this.getObjectDetails(flagKey, defaultValue, context, options)).value; | ||
}); | ||
async getObjectValue(flagKey, defaultValue, context, options) { | ||
return (await this.getObjectDetails(flagKey, defaultValue, context, options)).value; | ||
} | ||
@@ -451,97 +398,91 @@ getObjectDetails(flagKey, defaultValue, context, options) { | ||
} | ||
evaluate(_0, _1, _2, _3) { | ||
return __async(this, arguments, function* (flagKey, resolver, defaultValue, flagType, invocationContext = {}, options = {}) { | ||
var _a; | ||
const allHooks = [ | ||
...OpenFeature.getHooks(), | ||
...this.getHooks(), | ||
...options.hooks || [], | ||
...this._provider.hooks || [] | ||
]; | ||
const allHooksReversed = [...allHooks].reverse(); | ||
const mergedContext = __spreadValues(__spreadValues(__spreadValues(__spreadValues({}, OpenFeature.getContext()), OpenFeature.getTransactionContext()), this._context), invocationContext); | ||
const hookContext = { | ||
flagKey, | ||
defaultValue, | ||
flagValueType: flagType, | ||
clientMetadata: this.metadata, | ||
providerMetadata: OpenFeature.providerMetadata, | ||
context: mergedContext, | ||
logger: this._logger | ||
async evaluate(flagKey, resolver, defaultValue, flagType, invocationContext = {}, options = {}) { | ||
const allHooks = [ | ||
...OpenFeature.getHooks(), | ||
...this.getHooks(), | ||
...options.hooks || [], | ||
...this._provider.hooks || [] | ||
]; | ||
const allHooksReversed = [...allHooks].reverse(); | ||
const mergedContext = { | ||
...OpenFeature.getContext(), | ||
...OpenFeature.getTransactionContext(), | ||
...this._context, | ||
...invocationContext | ||
}; | ||
const hookContext = { | ||
flagKey, | ||
defaultValue, | ||
flagValueType: flagType, | ||
clientMetadata: this.metadata, | ||
providerMetadata: OpenFeature.providerMetadata, | ||
context: mergedContext, | ||
logger: this._logger | ||
}; | ||
try { | ||
const frozenContext = await this.beforeHooks(allHooks, hookContext, options); | ||
const resolution = await resolver.call(this._provider, flagKey, defaultValue, frozenContext, this._logger); | ||
const evaluationDetails = { | ||
...resolution, | ||
flagMetadata: Object.freeze(resolution.flagMetadata ?? {}), | ||
flagKey | ||
}; | ||
try { | ||
const frozenContext = yield this.beforeHooks(allHooks, hookContext, options); | ||
const resolution = yield resolver.call(this._provider, flagKey, defaultValue, frozenContext, this._logger); | ||
const evaluationDetails = __spreadProps(__spreadValues({}, resolution), { | ||
flagMetadata: Object.freeze((_a = resolution.flagMetadata) != null ? _a : {}), | ||
flagKey | ||
}); | ||
yield this.afterHooks(allHooksReversed, hookContext, evaluationDetails, options); | ||
return evaluationDetails; | ||
} catch (err) { | ||
const errorMessage = err == null ? void 0 : err.message; | ||
const errorCode = (err == null ? void 0 : err.code) || import_core7.ErrorCode.GENERAL; | ||
yield this.errorHooks(allHooksReversed, hookContext, err, options); | ||
return { | ||
errorCode, | ||
errorMessage, | ||
value: defaultValue, | ||
reason: import_core7.StandardResolutionReasons.ERROR, | ||
flagMetadata: Object.freeze({}), | ||
flagKey | ||
}; | ||
} finally { | ||
yield this.finallyHooks(allHooksReversed, hookContext, options); | ||
} | ||
}); | ||
await this.afterHooks(allHooksReversed, hookContext, evaluationDetails, options); | ||
return evaluationDetails; | ||
} catch (err) { | ||
const errorMessage = err?.message; | ||
const errorCode = err?.code || import_core7.ErrorCode.GENERAL; | ||
await this.errorHooks(allHooksReversed, hookContext, err, options); | ||
return { | ||
errorCode, | ||
errorMessage, | ||
value: defaultValue, | ||
reason: import_core7.StandardResolutionReasons.ERROR, | ||
flagMetadata: Object.freeze({}), | ||
flagKey | ||
}; | ||
} finally { | ||
await this.finallyHooks(allHooksReversed, hookContext, options); | ||
} | ||
} | ||
beforeHooks(hooks, hookContext, options) { | ||
return __async(this, null, function* () { | ||
var _a; | ||
for (const hook of hooks) { | ||
Object.freeze(hookContext); | ||
Object.assign(hookContext.context, __spreadValues(__spreadValues({}, hookContext.context), yield (_a = hook == null ? void 0 : hook.before) == null ? void 0 : _a.call(hook, hookContext, Object.freeze(options.hookHints)))); | ||
} | ||
return Object.freeze(hookContext.context); | ||
}); | ||
async beforeHooks(hooks, hookContext, options) { | ||
for (const hook of hooks) { | ||
Object.freeze(hookContext); | ||
Object.assign(hookContext.context, { | ||
...hookContext.context, | ||
...await hook?.before?.(hookContext, Object.freeze(options.hookHints)) | ||
}); | ||
} | ||
return Object.freeze(hookContext.context); | ||
} | ||
afterHooks(hooks, hookContext, evaluationDetails, options) { | ||
return __async(this, null, function* () { | ||
var _a; | ||
for (const hook of hooks) { | ||
yield (_a = hook == null ? void 0 : hook.after) == null ? void 0 : _a.call(hook, hookContext, evaluationDetails, options.hookHints); | ||
} | ||
}); | ||
async afterHooks(hooks, hookContext, evaluationDetails, options) { | ||
for (const hook of hooks) { | ||
await hook?.after?.(hookContext, evaluationDetails, options.hookHints); | ||
} | ||
} | ||
errorHooks(hooks, hookContext, err, options) { | ||
return __async(this, null, function* () { | ||
var _a; | ||
for (const hook of hooks) { | ||
try { | ||
yield (_a = hook == null ? void 0 : hook.error) == null ? void 0 : _a.call(hook, hookContext, err, options.hookHints); | ||
} catch (err2) { | ||
this._logger.error(`Unhandled error during 'error' hook: ${err2}`); | ||
if (err2 instanceof Error) { | ||
this._logger.error(err2.stack); | ||
} | ||
this._logger.error(err2 == null ? void 0 : err2.stack); | ||
async errorHooks(hooks, hookContext, err, options) { | ||
for (const hook of hooks) { | ||
try { | ||
await hook?.error?.(hookContext, err, options.hookHints); | ||
} catch (err2) { | ||
this._logger.error(`Unhandled error during 'error' hook: ${err2}`); | ||
if (err2 instanceof Error) { | ||
this._logger.error(err2.stack); | ||
} | ||
this._logger.error(err2?.stack); | ||
} | ||
}); | ||
} | ||
} | ||
finallyHooks(hooks, hookContext, options) { | ||
return __async(this, null, function* () { | ||
var _a; | ||
for (const hook of hooks) { | ||
try { | ||
yield (_a = hook == null ? void 0 : hook.finally) == null ? void 0 : _a.call(hook, hookContext, options.hookHints); | ||
} catch (err) { | ||
this._logger.error(`Unhandled error during 'finally' hook: ${err}`); | ||
if (err instanceof Error) { | ||
this._logger.error(err.stack); | ||
} | ||
this._logger.error(err == null ? void 0 : err.stack); | ||
async finallyHooks(hooks, hookContext, options) { | ||
for (const hook of hooks) { | ||
try { | ||
await hook?.finally?.(hookContext, options.hookHints); | ||
} catch (err) { | ||
this._logger.error(`Unhandled error during 'finally' hook: ${err}`); | ||
if (err instanceof Error) { | ||
this._logger.error(err.stack); | ||
} | ||
this._logger.error(err?.stack); | ||
} | ||
}); | ||
} | ||
} | ||
@@ -548,0 +489,0 @@ get _provider() { |
@@ -1,41 +0,1 @@ | ||
var __defProp = Object.defineProperty; | ||
var __defProps = Object.defineProperties; | ||
var __getOwnPropDescs = Object.getOwnPropertyDescriptors; | ||
var __getOwnPropSymbols = Object.getOwnPropertySymbols; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __propIsEnum = Object.prototype.propertyIsEnumerable; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __spreadValues = (a, b) => { | ||
for (var prop in b || (b = {})) | ||
if (__hasOwnProp.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
if (__getOwnPropSymbols) | ||
for (var prop of __getOwnPropSymbols(b)) { | ||
if (__propIsEnum.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
} | ||
return a; | ||
}; | ||
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); | ||
var __async = (__this, __arguments, generator) => { | ||
return new Promise((resolve, reject) => { | ||
var fulfilled = (value) => { | ||
try { | ||
step(generator.next(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}; | ||
var rejected = (value) => { | ||
try { | ||
step(generator.throw(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}; | ||
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); | ||
step((generator = generator.apply(__this, __arguments)).next()); | ||
}); | ||
}; | ||
// src/client/open-feature-client.ts | ||
@@ -53,7 +13,5 @@ import { | ||
var NoopFeatureProvider = class { | ||
constructor() { | ||
this.metadata = { | ||
name: "No-op Provider" | ||
}; | ||
} | ||
metadata = { | ||
name: "No-op Provider" | ||
}; | ||
get status() { | ||
@@ -95,2 +53,3 @@ return ProviderStatus.NOT_READY; | ||
var VariantFoundError = class extends OpenFeatureError { | ||
code; | ||
constructor(message) { | ||
@@ -106,9 +65,10 @@ super(message); | ||
var InMemoryProvider = class { | ||
events = new OpenFeatureEventEmitter(); | ||
runsOn = "server"; | ||
metadata = { | ||
name: "in-memory" | ||
}; | ||
_flagConfiguration; | ||
constructor(flagConfiguration = {}) { | ||
this.events = new OpenFeatureEventEmitter(); | ||
this.runsOn = "server"; | ||
this.metadata = { | ||
name: "in-memory" | ||
}; | ||
this._flagConfiguration = __spreadValues({}, flagConfiguration); | ||
this._flagConfiguration = { ...flagConfiguration }; | ||
} | ||
@@ -121,3 +81,3 @@ /** | ||
const flagsChanged = Object.entries(flagConfiguration).filter(([key, value]) => this._flagConfiguration[key] !== value).map(([key]) => key); | ||
this._flagConfiguration = __spreadValues({}, flagConfiguration); | ||
this._flagConfiguration = { ...flagConfiguration }; | ||
this.events.emit(ServerProviderEvents.ConfigurationChanged, { flagsChanged }); | ||
@@ -131,33 +91,26 @@ } | ||
} | ||
resolveStringEvaluation(flagKey, defaultValue, context, logger) { | ||
return __async(this, null, function* () { | ||
return this.resolveFlagWithReason(flagKey, defaultValue, context, logger); | ||
}); | ||
async resolveStringEvaluation(flagKey, defaultValue, context, logger) { | ||
return this.resolveFlagWithReason(flagKey, defaultValue, context, logger); | ||
} | ||
resolveObjectEvaluation(flagKey, defaultValue, context, logger) { | ||
return __async(this, null, function* () { | ||
return this.resolveFlagWithReason(flagKey, defaultValue, context, logger); | ||
}); | ||
async resolveObjectEvaluation(flagKey, defaultValue, context, logger) { | ||
return this.resolveFlagWithReason(flagKey, defaultValue, context, logger); | ||
} | ||
resolveFlagWithReason(flagKey, defaultValue, ctx, logger) { | ||
return __async(this, null, function* () { | ||
try { | ||
const resolutionResult = this.lookupFlagValue(flagKey, defaultValue, ctx, logger); | ||
if (typeof (resolutionResult == null ? void 0 : resolutionResult.value) != typeof defaultValue) { | ||
throw new TypeMismatchError(); | ||
} | ||
return resolutionResult; | ||
} catch (error) { | ||
if (!(error instanceof OpenFeatureError2)) { | ||
throw new GeneralError((error == null ? void 0 : error.message) || "unknown error"); | ||
} | ||
throw error; | ||
async resolveFlagWithReason(flagKey, defaultValue, ctx, logger) { | ||
try { | ||
const resolutionResult = this.lookupFlagValue(flagKey, defaultValue, ctx, logger); | ||
if (typeof resolutionResult?.value != typeof defaultValue) { | ||
throw new TypeMismatchError(); | ||
} | ||
}); | ||
return resolutionResult; | ||
} catch (error) { | ||
if (!(error instanceof OpenFeatureError2)) { | ||
throw new GeneralError(error?.message || "unknown error"); | ||
} | ||
throw error; | ||
} | ||
} | ||
lookupFlagValue(flagKey, defaultValue, ctx, logger) { | ||
var _a; | ||
if (!(flagKey in this._flagConfiguration)) { | ||
const message = `no flag found with key ${flagKey}`; | ||
logger == null ? void 0 : logger.debug(message); | ||
logger?.debug(message); | ||
throw new FlagNotFoundError(message); | ||
@@ -169,15 +122,15 @@ } | ||
} | ||
const isContextEval = ctx && (flagSpec == null ? void 0 : flagSpec.contextEvaluator); | ||
const variant = isContextEval ? (_a = flagSpec.contextEvaluator) == null ? void 0 : _a.call(flagSpec, ctx) : flagSpec.defaultVariant; | ||
const value = variant && (flagSpec == null ? void 0 : flagSpec.variants[variant]); | ||
const isContextEval = ctx && flagSpec?.contextEvaluator; | ||
const variant = isContextEval ? flagSpec.contextEvaluator?.(ctx) : flagSpec.defaultVariant; | ||
const value = variant && flagSpec?.variants[variant]; | ||
if (value === void 0) { | ||
const message = `no value associated with variant ${variant}`; | ||
logger == null ? void 0 : logger.error(message); | ||
logger?.error(message); | ||
throw new VariantFoundError(message); | ||
} | ||
return __spreadProps(__spreadValues({ | ||
value | ||
}, variant && { variant }), { | ||
return { | ||
value, | ||
...variant && { variant }, | ||
reason: isContextEval ? StandardResolutionReasons.TARGETING_MATCH : StandardResolutionReasons.STATIC | ||
}); | ||
}; | ||
} | ||
@@ -208,8 +161,7 @@ }; | ||
var OpenFeatureEventEmitter = class extends GenericEventEmitter { | ||
eventEmitter = new EventEmitter({ captureRejections: true }); | ||
constructor() { | ||
super(); | ||
this.eventEmitter = new EventEmitter({ captureRejections: true }); | ||
this.eventEmitter.on("error", (err) => { | ||
var _a; | ||
(_a = this._logger) == null ? void 0 : _a.error("Error running event handler:", err); | ||
this._logger?.error("Error running event handler:", err); | ||
}); | ||
@@ -226,8 +178,8 @@ } | ||
var OpenFeatureAPI = class extends OpenFeatureCommonAPI { | ||
_events = new OpenFeatureEventEmitter(); | ||
_defaultProvider = NOOP_PROVIDER; | ||
_createEventEmitter = () => new OpenFeatureEventEmitter(); | ||
_transactionContextPropagator = NOOP_TRANSACTION_CONTEXT_PROPAGATOR; | ||
constructor() { | ||
super("server"); | ||
this._events = new OpenFeatureEventEmitter(); | ||
this._defaultProvider = NOOP_PROVIDER; | ||
this._createEventEmitter = () => new OpenFeatureEventEmitter(); | ||
this._transactionContextPropagator = NOOP_TRANSACTION_CONTEXT_PROPAGATOR; | ||
} | ||
@@ -256,6 +208,5 @@ /** | ||
getClient(nameOrContext, versionOrContext, contextOrUndefined) { | ||
var _a, _b; | ||
const name = stringOrUndefined(nameOrContext); | ||
const version = stringOrUndefined(versionOrContext); | ||
const context = (_b = (_a = objectOrUndefined(nameOrContext)) != null ? _a : objectOrUndefined(versionOrContext)) != null ? _b : objectOrUndefined(contextOrUndefined); | ||
const context = objectOrUndefined(nameOrContext) ?? objectOrUndefined(versionOrContext) ?? objectOrUndefined(contextOrUndefined); | ||
return new OpenFeatureClient( | ||
@@ -278,5 +229,5 @@ () => this.getProviderForClient(name), | ||
const baseMessage = "Invalid TransactionContextPropagator, will not be set: "; | ||
if (typeof (transactionContextPropagator == null ? void 0 : transactionContextPropagator.getTransactionContext) !== "function") { | ||
if (typeof transactionContextPropagator?.getTransactionContext !== "function") { | ||
this._logger.error(`${baseMessage}: getTransactionContext is not a function.`); | ||
} else if (typeof (transactionContextPropagator == null ? void 0 : transactionContextPropagator.setTransactionContext) !== "function") { | ||
} else if (typeof transactionContextPropagator?.setTransactionContext !== "function") { | ||
this._logger.error(`${baseMessage}: setTransactionContext is not a function.`); | ||
@@ -296,4 +247,4 @@ } else { | ||
const error = err; | ||
this._logger.error(`Error getting transaction context: ${error == null ? void 0 : error.message}, returning empty context.`); | ||
this._logger.error(error == null ? void 0 : error.stack); | ||
this._logger.error(`Error getting transaction context: ${error?.message}, returning empty context.`); | ||
this._logger.error(error?.stack); | ||
return {}; | ||
@@ -312,5 +263,7 @@ } | ||
this.options = options; | ||
this._hooks = []; | ||
this._context = context; | ||
} | ||
_context; | ||
_hooks = []; | ||
_clientLogger; | ||
get metadata() { | ||
@@ -324,3 +277,2 @@ return { | ||
addHandler(eventType, handler) { | ||
var _a; | ||
this.emitterAccessor().addHandler(eventType, handler); | ||
@@ -332,3 +284,3 @@ const shouldRunNow = statusMatchesEvent(eventType, this._provider.status); | ||
} catch (err) { | ||
(_a = this._logger) == null ? void 0 : _a.error("Error running event handler:", err); | ||
this._logger?.error("Error running event handler:", err); | ||
} | ||
@@ -365,6 +317,4 @@ } | ||
} | ||
getBooleanValue(flagKey, defaultValue, context, options) { | ||
return __async(this, null, function* () { | ||
return (yield this.getBooleanDetails(flagKey, defaultValue, context, options)).value; | ||
}); | ||
async getBooleanValue(flagKey, defaultValue, context, options) { | ||
return (await this.getBooleanDetails(flagKey, defaultValue, context, options)).value; | ||
} | ||
@@ -381,6 +331,4 @@ getBooleanDetails(flagKey, defaultValue, context, options) { | ||
} | ||
getStringValue(flagKey, defaultValue, context, options) { | ||
return __async(this, null, function* () { | ||
return (yield this.getStringDetails(flagKey, defaultValue, context, options)).value; | ||
}); | ||
async getStringValue(flagKey, defaultValue, context, options) { | ||
return (await this.getStringDetails(flagKey, defaultValue, context, options)).value; | ||
} | ||
@@ -398,6 +346,4 @@ getStringDetails(flagKey, defaultValue, context, options) { | ||
} | ||
getNumberValue(flagKey, defaultValue, context, options) { | ||
return __async(this, null, function* () { | ||
return (yield this.getNumberDetails(flagKey, defaultValue, context, options)).value; | ||
}); | ||
async getNumberValue(flagKey, defaultValue, context, options) { | ||
return (await this.getNumberDetails(flagKey, defaultValue, context, options)).value; | ||
} | ||
@@ -415,6 +361,4 @@ getNumberDetails(flagKey, defaultValue, context, options) { | ||
} | ||
getObjectValue(flagKey, defaultValue, context, options) { | ||
return __async(this, null, function* () { | ||
return (yield this.getObjectDetails(flagKey, defaultValue, context, options)).value; | ||
}); | ||
async getObjectValue(flagKey, defaultValue, context, options) { | ||
return (await this.getObjectDetails(flagKey, defaultValue, context, options)).value; | ||
} | ||
@@ -424,97 +368,91 @@ getObjectDetails(flagKey, defaultValue, context, options) { | ||
} | ||
evaluate(_0, _1, _2, _3) { | ||
return __async(this, arguments, function* (flagKey, resolver, defaultValue, flagType, invocationContext = {}, options = {}) { | ||
var _a; | ||
const allHooks = [ | ||
...OpenFeature.getHooks(), | ||
...this.getHooks(), | ||
...options.hooks || [], | ||
...this._provider.hooks || [] | ||
]; | ||
const allHooksReversed = [...allHooks].reverse(); | ||
const mergedContext = __spreadValues(__spreadValues(__spreadValues(__spreadValues({}, OpenFeature.getContext()), OpenFeature.getTransactionContext()), this._context), invocationContext); | ||
const hookContext = { | ||
flagKey, | ||
defaultValue, | ||
flagValueType: flagType, | ||
clientMetadata: this.metadata, | ||
providerMetadata: OpenFeature.providerMetadata, | ||
context: mergedContext, | ||
logger: this._logger | ||
async evaluate(flagKey, resolver, defaultValue, flagType, invocationContext = {}, options = {}) { | ||
const allHooks = [ | ||
...OpenFeature.getHooks(), | ||
...this.getHooks(), | ||
...options.hooks || [], | ||
...this._provider.hooks || [] | ||
]; | ||
const allHooksReversed = [...allHooks].reverse(); | ||
const mergedContext = { | ||
...OpenFeature.getContext(), | ||
...OpenFeature.getTransactionContext(), | ||
...this._context, | ||
...invocationContext | ||
}; | ||
const hookContext = { | ||
flagKey, | ||
defaultValue, | ||
flagValueType: flagType, | ||
clientMetadata: this.metadata, | ||
providerMetadata: OpenFeature.providerMetadata, | ||
context: mergedContext, | ||
logger: this._logger | ||
}; | ||
try { | ||
const frozenContext = await this.beforeHooks(allHooks, hookContext, options); | ||
const resolution = await resolver.call(this._provider, flagKey, defaultValue, frozenContext, this._logger); | ||
const evaluationDetails = { | ||
...resolution, | ||
flagMetadata: Object.freeze(resolution.flagMetadata ?? {}), | ||
flagKey | ||
}; | ||
try { | ||
const frozenContext = yield this.beforeHooks(allHooks, hookContext, options); | ||
const resolution = yield resolver.call(this._provider, flagKey, defaultValue, frozenContext, this._logger); | ||
const evaluationDetails = __spreadProps(__spreadValues({}, resolution), { | ||
flagMetadata: Object.freeze((_a = resolution.flagMetadata) != null ? _a : {}), | ||
flagKey | ||
}); | ||
yield this.afterHooks(allHooksReversed, hookContext, evaluationDetails, options); | ||
return evaluationDetails; | ||
} catch (err) { | ||
const errorMessage = err == null ? void 0 : err.message; | ||
const errorCode = (err == null ? void 0 : err.code) || ErrorCode2.GENERAL; | ||
yield this.errorHooks(allHooksReversed, hookContext, err, options); | ||
return { | ||
errorCode, | ||
errorMessage, | ||
value: defaultValue, | ||
reason: StandardResolutionReasons2.ERROR, | ||
flagMetadata: Object.freeze({}), | ||
flagKey | ||
}; | ||
} finally { | ||
yield this.finallyHooks(allHooksReversed, hookContext, options); | ||
} | ||
}); | ||
await this.afterHooks(allHooksReversed, hookContext, evaluationDetails, options); | ||
return evaluationDetails; | ||
} catch (err) { | ||
const errorMessage = err?.message; | ||
const errorCode = err?.code || ErrorCode2.GENERAL; | ||
await this.errorHooks(allHooksReversed, hookContext, err, options); | ||
return { | ||
errorCode, | ||
errorMessage, | ||
value: defaultValue, | ||
reason: StandardResolutionReasons2.ERROR, | ||
flagMetadata: Object.freeze({}), | ||
flagKey | ||
}; | ||
} finally { | ||
await this.finallyHooks(allHooksReversed, hookContext, options); | ||
} | ||
} | ||
beforeHooks(hooks, hookContext, options) { | ||
return __async(this, null, function* () { | ||
var _a; | ||
for (const hook of hooks) { | ||
Object.freeze(hookContext); | ||
Object.assign(hookContext.context, __spreadValues(__spreadValues({}, hookContext.context), yield (_a = hook == null ? void 0 : hook.before) == null ? void 0 : _a.call(hook, hookContext, Object.freeze(options.hookHints)))); | ||
} | ||
return Object.freeze(hookContext.context); | ||
}); | ||
async beforeHooks(hooks, hookContext, options) { | ||
for (const hook of hooks) { | ||
Object.freeze(hookContext); | ||
Object.assign(hookContext.context, { | ||
...hookContext.context, | ||
...await hook?.before?.(hookContext, Object.freeze(options.hookHints)) | ||
}); | ||
} | ||
return Object.freeze(hookContext.context); | ||
} | ||
afterHooks(hooks, hookContext, evaluationDetails, options) { | ||
return __async(this, null, function* () { | ||
var _a; | ||
for (const hook of hooks) { | ||
yield (_a = hook == null ? void 0 : hook.after) == null ? void 0 : _a.call(hook, hookContext, evaluationDetails, options.hookHints); | ||
} | ||
}); | ||
async afterHooks(hooks, hookContext, evaluationDetails, options) { | ||
for (const hook of hooks) { | ||
await hook?.after?.(hookContext, evaluationDetails, options.hookHints); | ||
} | ||
} | ||
errorHooks(hooks, hookContext, err, options) { | ||
return __async(this, null, function* () { | ||
var _a; | ||
for (const hook of hooks) { | ||
try { | ||
yield (_a = hook == null ? void 0 : hook.error) == null ? void 0 : _a.call(hook, hookContext, err, options.hookHints); | ||
} catch (err2) { | ||
this._logger.error(`Unhandled error during 'error' hook: ${err2}`); | ||
if (err2 instanceof Error) { | ||
this._logger.error(err2.stack); | ||
} | ||
this._logger.error(err2 == null ? void 0 : err2.stack); | ||
async errorHooks(hooks, hookContext, err, options) { | ||
for (const hook of hooks) { | ||
try { | ||
await hook?.error?.(hookContext, err, options.hookHints); | ||
} catch (err2) { | ||
this._logger.error(`Unhandled error during 'error' hook: ${err2}`); | ||
if (err2 instanceof Error) { | ||
this._logger.error(err2.stack); | ||
} | ||
this._logger.error(err2?.stack); | ||
} | ||
}); | ||
} | ||
} | ||
finallyHooks(hooks, hookContext, options) { | ||
return __async(this, null, function* () { | ||
var _a; | ||
for (const hook of hooks) { | ||
try { | ||
yield (_a = hook == null ? void 0 : hook.finally) == null ? void 0 : _a.call(hook, hookContext, options.hookHints); | ||
} catch (err) { | ||
this._logger.error(`Unhandled error during 'finally' hook: ${err}`); | ||
if (err instanceof Error) { | ||
this._logger.error(err.stack); | ||
} | ||
this._logger.error(err == null ? void 0 : err.stack); | ||
async finallyHooks(hooks, hookContext, options) { | ||
for (const hook of hooks) { | ||
try { | ||
await hook?.finally?.(hookContext, options.hookHints); | ||
} catch (err) { | ||
this._logger.error(`Unhandled error during 'finally' hook: ${err}`); | ||
if (err instanceof Error) { | ||
this._logger.error(err.stack); | ||
} | ||
this._logger.error(err?.stack); | ||
} | ||
}); | ||
} | ||
} | ||
@@ -521,0 +459,0 @@ get _provider() { |
@@ -474,3 +474,3 @@ import EventEmitter from 'events'; | ||
abstract code: ErrorCode; | ||
constructor(message?: string); | ||
constructor(message?: string, options?: ErrorOptions); | ||
} | ||
@@ -480,3 +480,3 @@ | ||
code: ErrorCode; | ||
constructor(message?: string); | ||
constructor(message?: string, options?: ErrorOptions); | ||
} | ||
@@ -486,3 +486,3 @@ | ||
code: ErrorCode; | ||
constructor(message?: string); | ||
constructor(message?: string, options?: ErrorOptions); | ||
} | ||
@@ -492,3 +492,3 @@ | ||
code: ErrorCode; | ||
constructor(message?: string); | ||
constructor(message?: string, options?: ErrorOptions); | ||
} | ||
@@ -498,3 +498,3 @@ | ||
code: ErrorCode; | ||
constructor(message?: string); | ||
constructor(message?: string, options?: ErrorOptions); | ||
} | ||
@@ -504,3 +504,3 @@ | ||
code: ErrorCode; | ||
constructor(message?: string); | ||
constructor(message?: string, options?: ErrorOptions); | ||
} | ||
@@ -510,3 +510,3 @@ | ||
code: ErrorCode; | ||
constructor(message?: string); | ||
constructor(message?: string, options?: ErrorOptions); | ||
} | ||
@@ -516,3 +516,3 @@ | ||
code: ErrorCode; | ||
constructor(message?: string); | ||
constructor(message?: string, options?: ErrorOptions); | ||
} | ||
@@ -519,0 +519,0 @@ |
{ | ||
"name": "@openfeature/server-sdk", | ||
"version": "1.9.1", | ||
"version": "1.10.0", | ||
"description": "OpenFeature SDK for JavaScript", | ||
@@ -20,4 +20,4 @@ "main": "./dist/cjs/index.js", | ||
"clean": "shx rm -rf ./dist", | ||
"build:esm": "esbuild src/index.ts --bundle --external:@openfeature/core --sourcemap --target=es2016 --platform=node --format=esm --outfile=./dist/esm/index.js --analyze", | ||
"build:cjs": "esbuild src/index.ts --bundle --external:@openfeature/core --sourcemap --target=es2016 --platform=node --format=cjs --outfile=./dist/cjs/index.js --analyze", | ||
"build:esm": "esbuild src/index.ts --bundle --external:@openfeature/core --sourcemap --target=es2022 --platform=node --format=esm --outfile=./dist/esm/index.js --analyze", | ||
"build:cjs": "esbuild src/index.ts --bundle --external:@openfeature/core --sourcemap --target=es2022 --platform=node --format=cjs --outfile=./dist/cjs/index.js --analyze", | ||
"build:rollup-types": "rollup -c ../../rollup.config.mjs", | ||
@@ -52,7 +52,7 @@ "build": "npm run clean && npm run build:esm && npm run build:cjs && npm run build:rollup-types", | ||
"peerDependencies": { | ||
"@openfeature/core": "0.0.23" | ||
"@openfeature/core": "0.0.24" | ||
}, | ||
"devDependencies": { | ||
"@openfeature/core": "0.0.23" | ||
"@openfeature/core": "0.0.24" | ||
} | ||
} |
@@ -19,4 +19,4 @@ <!-- markdownlint-disable MD033 --> | ||
<!-- x-release-please-start-version --> | ||
<a href="https://github.com/open-feature/js-sdk/releases/tag/server-sdk-v1.9.1"> | ||
<img alt="Release" src="https://img.shields.io/static/v1?label=release&message=v1.9.1&color=blue&style=for-the-badge" /> | ||
<a href="https://github.com/open-feature/js-sdk/releases/tag/server-sdk-v1.10.0"> | ||
<img alt="Release" src="https://img.shields.io/static/v1?label=release&message=v1.10.0&color=blue&style=for-the-badge" /> | ||
</a> | ||
@@ -23,0 +23,0 @@ <!-- x-release-please-end --> |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
176151
1906