@openfeature/web-sdk 0.4.2 → 0.4.4

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
@@ -12,19 +12,19 @@
12
12
  <!-- x-hide-in-docs-end -->
13
13
  <!-- The 'github-badges' class is used in the docs -->
14
14
  <p align="center" class="github-badges">
15
- <a href="https://github.com/open-feature/spec/tree/v0.7.0">
15
+ <a href="https://github.com/open-feature/spec/releases/tag/v0.7.0">
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.2">
20
- <img alt="Release" src="https://img.shields.io/static/v1?label=release&message=v0.4.2&color=blue&style=for-the-badge" />
19
+ <a href="https://github.com/open-feature/js-sdk/releases/tag/web-sdk-v0.4.4">
20
+ <img alt="Release" src="https://img.shields.io/static/v1?label=release&message=v0.4.4&color=blue&style=for-the-badge" />
21
21
  </a>
22
22
  <!-- x-release-please-end -->
23
23
  <br/>
24
24
  <a href="https://www.repostatus.org/#wip">
25
25
  <img alt="Project Status" src="https://www.repostatus.org/badges/latest/wip.svg" />
26
26
  </a>
27
- <a href="https://open-feature.github.io/js-sdk/modules/OpenFeature_Web_SDK.html">
27
+ <a href="https://open-feature.github.io/js-sdk/modules/_openfeature_web_sdk.html">
28
28
  <img alt="API Reference" src="https://img.shields.io/badge/reference-teal?logo=javascript&logoColor=white" />
29
29
  </a>
30
30
  <a href="https://www.npmjs.com/package/@openfeature/web-sdk">
@@ -39,7 +39,7 @@
39
39
  </p>
40
40
  <!-- x-hide-in-docs-start -->
41
41
 
42
- [OpenFeature](https://openfeature.dev) is an open standard that provides a vendor-agnostic, community-driven API for feature flagging that works with your favorite feature flag management tool.
42
+ [OpenFeature](https://openfeature.dev) is an open specification that provides a vendor-agnostic, community-driven API for feature flagging that works with your favorite feature flag management tool.
43
43
 
44
44
  <!-- x-hide-in-docs-end -->
45
45
 
@@ -60,7 +60,8 @@ npm install --save @openfeature/web-sdk
60
60
  #### yarn
61
61
 
62
62
  ```sh
63
- yarn add @openfeature/web-sdk
63
+ # yarn requires manual installation of the @openfeature/core peer-dependency
64
+ yarn add @openfeature/web-sdk @openfeature/core
64
65
  ```
65
66
 
66
67
  ### Usage
@@ -69,7 +70,7 @@ yarn add @openfeature/web-sdk
69
70
  import { OpenFeature } from '@openfeature/web-sdk';
70
71
 
71
72
  // Register your feature flag provider
72
- OpenFeature.setProvider(new YourProviderOfChoice());
73
+ await OpenFeature.setProviderAndWait(new YourProviderOfChoice());
73
74
 
74
75
  // create a new client
75
76
  const client = OpenFeature.getClient();
@@ -84,7 +85,7 @@ if (v2Enabled) {
84
85
 
85
86
  ### API Reference
86
87
 
87
- See [here](https://open-feature.github.io/js-sdk/modules/OpenFeature_Web_SDK.html) for the complete API documentation.
88
+ See [here](https://open-feature.github.io/js-sdk/modules/_openfeature_web_sdk.html) for the complete API documentation.
88
89
 
89
90
  ## 🌟 Features
90
91
 
@@ -109,10 +110,24 @@ If the provider you're looking for hasn't been created yet, see the [develop a p
109
110
 
110
111
  Once you've added a provider as a dependency, it can be registered with OpenFeature like this:
111
112
 
113
+ #### Awaitable
114
+
115
+ To register a provider and ensure it is ready before further actions are taken, you can use the `setProviderAndWait` method as shown below:
116
+
117
+ ```ts
118
+ await OpenFeature.setProviderAndWait(new MyProvider());
119
+ ```
120
+
121
+ #### Synchronous
122
+
123
+ To register a provider in a synchronous manner, you can use the `setProvider` method as shown below:
124
+
112
125
  ```ts
113
- OpenFeature.setProvider(new MyProvider())
126
+ OpenFeature.setProvider(new MyProvider());
114
127
  ```
115
128
 
129
+ Once the provider has been registered, the status can be tracked using [events](#eventing).
130
+
116
131
  In some situations, it may be beneficial to register multiple providers in the same application.
117
132
  This is possible using [named clients](#named-clients), which is covered in more detail below.
118
133
 
@@ -130,22 +145,7 @@ sequenceDiagram
130
145
  In (1) the Client sends a request to the provider backend in order to get all values from all feature flags that it has.
131
146
  Once the provider backend replies (2) the client holds all flag values and therefore the flag evaluation process is synchronous.
132
147
 
133
- In order to prevent flag evaluation to the default value while flags are still being fetched, it is highly recommended to only look for flag value after the provider has emitted the `Ready` event.
134
- The following code snippet provides an example.
135
-
136
- ```ts
137
- import { OpenFeature, ProviderEvents } from '@openfeature/web-sdk';
138
-
139
- OpenFeature.setProvider( /*set a provider*/ );
140
-
141
- // OpenFeature API
142
- OpenFeature.addHandler(ProviderEvents.Ready, () => {
143
- const client = OpenFeature.getClient();
144
- const stringFlag = client.getStringValue('string-flag', "default value"))
145
-
146
- //use stringFlag from this point
147
- });
148
- ```
148
+ In order to prevent flag evaluations from defaulting while the provider is initializing, it is highly recommended to evaluate flags only after the provider is ready. This can be done using the `setProviderAndWait` method or using the `setProvider` method and listening for the `READY` [event](#eventing).
149
149
 
150
150
  ### Targeting and Context
151
151
 
@@ -237,13 +237,13 @@ import { OpenFeature, ProviderEvents } from '@openfeature/web-sdk';
237
237
 
238
238
  // OpenFeature API
239
239
  OpenFeature.addHandler(ProviderEvents.Ready, (eventDetails) => {
240
- console.log(`Ready event from: ${eventDetails?.clientName}:`, eventDetails);
240
+ console.log(`Ready event from: ${eventDetails?.providerName}:`, eventDetails);
241
241
  });
242
242
 
243
243
  // Specific client
244
244
  const client = OpenFeature.getClient();
245
245
  client.addHandler(ProviderEvents.Error, (eventDetails) => {
246
- console.log(`Error event from: ${eventDetails?.clientName}:`, eventDetails);
246
+ console.log(`Error event from: ${eventDetails?.providerName}:`, eventDetails);
247
247
  });
248
248
  ```
249
249
 
@@ -271,6 +271,8 @@ import { JsonValue, Provider, ResolutionDetails } from '@openfeature/web-sdk';
271
271
 
272
272
  // implement the provider interface
273
273
  class MyProvider implements Provider {
274
+ // Adds runtime validation that the provider is used with the expected SDK
275
+ public readonly runsOn = 'client';
274
276
 
275
277
  readonly metadata = {
276
278
  name: 'My Provider',