@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 +235 -138
- package/dist/cjs/index.js +85 -33
- package/dist/cjs/index.js.map +4 -4
- package/dist/esm/index.js +85 -33
- package/dist/esm/index.js.map +4 -4
- package/dist/types.d.ts +179 -131
- package/package.json +1 -1
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
|
-
<
|
|
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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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]
|
|
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
|
-
|
|
44
|
+
<!-- x-hide-in-docs-end -->
|
|
24
45
|
|
|
25
|
-
|
|
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
|
-
|
|
48
|
+
### Requirements
|
|
30
49
|
|
|
31
50
|
- ES2015-compatible web browser (Chrome, Edge, Firefox, etc)
|
|
32
51
|
|
|
33
|
-
|
|
52
|
+
### Install
|
|
34
53
|
|
|
35
|
-
|
|
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
|
-
|
|
60
|
+
#### yarn
|
|
42
61
|
|
|
43
62
|
```sh
|
|
44
63
|
yarn add @openfeature/web-sdk
|
|
45
64
|
```
|
|
46
65
|
|
|
47
|
-
|
|
66
|
+
### Usage
|
|
48
67
|
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
|
|
71
|
+
// Register your feature flag provider
|
|
72
|
+
OpenFeature.setProvider(new YourProviderOfChoice());
|
|
55
73
|
|
|
56
|
-
|
|
74
|
+
// create a new client
|
|
75
|
+
const client = OpenFeature.getClient();
|
|
57
76
|
|
|
58
|
-
|
|
59
|
-
|
|
77
|
+
// Evaluate your feature flag
|
|
78
|
+
const v2Enabled = client.getBooleanValue('v2_enabled', false);
|
|
60
79
|
|
|
61
|
-
|
|
62
|
-
|
|
80
|
+
if (v2Enabled) {
|
|
81
|
+
console.log("v2 is enabled");
|
|
82
|
+
}
|
|
83
|
+
```
|
|
63
84
|
|
|
64
|
-
|
|
65
|
-
const client = OpenFeature.getClient('my-app');
|
|
85
|
+
### API Reference
|
|
66
86
|
|
|
67
|
-
|
|
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
|
-
|
|
89
|
+
## 🌟 Features
|
|
72
90
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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
|
-
|
|
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
|
-
|
|
83
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
123
|
+
```mermaid
|
|
124
|
+
sequenceDiagram
|
|
125
|
+
autonumber
|
|
126
|
+
Client-->+Feature Flag Provider: ResolveAll (context)
|
|
127
|
+
Feature Flag Provider-->-Client: Flags values
|
|
128
|
+
```
|
|
101
129
|
|
|
102
|
-
|
|
103
|
-
|
|
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
|
-
|
|
107
|
-
|
|
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
|
-
|
|
111
|
-
|
|
112
|
-
}
|
|
136
|
+
```ts
|
|
137
|
+
import { OpenFeature, ProviderEvents } from '@openfeature/web-sdk';
|
|
113
138
|
|
|
114
|
-
|
|
115
|
-
// resolve an object flag value
|
|
116
|
-
}
|
|
117
|
-
```
|
|
139
|
+
OpenFeature.setProvider( /*set a provider*/ );
|
|
118
140
|
|
|
119
|
-
|
|
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
|
-
|
|
146
|
+
//use stringFlag from this point
|
|
147
|
+
});
|
|
148
|
+
```
|
|
122
149
|
|
|
123
|
-
|
|
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
|
-
|
|
128
|
-
|
|
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
|
-
|
|
131
|
-
|
|
132
|
-
|
|
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
|
-
|
|
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
|
-
###
|
|
165
|
+
### Hooks
|
|
142
166
|
|
|
143
|
-
|
|
144
|
-
|
|
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
|
-
|
|
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
|
-
|
|
154
|
-
|
|
155
|
-
}
|
|
173
|
+
```ts
|
|
174
|
+
import { OpenFeature } from "@openfeature/web-sdk";
|
|
156
175
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
}
|
|
176
|
+
// add a hook globally, to run on all evaluations
|
|
177
|
+
OpenFeature.addHooks(new ExampleGlobalHook());
|
|
160
178
|
|
|
161
|
-
|
|
162
|
-
|
|
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
|
-
//
|
|
167
|
-
|
|
183
|
+
// add a hook for this evaluation only
|
|
184
|
+
const boolValue = client.getBooleanValue("bool-flag", false, { hooks: [new ExampleHook()]});
|
|
168
185
|
```
|
|
169
186
|
|
|
170
|
-
###
|
|
187
|
+
### Logging
|
|
171
188
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
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
|
-
```
|
|
177
|
-
import {
|
|
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
|
-
|
|
180
|
-
OpenFeature.
|
|
199
|
+
// Sets a global logger
|
|
200
|
+
OpenFeature.setLogger(logger);
|
|
181
201
|
|
|
182
|
-
//
|
|
183
|
-
const
|
|
202
|
+
// Sets a client logger
|
|
203
|
+
const client = OpenFeature.getClient();
|
|
204
|
+
client.setLogger(logger);
|
|
205
|
+
```
|
|
184
206
|
|
|
185
|
-
|
|
186
|
-
const client1 = OpenFeature.getClient("client-1")
|
|
207
|
+
### Named clients
|
|
187
208
|
|
|
188
|
-
|
|
189
|
-
|
|
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
|
-
###
|
|
227
|
+
### Eventing
|
|
193
228
|
|
|
194
|
-
Events
|
|
195
|
-
|
|
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
|
-
```
|
|
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
|
|
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,
|
|
213
|
-
console.log(`Error event from: ${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
|
-
```
|
|
223
|
-
import { OpenFeature
|
|
255
|
+
```ts
|
|
256
|
+
import { OpenFeature } from '@openfeature/web-sdk';
|
|
224
257
|
|
|
225
258
|
await OpenFeature.close()
|
|
226
259
|
```
|
|
227
260
|
|
|
228
|
-
|
|
261
|
+
## Extending
|
|
229
262
|
|
|
230
|
-
|
|
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
|
-
[
|
|
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!
|