@openfeature/web-sdk 0.4.11 → 0.4.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -16,8 +16,8 @@
16
16
  <img alt="Specification" src="https://img.shields.io/static/v1?label=specification&message=v0.7.0&color=yellow&style=for-the-badge" />
17
17
  </a>
18
18
  <!-- x-release-please-start-version -->
19
- <a href="https://github.com/open-feature/js-sdk/releases/tag/web-sdk-v0.4.11">
20
- <img alt="Release" src="https://img.shields.io/static/v1?label=release&message=v0.4.11&color=blue&style=for-the-badge" />
19
+ <a href="https://github.com/open-feature/js-sdk/releases/tag/web-sdk-v0.4.13">
20
+ <img alt="Release" src="https://img.shields.io/static/v1?label=release&message=v0.4.13&color=blue&style=for-the-badge" />
21
21
  </a>
22
22
  <!-- x-release-please-end -->
23
23
  <br/>
@@ -89,16 +89,16 @@ See [here](https://open-feature.github.io/js-sdk/modules/_openfeature_web_sdk.ht
89
89
 
90
90
  ## 🌟 Features
91
91
 
92
- | Status | Features | Description |
93
- | ------ | ------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
94
- | ✅ | [Providers](#providers) | Integrate with a commercial, open source, or in-house feature management tool. |
95
- | ✅ | [Targeting](#targeting-and-context) | Contextually-aware flag evaluation using [evaluation context](https://openfeature.dev/docs/reference/concepts/evaluation-context). |
96
- | ✅ | [Hooks](#hooks) | Add functionality to various stages of the flag evaluation life-cycle. |
97
- | ✅ | [Logging](#logging) | Integrate with popular logging packages. |
98
- | ✅ | [Named clients](#named-clients) | Utilize multiple providers in a single application. |
99
- | ✅ | [Eventing](#eventing) | React to state changes in the provider or flag management system. |
100
- | ✅ | [Shutdown](#shutdown) | Gracefully clean up a provider during application shutdown. |
101
- | ✅ | [Extending](#extending) | Extend OpenFeature with custom providers and hooks. |
92
+ | Status | Features | Description |
93
+ | ------ | ----------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
94
+ | ✅ | [Providers](#providers) | Integrate with a commercial, open source, or in-house feature management tool. |
95
+ | ✅ | [Targeting](#targeting-and-context) | Contextually-aware flag evaluation using [evaluation context](https://openfeature.dev/docs/reference/concepts/evaluation-context). |
96
+ | ✅ | [Hooks](#hooks) | Add functionality to various stages of the flag evaluation life-cycle. |
97
+ | ✅ | [Logging](#logging) | Integrate with popular logging packages. |
98
+ | ✅ | [Domains](#domains) | Logically bind clients with providers. |
99
+ | ✅ | [Eventing](#eventing) | React to state changes in the provider or flag management system. |
100
+ | ✅ | [Shutdown](#shutdown) | Gracefully clean up a provider during application shutdown. |
101
+ | ✅ | [Extending](#extending) | Extend OpenFeature with custom providers and hooks. |
102
102
 
103
103
  <sub>Implemented: ✅ | In-progress: ⚠️ | Not implemented yet: ❌</sub>
104
104
 
@@ -129,7 +129,7 @@ OpenFeature.setProvider(new MyProvider());
129
129
  Once the provider has been registered, the status can be tracked using [events](#eventing).
130
130
 
131
131
  In some situations, it may be beneficial to register multiple providers in the same application.
132
- This is possible using [named clients](#named-clients), which is covered in more detail below.
132
+ This is possible using [domains](#domains), which is covered in more detail below.
133
133
 
134
134
  ### Flag evaluation flow
135
135
 
@@ -159,12 +159,13 @@ await OpenFeature.setContext({ origin: document.location.host });
159
159
  ```
160
160
 
161
161
  Context is global and setting it is `async`.
162
- Providers may implement an `onContextChanged` method that receives the old context and the newer one.
163
- This method is used internally by the provider to detect if, given the context change, the flags values cached on client side are invalid. If needed a request will be made to the provider with the new context in order to get the correct flags values.
162
+ Providers may implement an `onContextChanged` method that receives the old and newer contexts.
163
+ Given a context change, providers can use this method internally to detect if the flag values cached on the client are still valid.
164
+ If needed, a request will be made to the provider with the new context in order to get the correct flag values.
164
165
 
165
166
  ### Hooks
166
167
 
167
- [Hooks](https://openfeature.dev/docs/reference/concepts/hooks) allow for custom logic to be added at well-defined points of the flag evaluation life-cycle
168
+ [Hooks](https://openfeature.dev/docs/reference/concepts/hooks) allow for custom logic to be added at well-defined points of the flag evaluation life-cycle.
168
169
  Look [here](https://openfeature.dev/ecosystem/?instant_search%5BrefinementList%5D%5Btype%5D%5B0%5D=Hook&instant_search%5BrefinementList%5D%5Bcategory%5D%5B0%5D=Client-side&instant_search%5BrefinementList%5D%5Btechnology%5D%5B0%5D=JavaScript) for a complete list of available hooks.
169
170
  If the hook you're looking for hasn't been created yet, see the [develop a hook](#develop-a-hook) section to learn how to build it yourself.
170
171
 
@@ -186,7 +187,7 @@ const boolValue = client.getBooleanValue("bool-flag", false, { hooks: [new Examp
186
187
 
187
188
  ### Logging
188
189
 
189
- The JS SDK will log warnings and errors to the console by default.
190
+ The Web SDK will log warnings and errors to the console by default.
190
191
  This behavior can be overridden by passing a custom logger either globally or per client.
191
192
  A custom logger must implement the [Logger interface](../shared/src/logger/logger.ts).
192
193
 
@@ -204,26 +205,29 @@ const client = OpenFeature.getClient();
204
205
  client.setLogger(logger);
205
206
  ```
206
207
 
207
- ### Named clients
208
+ ### Domains
208
209
 
209
- Clients can be given a name.
210
- A name is a logical identifier that can be used to associate clients with a particular provider.
211
- If a name has no associated provider, the global provider is used.
210
+ Clients can be assigned to a domain.
211
+ A domain is a logical identifier which can be used to associate clients with a particular provider.
212
+ If a domain has no associated provider, the default provider is used.
212
213
 
213
214
  ```ts
214
- import { OpenFeature } from "@openfeature/web-sdk";
215
+ import { OpenFeature, InMemoryProvider } from "@openfeature/web-sdk";
215
216
 
216
217
  // Registering the default provider
217
- OpenFeature.setProvider(NewLocalProvider());
218
- // Registering a named provider
219
- OpenFeature.setProvider("clientForCache", new NewCachedProvider());
218
+ OpenFeature.setProvider(InMemoryProvider(myFlags));
219
+ // Registering a provider to a domain
220
+ OpenFeature.setProvider("my-domain", new InMemoryProvider(someOtherFlags));
220
221
 
221
- // A Client backed by default provider
222
+ // A Client bound to the default provider
222
223
  const clientWithDefault = OpenFeature.getClient();
223
- // A Client backed by NewCachedProvider
224
- const clientForCache = OpenFeature.getClient("clientForCache");
224
+ // A Client bound to the InMemoryProvider provider
225
+ const domainScopedClient = OpenFeature.getClient("my-domain");
225
226
  ```
226
227
 
228
+ Domains can be defined on a provider during registration.
229
+ For more details, please refer to the [providers](#providers) section.
230
+
227
231
  ### Eventing
228
232
 
229
233
  Events allow you to react to state changes in the provider or underlying flag management system, such as flag definition changes, provider readiness, or error conditions.
@@ -267,43 +271,52 @@ This can be a new repository or included in [the existing contrib repository](ht
267
271
  You’ll then need to write the provider by implementing the [Provider interface](./src/provider/provider.ts) exported by the OpenFeature SDK.
268
272
 
269
273
  ```ts
270
- import { JsonValue, Provider, ResolutionDetails } from '@openfeature/web-sdk';
274
+ import {
275
+ AnyProviderEvent,
276
+ EvaluationContext,
277
+ Hook,
278
+ JsonValue,
279
+ Logger,
280
+ Provider,
281
+ ProviderEventEmitter,
282
+ ProviderStatus,
283
+ ResolutionDetails
284
+ } from '@openfeature/web-sdk';
271
285
 
272
286
  // implement the provider interface
273
287
  class MyProvider implements Provider {
274
288
  // Adds runtime validation that the provider is used with the expected SDK
275
289
  public readonly runsOn = 'client';
276
-
277
290
  readonly metadata = {
278
291
  name: 'My Provider',
279
292
  } as const;
280
-
281
293
  // Optional provider managed hooks
282
- hooks?: Hook<FlagValue>[];
283
-
294
+ hooks?: Hook[];
284
295
  resolveBooleanEvaluation(flagKey: string, defaultValue: boolean, context: EvaluationContext, logger: Logger): ResolutionDetails<boolean> {
285
296
  // code to evaluate a boolean
286
297
  }
287
-
288
298
  resolveStringEvaluation(flagKey: string, defaultValue: string, context: EvaluationContext, logger: Logger): ResolutionDetails<string> {
289
299
  // code to evaluate a string
290
300
  }
291
-
292
301
  resolveNumberEvaluation(flagKey: string, defaultValue: number, context: EvaluationContext, logger: Logger): ResolutionDetails<number> {
293
302
  // code to evaluate a number
294
303
  }
295
-
296
304
  resolveObjectEvaluation<T extends JsonValue>(flagKey: string, defaultValue: T, context: EvaluationContext, logger: Logger): ResolutionDetails<T> {
297
305
  // code to evaluate an object
298
306
  }
299
307
 
308
+ onContextChange?(oldContext: EvaluationContext, newContext: EvaluationContext): Promise<void> {
309
+ // reconcile the provider's cached flags, if applicable
310
+ }
311
+
300
312
  status?: ProviderStatus | undefined;
301
- events?: OpenFeatureEventEmitter | undefined;
313
+
314
+ // implement with "new OpenFeatureEventEmitter()", and use "emit()" to emit events
315
+ events?: ProviderEventEmitter<AnyProviderEvent> | undefined;
302
316
 
303
317
  initialize?(context?: EvaluationContext | undefined): Promise<void> {
304
318
  // code to initialize your provider
305
319
  }
306
-
307
320
  onClose?(): Promise<void> {
308
321
  // code to shut down your provider
309
322
  }
package/dist/cjs/index.js CHANGED
@@ -474,11 +474,11 @@ var import_core5 = require("@openfeature/core");
474
474
 
475
475
  // src/provider/in-memory-provider/variant-not-found-error.ts
476
476
  var import_core4 = require("@openfeature/core");
477
- var VariantNotFoundError = class extends import_core4.OpenFeatureError {
477
+ var VariantNotFoundError = class _VariantNotFoundError extends import_core4.OpenFeatureError {
478
478
  code;
479
479
  constructor(message) {
480
480
  super(message);
481
- Object.setPrototypeOf(this, VariantNotFoundError.prototype);
481
+ Object.setPrototypeOf(this, _VariantNotFoundError.prototype);
482
482
  this.name = "VariantNotFoundError";
483
483
  this.code = import_core4.ErrorCode.GENERAL;
484
484
  }
@@ -588,11 +588,10 @@ var InMemoryProvider = class {
588
588
  // src/open-feature.ts
589
589
  var GLOBAL_OPENFEATURE_API_KEY = Symbol.for("@openfeature/web-sdk/api");
590
590
  var _globalThis = globalThis;
591
- var OpenFeatureAPI = class extends import_core6.OpenFeatureCommonAPI {
591
+ var OpenFeatureAPI = class _OpenFeatureAPI extends import_core6.OpenFeatureCommonAPI {
592
592
  _events = new OpenFeatureEventEmitter();
593
593
  _defaultProvider = NOOP_PROVIDER;
594
594
  _createEventEmitter = () => new OpenFeatureEventEmitter();
595
- _namedProviderContext = /* @__PURE__ */ new Map();
596
595
  constructor() {
597
596
  super("client");
598
597
  }
@@ -606,64 +605,64 @@ var OpenFeatureAPI = class extends import_core6.OpenFeatureCommonAPI {
606
605
  if (globalApi) {
607
606
  return globalApi;
608
607
  }
609
- const instance = new OpenFeatureAPI();
608
+ const instance = new _OpenFeatureAPI();
610
609
  _globalThis[GLOBAL_OPENFEATURE_API_KEY] = instance;
611
610
  return instance;
612
611
  }
613
- async setContext(nameOrContext, contextOrUndefined) {
614
- const clientName = (0, import_core6.stringOrUndefined)(nameOrContext);
615
- const context = (0, import_core6.objectOrUndefined)(nameOrContext) ?? (0, import_core6.objectOrUndefined)(contextOrUndefined) ?? {};
616
- if (clientName) {
617
- const provider = this._clientProviders.get(clientName);
612
+ async setContext(domainOrContext, contextOrUndefined) {
613
+ const domain = (0, import_core6.stringOrUndefined)(domainOrContext);
614
+ const context = (0, import_core6.objectOrUndefined)(domainOrContext) ?? (0, import_core6.objectOrUndefined)(contextOrUndefined) ?? {};
615
+ if (domain) {
616
+ const provider = this._domainScopedProviders.get(domain);
618
617
  if (provider) {
619
- const oldContext = this.getContext(clientName);
620
- this._namedProviderContext.set(clientName, context);
621
- await this.runProviderContextChangeHandler(clientName, provider, oldContext, context);
618
+ const oldContext = this.getContext(domain);
619
+ this._domainScopedContext.set(domain, context);
620
+ await this.runProviderContextChangeHandler(domain, provider, oldContext, context);
622
621
  } else {
623
- this._namedProviderContext.set(clientName, context);
622
+ this._domainScopedContext.set(domain, context);
624
623
  }
625
624
  } else {
626
625
  const oldContext = this._context;
627
626
  this._context = context;
628
- const defaultContextNameProviders = Array.from(this._clientProviders.entries()).filter(([name]) => !this._namedProviderContext.has(name)).reduce((acc, [name, provider]) => {
629
- acc.push({ name, provider });
627
+ const unboundProviders = Array.from(this._domainScopedProviders.entries()).filter(([domain2]) => !this._domainScopedContext.has(domain2)).reduce((acc, [domain2, provider]) => {
628
+ acc.push({ domain: domain2, provider });
630
629
  return acc;
631
630
  }, []);
632
631
  const allProviders = [
633
- // add in the default (no name)
634
- { name: void 0, provider: this._defaultProvider },
635
- ...defaultContextNameProviders
632
+ // add in the default (no domain)
633
+ { domain: void 0, provider: this._defaultProvider },
634
+ ...unboundProviders
636
635
  ];
637
636
  await Promise.all(
638
637
  allProviders.map(
639
- (tuple) => this.runProviderContextChangeHandler(tuple.name, tuple.provider, oldContext, context)
638
+ (tuple) => this.runProviderContextChangeHandler(tuple.domain, tuple.provider, oldContext, context)
640
639
  )
641
640
  );
642
641
  }
643
642
  }
644
- getContext(nameOrUndefined) {
645
- const clientName = (0, import_core6.stringOrUndefined)(nameOrUndefined);
646
- if (clientName) {
647
- const context = this._namedProviderContext.get(clientName);
643
+ getContext(domainOrUndefined) {
644
+ const domain = (0, import_core6.stringOrUndefined)(domainOrUndefined);
645
+ if (domain) {
646
+ const context = this._domainScopedContext.get(domain);
648
647
  if (context) {
649
648
  return context;
650
649
  } else {
651
- this._logger.debug(`Unable to find context for '${clientName}'.`);
650
+ this._logger.debug(`Unable to find context for '${domain}'.`);
652
651
  }
653
652
  }
654
653
  return this._context;
655
654
  }
656
- async clearContext(nameOrUndefined) {
657
- const clientName = (0, import_core6.stringOrUndefined)(nameOrUndefined);
658
- if (clientName) {
659
- const provider = this._clientProviders.get(clientName);
655
+ async clearContext(domainOrUndefined) {
656
+ const domain = (0, import_core6.stringOrUndefined)(domainOrUndefined);
657
+ if (domain) {
658
+ const provider = this._domainScopedProviders.get(domain);
660
659
  if (provider) {
661
- const oldContext = this.getContext(clientName);
662
- this._namedProviderContext.delete(clientName);
660
+ const oldContext = this.getContext(domain);
661
+ this._domainScopedContext.delete(domain);
663
662
  const newContext = this.getContext();
664
- await this.runProviderContextChangeHandler(clientName, provider, oldContext, newContext);
663
+ await this.runProviderContextChangeHandler(domain, provider, oldContext, newContext);
665
664
  } else {
666
- this._namedProviderContext.delete(clientName);
665
+ this._domainScopedContext.delete(domain);
667
666
  }
668
667
  } else {
669
668
  return this.setContext({});
@@ -671,11 +670,11 @@ var OpenFeatureAPI = class extends import_core6.OpenFeatureCommonAPI {
671
670
  }
672
671
  /**
673
672
  * Resets the global evaluation context and removes the evaluation context for
674
- * all named clients.
673
+ * all domains.
675
674
  */
676
675
  async clearContexts() {
677
676
  await this.clearContext();
678
- await Promise.allSettled(Array.from(this._clientProviders.keys()).map((name) => this.clearContext(name)));
677
+ await Promise.allSettled(Array.from(this._domainScopedProviders.keys()).map((domain) => this.clearContext(domain)));
679
678
  }
680
679
  /**
681
680
  * A factory function for creating new named OpenFeature clients. Clients can contain
@@ -684,18 +683,18 @@ var OpenFeatureAPI = class extends import_core6.OpenFeatureCommonAPI {
684
683
  *
685
684
  * If there is already a provider bound to this name via {@link this.setProvider setProvider}, this provider will be used.
686
685
  * Otherwise, the default provider is used until a provider is assigned to that name.
687
- * @param {string} name The name of the client
686
+ * @param {string} domain An identifier which logically binds clients with providers
688
687
  * @param {string} version The version of the client (only used for metadata)
689
688
  * @returns {Client} OpenFeature Client
690
689
  */
691
- getClient(name, version) {
690
+ getClient(domain, version) {
692
691
  return new OpenFeatureClient(
693
692
  // functions are passed here to make sure that these values are always up to date,
694
693
  // and so we don't have to make these public properties on the API class.
695
- () => this.getProviderForClient(name),
696
- () => this.buildAndCacheEventEmitterForClient(name),
694
+ () => this.getProviderForClient(domain),
695
+ () => this.buildAndCacheEventEmitterForClient(domain),
697
696
  () => this._logger,
698
- { name, version }
697
+ { domain, version }
699
698
  );
700
699
  }
701
700
  /**
@@ -704,24 +703,24 @@ var OpenFeatureAPI = class extends import_core6.OpenFeatureCommonAPI {
704
703
  */
705
704
  async clearProviders() {
706
705
  await super.clearProvidersAndSetDefault(NOOP_PROVIDER);
707
- this._namedProviderContext.clear();
706
+ this._domainScopedContext.clear();
708
707
  }
709
- async runProviderContextChangeHandler(clientName, provider, oldContext, newContext) {
708
+ async runProviderContextChangeHandler(domain, provider, oldContext, newContext) {
710
709
  const providerName = provider.metadata.name;
711
710
  try {
712
711
  await provider.onContextChange?.(oldContext, newContext);
713
- this.getAssociatedEventEmitters(clientName).forEach((emitter) => {
714
- emitter?.emit(import_core2.ClientProviderEvents.ContextChanged, { clientName, providerName });
712
+ this.getAssociatedEventEmitters(domain).forEach((emitter) => {
713
+ emitter?.emit(import_core2.ClientProviderEvents.ContextChanged, { clientName: domain, domain, providerName });
715
714
  });
716
- this._events?.emit(import_core2.ClientProviderEvents.ContextChanged, { clientName, providerName });
715
+ this._events?.emit(import_core2.ClientProviderEvents.ContextChanged, { clientName: domain, domain, providerName });
717
716
  } catch (err) {
718
717
  const error = err;
719
718
  const message = `Error running ${provider?.metadata?.name}'s context change handler: ${error?.message}`;
720
719
  this._logger?.error(`${message}`, err);
721
- this.getAssociatedEventEmitters(clientName).forEach((emitter) => {
722
- emitter?.emit(import_core2.ClientProviderEvents.Error, { clientName, providerName, message });
720
+ this.getAssociatedEventEmitters(domain).forEach((emitter) => {
721
+ emitter?.emit(import_core2.ClientProviderEvents.Error, { clientName: domain, domain, providerName, message });
723
722
  });
724
- this._events?.emit(import_core2.ClientProviderEvents.Error, { clientName, providerName, message });
723
+ this._events?.emit(import_core2.ClientProviderEvents.Error, { clientName: domain, domain, providerName, message });
725
724
  }
726
725
  }
727
726
  };
@@ -739,7 +738,9 @@ var OpenFeatureClient = class {
739
738
  _clientLogger;
740
739
  get metadata() {
741
740
  return {
742
- name: this.options.name,
741
+ // Use domain if name is not provided
742
+ name: this.options.domain ?? this.options.name,
743
+ domain: this.options.domain ?? this.options.name,
743
744
  version: this.options.version,
744
745
  providerMetadata: this.providerAccessor().metadata
745
746
  };
@@ -752,7 +753,11 @@ var OpenFeatureClient = class {
752
753
  const shouldRunNow = (0, import_core7.statusMatchesEvent)(eventType, this._provider.status);
753
754
  if (shouldRunNow) {
754
755
  try {
755
- handler({ clientName: this.metadata.name, providerName: this._provider.metadata.name });
756
+ handler({
757
+ clientName: this.metadata.name,
758
+ domain: this.metadata.domain,
759
+ providerName: this._provider.metadata.name
760
+ });
756
761
  } catch (err) {
757
762
  this._logger?.error("Error running event handler:", err);
758
763
  }
@@ -826,7 +831,7 @@ var OpenFeatureClient = class {
826
831
  ];
827
832
  const allHooksReversed = [...allHooks].reverse();
828
833
  const context = {
829
- ...OpenFeature.getContext(this?.options?.name)
834
+ ...OpenFeature.getContext(this?.options?.domain)
830
835
  };
831
836
  const hookContext = {
832
837
  flagKey,