@splitsoftware/openfeature-web-split-provider 1.0.0 → 1.2.0
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/CHANGES.txt +13 -2
- package/README.md +190 -16
- package/es/lib/context.js +55 -0
- package/es/lib/evaluation.js +21 -0
- package/es/lib/events.js +69 -0
- package/es/lib/js-split-provider.js +80 -142
- package/es/lib/parsers.js +38 -0
- package/es/lib/types.js +2 -0
- package/lib/lib/context.js +61 -0
- package/lib/lib/evaluation.js +25 -0
- package/lib/lib/events.js +74 -0
- package/lib/lib/js-split-provider.js +78 -140
- package/lib/lib/parsers.js +44 -0
- package/lib/lib/types.js +5 -0
- package/package.json +2 -2
- package/src/__tests__/bucketingKey.spec.js +145 -0
- package/src/__tests__/context.spec.js +146 -0
- package/src/__tests__/evaluation.spec.js +76 -0
- package/src/__tests__/events.spec.js +182 -0
- package/src/__tests__/integration/bucketing-key.test.js +81 -0
- package/src/__tests__/integration/e2e-events.test.js +1 -8
- package/src/__tests__/integration/openfeature-evaluation.test.js +162 -0
- package/src/__tests__/parsers.spec.js +115 -0
- package/src/__tests__/provider.spec.js +59 -3
- package/src/__tests__/types.spec.js +15 -0
- package/src/lib/context.ts +62 -0
- package/src/lib/evaluation.ts +33 -0
- package/src/lib/events.ts +105 -0
- package/src/lib/js-split-provider.ts +103 -180
- package/src/lib/parsers.ts +43 -0
- package/src/lib/types.ts +12 -0
- package/types/lib/context.d.ts +15 -0
- package/types/lib/evaluation.d.ts +7 -0
- package/types/lib/events.d.ts +20 -0
- package/types/lib/js-split-provider.d.ts +11 -7
- package/types/lib/parsers.d.ts +8 -0
- package/types/lib/types.d.ts +10 -0
package/CHANGES.txt
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
1.2.0 (November 27, 2026)
|
|
2
|
+
- Added support for the `bucketingKey` evaluation context attribute: the provider uses the Split key { matchingKey: targetingKey, bucketingKey }. Changing only the bucketingKey switches to the client for the new key. Blank or non-string values are ignored, and a bucketingKey over 250 characters is ignored and the client uses the targeting key only. A bucketingKey without a targeting key is also ignored.
|
|
3
|
+
- Ignored bucketingKey values are reported as a warning through the Split SDK logger (visible when SDK logging is enabled with `debug`). The warning names the reason, never the value.
|
|
4
|
+
- Behavior change: `bucketingKey` is no longer forwarded to Split as a targeting attribute.
|
|
5
|
+
- Behavior change: the context set before the provider is initialized is now applied (previously the factory key was used until the next context change).
|
|
6
|
+
|
|
7
|
+
1.1.0 (March 2, 2026)
|
|
8
|
+
- Added ProviderEvents.Ready payload with Split SdkReadyMetadata
|
|
9
|
+
- Updated ConfigurationChanged to forward SdkUpdateMetadata in metadata
|
|
10
|
+
- Requires @splitsoftware/splitio-browserjs ^1.7.0 for SDK_UPDATE metadata support
|
|
11
|
+
|
|
1
12
|
1.0.0 (October 1, 2025)
|
|
2
|
-
- First release.
|
|
3
|
-
- Up to date with @openfeature/web-sdk v1.6.1, and @splitsoftware/splitio-browserjs 1.4.0
|
|
13
|
+
- First release.
|
|
14
|
+
- Up to date with @openfeature/web-sdk v1.6.1, and @splitsoftware/splitio-browserjs 1.4.0
|
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Split OpenFeature Provider for
|
|
1
|
+
# Split OpenFeature Provider for Web JavaScript
|
|
2
2
|
[](https://twitter.com/intent/follow?screen_name=splitsoftware)
|
|
3
3
|
|
|
4
4
|
## Overview
|
|
@@ -27,11 +27,17 @@ import { OpenFeatureSplitProvider } from '@splitsoftware/openfeature-web-split-p
|
|
|
27
27
|
|
|
28
28
|
const splitFactory = SplitFactory({
|
|
29
29
|
core: {
|
|
30
|
-
authorizationKey: '
|
|
30
|
+
authorizationKey: 'CLIENT_SIDE_SDK_KEY',
|
|
31
|
+
key: 'TARGETING_KEY'
|
|
31
32
|
}
|
|
32
33
|
});
|
|
34
|
+
|
|
33
35
|
const provider = new OpenFeatureSplitProvider(splitFactory);
|
|
34
|
-
|
|
36
|
+
|
|
37
|
+
// Register provider and wait for the default SDK client for 'TARGETING_KEY' to be ready
|
|
38
|
+
await OpenFeature.setProviderAndWait(provider);
|
|
39
|
+
|
|
40
|
+
const client = OpenFeature.getClient();
|
|
35
41
|
```
|
|
36
42
|
|
|
37
43
|
## Use of OpenFeature with Split
|
|
@@ -43,9 +49,36 @@ const context: EvaluationContext = {
|
|
|
43
49
|
targetingKey: 'TARGETING_KEY',
|
|
44
50
|
trafficType: 'account'
|
|
45
51
|
};
|
|
46
|
-
OpenFeature.setContext(context)
|
|
52
|
+
await OpenFeature.setContext(context)
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Bucketing key
|
|
56
|
+
Set `bucketingKey` next to `targetingKey` to have Split bucket on a different key than the one used for matching (Split key `{ matchingKey: targetingKey, bucketingKey }`):
|
|
57
|
+
```js
|
|
58
|
+
await OpenFeature.setContext({ targetingKey: 'user-123', bucketingKey: 'account-42' });
|
|
47
59
|
```
|
|
60
|
+
- Changing only `bucketingKey` switches to the Split client for the new key, the same as changing the targeting key.
|
|
61
|
+
- `bucketingKey` is reserved: it is **not** forwarded to Split as an attribute.
|
|
62
|
+
- It is trimmed, and ignored (targeting key used alone) when it has no targeting key, is blank, is not a string, or is longer than 250 characters.
|
|
63
|
+
- An ignored `bucketingKey` logs a warning with the reason (never the value) through the Split SDK logger, so it shows when SDK logging is enabled (`debug: 'WARN'` or `factory.Logger.setLogLevel('WARN')`).
|
|
64
|
+
- The context set before the provider (`setContext` before `setProvider`) is applied when the provider initializes.
|
|
65
|
+
|
|
66
|
+
## Configuration changed event (SDK_UPDATE)
|
|
67
|
+
|
|
68
|
+
When the Split SDK emits the `SDK_UPDATE` **event** (flags or segments changed), the provider emits OpenFeature’s `ConfigurationChanged` and forwards the event metadata. The metadata shape matches [javascript-commons SdkUpdateMetadata](https://github.com/splitio/javascript-commons): `type` is `'FLAGS_UPDATE' | 'SEGMENTS_UPDATE'` and `names` is the list of flag or segment names that were updated. Handlers receive [Provider Event Details](https://openfeature.dev/specification/types#provider-event-details): `flagsChanged` (when `type === 'FLAGS_UPDATE'`, the `names` array) and `metadata` (`type` as string).
|
|
48
69
|
|
|
70
|
+
Requires `@splitsoftware/splitio-browserjs` **1.7.0 or later** (metadata was added in 1.7.0).
|
|
71
|
+
|
|
72
|
+
```js
|
|
73
|
+
const { OpenFeature, ProviderEvents } = require('@openfeature/web-sdk');
|
|
74
|
+
|
|
75
|
+
const client = OpenFeature.getClient();
|
|
76
|
+
client.addHandler(ProviderEvents.ConfigurationChanged, (eventDetails) => {
|
|
77
|
+
console.log('Flags changed:', eventDetails.flagsChanged);
|
|
78
|
+
console.log('Event metadata:', eventDetails.metadata);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
```
|
|
49
82
|
## Evaluate with details
|
|
50
83
|
Use the get*Details(...) APIs to get the value and rich context (variant, reason, error code, metadata). This provider includes the Split treatment config as a raw JSON string under flagMetadata["config"]
|
|
51
84
|
|
|
@@ -62,10 +95,11 @@ Evaluation attributes must be set in context before evaluation
|
|
|
62
95
|
const context: EvaluationContext = {
|
|
63
96
|
targetingKey: 'TARGETING_KEY',
|
|
64
97
|
trafficType: 'account',
|
|
98
|
+
// Evaluation attributes:
|
|
65
99
|
plan: 'premium',
|
|
66
|
-
|
|
100
|
+
coupon: 'WELCOME10'
|
|
67
101
|
};
|
|
68
|
-
OpenFeature.setContext(context);
|
|
102
|
+
await OpenFeature.setContext(context);
|
|
69
103
|
const booleanTreatment = client.getBooleanDetails('boolFlag', false);
|
|
70
104
|
```
|
|
71
105
|
|
|
@@ -89,11 +123,152 @@ Example:
|
|
|
89
123
|
const context = { targetingKey: 'user-123', trafficType: 'account' }
|
|
90
124
|
const details = { value: 19.99, properties: { plan: 'pro', coupon: 'WELCOME10' }}
|
|
91
125
|
|
|
92
|
-
|
|
126
|
+
await OpenFeature.setContext(context)
|
|
93
127
|
client.track('checkout.completed', details)
|
|
94
128
|
```
|
|
129
|
+
|
|
130
|
+
## Angular Usage
|
|
131
|
+
|
|
132
|
+
### Install dependencies
|
|
133
|
+
```bash
|
|
134
|
+
npm i @openfeature/angular-sdk @splitsoftware/splitio-browserjs @splitsoftware/openfeature-web-split-provider
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### Initialize the Angular SDK
|
|
138
|
+
|
|
139
|
+
#### Traditional (NgModule) setup
|
|
140
|
+
```js
|
|
141
|
+
import { NgModule } from '@angular/core';
|
|
142
|
+
import { RouterModule, Routes } from '@angular/router';
|
|
143
|
+
import { OpenFeatureModule } from '@openfeature/angular-sdk';
|
|
144
|
+
import { SplitFactory } from '@splitsoftware/splitio-browserjs';
|
|
145
|
+
import { OpenFeatureSplitProvider } from '@splitsoftware/openfeature-web-split-provider';
|
|
146
|
+
|
|
147
|
+
const splitFactory = SplitFactory({
|
|
148
|
+
core: {
|
|
149
|
+
authorizationKey: 'CLIENT_SIDE_SDK_KEY',
|
|
150
|
+
key: 'TARGETING_KEY'
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
const openFeatureProvider = new OpenFeatureSplitProvider(splitFactory);
|
|
154
|
+
|
|
155
|
+
@NgModule({
|
|
156
|
+
imports: [
|
|
157
|
+
RouterModule.forRoot(routes),
|
|
158
|
+
OpenFeatureModule.forRoot({
|
|
159
|
+
provider: openFeatureProvider
|
|
160
|
+
})
|
|
161
|
+
],
|
|
162
|
+
bootstrap: [AppComponent]
|
|
163
|
+
})
|
|
164
|
+
export class AppModule {}
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
#### Standalone (Angular 16+) setup
|
|
168
|
+
```js
|
|
169
|
+
import { ApplicationConfig, provideBrowserGlobalErrorListeners, importProvidersFrom } from '@angular/core';
|
|
170
|
+
import { OpenFeatureModule } from '@openfeature/angular-sdk';
|
|
171
|
+
import { SplitFactory } from '@splitsoftware/splitio-browserjs';
|
|
172
|
+
import { OpenFeatureSplitProvider } from '@splitsoftware/openfeature-web-split-provider';
|
|
173
|
+
|
|
174
|
+
const splitFactory = SplitFactory({
|
|
175
|
+
core: {
|
|
176
|
+
authorizationKey: 'CLIENT_SIDE_SDK_KEY',
|
|
177
|
+
key: 'TARGETING_KEY'
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
const openFeatureProvider = new OpenFeatureSplitProvider(splitFactory);
|
|
181
|
+
|
|
182
|
+
export const appConfig: ApplicationConfig = {
|
|
183
|
+
providers: [
|
|
184
|
+
provideBrowserGlobalErrorListeners(),
|
|
185
|
+
importProvidersFrom(
|
|
186
|
+
OpenFeatureModule.forRoot({
|
|
187
|
+
provider: openFeatureProvider
|
|
188
|
+
})
|
|
189
|
+
)
|
|
190
|
+
]
|
|
191
|
+
};
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
#### Component injection and usage
|
|
195
|
+
```js
|
|
196
|
+
import { FeatureFlagService, EvaluationDetails } from '@openfeature/angular-sdk';
|
|
197
|
+
|
|
198
|
+
@Component({
|
|
199
|
+
selector: 'app-root',
|
|
200
|
+
imports: [RouterOutlet],
|
|
201
|
+
templateUrl: './app.html',
|
|
202
|
+
styleUrl: './app.css'
|
|
203
|
+
})
|
|
204
|
+
export class App {
|
|
205
|
+
constructor(private splitService: FeatureFlagService) {}
|
|
206
|
+
|
|
207
|
+
ngOnInit() {
|
|
208
|
+
this.splitService.getStringDetails('featureFlagName', 'default').subscribe((result: EvaluationDetails<string>) => {
|
|
209
|
+
console.log('Feature flag result:', result.value);
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
```
|
|
214
|
+
> 🔗 For more information on Angular integration, visit the [official OpenFeature Angular SDK documentation][angular-docs].
|
|
215
|
+
|
|
216
|
+
[angular-docs]: https://openfeature.dev/docs/reference/sdks/client/web/angular
|
|
217
|
+
|
|
218
|
+
## React usage
|
|
219
|
+
|
|
220
|
+
### Install dependencies
|
|
221
|
+
```bash
|
|
222
|
+
npm i @openfeature/react-sdk @splitsoftware/splitio-browserjs @splitsoftware/openfeature-web-split-provider
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
### Initialize the React SDK
|
|
226
|
+
```js
|
|
227
|
+
import { OpenFeature } from '@openfeature/react-sdk';
|
|
228
|
+
import { OpenFeatureSplitProvider } from '@splitsoftware/openfeature-web-split-provider';
|
|
229
|
+
import { DebugLogger, SplitFactory } from '@splitsoftware/splitio-browserjs';
|
|
230
|
+
|
|
231
|
+
const splitFactory = SplitFactory({
|
|
232
|
+
core: {
|
|
233
|
+
authorizationKey: 'CLIENT_SIDE_SDK_KEY',
|
|
234
|
+
key: 'TARGETING_KEY'
|
|
235
|
+
}
|
|
236
|
+
})
|
|
237
|
+
const openFeatureProvider = new OpenFeatureSplitProvider(splitFactory);
|
|
238
|
+
|
|
239
|
+
OpenFeature.setProvider(openFeatureProvider);
|
|
240
|
+
|
|
241
|
+
function App() {
|
|
242
|
+
return (
|
|
243
|
+
<OpenFeatureProvider>
|
|
244
|
+
<Page></Page>
|
|
245
|
+
</OpenFeatureProvider>
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
#### Evaluation hooks
|
|
251
|
+
```js
|
|
252
|
+
import { useFlag } from '@openfeature/react-sdk';
|
|
253
|
+
|
|
254
|
+
function Page() {
|
|
255
|
+
// Use the "query-style" flag evaluation hook, specifying a flag-key and a default value.
|
|
256
|
+
const { value: showNewMessage } = useFlag('new-message', true);
|
|
257
|
+
return (
|
|
258
|
+
<div className="App">
|
|
259
|
+
<header className="App-header">
|
|
260
|
+
{showNewMessage ? <p>Welcome to this OpenFeature-enabled React app!</p> : <p>Welcome to this React app.</p>}
|
|
261
|
+
</header>
|
|
262
|
+
</div>
|
|
263
|
+
)
|
|
264
|
+
}
|
|
265
|
+
```
|
|
266
|
+
> 🔗 For more information on React integration, visit the [official OpenFeature React SDK documentation][react-docs].
|
|
267
|
+
|
|
268
|
+
[react-docs]: https://openfeature.dev/docs/reference/sdks/client/web/react
|
|
269
|
+
|
|
95
270
|
## Submitting issues
|
|
96
|
-
|
|
271
|
+
|
|
97
272
|
The Split team monitors all issues submitted to this [issue tracker](https://github.com/splitio/split-openfeature-provider-web-js/issues). We encourage you to use this issue tracker to submit any bug reports, feedback, and feature enhancements. We'll do our best to respond in a timely manner.
|
|
98
273
|
|
|
99
274
|
## Contributing
|
|
@@ -103,13 +278,13 @@ Please see [Contributors Guide](CONTRIBUTORS-GUIDE.md) to find all you need to s
|
|
|
103
278
|
Licensed under the Apache License, Version 2.0. See: [Apache License](http://www.apache.org/licenses/).
|
|
104
279
|
|
|
105
280
|
## About Split
|
|
106
|
-
|
|
281
|
+
|
|
107
282
|
Split is the leading Feature Delivery Platform for engineering teams that want to confidently deploy features as fast as they can develop them. Split’s fine-grained management, real-time monitoring, and data-driven experimentation ensure that new features will improve the customer experience without breaking or degrading performance. Companies like Twilio, Salesforce, GoDaddy and WePay trust Split to power their feature delivery.
|
|
108
|
-
|
|
283
|
+
|
|
109
284
|
To learn more about Split, contact hello@split.io, or get started with feature flags for free at https://www.split.io/signup.
|
|
110
|
-
|
|
285
|
+
|
|
111
286
|
Split has built and maintains SDKs for:
|
|
112
|
-
|
|
287
|
+
|
|
113
288
|
* .NET [Github](https://github.com/splitio/dotnet-client) [Docs](https://help.split.io/hc/en-us/articles/360020240172--NET-SDK)
|
|
114
289
|
* Android [Github](https://github.com/splitio/android-client) [Docs](https://help.split.io/hc/en-us/articles/360020343291-Android-SDK)
|
|
115
290
|
* Angular [Github](https://github.com/splitio/angular-sdk-plugin) [Docs](https://help.split.io/hc/en-us/articles/6495326064397-Angular-utilities)
|
|
@@ -128,10 +303,9 @@ Split has built and maintains SDKs for:
|
|
|
128
303
|
* React Native [Github](https://github.com/splitio/react-native-client) [Docs](https://help.split.io/hc/en-us/articles/4406066357901-React-Native-SDK)
|
|
129
304
|
* Redux [Github](https://github.com/splitio/redux-client) [Docs](https://help.split.io/hc/en-us/articles/360038851551-Redux-SDK)
|
|
130
305
|
* Ruby [Github](https://github.com/splitio/ruby-client) [Docs](https://help.split.io/hc/en-us/articles/360020673251-Ruby-SDK)
|
|
131
|
-
|
|
306
|
+
|
|
132
307
|
For a comprehensive list of open source projects visit our [Github page](https://github.com/splitio?utf8=%E2%9C%93&query=%20only%3Apublic%20).
|
|
133
|
-
|
|
308
|
+
|
|
134
309
|
**Learn more about Split:**
|
|
135
|
-
|
|
136
|
-
Visit [split.io/product](https://www.split.io/product) for an overview of Split, or visit our documentation at [help.split.io](http://help.split.io) for more detailed information.
|
|
137
310
|
|
|
311
|
+
Visit [split.io/product](https://www.split.io/product) for an overview of Split, or visit our documentation at [help.split.io](http://help.split.io) for more detailed information.
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { __rest } from "tslib";
|
|
2
|
+
/** Split SDK limit for key values. */
|
|
3
|
+
var KEY_MAX_LENGTH = 250;
|
|
4
|
+
/** Trimmed bucketing key, or the reason it cannot be used. The reason never includes the value. */
|
|
5
|
+
function parseBucketingKey(value) {
|
|
6
|
+
if (value === undefined || value === null)
|
|
7
|
+
return {};
|
|
8
|
+
if (typeof value !== 'string')
|
|
9
|
+
return { reason: 'it is not a string' };
|
|
10
|
+
var bucketingKey = value.trim();
|
|
11
|
+
if (!bucketingKey)
|
|
12
|
+
return { reason: 'it is blank' };
|
|
13
|
+
if (bucketingKey.length > KEY_MAX_LENGTH)
|
|
14
|
+
return { reason: "it is longer than ".concat(KEY_MAX_LENGTH, " characters") };
|
|
15
|
+
return { value: bucketingKey };
|
|
16
|
+
}
|
|
17
|
+
function readBucketingKey(value) {
|
|
18
|
+
return parseBucketingKey(value).value;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Warning for a bucketingKey in the context that will not be used, or undefined when there is nothing to report.
|
|
22
|
+
* Names the reason only, never the value.
|
|
23
|
+
*/
|
|
24
|
+
export function bucketingKeyWarning(context) {
|
|
25
|
+
var _a = parseBucketingKey(context.bucketingKey), value = _a.value, reason = _a.reason;
|
|
26
|
+
if (reason) {
|
|
27
|
+
return "bucketingKey ignored because ".concat(reason).concat(context.targetingKey ? '; using the targeting key only' : '', ".");
|
|
28
|
+
}
|
|
29
|
+
if (value && !context.targetingKey)
|
|
30
|
+
return 'bucketingKey ignored because the context has no targetingKey.';
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Transform OpenFeature evaluation context into a consumer object for the Split API.
|
|
35
|
+
* `bucketingKey` is reserved: it is never forwarded as an attribute.
|
|
36
|
+
*/
|
|
37
|
+
export function transformContext(context, defaultTrafficType) {
|
|
38
|
+
var targetingKey = context.targetingKey, bucketingKey = context.bucketingKey, ttVal = context.trafficType, attributes = __rest(context, ["targetingKey", "bucketingKey", "trafficType"]);
|
|
39
|
+
var trafficType = ttVal != null && typeof ttVal === 'string' && ttVal.trim() !== ''
|
|
40
|
+
? ttVal
|
|
41
|
+
: defaultTrafficType;
|
|
42
|
+
return {
|
|
43
|
+
targetingKey: targetingKey,
|
|
44
|
+
bucketingKey: readBucketingKey(bucketingKey),
|
|
45
|
+
trafficType: trafficType,
|
|
46
|
+
attributes: JSON.parse(JSON.stringify(attributes)),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/** Split key for a consumer: `{ matchingKey, bucketingKey }` or the targeting key alone. */
|
|
50
|
+
export function toSplitKey(_a) {
|
|
51
|
+
var targetingKey = _a.targetingKey, bucketingKey = _a.bucketingKey;
|
|
52
|
+
if (!targetingKey)
|
|
53
|
+
return undefined;
|
|
54
|
+
return bucketingKey ? { matchingKey: targetingKey, bucketingKey: bucketingKey } : targetingKey;
|
|
55
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { FlagNotFoundError, StandardResolutionReasons } from "@openfeature/web-sdk";
|
|
2
|
+
import { CONTROL_TREATMENT, CONTROL_VALUE_ERROR_MESSAGE } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* Evaluate a flag with the Split client and return string resolution details.
|
|
5
|
+
*/
|
|
6
|
+
export function evaluateTreatment(client, flagKey, consumer) {
|
|
7
|
+
if (flagKey == null || flagKey === '') {
|
|
8
|
+
throw new FlagNotFoundError('flagKey must be a non-empty string');
|
|
9
|
+
}
|
|
10
|
+
var treatment = client.getTreatmentWithConfig(flagKey, consumer.attributes);
|
|
11
|
+
var value = treatment.treatment, config = treatment.config;
|
|
12
|
+
if (value === CONTROL_TREATMENT) {
|
|
13
|
+
throw new FlagNotFoundError(CONTROL_VALUE_ERROR_MESSAGE);
|
|
14
|
+
}
|
|
15
|
+
return {
|
|
16
|
+
value: value,
|
|
17
|
+
variant: value,
|
|
18
|
+
flagMetadata: { config: config ? config : '' },
|
|
19
|
+
reason: StandardResolutionReasons.TARGETING_MATCH,
|
|
20
|
+
};
|
|
21
|
+
}
|
package/es/lib/events.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { __assign } from "tslib";
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
|
+
import { ProviderEvents } from "@openfeature/web-sdk";
|
|
4
|
+
/** Map Split SdkReadyMetadata to OpenFeature EventMetadata (string | boolean | number). */
|
|
5
|
+
function toReadyEventMetadata(sdkReadyMetadata) {
|
|
6
|
+
var meta = {
|
|
7
|
+
initialCacheLoad: sdkReadyMetadata.initialCacheLoad,
|
|
8
|
+
};
|
|
9
|
+
if (sdkReadyMetadata.lastUpdateTimestamp != null) {
|
|
10
|
+
meta.lastUpdateTimestamp = sdkReadyMetadata.lastUpdateTimestamp;
|
|
11
|
+
}
|
|
12
|
+
return meta;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Emit OpenFeature ProviderEvents.Ready with Split SDK ready metadata, then invoke the callback.
|
|
16
|
+
* When sdkReadyMetadata is provided (from SDK_READY event), use it; otherwise emit with empty metadata (e.g. when already ready).
|
|
17
|
+
*/
|
|
18
|
+
function emitReadyWithSplitMetadata(eventsEmitter, providerName, onSdkReady, sdkReadyMetadata) {
|
|
19
|
+
var readyDetails = {
|
|
20
|
+
providerName: providerName,
|
|
21
|
+
metadata: sdkReadyMetadata ? toReadyEventMetadata(sdkReadyMetadata) : {},
|
|
22
|
+
};
|
|
23
|
+
eventsEmitter.emit(ProviderEvents.Ready, readyDetails);
|
|
24
|
+
onSdkReady();
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Attach Split SDK event listeners and resolve/reject the ready promise based on client status.
|
|
28
|
+
* When the Split SDK becomes ready, emits ProviderEvents.Ready with Split ready metadata before resolving.
|
|
29
|
+
*/
|
|
30
|
+
export function attachSplitReadyHandlers(client, eventsEmitter, callbacks, providerName) {
|
|
31
|
+
var onSdkReady = callbacks.onSdkReady, onSdkTimedOut = callbacks.onSdkTimedOut;
|
|
32
|
+
var onSdkReadyFromCache = function () {
|
|
33
|
+
eventsEmitter.emit(ProviderEvents.Stale, { message: 'Split ready from cache' });
|
|
34
|
+
};
|
|
35
|
+
var clientStatus = client.__getStatus();
|
|
36
|
+
if (clientStatus.isReady) {
|
|
37
|
+
emitReadyWithSplitMetadata(eventsEmitter, providerName, onSdkReady);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if (clientStatus.isReadyFromCache) {
|
|
41
|
+
onSdkReadyFromCache();
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
client.on(client.Event.SDK_READY_FROM_CACHE, onSdkReadyFromCache);
|
|
45
|
+
}
|
|
46
|
+
if (clientStatus.hasTimedout) {
|
|
47
|
+
onSdkTimedOut();
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
client.on(client.Event.SDK_READY_TIMED_OUT, onSdkTimedOut);
|
|
51
|
+
}
|
|
52
|
+
client.on(client.Event.SDK_READY, function (sdkReadyMetadata) {
|
|
53
|
+
emitReadyWithSplitMetadata(eventsEmitter, providerName, onSdkReady, sdkReadyMetadata);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Subscribe to Split SDK_UPDATE and emit OpenFeature ConfigurationChanged with details.
|
|
58
|
+
*/
|
|
59
|
+
export function attachSplitUpdateHandler(client, eventsEmitter, providerName) {
|
|
60
|
+
client.on(client.Event.SDK_UPDATE, function (updateMetadata) {
|
|
61
|
+
var eventDetails = { providerName: providerName };
|
|
62
|
+
if (updateMetadata) {
|
|
63
|
+
eventDetails = __assign(__assign({}, eventDetails), { metadata: {
|
|
64
|
+
type: updateMetadata.type,
|
|
65
|
+
}, flagsChanged: updateMetadata.names || [] });
|
|
66
|
+
}
|
|
67
|
+
eventsEmitter.emit(ProviderEvents.ConfigurationChanged, eventDetails);
|
|
68
|
+
});
|
|
69
|
+
}
|