@openfeature/web-sdk 0.4.0 → 0.4.1

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
@@ -1,232 +1,329 @@
1
1
  <!-- markdownlint-disable MD033 -->
2
+ <!-- x-hide-in-docs-start -->
2
3
  <p align="center">
3
4
  <picture>
4
- <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/open-feature/community/0e23508c163a6a1ac8c0ced3e4bd78faafe627c7/assets/logo/horizontal/white/openfeature-horizontal-white.svg">
5
- <source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/open-feature/community/0e23508c163a6a1ac8c0ced3e4bd78faafe627c7/assets/logo/horizontal/black/openfeature-horizontal-black.svg">
6
- <img align="center" alt="OpenFeature Logo">
5
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/open-feature/community/0e23508c163a6a1ac8c0ced3e4bd78faafe627c7/assets/logo/horizontal/white/openfeature-horizontal-white.svg" />
6
+ <img align="center" alt="OpenFeature Logo" src="https://raw.githubusercontent.com/open-feature/community/0e23508c163a6a1ac8c0ced3e4bd78faafe627c7/assets/logo/horizontal/black/openfeature-horizontal-black.svg" />
7
7
  </picture>
8
8
  </p>
9
9
 
10
10
  <h2 align="center">OpenFeature Web SDK</h2>
11
11
 
