@openfeature/web-sdk
Advanced tools
Comparing version 0.4.3 to 0.4.4
{ | ||
"name": "@openfeature/web-sdk", | ||
"version": "0.4.3", | ||
"version": "0.4.4", | ||
"description": "OpenFeature SDK for Web", | ||
@@ -49,7 +49,7 @@ "main": "./dist/cjs/index.js", | ||
"peerDependencies": { | ||
"@openfeature/core": "0.0.16" | ||
"@openfeature/core": "0.0.17" | ||
}, | ||
"devDependencies": { | ||
"@openfeature/core": "0.0.16" | ||
"@openfeature/core": "0.0.17" | ||
} | ||
} |
@@ -15,8 +15,8 @@ <!-- markdownlint-disable MD033 --> | ||
<p align="center" class="github-badges"> | ||
<a href="https://github.com/open-feature/spec/tree/v0.7.0"> | ||
<a href="https://github.com/open-feature/spec/releases/tag/v0.7.0"> | ||
<img alt="Specification" src="https://img.shields.io/static/v1?label=specification&message=v0.7.0&color=yellow&style=for-the-badge" /> | ||
</a> | ||
<!-- x-release-please-start-version --> | ||
<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 href="https://github.com/open-feature/js-sdk/releases/tag/web-sdk-v0.4.4"> | ||
<img alt="Release" src="https://img.shields.io/static/v1?label=release&message=v0.4.4&color=blue&style=for-the-badge" /> | ||
</a> | ||
@@ -43,3 +43,3 @@ <!-- x-release-please-end --> | ||
[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. | ||
[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. | ||
@@ -65,3 +65,4 @@ <!-- x-hide-in-docs-end --> | ||
```sh | ||
yarn add @openfeature/web-sdk | ||
# yarn requires manual installation of the @openfeature/core peer-dependency | ||
yarn add @openfeature/web-sdk @openfeature/core | ||
``` | ||
@@ -75,3 +76,3 @@ | ||
// Register your feature flag provider | ||
OpenFeature.setProvider(new YourProviderOfChoice()); | ||
await OpenFeature.setProviderAndWait(new YourProviderOfChoice()); | ||
@@ -116,6 +117,20 @@ // create a new client | ||
#### Awaitable | ||
To register a provider and ensure it is ready before further actions are taken, you can use the `setProviderAndWait` method as shown below: | ||
```ts | ||
OpenFeature.setProvider(new MyProvider()) | ||
await OpenFeature.setProviderAndWait(new MyProvider()); | ||
``` | ||
#### Synchronous | ||
To register a provider in a synchronous manner, you can use the `setProvider` method as shown below: | ||
```ts | ||
OpenFeature.setProvider(new MyProvider()); | ||
``` | ||
Once the provider has been registered, the status can be tracked using [events](#eventing). | ||
In some situations, it may be beneficial to register multiple providers in the same application. | ||
@@ -138,19 +153,4 @@ This is possible using [named clients](#named-clients), which is covered in more detail below. | ||
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. | ||
The following code snippet provides an example. | ||
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). | ||
```ts | ||
import { OpenFeature, ProviderEvents } from '@openfeature/web-sdk'; | ||
OpenFeature.setProvider( /*set a provider*/ ); | ||
// OpenFeature API | ||
OpenFeature.addHandler(ProviderEvents.Ready, () => { | ||
const client = OpenFeature.getClient(); | ||
const stringFlag = client.getStringValue('string-flag', "default value")) | ||
//use stringFlag from this point | ||
}); | ||
``` | ||
### Targeting and Context | ||
@@ -246,3 +246,3 @@ | ||
OpenFeature.addHandler(ProviderEvents.Ready, (eventDetails) => { | ||
console.log(`Ready event from: ${eventDetails?.clientName}:`, eventDetails); | ||
console.log(`Ready event from: ${eventDetails?.providerName}:`, eventDetails); | ||
}); | ||
@@ -253,3 +253,3 @@ | ||
client.addHandler(ProviderEvents.Error, (eventDetails) => { | ||
console.log(`Error event from: ${eventDetails?.clientName}:`, eventDetails); | ||
console.log(`Error event from: ${eventDetails?.providerName}:`, eventDetails); | ||
}); | ||
@@ -282,2 +282,4 @@ ``` | ||
class MyProvider implements Provider { | ||
// Adds runtime validation that the provider is used with the expected SDK | ||
public readonly runsOn = 'client'; | ||
@@ -284,0 +286,0 @@ readonly metadata = { |
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
180583
332