@openfeature/web-sdk 0.3.2-experimental → 0.3.4-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 +79 -8
- package/dist/cjs/index.js +278 -121
- package/dist/cjs/index.js.map +4 -4
- package/dist/esm/index.js +278 -122
- package/dist/esm/index.js.map +4 -4
- package/dist/types.d.ts +167 -126
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
|
|
12
12
|
[](https://www.repostatus.org/#wip)
|
|
13
13
|
[](https://www.npmjs.com/package/@openfeature/web-sdk)
|
|
14
|
-
[](https://github.com/open-feature/spec/tree/v0.6.0)
|
|
15
15
|
|
|
16
16
|
## 🧪 This SDK is experimental
|
|
17
17
|
|
|
@@ -22,11 +22,14 @@ For more information, see this [issue](https://github.com/open-feature/spec/issu
|
|
|
22
22
|
|
|
23
23
|
### What is OpenFeature?
|
|
24
24
|
|
|
25
|
-
[OpenFeature][openfeature-website] is an open standard that provides a vendor-agnostic, community-driven API for feature
|
|
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.
|
|
26
27
|
|
|
27
28
|
### Why standardize feature flags?
|
|
28
29
|
|
|
29
|
-
Standardizing feature flags unifies tools and vendors behind a common interface which avoids vendor lock-in at the code
|
|
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.
|
|
30
33
|
|
|
31
34
|
## 🔍 Requirements:
|
|
32
35
|
|
|
@@ -72,7 +75,8 @@ const boolValue = client.getBooleanValue('boolFlag', false);
|
|
|
72
75
|
|
|
73
76
|
### Context-aware evaluation:
|
|
74
77
|
|
|
75
|
-
Sometimes the value of a flag must take into account some dynamic criteria about the application or user, such as the
|
|
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.
|
|
76
80
|
In OpenFeature, we refer to this as [`targeting`](https://openfeature.dev/specification/glossary#targeting).
|
|
77
81
|
If the flag system you're using supports targeting, you can provide the input data using the `EvaluationContext`.
|
|
78
82
|
|
|
@@ -86,7 +90,10 @@ const boolValue = client.getBooleanValue('some-flag', false);
|
|
|
86
90
|
|
|
87
91
|
### Providers:
|
|
88
92
|
|
|
89
|
-
To develop a provider, you need to create a new project and include the OpenFeature SDK as a dependency. This can be a
|
|
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.
|
|
90
97
|
|
|
91
98
|
```typescript
|
|
92
99
|
import { JsonValue, Provider, ResolutionDetails } from '@openfeature/web-sdk';
|
|
@@ -94,7 +101,7 @@ import { JsonValue, Provider, ResolutionDetails } from '@openfeature/web-sdk';
|
|
|
94
101
|
// implement the provider interface
|
|
95
102
|
class MyProvider implements Provider {
|
|
96
103
|
readonly metadata = {
|
|
97
|
-
name: 'My Provider'
|
|
104
|
+
name: 'My Provider'
|
|
98
105
|
} as const;
|
|
99
106
|
|
|
100
107
|
resolveBooleanEvaluation(flagKey: string, defaultValue: boolean): ResolutionDetails<boolean> {
|
|
@@ -118,7 +125,9 @@ See [here](https://openfeature.dev/docs/reference/technologies/server/javascript
|
|
|
118
125
|
|
|
119
126
|
### Hooks:
|
|
120
127
|
|
|
121
|
-
Hooks are a mechanism that allow for the addition of arbitrary behavior at well-defined points of the flag evaluation
|
|
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.
|
|
122
131
|
|
|
123
132
|
```typescript
|
|
124
133
|
import { OpenFeature, Hook, HookContext } from '@openfeature/web-sdk';
|
|
@@ -136,7 +145,8 @@ See [here](https://openfeature.dev/docs/reference/technologies/server/javascript
|
|
|
136
145
|
|
|
137
146
|
### Logging:
|
|
138
147
|
|
|
139
|
-
You can implement the `Logger` interface (compatible with the `console` object, and implementations from common logging
|
|
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.
|
|
140
150
|
|
|
141
151
|
```typescript
|
|
142
152
|
// implement logger
|
|
@@ -144,12 +154,15 @@ class MyLogger implements Logger {
|
|
|
144
154
|
error(...args: unknown[]): void {
|
|
145
155
|
// implement me
|
|
146
156
|
}
|
|
157
|
+
|
|
147
158
|
warn(...args: unknown[]): void {
|
|
148
159
|
// implement me
|
|
149
160
|
}
|
|
161
|
+
|
|
150
162
|
info(...args: unknown[]): void {
|
|
151
163
|
// implement me
|
|
152
164
|
}
|
|
165
|
+
|
|
153
166
|
debug(...args: unknown[]): void {
|
|
154
167
|
// implement me
|
|
155
168
|
}
|
|
@@ -159,6 +172,64 @@ class MyLogger implements Logger {
|
|
|
159
172
|
OpenFeature.setLogger(new MyLogger());
|
|
160
173
|
```
|
|
161
174
|
|
|
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 registered providers.
|
|
225
|
+
This should only be called when your application is in the process of shutting down.
|
|
226
|
+
|
|
227
|
+
```typescript
|
|
228
|
+
import { OpenFeature, ProviderEvents } from '@openfeature/web-sdk';
|
|
229
|
+
|
|
230
|
+
await OpenFeature.close()
|
|
231
|
+
```
|
|
232
|
+
|
|
162
233
|
### Complete API documentation:
|
|
163
234
|
|
|
164
235
|
See [here](https://open-feature.github.io/js-sdk/modules/OpenFeature_Web_SDK.html) for the complete API documentation.
|