12
- [![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)
13
- [![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.6.0&color=yellow)](https://github.com/open-feature/spec/tree/v0.6.0)
15
-
16
- ## 👋 Hey there! Thanks for checking out the OpenFeature Web SDK
17
-
18
- ### What is OpenFeature?
12
+ <!-- x-hide-in-docs-end -->
13
+ <!-- The 'github-badges' class is used in the docs -->
14
+ <p align="center" class="github-badges">
15
+ <a href="https://github.com/open-feature/spec/tree/v0.7.0">
16
+ <img alt="Specification" src="https://img.shields.io/static/v1?label=specification&message=v0.7.0&color=yellow&style=for-the-badge" />
17
+ </a>
18
+ <!-- x-release-please-start-version -->
19
+ <a href="https://github.com/open-feature/js-sdk/releases/tag/web-sdk-v0.4.1">
20
+ <img alt="Release" src="https://img.shields.io/static/v1?label=release&message=v0.4.1&color=blue&style=for-the-badge" />
21
+ </a>
22
+ <!-- x-release-please-end -->
23
+ <br/>
24
+ <a href="https://www.repostatus.org/#wip">
25
+ <img alt="Project Status" src="https://www.repostatus.org/badges/latest/wip.svg" />
26
+ </a>
27
+ <a href="https://open-feature.github.io/js-sdk/modules/OpenFeature_Web_SDK.html">
28
+ <img alt="API Reference" src="https://img.shields.io/badge/reference-teal?logo=javascript&logoColor=white" />
29
+ </a>
30
+ <a href="https://www.npmjs.com/package/@openfeature/web-sdk">
31
+ <img alt="NPM Download" src="https://img.shields.io/npm/dm/%40openfeature%2Fweb-sdk" />
32
+ </a>
33
+ <a href="https://codecov.io/gh/open-feature/js-sdk">
34
+ <img alt="codecov" src="https://codecov.io/gh/open-feature/js-sdk/branch/main/graph/badge.svg?token=3DC5XOEHMY" />
35
+ </a>
36
+ <a href="https://bestpractices.coreinfrastructure.org/projects/6594">
37
+ <img alt="CII Best Practices" src="https://bestpractices.coreinfrastructure.org/projects/6594/badge" />
38
+ </a>
39
+ </p>
40
+ <!-- x-hide-in-docs-start -->
19
41
 
20
- [OpenFeature][openfeature-website] is an open standard that provides a vendor-agnostic, community-driven API for feature
21
- flagging that works with your favorite feature flag management tool.
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.
22
43
 
23
- ### Why standardize feature flags?
44
+ <!-- x-hide-in-docs-end -->
24
45
 
25
- Standardizing feature flags unifies tools and vendors behind a common interface which avoids vendor lock-in at the code
26
- level. Additionally, it offers a framework for building extensions and integrations and allows providers to focus on
27
- their unique value proposition.
46
+ ## 🚀 Quick start
28
47
 
29
- ## 🔍 Requirements:
48
+ ### Requirements
30
49
 
31
50
  - ES2015-compatible web browser (Chrome, Edge, Firefox, etc)
32
51
 
33
- ## 📦 Installation:
52
+ ### Install
34
53
 
35
- ### npm
54
+ #### npm
36
55
 
37
56
  ```sh
38
- npm install @openfeature/web-sdk
57
+ npm install --save @openfeature/web-sdk
39
58
  ```
40
59
 
41
- ### yarn
60
+ #### yarn
42
61
 
43
62
  ```sh
44
63
  yarn add @openfeature/web-sdk
45
64
  ```
46
65
 
47
- ## 🌟 Features:
66
+ ### Usage
48
67
 
49
- - support for various [providers](https://openfeature.dev/docs/reference/concepts/provider)
50
- - easy integration and extension via [hooks](https://openfeature.dev/docs/reference/concepts/hooks)
51
- - handle flags of any type: bool, string, numeric and object
52
- - [context-aware](https://openfeature.dev/docs/reference/concepts/evaluation-context) evaluation
68
+ ```ts
69
+ import { OpenFeature } from '@openfeature/web-sdk';
53
70
 
54
- ## 🚀 Usage:
71
+ // Register your feature flag provider
72
+ OpenFeature.setProvider(new YourProviderOfChoice());
55
73
 
56
- ### Basics:
74
+ // create a new client
75
+ const client = OpenFeature.getClient();
57
76
 
58
- ```typescript
59
- import { OpenFeature } from '@openfeature/web-sdk';
77
+ // Evaluate your feature flag
78
+ const v2Enabled = client.getBooleanValue('v2_enabled', false);
60
79
 
61
- // configure a provider
62
- await OpenFeature.setProvider(new YourProviderOfChoice());
80
+ if (v2Enabled) {
81
+ console.log("v2 is enabled");
82
+ }
83
+ ```
63
84
 
64
- // create a client
65
- const client = OpenFeature.getClient('my-app');
85
+ ### API Reference
66
86
 
67
- // get a bool flag value
68
- const boolValue = client.getBooleanValue('boolFlag', false);
69
- ```
87
+ See [here](https://open-feature.github.io/js-sdk/modules/OpenFeature_Web_SDK.html) for the complete API documentation.
70
88
 
71
- ### Context-aware evaluation:
89
+ ## 🌟 Features
72
90
 
73
- Sometimes the value of a flag must take into account some dynamic criteria about the application or user, such as the
74
- user location, IP, email address, or the location of the server.
75
- In OpenFeature, we refer to this as [`targeting`](https://openfeature.dev/specification/glossary#targeting).
76
- If the flag system you're using supports targeting, you can provide the input data using the `EvaluationContext`.
91
+ | Status | Features | Description |
92
+ | ------ | ------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
93
+ | ✅ | [Providers](#providers) | Integrate with a commercial, open source, or in-house feature management tool. |
94
+ | ✅ | [Targeting](#targeting-and-context) | Contextually-aware flag evaluation using [evaluation context](https://openfeature.dev/docs/reference/concepts/evaluation-context). |
95
+ | ✅ | [Hooks](#hooks) | Add functionality to various stages of the flag evaluation life-cycle. |
96
+ | ✅ | [Logging](#logging) | Integrate with popular logging packages. |
97
+ | ✅ | [Named clients](#named-clients) | Utilize multiple providers in a single application. |
98
+ | ✅ | [Eventing](#eventing) | React to state changes in the provider or flag management system. |
99
+ | ✅ | [Shutdown](#shutdown) | Gracefully clean up a provider during application shutdown. |
100
+ | ✅ | [Extending](#extending) | Extend OpenFeature with custom providers and hooks. |
77
101
 
78
- ```typescript
79
- // global context for static data
80
- await OpenFeature.setContext({ origin: document.location.host })
102
+ <sub>Implemented: ✅ | In-progress: ⚠️ | Not implemented yet: ❌</sub>
81
103
 
82
- // use contextual data to determine a flag value
83
- const boolValue = client.getBooleanValue('some-flag', false);
104
+ ### Providers
105
+
106
+ [Providers](https://openfeature.dev/docs/reference/concepts/provider) are an abstraction between a flag management system and the OpenFeature SDK.
107
+ Look [here](https://openfeature.dev/ecosystem/?instant_search%5BrefinementList%5D%5Btype%5D%5B0%5D=Provider&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 providers.
108
+ If the provider you're looking for hasn't been created yet, see the [develop a provider](#develop-a-provider) section to learn how to build it yourself.
109
+
110
+ Once you've added a provider as a dependency, it can be registered with OpenFeature like this:
111
+
112
+ ```ts
113
+ OpenFeature.setProvider(new MyProvider())
84
114
  ```
85
115
 
86
- ### Providers:
116
+ In some situations, it may be beneficial to register multiple providers in the same application.
117
+ This is possible using [named clients](#named-clients), which is covered in more detail below.
87
118
 
88
- To develop a provider, you need to create a new project and include the OpenFeature SDK as a dependency. This can be a
89
- new repository or included in an existing contrib repository available under the OpenFeature organization. Finally,
90
- you’ll then need to write the provider itself. In most languages, this can be accomplished by implementing the provider
91
- interface exported by the OpenFeature SDK.
119
+ ### Flag evaluation flow
92
120
 
93
- ```typescript
94
- import { JsonValue, Provider, ResolutionDetails } from '@openfeature/web-sdk';
121
+ When a new provider is added to OpenFeature client the following process happens:
95
122
 
96
- // implement the provider interface
97
- class MyProvider implements Provider {
98
- readonly metadata = {
99
- name: 'My Provider'
100
- } as const;
123
+ ```mermaid
124
+ sequenceDiagram
125
+ autonumber
126
+ Client-->+Feature Flag Provider: ResolveAll (context)
127
+ Feature Flag Provider-->-Client: Flags values
128
+ ```
101
129
 
102
- resolveBooleanEvaluation(flagKey: string, defaultValue: boolean): ResolutionDetails<boolean> {
103
- // resolve a boolean flag value
104
- }
130
+ 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
+ Once the provider backend replies (2) the client holds all flag values and therefore the flag evaluation process is synchronous.
105
132
 
106
- resolveStringEvaluation(flagKey: string, defaultValue: string): ResolutionDetails<string> {
107
- // resolve a string flag value
108
- }
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.
109
135
 
110
- resolveNumberEvaluation(flagKey: string, defaultValue: number): ResolutionDetails<number> {
111
- // resolve a numeric flag value
112
- }
136
+ ```ts
137
+ import { OpenFeature, ProviderEvents } from '@openfeature/web-sdk';
113
138
 
114
- resolveObjectEvaluation<T extends JsonValue>(flagKey: string, defaultValue: T): ResolutionDetails<T> {
115
- // resolve an object flag value
116
- }
117
- ```
139
+ OpenFeature.setProvider( /*set a provider*/ );
118
140
 
119
- See [here](https://openfeature.dev/docs/reference/technologies/server/javascript) for a catalog of available providers.
141
+ // OpenFeature API
142
+ OpenFeature.addHandler(ProviderEvents.Ready, () => {
143
+ const client = OpenFeature.getClient();
144
+ const stringFlag = client.getStringValue('string-flag', "default value"))
120
145
 
121
- ### Hooks:
146
+ //use stringFlag from this point
147
+ });
148
+ ```
122
149
 
123
- Hooks are a mechanism that allow for the addition of arbitrary behavior at well-defined points of the flag evaluation
124
- life-cycle. Use cases include validation of the resolved flag value, modifying or adding data to the evaluation context,
125
- logging, telemetry, and tracking.
150
+ ### Targeting and Context
126
151
 
127
- ```typescript
128
- import { OpenFeature, Hook, HookContext } from '@openfeature/web-sdk';
152
+ Sometimes, the value of a flag must consider some dynamic criteria about the application or user, such as the user's location, IP, email address, or the server's location.
153
+ In OpenFeature, we refer to this as [targeting](https://openfeature.dev/specification/glossary#targeting).
154
+ If the flag management system you're using supports targeting, you can provide the input data using the [evaluation context](https://openfeature.dev/docs/reference/concepts/evaluation-context).
129
155
 
130
- // Example hook that logs if an error occurs during flag evaluation
131
- export class GlobalDebugHook implements Hook {
132
- after(hookContext: HookContext, err: Error) {
133
- console.log('hook context', hookContext);
134
- console.error(err);
135
- }
136
- }
156
+ ```ts
157
+ // Set a value to the global context
158
+ await OpenFeature.setContext({ origin: document.location.host });
137
159
  ```
138
160
 
139
- See [here](https://openfeature.dev/docs/reference/technologies/server/javascript) for a catalog of available hooks.
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.
140
164
 
141
- ### Logging:
165
+ ### Hooks
142
166
 
143
- You can implement the `Logger` interface (compatible with the `console` object, and implementations from common logging
144
- libraries such as [winston](https://www.npmjs.com/package/winston)) and set it on the global API object.
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
+ 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
+ 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.
145
170
 
146
- ```typescript
147
- // implement logger
148
- class MyLogger implements Logger {
149
- error(...args: unknown[]): void {
150
- // implement me
151
- }
171
+ Once you've added a hook as a dependency, it can be registered at the global, client, or flag invocation level.
152
172
 
153
- warn(...args: unknown[]): void {
154
- // implement me
155
- }
173
+ ```ts
174
+ import { OpenFeature } from "@openfeature/web-sdk";
156
175
 
157
- info(...args: unknown[]): void {
158
- // implement me
159
- }
176
+ // add a hook globally, to run on all evaluations
177
+ OpenFeature.addHooks(new ExampleGlobalHook());
160
178
 
161
- debug(...args: unknown[]): void {
162
- // implement me
163
- }
164
- }
179
+ // add a hook on this client, to run on all evaluations made by this client
180
+ const client = OpenFeature.getClient();
181
+ client.addHooks(new ExampleClientHook());
165
182
 
166
- // set the logger
167
- OpenFeature.setLogger(new MyLogger());
183
+ // add a hook for this evaluation only
184
+ const boolValue = client.getBooleanValue("bool-flag", false, { hooks: [new ExampleHook()]});
168
185
  ```
169
186
 
170
- ### Named clients:
187
+ ### Logging
171
188
 
172
- You can have several clients, that can be referenced by a name.
173
- Every client can have a different provider assigned. If no provider is assigned to a named client, the global default
174
- provider is used.
189
+ The JS SDK will log warnings and errors to the console by default.
190
+ This behavior can be overridden by passing a custom logger either globally or per client.
191
+ A custom logger must implement the [Logger interface](../shared/src/logger/logger.ts).
175
192
 
176
- ```typescript
177
- import { OpenFeature, ProviderEvents } from '@openfeature/web-sdk';
193
+ ```ts
194
+ import type { Logger } from "@openfeature/web-sdk";
195
+
196
+ // The logger can be anything that conforms with the Logger interface
197
+ const logger: Logger = console;
178
198
 
179
- OpenFeature.setProvider(new YourProviderOfChoice())
180
- OpenFeature.setProvider("client-1", new YourOtherProviderOfChoice())
199
+ // Sets a global logger
200
+ OpenFeature.setLogger(logger);
181
201
 
182
- // Uses YourProviderOfChoice (the default)
183
- const unnamedClient = OpenFeature.getClient()
202
+ // Sets a client logger
203
+ const client = OpenFeature.getClient();
204
+ client.setLogger(logger);
205
+ ```
184
206
 
185
- // Uses YourOtherProviderOfChoice as it is set explicitly
186
- const client1 = OpenFeature.getClient("client-1")
207
+ ### Named clients
187
208
 
188
- // Uses YourProviderOfChoice as no provider is set
189
- const client2 = OpenFeature.getClient("client-2")
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.
212
+
213
+ ```ts
214
+ import { OpenFeature } from "@openfeature/web-sdk";
215
+
216
+ // Registering the default provider
217
+ OpenFeature.setProvider(NewLocalProvider());
218
+ // Registering a named provider
219
+ OpenFeature.setProvider("clientForCache", new NewCachedProvider());
220
+
221
+ // A Client backed by default provider
222
+ const clientWithDefault = OpenFeature.getClient();
223
+ // A Client backed by NewCachedProvider
224
+ const clientForCache = OpenFeature.getClient("clientForCache");
190
225
  ```
191
226
 
192
- ### Events:
227
+ ### Eventing
193
228
 
194
- Events provide a way to react to state changes in the provider or underlying flag management system.
195
- You can listen to events of either the OpenFeature API or individual clients.
229
+ 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.
230
+ Initialization events (`PROVIDER_READY` on success, `PROVIDER_ERROR` on failure) are dispatched for every provider.
231
+ Some providers support additional events, such as `PROVIDER_CONFIGURATION_CHANGED`.
196
232
 
197
- The events after initialization, `PROVIDER_READY` on success, `PROVIDER_ERROR` on failure during initialization,
198
- are dispatched for every provider.
199
- However, other event types may not be supported by your provider.
200
233
  Please refer to the documentation of the provider you're using to see what events are supported.
201
234
 
202
- ```typescript
235
+ ```ts
203
236
  import { OpenFeature, ProviderEvents } from '@openfeature/web-sdk';
204
237
 
205
238
  // OpenFeature API
206
239
  OpenFeature.addHandler(ProviderEvents.Ready, (eventDetails) => {
207
- console.log(`Ready event from: ${eventDetails.clientName}:`, eventDetails);
240
+ console.log(`Ready event from: ${eventDetails?.clientName}:`, eventDetails);
208
241
  });
209
242
 
210
243
  // Specific client
211
244
  const client = OpenFeature.getClient();
212
- client.addHandler(ProviderEvents.Error, async (eventDetails) => {
213
- console.log(`Error event from: ${eventDetails.clientName}:`, eventDetails);
245
+ client.addHandler(ProviderEvents.Error, (eventDetails) => {
246
+ console.log(`Error event from: ${eventDetails?.clientName}:`, eventDetails);
214
247
  });
215
248
  ```
216
249
 
217
- ### Shutdown:
250
+ ### Shutdown
218
251
 
219
252
  The OpenFeature API provides a close function to perform a cleanup of all registered providers.
220
253
  This should only be called when your application is in the process of shutting down.
221
254
 
222
- ```typescript
223
- import { OpenFeature, ProviderEvents } from '@openfeature/web-sdk';
255
+ ```ts
256
+ import { OpenFeature } from '@openfeature/web-sdk';
224
257
 
225
258
  await OpenFeature.close()
226
259
  ```
227
260
 
228
- ### Complete API documentation:
261
+ ## Extending
229
262
 
230
- See [here](https://open-feature.github.io/js-sdk/modules/OpenFeature_Web_SDK.html) for the complete API documentation.
263
+ ### Develop a provider
264
+
265
+ To develop a provider, you need to create a new project and include the OpenFeature SDK as a dependency.
266
+ This can be a new repository or included in [the existing contrib repository](https://github.com/open-feature/js-sdk-contrib) available under the OpenFeature organization.
267
+ You’ll then need to write the provider by implementing the [Provider interface](./src/provider/provider.ts) exported by the OpenFeature SDK.
268
+
269
+ ```ts
270
+ import { JsonValue, Provider, ResolutionDetails } from '@openfeature/web-sdk';
271
+
272
+ // implement the provider interface
273
+ class MyProvider implements Provider {
274
+
275
+ readonly metadata = {
276
+ name: 'My Provider',
277
+ } as const;
278
+
279
+ // Optional provider managed hooks
280
+ hooks?: Hook<FlagValue>[];
281
+
282
+ resolveBooleanEvaluation(flagKey: string, defaultValue: boolean, context: EvaluationContext, logger: Logger): ResolutionDetails<boolean> {
283
+ // code to evaluate a boolean
284
+ }
285
+
286
+ resolveStringEvaluation(flagKey: string, defaultValue: string, context: EvaluationContext, logger: Logger): ResolutionDetails<string> {
287
+ // code to evaluate a string
288
+ }
289
+
290
+ resolveNumberEvaluation(flagKey: string, defaultValue: number, context: EvaluationContext, logger: Logger): ResolutionDetails<number> {
291
+ // code to evaluate a number
292
+ }
293
+
294
+ resolveObjectEvaluation<T extends JsonValue>(flagKey: string, defaultValue: T, context: EvaluationContext, logger: Logger): ResolutionDetails<T> {
295
+ // code to evaluate an object
296
+ }
297
+
298
+ status?: ProviderStatus | undefined;
299
+ events?: OpenFeatureEventEmitter | undefined;
300
+
301
+ initialize?(context?: EvaluationContext | undefined): Promise<void> {
302
+ // code to initialize your provider
303
+ }
304
+
305
+ onClose?(): Promise<void> {
306
+ // code to shut down your provider
307
+ }
308
+ }
309
+ ```
310
+
311
+ > Built a new provider? [Let us know](https://github.com/open-feature/openfeature.dev/issues/new?assignees=&labels=provider&projects=&template=document-provider.yaml&title=%5BProvider%5D%3A+) so we can add it to the docs!
312
+
313
+ ### Develop a hook
314
+
315
+ To develop a hook, you need to create a new project and include the OpenFeature SDK as a dependency.
316
+ This can be a new repository or included in [the existing contrib repository](https://github.com/open-feature/js-sdk-contrib) available under the OpenFeature organization.
317
+ Implement your own hook by conforming to the [Hook interface](../shared/src/hooks/hook.ts).
318
+
319
+ ```ts
320
+ import type { Hook, HookContext, EvaluationDetails, FlagValue } from "@openfeature/web-sdk";
321
+
322
+ export class MyHook implements Hook {
323
+ after(hookContext: HookContext, evaluationDetails: EvaluationDetails<FlagValue>) {
324
+ // code that runs when there's an error during a flag evaluation
325
+ }
326
+ }
327
+ ```
231
328
 
232
- [openfeature-website]: https://openfeature.dev
329
+ > Built a new hook? [Let us know](https://github.com/open-feature/openfeature.dev/issues/new?assignees=&labels=hook&projects=&template=document-hook.yaml&title=%5BHook%5D%3A+) so we can add it to the docs!