@openfeature/web-sdk
Advanced tools
Comparing version 0.4.7 to 0.4.8
@@ -12,2 +12,3 @@ "use strict"; | ||
var __propIsEnum = Object.prototype.propertyIsEnumerable; | ||
var __reflectGet = Reflect.get; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
@@ -51,2 +52,3 @@ var __spreadValues = (a, b) => { | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
var __superGet = (cls, obj, key) => __reflectGet(__getProtoOf(cls), key, obj); | ||
var __async = (__this, __arguments, generator) => { | ||
@@ -632,3 +634,2 @@ return new Promise((resolve, reject) => { | ||
var OpenFeatureAPI = class extends import_core5.OpenFeatureCommonAPI { | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
constructor() { | ||
@@ -639,2 +640,3 @@ super("client"); | ||
this._createEventEmitter = () => new OpenFeatureEventEmitter(); | ||
this._namedProviderContext = /* @__PURE__ */ new Map(); | ||
} | ||
@@ -655,23 +657,71 @@ /** | ||
} | ||
setContext(context) { | ||
setContext(nameOrContext, contextOrUndefined) { | ||
return __async(this, null, function* () { | ||
const oldContext = this._context; | ||
this._context = context; | ||
const allProviders = [this._defaultProvider, ...this._clientProviders.values()]; | ||
yield Promise.all( | ||
allProviders.map((provider) => __async(this, null, function* () { | ||
var _a, _b; | ||
try { | ||
return yield (_a = provider.onContextChange) == null ? void 0 : _a.call(provider, oldContext, context); | ||
} catch (err) { | ||
(_b = this._logger) == null ? void 0 : _b.error(`Error running context change handler of provider ${provider.metadata.name}:`, err); | ||
} | ||
})) | ||
); | ||
var _a, _b; | ||
const clientName = (0, import_core5.stringOrUndefined)(nameOrContext); | ||
const context = (_b = (_a = (0, import_core5.objectOrUndefined)(nameOrContext)) != null ? _a : (0, import_core5.objectOrUndefined)(contextOrUndefined)) != null ? _b : {}; | ||
if (clientName) { | ||
const provider = this._clientProviders.get(clientName); | ||
if (provider) { | ||
const oldContext = this.getContext(clientName); | ||
this._namedProviderContext.set(clientName, context); | ||
yield this.runProviderContextChangeHandler(provider, oldContext, context); | ||
} else { | ||
this._namedProviderContext.set(clientName, context); | ||
} | ||
} else { | ||
const oldContext = this._context; | ||
this._context = context; | ||
const providersWithoutContextOverride = Array.from(this._clientProviders.entries()).filter(([name]) => !this._namedProviderContext.has(name)).reduce((acc, [, provider]) => { | ||
acc.push(provider); | ||
return acc; | ||
}, []); | ||
const allProviders = [this._defaultProvider, ...providersWithoutContextOverride]; | ||
yield Promise.all( | ||
allProviders.map((provider) => this.runProviderContextChangeHandler(provider, oldContext, context)) | ||
); | ||
} | ||
}); | ||
} | ||
getContext() { | ||
getContext(nameOrUndefined) { | ||
const clientName = (0, import_core5.stringOrUndefined)(nameOrUndefined); | ||
if (clientName) { | ||
const context = this._namedProviderContext.get(clientName); | ||
if (context) { | ||
return context; | ||
} else { | ||
this._logger.debug(`Unable to find context for '${clientName}'.`); | ||
} | ||
} | ||
return this._context; | ||
} | ||
clearContext(nameOrUndefined) { | ||
return __async(this, null, function* () { | ||
const clientName = (0, import_core5.stringOrUndefined)(nameOrUndefined); | ||
if (clientName) { | ||
const provider = this._clientProviders.get(clientName); | ||
if (provider) { | ||
const oldContext = this.getContext(clientName); | ||
this._namedProviderContext.delete(clientName); | ||
const newContext = this.getContext(); | ||
yield this.runProviderContextChangeHandler(provider, oldContext, newContext); | ||
} else { | ||
this._namedProviderContext.delete(clientName); | ||
} | ||
} else { | ||
return this.setContext({}); | ||
} | ||
}); | ||
} | ||
/** | ||
* Resets the global evaluation context and removes the evaluation context for | ||
* all named clients. | ||
*/ | ||
clearContexts() { | ||
return __async(this, null, function* () { | ||
yield this.clearContext(); | ||
yield Promise.allSettled(Array.from(this._clientProviders.keys()).map((name) => this.clearContext(name))); | ||
}); | ||
} | ||
/** | ||
* A factory function for creating new named OpenFeature clients. Clients can contain | ||
@@ -702,4 +752,17 @@ * their own state (e.g. logger, hook, context). Multiple clients can be used | ||
clearProviders() { | ||
return super.clearProvidersAndSetDefault(NOOP_PROVIDER); | ||
return __async(this, null, function* () { | ||
yield __superGet(OpenFeatureAPI.prototype, this, "clearProvidersAndSetDefault").call(this, NOOP_PROVIDER); | ||
this._namedProviderContext.clear(); | ||
}); | ||
} | ||
runProviderContextChangeHandler(provider, oldContext, newContext) { | ||
return __async(this, null, function* () { | ||
var _a, _b; | ||
try { | ||
return yield (_a = provider.onContextChange) == null ? void 0 : _a.call(provider, oldContext, newContext); | ||
} catch (err) { | ||
(_b = this._logger) == null ? void 0 : _b.error(`Error running ${provider.metadata.name}'s context change handler:`, err); | ||
} | ||
}); | ||
} | ||
}; | ||
@@ -724,7 +787,11 @@ var OpenFeature = OpenFeatureAPI.getInstance(); | ||
} | ||
get providerStatus() { | ||
var _a; | ||
return ((_a = this.providerAccessor()) == null ? void 0 : _a.status) || import_core6.ProviderStatus.READY; | ||
} | ||
addHandler(eventType, handler) { | ||
var _a; | ||
this.emitterAccessor().addHandler(eventType, handler); | ||
const providerReady = !this._provider.status || this._provider.status === import_core6.ProviderStatus.READY; | ||
if (eventType === import_core6.ProviderEvents.Ready && providerReady) { | ||
const shouldRunNow = (0, import_core6.statusMatchesEvent)(eventType, this._provider.status); | ||
if (shouldRunNow) { | ||
try { | ||
@@ -731,0 +798,0 @@ handler({ clientName: this.metadata.name, providerName: this._provider.metadata.name }); |
@@ -11,2 +11,3 @@ var __create = Object.create; | ||
var __propIsEnum = Object.prototype.propertyIsEnumerable; | ||
var __reflectGet = Reflect.get; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
@@ -44,2 +45,3 @@ var __spreadValues = (a, b) => { | ||
)); | ||
var __superGet = (cls, obj, key) => __reflectGet(__getProtoOf(cls), key, obj); | ||
var __async = (__this, __arguments, generator) => { | ||
@@ -441,10 +443,14 @@ return new Promise((resolve, reject) => { | ||
ErrorCode as ErrorCode2, | ||
ProviderEvents as ProviderEvents2, | ||
ProviderStatus as ProviderStatus3, | ||
SafeLogger, | ||
StandardResolutionReasons as StandardResolutionReasons2 | ||
StandardResolutionReasons as StandardResolutionReasons2, | ||
statusMatchesEvent | ||
} from "@openfeature/core"; | ||
// src/open-feature.ts | ||
import { OpenFeatureCommonAPI } from "@openfeature/core"; | ||
import { | ||
OpenFeatureCommonAPI, | ||
objectOrUndefined, | ||
stringOrUndefined | ||
} from "@openfeature/core"; | ||
@@ -628,3 +634,2 @@ // src/provider/no-op-provider.ts | ||
var OpenFeatureAPI = class extends OpenFeatureCommonAPI { | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
constructor() { | ||
@@ -635,2 +640,3 @@ super("client"); | ||
this._createEventEmitter = () => new OpenFeatureEventEmitter(); | ||
this._namedProviderContext = /* @__PURE__ */ new Map(); | ||
} | ||
@@ -651,23 +657,71 @@ /** | ||
} | ||
setContext(context) { | ||
setContext(nameOrContext, contextOrUndefined) { | ||
return __async(this, null, function* () { | ||
const oldContext = this._context; | ||
this._context = context; | ||
const allProviders = [this._defaultProvider, ...this._clientProviders.values()]; | ||
yield Promise.all( | ||
allProviders.map((provider) => __async(this, null, function* () { | ||
var _a, _b; | ||
try { | ||
return yield (_a = provider.onContextChange) == null ? void 0 : _a.call(provider, oldContext, context); | ||
} catch (err) { | ||
(_b = this._logger) == null ? void 0 : _b.error(`Error running context change handler of provider ${provider.metadata.name}:`, err); | ||
} | ||
})) | ||
); | ||
var _a, _b; | ||
const clientName = stringOrUndefined(nameOrContext); | ||
const context = (_b = (_a = objectOrUndefined(nameOrContext)) != null ? _a : objectOrUndefined(contextOrUndefined)) != null ? _b : {}; | ||
if (clientName) { | ||
const provider = this._clientProviders.get(clientName); | ||
if (provider) { | ||
const oldContext = this.getContext(clientName); | ||
this._namedProviderContext.set(clientName, context); | ||
yield this.runProviderContextChangeHandler(provider, oldContext, context); | ||
} else { | ||
this._namedProviderContext.set(clientName, context); | ||
} | ||
} else { | ||
const oldContext = this._context; | ||
this._context = context; | ||
const providersWithoutContextOverride = Array.from(this._clientProviders.entries()).filter(([name]) => !this._namedProviderContext.has(name)).reduce((acc, [, provider]) => { | ||
acc.push(provider); | ||
return acc; | ||
}, []); | ||
const allProviders = [this._defaultProvider, ...providersWithoutContextOverride]; | ||
yield Promise.all( | ||
allProviders.map((provider) => this.runProviderContextChangeHandler(provider, oldContext, context)) | ||
); | ||
} | ||
}); | ||
} | ||
getContext() { | ||
getContext(nameOrUndefined) { | ||
const clientName = stringOrUndefined(nameOrUndefined); | ||
if (clientName) { | ||
const context = this._namedProviderContext.get(clientName); | ||
if (context) { | ||
return context; | ||
} else { | ||
this._logger.debug(`Unable to find context for '${clientName}'.`); | ||
} | ||
} | ||
return this._context; | ||
} | ||
clearContext(nameOrUndefined) { | ||
return __async(this, null, function* () { | ||
const clientName = stringOrUndefined(nameOrUndefined); | ||
if (clientName) { | ||
const provider = this._clientProviders.get(clientName); | ||
if (provider) { | ||
const oldContext = this.getContext(clientName); | ||
this._namedProviderContext.delete(clientName); | ||
const newContext = this.getContext(); | ||
yield this.runProviderContextChangeHandler(provider, oldContext, newContext); | ||
} else { | ||
this._namedProviderContext.delete(clientName); | ||
} | ||
} else { | ||
return this.setContext({}); | ||
} | ||
}); | ||
} | ||
/** | ||
* Resets the global evaluation context and removes the evaluation context for | ||
* all named clients. | ||
*/ | ||
clearContexts() { | ||
return __async(this, null, function* () { | ||
yield this.clearContext(); | ||
yield Promise.allSettled(Array.from(this._clientProviders.keys()).map((name) => this.clearContext(name))); | ||
}); | ||
} | ||
/** | ||
* A factory function for creating new named OpenFeature clients. Clients can contain | ||
@@ -698,4 +752,17 @@ * their own state (e.g. logger, hook, context). Multiple clients can be used | ||
clearProviders() { | ||
return super.clearProvidersAndSetDefault(NOOP_PROVIDER); | ||
return __async(this, null, function* () { | ||
yield __superGet(OpenFeatureAPI.prototype, this, "clearProvidersAndSetDefault").call(this, NOOP_PROVIDER); | ||
this._namedProviderContext.clear(); | ||
}); | ||
} | ||
runProviderContextChangeHandler(provider, oldContext, newContext) { | ||
return __async(this, null, function* () { | ||
var _a, _b; | ||
try { | ||
return yield (_a = provider.onContextChange) == null ? void 0 : _a.call(provider, oldContext, newContext); | ||
} catch (err) { | ||
(_b = this._logger) == null ? void 0 : _b.error(`Error running ${provider.metadata.name}'s context change handler:`, err); | ||
} | ||
}); | ||
} | ||
}; | ||
@@ -720,7 +787,11 @@ var OpenFeature = OpenFeatureAPI.getInstance(); | ||
} | ||
get providerStatus() { | ||
var _a; | ||
return ((_a = this.providerAccessor()) == null ? void 0 : _a.status) || ProviderStatus3.READY; | ||
} | ||
addHandler(eventType, handler) { | ||
var _a; | ||
this.emitterAccessor().addHandler(eventType, handler); | ||
const providerReady = !this._provider.status || this._provider.status === ProviderStatus3.READY; | ||
if (eventType === ProviderEvents2.Ready && providerReady) { | ||
const shouldRunNow = statusMatchesEvent(eventType, this._provider.status); | ||
if (shouldRunNow) { | ||
try { | ||
@@ -727,0 +798,0 @@ handler({ clientName: this.metadata.name, providerName: this._provider.metadata.name }); |
@@ -678,4 +678,16 @@ import EventEmitter from 'events'; | ||
readonly metadata: ClientMetadata; | ||
/** | ||
* Returns the status of the associated provider. | ||
*/ | ||
readonly providerStatus: ProviderStatus; | ||
} | ||
/** | ||
* The InternalEventEmitter is not exported publicly and should only be used within the SDK. It extends the | ||
* OpenFeatureEventEmitter to include additional properties that can be included | ||
* in the event details. | ||
*/ | ||
declare abstract class InternalEventEmitter extends GenericEventEmitter<CommonEventDetails> { | ||
} | ||
type Hook = BaseHook<FlagValue, void, void>; | ||
@@ -812,10 +824,2 @@ | ||
/** | ||
* The InternalEventEmitter is not exported publicly and should only be used within the SDK. It extends the | ||
* OpenFeatureEventEmitter to include additional properties that can be included | ||
* in the event details. | ||
*/ | ||
declare abstract class InternalEventEmitter extends GenericEventEmitter<CommonEventDetails> { | ||
} | ||
type OpenFeatureClientOptions = { | ||
@@ -834,2 +838,3 @@ name?: string; | ||
get metadata(): ClientMetadata; | ||
get providerStatus(): ProviderStatus; | ||
addHandler<T extends ProviderEvents>(eventType: T, handler: EventHandler<T>): void; | ||
@@ -863,2 +868,3 @@ removeHandler<T extends ProviderEvents>(notificationType: T, handler: EventHandler<T>): void; | ||
protected _createEventEmitter: () => OpenFeatureEventEmitter; | ||
protected _namedProviderContext: Map<string, EvaluationContext>; | ||
private constructor(); | ||
@@ -871,5 +877,48 @@ /** | ||
static getInstance(): OpenFeatureAPI; | ||
/** | ||
* Sets the evaluation context globally. | ||
* This will be used by all providers that have not been overridden with a named client. | ||
* @param {EvaluationContext} context Evaluation context | ||
* @example | ||
* await OpenFeature.setContext({ region: "us" }); | ||
*/ | ||
setContext(context: EvaluationContext): Promise<void>; | ||
/** | ||
* Sets the evaluation context for a specific provider. | ||
* This will only affect providers with a matching client name. | ||
* @param {string} clientName The name to identify the client | ||
* @param {EvaluationContext} context Evaluation context | ||
* @example | ||
* await OpenFeature.setContext("test", { scope: "provider" }); | ||
* OpenFeature.setProvider(new MyProvider()) // Uses the default context | ||
* OpenFeature.setProvider("test", new MyProvider()) // Uses context: { scope: "provider" } | ||
*/ | ||
setContext(clientName: string, context: EvaluationContext): Promise<void>; | ||
/** | ||
* Access the global evaluation context. | ||
* @returns {EvaluationContext} Evaluation context | ||
*/ | ||
getContext(): EvaluationContext; | ||
/** | ||
* Access the evaluation context for a specific named client. | ||
* The global evaluation context is returned if a matching named client is not found. | ||
* @param {string} clientName The name to identify the client | ||
* @returns {EvaluationContext} Evaluation context | ||
*/ | ||
getContext(clientName: string): EvaluationContext; | ||
/** | ||
* Resets the global evaluation context to an empty object. | ||
*/ | ||
clearContext(): Promise<void>; | ||
/** | ||
* Removes the evaluation context for a specific named client. | ||
* @param {string} clientName The name to identify the client | ||
*/ | ||
clearContext(clientName: string): Promise<void>; | ||
/** | ||
* Resets the global evaluation context and removes the evaluation context for | ||
* all named clients. | ||
*/ | ||
clearContexts(): Promise<void>; | ||
/** | ||
* A factory function for creating new named OpenFeature clients. Clients can contain | ||
@@ -891,2 +940,3 @@ * their own state (e.g. logger, hook, context). Multiple clients can be used | ||
clearProviders(): Promise<void>; | ||
private runProviderContextChangeHandler; | ||
} | ||
@@ -893,0 +943,0 @@ /** |
{ | ||
"name": "@openfeature/web-sdk", | ||
"version": "0.4.7", | ||
"version": "0.4.8", | ||
"description": "OpenFeature SDK for Web", | ||
@@ -5,0 +5,0 @@ "main": "./dist/cjs/index.js", |
@@ -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.7"> | ||
<img alt="Release" src="https://img.shields.io/static/v1?label=release&message=v0.4.7&color=blue&style=for-the-badge" /> | ||
<a href="https://github.com/open-feature/js-sdk/releases/tag/web-sdk-v0.4.8"> | ||
<img alt="Release" src="https://img.shields.io/static/v1?label=release&message=v0.4.8&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
258885
2795