Skip to content

Commit efa1a0d

Browse files
chore: add 0.6.0 examples to README.md (#441)
<!-- Please use this template for your pull request. --> <!-- Please use the sections that you need and delete other sections --> ## This PR <!-- add the description of the PR here --> Adds examples for named providers, events and the global shutdown of spec 0.6.0. It does not provide an example for flag metadata, as I think an example is not needed here. ### Related Issues <!-- add here the GitHub issue that this PR resolves if applicable --> Fixes #440 ### Notes <!-- any additional notes for this PR --> ### Follow-up Tasks <!-- anything that is related to this PR but not done here should be noted under this section --> <!-- if there is a need for a new issue, please link it here --> ### How to test <!-- if applicable, add testing instructions under this section --> --------- Signed-off-by: Lukas Reining <lukas.reining@codecentric.de> Signed-off-by: Michael Beemer <beeme1mr@users.noreply.github.com> Co-authored-by: Michael Beemer <beeme1mr@users.noreply.github.com>
1 parent 2a357c7 commit efa1a0d

File tree

2 files changed

+136
-9
lines changed

2 files changed

+136
-9
lines changed

packages/client/README.md

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
[![Project Status: WIP – Initial development is in progress, but there has not yet been a stable, usable release suitable for the public.](https://www.repostatus.org/badges/latest/wip.svg)](https://www.repostatus.org/#wip)
1313
[![npm version](https://badge.fury.io/js/@openfeature%2Fweb-sdk.svg)](https://www.npmjs.com/package/@openfeature/web-sdk)
14-
[![Specification](https://img.shields.io/static/v1?label=Specification&message=v0.5.2&color=yellow)](https://github.com/open-feature/spec/tree/v0.5.2)
14+
[![Specification](https://img.shields.io/static/v1?label=Specification&message=v0.6.0&color=yellow)](https://github.com/open-feature/spec/tree/v0.6.0)
1515

1616
## 🧪 This SDK is experimental
1717

@@ -22,11 +22,14 @@ For more information, see this [issue](https://github.com/open-feature/spec/issu
2222

2323
### What is OpenFeature?
2424

25-
[OpenFeature][openfeature-website] is an open standard that provides a vendor-agnostic, community-driven API for feature flagging that works with your favorite feature flag management tool.
25+
[OpenFeature][openfeature-website] is an open standard that provides a vendor-agnostic, community-driven API for feature
26+
flagging that works with your favorite feature flag management tool.
2627

2728
### Why standardize feature flags?
2829

29-
Standardizing feature flags unifies tools and vendors behind a common interface which avoids vendor lock-in at the code level. Additionally, it offers a framework for building extensions and integrations and allows providers to focus on their unique value proposition.
30+
Standardizing feature flags unifies tools and vendors behind a common interface which avoids vendor lock-in at the code
31+
level. Additionally, it offers a framework for building extensions and integrations and allows providers to focus on
32+
their unique value proposition.
3033

3134
## 🔍 Requirements:
3235

@@ -72,7 +75,8 @@ const boolValue = client.getBooleanValue('boolFlag', false);
7275

7376
### Context-aware evaluation:
7477

75-
Sometimes the value of a flag must take into account some dynamic criteria about the application or user, such as the user location, IP, email address, or the location of the server.
78+
Sometimes the value of a flag must take into account some dynamic criteria about the application or user, such as the
79+
user location, IP, email address, or the location of the server.
7680
In OpenFeature, we refer to this as [`targeting`](https://openfeature.dev/specification/glossary#targeting).
7781
If the flag system you're using supports targeting, you can provide the input data using the `EvaluationContext`.
7882

@@ -86,15 +90,18 @@ const boolValue = client.getBooleanValue('some-flag', false);
8690

8791
### Providers:
8892

89-
To develop a provider, you need to create a new project and include the OpenFeature SDK as a dependency. This can be a new repository or included in an existing contrib repository available under the OpenFeature organization. Finally, you’ll then need to write the provider itself. In most languages, this can be accomplished by implementing the provider interface exported by the OpenFeature SDK.
93+
To develop a provider, you need to create a new project and include the OpenFeature SDK as a dependency. This can be a
94+
new repository or included in an existing contrib repository available under the OpenFeature organization. Finally,
95+
you’ll then need to write the provider itself. In most languages, this can be accomplished by implementing the provider
96+
interface exported by the OpenFeature SDK.
9097

9198
```typescript
9299
import { JsonValue, Provider, ResolutionDetails } from '@openfeature/web-sdk';
93100

94101
// implement the provider interface
95102
class MyProvider implements Provider {
96103
readonly metadata = {
97-
name: 'My Provider',
104+
name: 'My Provider'
98105
} as const;
99106

100107
resolveBooleanEvaluation(flagKey: string, defaultValue: boolean): ResolutionDetails<boolean> {
@@ -118,7 +125,9 @@ See [here](https://openfeature.dev/docs/reference/technologies/server/javascript
118125
119126
### Hooks:
120127
121-
Hooks are a mechanism that allow for the addition of arbitrary behavior at well-defined points of the flag evaluation life-cycle. Use cases include validation of the resolved flag value, modifying or adding data to the evaluation context, logging, telemetry, and tracking.
128+
Hooks are a mechanism that allow for the addition of arbitrary behavior at well-defined points of the flag evaluation
129+
life-cycle. Use cases include validation of the resolved flag value, modifying or adding data to the evaluation context,
130+
logging, telemetry, and tracking.
122131
123132
```typescript
124133
import { OpenFeature, Hook, HookContext } from '@openfeature/web-sdk';
@@ -136,20 +145,24 @@ See [here](https://openfeature.dev/docs/reference/technologies/server/javascript
136145
137146
### Logging:
138147
139-
You can implement the `Logger` interface (compatible with the `console` object, and implementations from common logging libraries such as [winston](https://www.npmjs.com/package/winston)) and set it on the global API object.
148+
You can implement the `Logger` interface (compatible with the `console` object, and implementations from common logging
149+
libraries such as [winston](https://www.npmjs.com/package/winston)) and set it on the global API object.
140150
141151
```typescript
142152
// implement logger
143153
class MyLogger implements Logger {
144154
error(...args: unknown[]): void {
145155
// implement me
146156
}
157+
147158
warn(...args: unknown[]): void {
148159
// implement me
149160
}
161+
150162
info(...args: unknown[]): void {
151163
// implement me
152164
}
165+
153166
debug(...args: unknown[]): void {
154167
// implement me
155168
}
@@ -159,6 +172,63 @@ class MyLogger implements Logger {
159172
OpenFeature.setLogger(new MyLogger());
160173
```
161174
175+
### Named clients:
176+
177+
You can have several clients, that can be referenced by a name.
178+
Every client can have a different provider assigned. If no provider is assigned to a named client, the global default
179+
provider is used.
180+
181+
```typescript
182+
import { OpenFeature, ProviderEvents } from '@openfeature/web-sdk';
183+
184+
OpenFeature.setProvider(new YourProviderOfChoice())
185+
OpenFeature.setProvider("client-1", new YourOtherProviderOfChoice())
186+
187+
// Uses YourProviderOfChoice (the default)
188+
const unnamedClient = OpenFeature.getClient()
189+
190+
// Uses YourOtherProviderOfChoice as it is set explicitly
191+
const client1 = OpenFeature.getClient("client-1")
192+
193+
// Uses YourProviderOfChoice as no provider is set
194+
const client2 = OpenFeature.getClient("client-2")
195+
```
196+
197+
### Events:
198+
199+
Events provide a way to react to state changes in the provider or underlying flag management system.
200+
You can listen to events of either the OpenFeature API or individual clients.
201+
202+
The events after initialization, `PROVIDER_READY` on success, `PROVIDER_ERROR` on failure during initialization,
203+
are dispatched for every provider.
204+
However, other event types may not be supported by your provider.
205+
Please refer to the documentation of the provider you're using to see what events are supported.
206+
207+
```typescript
208+
import { OpenFeature, ProviderEvents } from '@openfeature/web-sdk';
209+
210+
// OpenFeature API
211+
OpenFeature.addHandler(ProviderEvents.Ready, (eventDetails) => {
212+
console.log(`Ready event from: ${eventDetails.clientName}:`, eventDetails);
213+
});
214+
215+
// Specific client
216+
const client = OpenFeature.getClient();
217+
client.addHandler(ProviderEvents.Error, async (eventDetails) => {
218+
console.log(`Error event from: ${eventDetails.clientName}:`, eventDetails);
219+
});
220+
```
221+
222+
### Shutdown:
223+
224+
The OpenFeature API provides a close function to perform a cleanup of all providers attached to any client.
225+
226+
```typescript
227+
import { OpenFeature, ProviderEvents } from '@openfeature/web-sdk';
228+
229+
await OpenFeature.close()
230+
```
231+
162232
### Complete API documentation:
163233
164234
See [here](https://open-feature.github.io/js-sdk/modules/OpenFeature_Web_SDK.html) for the complete API documentation.

packages/server/README.md

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
[![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
1313
[![npm version](https://badge.fury.io/js/@openfeature%2Fjs-sdk.svg)](https://www.npmjs.com/package/@openfeature/js-sdk)
14-
[![Specification](https://img.shields.io/static/v1?label=Specification&message=v0.5.2&color=yellow)](https://github.com/open-feature/spec/tree/v0.5.2)
14+
[![Specification](https://img.shields.io/static/v1?label=Specification&message=v0.6.0&color=yellow)](https://github.com/open-feature/spec/tree/v0.6.0)
1515

1616
## 👋 Hey there! Thanks for checking out the OpenFeature Server SDK
1717

@@ -161,6 +161,63 @@ class MyLogger implements Logger {
161161
OpenFeature.setLogger(new MyLogger());
162162
```
163163
164+
### Named clients:
165+
166+
You can have several clients, that can be referenced by a name.
167+
Every client can have a different provider assigned. If no provider is assigned to a named client, the global default
168+
provider is used.
169+
170+
```typescript
171+
import { OpenFeature, ProviderEvents } from '@openfeature/web-sdk';
172+
173+
OpenFeature.setProvider(new YourProviderOfChoice())
174+
OpenFeature.setProvider("client-1", new YourOtherProviderOfChoice())
175+
176+
// Uses YourProviderOfChoice (the default)
177+
const unnamedClient = OpenFeature.getClient()
178+
179+
// Uses YourOtherProviderOfChoice as it is set explicitly
180+
const client1 = OpenFeature.getClient("client-1")
181+
182+
// Uses YourProviderOfChoice as no provider is set
183+
const client2 = OpenFeature.getClient("client-2")
184+
```
185+
186+
### Events:
187+
188+
Events provide a way to react to state changes in the provider or underlying flag management system.
189+
You can listen to events of either the OpenFeature API or individual clients.
190+
191+
The events after initialization, `PROVIDER_READY` on success, `PROVIDER_ERROR` on failure during initialization,
192+
are dispatched for every provider.
193+
However, other event types may not be supported by your provider.
194+
Please refer to the documentation of the provider you're using to see what events are supported.
195+
196+
```typescript
197+
import { OpenFeature, ProviderEvents } from '@openfeature/web-sdk';
198+
199+
// OpenFeature API
200+
OpenFeature.addHandler(ProviderEvents.Ready, (eventDetails) => {
201+
console.log(`Ready event from: ${eventDetails.clientName}:`, eventDetails);
202+
});
203+
204+
// Specific client
205+
const client = OpenFeature.getClient();
206+
client.addHandler(ProviderEvents.Error, async (eventDetails) => {
207+
console.log(`Error event from: ${eventDetails.clientName}:`, eventDetails);
208+
});
209+
```
210+
211+
### Shutdown:
212+
213+
The OpenFeature API provides a close function to perform a cleanup of all providers attached to any client.
214+
215+
```typescript
216+
import { OpenFeature, ProviderEvents } from '@openfeature/web-sdk';
217+
218+
await OpenFeature.close()
219+
```
220+
164221
### Complete API documentation:
165222
166223
See [here](https://open-feature.github.io/js-sdk/modules/OpenFeature_JS_SDK.html) for the complete API documentation.

0 commit comments

Comments
 (0)