@openfeature/web-sdk 0.3.1-experimental โ†’ 0.3.3-experimental

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,18 +1,166 @@
1
- # @openfeature/web-sdk
1
+ <!-- markdownlint-disable MD033 -->
2
+ <p align="center">
3
+ <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">
7
+ </picture>
8
+ </p>
2
9
 
3
- Experimental web implementation of OpenFeature intended for use in web-browsers.
4
-
5
- ## Installation
10
+ <h2 align="center">OpenFeature Web SDK</h2>
6
11
 
7
- ```shell
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.5.2&color=yellow)](https://github.com/open-feature/spec/tree/v0.5.2)
15
+
16
+ ## ๐Ÿงช This SDK is experimental
17
+
18
+ The Web SDK is under development and based on a experimental client concepts.
19
+ For more information, see this [issue](https://github.com/open-feature/spec/issues/167).
20
+
21
+ ## ๐Ÿ‘‹ Hey there! Thanks for checking out the OpenFeature Web SDK
22
+
23
+ ### What is OpenFeature?
24
+
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.
26
+
27
+ ### Why standardize feature flags?
28
+
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
+
31
+ ## ๐Ÿ” Requirements:
32
+
33
+ - ES2015-compatible web browser (Chrome, Edge, Firefox, etc)
34
+
35
+ ## ๐Ÿ“ฆ Installation:
36
+
37
+ ### npm
38
+
39
+ ```sh
8
40
  npm install @openfeature/web-sdk
9
41
  ```
10
42
 
11
- or
43
+ ### yarn
12
44
 
13
- ```shell
45
+ ```sh
14
46
  yarn add @openfeature/web-sdk
15
47
  ```
16
48
 
17
- ## Usage
18
- Coming soon!
49
+ ## ๐ŸŒŸ Features:
50
+
51
+ - support for various [providers](https://openfeature.dev/docs/reference/concepts/provider)
52
+ - easy integration and extension via [hooks](https://openfeature.dev/docs/reference/concepts/hooks)
53
+ - handle flags of any type: bool, string, numeric and object
54
+ - [context-aware](https://openfeature.dev/docs/reference/concepts/evaluation-context) evaluation
55
+
56
+ ## ๐Ÿš€ Usage:
57
+
58
+ ### Basics:
59
+
60
+ ```typescript
61
+ import { OpenFeature } from '@openfeature/web-sdk';
62
+
63
+ // configure a provider
64
+ await OpenFeature.setProvider(new YourProviderOfChoice());
65
+
66
+ // create a client
67
+ const client = OpenFeature.getClient('my-app');
68
+
69
+ // get a bool flag value
70
+ const boolValue = client.getBooleanValue('boolFlag', false);
71
+ ```
72
+
73
+ ### Context-aware evaluation:
74
+
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.
76
+ In OpenFeature, we refer to this as [`targeting`](https://openfeature.dev/specification/glossary#targeting).
77
+ If the flag system you're using supports targeting, you can provide the input data using the `EvaluationContext`.
78
+
79
+ ```typescript
80
+ // global context for static data
81
+ await OpenFeature.setContext({ origin: document.location.host })
82
+
83
+ // use contextual data to determine a flag value
84
+ const boolValue = client.getBooleanValue('some-flag', false);
85
+ ```
86
+
87
+ ### Providers:
88
+
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.
90
+
91
+ ```typescript
92
+ import { JsonValue, Provider, ResolutionDetails } from '@openfeature/web-sdk';
93
+
94
+ // implement the provider interface
95
+ class MyProvider implements Provider {
96
+ readonly metadata = {
97
+ name: 'My Provider',
98
+ } as const;
99
+
100
+ resolveBooleanEvaluation(flagKey: string, defaultValue: boolean): ResolutionDetails<boolean> {
101
+ // resolve a boolean flag value
102
+ }
103
+
104
+ resolveStringEvaluation(flagKey: string, defaultValue: string): ResolutionDetails<string> {
105
+ // resolve a string flag value
106
+ }
107
+
108
+ resolveNumberEvaluation(flagKey: string, defaultValue: number): ResolutionDetails<number> {
109
+ // resolve a numeric flag value
110
+ }
111
+
112
+ resolveObjectEvaluation<T extends JsonValue>(flagKey: string, defaultValue: T): ResolutionDetails<T> {
113
+ // resolve an object flag value
114
+ }
115
+ ```
116
+
117
+ See [here](https://openfeature.dev/docs/reference/technologies/server/javascript) for a catalog of available providers.
118
+
119
+ ### Hooks:
120
+
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.
122
+
123
+ ```typescript
124
+ import { OpenFeature, Hook, HookContext } from '@openfeature/web-sdk';
125
+
126
+ // Example hook that logs if an error occurs during flag evaluation
127
+ export class GlobalDebugHook implements Hook {
128
+ after(hookContext: HookContext, err: Error) {
129
+ console.log('hook context', hookContext);
130
+ console.error(err);
131
+ }
132
+ }
133
+ ```
134
+
135
+ See [here](https://openfeature.dev/docs/reference/technologies/server/javascript) for a catalog of available hooks.
136
+
137
+ ### Logging:
138
+
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.
140
+
141
+ ```typescript
142
+ // implement logger
143
+ class MyLogger implements Logger {
144
+ error(...args: unknown[]): void {
145
+ // implement me
146
+ }
147
+ warn(...args: unknown[]): void {
148
+ // implement me
149
+ }
150
+ info(...args: unknown[]): void {
151
+ // implement me
152
+ }
153
+ debug(...args: unknown[]): void {
154
+ // implement me
155
+ }
156
+ }
157
+
158
+ // set the logger
159
+ OpenFeature.setLogger(new MyLogger());
160
+ ```
161
+
162
+ ### Complete API documentation:
163
+
164
+ See [here](https://open-feature.github.io/js-sdk/modules/OpenFeature_Web_SDK.html) for the complete API documentation.
165
+
166
+ [openfeature-website]: https://openfeature.dev