@splitsoftware/openfeature-web-split-provider 1.0.0 → 1.1.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 +7 -2
- package/README.md +179 -16
- package/es/lib/context.js +15 -0
- package/es/lib/evaluation.js +21 -0
- package/es/lib/events.js +69 -0
- package/es/lib/js-split-provider.js +42 -140
- package/es/lib/parsers.js +38 -0
- package/es/lib/types.js +2 -0
- package/lib/lib/context.js +19 -0
- package/lib/lib/evaluation.js +25 -0
- package/lib/lib/events.js +74 -0
- package/lib/lib/js-split-provider.js +40 -138
- package/lib/lib/parsers.js +44 -0
- package/lib/lib/types.js +5 -0
- package/package.json +2 -2
- package/src/__tests__/context.spec.js +64 -0
- package/src/__tests__/evaluation.spec.js +76 -0
- package/src/__tests__/events.spec.js +182 -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 +21 -0
- package/src/lib/evaluation.ts +33 -0
- package/src/lib/events.ts +105 -0
- package/src/lib/js-split-provider.ts +63 -177
- package/src/lib/parsers.ts +43 -0
- package/src/lib/types.ts +10 -0
- package/types/lib/context.d.ts +6 -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 +1 -6
- package/types/lib/parsers.d.ts +8 -0
- package/types/lib/types.d.ts +8 -0
package/CHANGES.txt
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
1.1.0 (March 2, 2026)
|
|
2
|
+
- Added ProviderEvents.Ready payload with Split SdkReadyMetadata
|
|
3
|
+
- Updated ConfigurationChanged to forward SdkUpdateMetadata in metadata
|
|
4
|
+
- Requires @splitsoftware/splitio-browserjs ^1.7.0 for SDK_UPDATE metadata support
|
|
5
|
+
|
|
1
6
|
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
|
|
7
|
+
- First release.
|
|
8
|
+
- 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,25 @@ const context: EvaluationContext = {
|
|
|
43
49
|
targetingKey: 'TARGETING_KEY',
|
|
44
50
|
trafficType: 'account'
|
|
45
51
|
};
|
|
46
|
-
OpenFeature.setContext(context)
|
|
52
|
+
await OpenFeature.setContext(context)
|
|
47
53
|
```
|
|
48
54
|
|
|
55
|
+
## Configuration changed event (SDK_UPDATE)
|
|
56
|
+
|
|
57
|
+
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).
|
|
58
|
+
|
|
59
|
+
Requires `@splitsoftware/splitio-browserjs` **1.7.0 or later** (metadata was added in 1.7.0).
|
|
60
|
+
|
|
61
|
+
```js
|
|
62
|
+
const { OpenFeature, ProviderEvents } = require('@openfeature/web-sdk');
|
|
63
|
+
|
|
64
|
+
const client = OpenFeature.getClient();
|
|
65
|
+
client.addHandler(ProviderEvents.ConfigurationChanged, (eventDetails) => {
|
|
66
|
+
console.log('Flags changed:', eventDetails.flagsChanged);
|
|
67
|
+
console.log('Event metadata:', eventDetails.metadata);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
```
|
|
49
71
|
## Evaluate with details
|
|
50
72
|
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
73
|
|
|
@@ -62,10 +84,11 @@ Evaluation attributes must be set in context before evaluation
|
|
|
62
84
|
const context: EvaluationContext = {
|
|
63
85
|
targetingKey: 'TARGETING_KEY',
|
|
64
86
|
trafficType: 'account',
|
|
87
|
+
// Evaluation attributes:
|
|
65
88
|
plan: 'premium',
|
|
66
|
-
|
|
89
|
+
coupon: 'WELCOME10'
|
|
67
90
|
};
|
|
68
|
-
OpenFeature.setContext(context);
|
|
91
|
+
await OpenFeature.setContext(context);
|
|
69
92
|
const booleanTreatment = client.getBooleanDetails('boolFlag', false);
|
|
70
93
|
```
|
|
71
94
|
|
|
@@ -89,11 +112,152 @@ Example:
|
|
|
89
112
|
const context = { targetingKey: 'user-123', trafficType: 'account' }
|
|
90
113
|
const details = { value: 19.99, properties: { plan: 'pro', coupon: 'WELCOME10' }}
|
|
91
114
|
|
|
92
|
-
|
|
115
|
+
await OpenFeature.setContext(context)
|
|
93
116
|
client.track('checkout.completed', details)
|
|
94
117
|
```
|
|
118
|
+
|
|
119
|
+
## Angular Usage
|
|
120
|
+
|
|
121
|
+
### Install dependencies
|
|
122
|
+
```bash
|
|
123
|
+
npm i @openfeature/angular-sdk @splitsoftware/splitio-browserjs @splitsoftware/openfeature-web-split-provider
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### Initialize the Angular SDK
|
|
127
|
+
|
|
128
|
+
#### Traditional (NgModule) setup
|
|
129
|
+
```js
|
|
130
|
+
import { NgModule } from '@angular/core';
|
|
131
|
+
import { RouterModule, Routes } from '@angular/router';
|
|
132
|
+
import { OpenFeatureModule } from '@openfeature/angular-sdk';
|
|
133
|
+
import { SplitFactory } from '@splitsoftware/splitio-browserjs';
|
|
134
|
+
import { OpenFeatureSplitProvider } from '@splitsoftware/openfeature-web-split-provider';
|
|
135
|
+
|
|
136
|
+
const splitFactory = SplitFactory({
|
|
137
|
+
core: {
|
|
138
|
+
authorizationKey: 'CLIENT_SIDE_SDK_KEY',
|
|
139
|
+
key: 'TARGETING_KEY'
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
const openFeatureProvider = new OpenFeatureSplitProvider(splitFactory);
|
|
143
|
+
|
|
144
|
+
@NgModule({
|
|
145
|
+
imports: [
|
|
146
|
+
RouterModule.forRoot(routes),
|
|
147
|
+
OpenFeatureModule.forRoot({
|
|
148
|
+
provider: openFeatureProvider
|
|
149
|
+
})
|
|
150
|
+
],
|
|
151
|
+
bootstrap: [AppComponent]
|
|
152
|
+
})
|
|
153
|
+
export class AppModule {}
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
#### Standalone (Angular 16+) setup
|
|
157
|
+
```js
|
|
158
|
+
import { ApplicationConfig, provideBrowserGlobalErrorListeners, importProvidersFrom } from '@angular/core';
|
|
159
|
+
import { OpenFeatureModule } from '@openfeature/angular-sdk';
|
|
160
|
+
import { SplitFactory } from '@splitsoftware/splitio-browserjs';
|
|
161
|
+
import { OpenFeatureSplitProvider } from '@splitsoftware/openfeature-web-split-provider';
|
|
162
|
+
|
|
163
|
+
const splitFactory = SplitFactory({
|
|
164
|
+
core: {
|
|
165
|
+
authorizationKey: 'CLIENT_SIDE_SDK_KEY',
|
|
166
|
+
key: 'TARGETING_KEY'
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
const openFeatureProvider = new OpenFeatureSplitProvider(splitFactory);
|
|
170
|
+
|
|
171
|
+
export const appConfig: ApplicationConfig = {
|
|
172
|
+
providers: [
|
|
173
|
+
provideBrowserGlobalErrorListeners(),
|
|
174
|
+
importProvidersFrom(
|
|
175
|
+
OpenFeatureModule.forRoot({
|
|
176
|
+
provider: openFeatureProvider
|
|
177
|
+
})
|
|
178
|
+
)
|
|
179
|
+
]
|
|
180
|
+
};
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
#### Component injection and usage
|
|
184
|
+
```js
|
|
185
|
+
import { FeatureFlagService, EvaluationDetails } from '@openfeature/angular-sdk';
|
|
186
|
+
|
|
187
|
+
@Component({
|
|
188
|
+
selector: 'app-root',
|
|
189
|
+
imports: [RouterOutlet],
|
|
190
|
+
templateUrl: './app.html',
|
|
191
|
+
styleUrl: './app.css'
|
|
192
|
+
})
|
|
193
|
+
export class App {
|
|
194
|
+
constructor(private splitService: FeatureFlagService) {}
|
|
195
|
+
|
|
196
|
+
ngOnInit() {
|
|
197
|
+
this.splitService.getStringDetails('featureFlagName', 'default').subscribe((result: EvaluationDetails<string>) => {
|
|
198
|
+
console.log('Feature flag result:', result.value);
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
```
|
|
203
|
+
> 🔗 For more information on Angular integration, visit the [official OpenFeature Angular SDK documentation][angular-docs].
|
|
204
|
+
|
|
205
|
+
[angular-docs]: https://openfeature.dev/docs/reference/sdks/client/web/angular
|
|
206
|
+
|
|
207
|
+
## React usage
|
|
208
|
+
|
|
209
|
+
### Install dependencies
|
|
210
|
+
```bash
|
|
211
|
+
npm i @openfeature/react-sdk @splitsoftware/splitio-browserjs @splitsoftware/openfeature-web-split-provider
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
### Initialize the React SDK
|
|
215
|
+
```js
|
|
216
|
+
import { OpenFeature } from '@openfeature/react-sdk';
|
|
217
|
+
import { OpenFeatureSplitProvider } from '@splitsoftware/openfeature-web-split-provider';
|
|
218
|
+
import { DebugLogger, SplitFactory } from '@splitsoftware/splitio-browserjs';
|
|
219
|
+
|
|
220
|
+
const splitFactory = SplitFactory({
|
|
221
|
+
core: {
|
|
222
|
+
authorizationKey: 'CLIENT_SIDE_SDK_KEY',
|
|
223
|
+
key: 'TARGETING_KEY'
|
|
224
|
+
}
|
|
225
|
+
})
|
|
226
|
+
const openFeatureProvider = new OpenFeatureSplitProvider(splitFactory);
|
|
227
|
+
|
|
228
|
+
OpenFeature.setProvider(openFeatureProvider);
|
|
229
|
+
|
|
230
|
+
function App() {
|
|
231
|
+
return (
|
|
232
|
+
<OpenFeatureProvider>
|
|
233
|
+
<Page></Page>
|
|
234
|
+
</OpenFeatureProvider>
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
#### Evaluation hooks
|
|
240
|
+
```js
|
|
241
|
+
import { useFlag } from '@openfeature/react-sdk';
|
|
242
|
+
|
|
243
|
+
function Page() {
|
|
244
|
+
// Use the "query-style" flag evaluation hook, specifying a flag-key and a default value.
|
|
245
|
+
const { value: showNewMessage } = useFlag('new-message', true);
|
|
246
|
+
return (
|
|
247
|
+
<div className="App">
|
|
248
|
+
<header className="App-header">
|
|
249
|
+
{showNewMessage ? <p>Welcome to this OpenFeature-enabled React app!</p> : <p>Welcome to this React app.</p>}
|
|
250
|
+
</header>
|
|
251
|
+
</div>
|
|
252
|
+
)
|
|
253
|
+
}
|
|
254
|
+
```
|
|
255
|
+
> 🔗 For more information on React integration, visit the [official OpenFeature React SDK documentation][react-docs].
|
|
256
|
+
|
|
257
|
+
[react-docs]: https://openfeature.dev/docs/reference/sdks/client/web/react
|
|
258
|
+
|
|
95
259
|
## Submitting issues
|
|
96
|
-
|
|
260
|
+
|
|
97
261
|
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
262
|
|
|
99
263
|
## Contributing
|
|
@@ -103,13 +267,13 @@ Please see [Contributors Guide](CONTRIBUTORS-GUIDE.md) to find all you need to s
|
|
|
103
267
|
Licensed under the Apache License, Version 2.0. See: [Apache License](http://www.apache.org/licenses/).
|
|
104
268
|
|
|
105
269
|
## About Split
|
|
106
|
-
|
|
270
|
+
|
|
107
271
|
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
|
-
|
|
272
|
+
|
|
109
273
|
To learn more about Split, contact hello@split.io, or get started with feature flags for free at https://www.split.io/signup.
|
|
110
|
-
|
|
274
|
+
|
|
111
275
|
Split has built and maintains SDKs for:
|
|
112
|
-
|
|
276
|
+
|
|
113
277
|
* .NET [Github](https://github.com/splitio/dotnet-client) [Docs](https://help.split.io/hc/en-us/articles/360020240172--NET-SDK)
|
|
114
278
|
* Android [Github](https://github.com/splitio/android-client) [Docs](https://help.split.io/hc/en-us/articles/360020343291-Android-SDK)
|
|
115
279
|
* Angular [Github](https://github.com/splitio/angular-sdk-plugin) [Docs](https://help.split.io/hc/en-us/articles/6495326064397-Angular-utilities)
|
|
@@ -128,10 +292,9 @@ Split has built and maintains SDKs for:
|
|
|
128
292
|
* React Native [Github](https://github.com/splitio/react-native-client) [Docs](https://help.split.io/hc/en-us/articles/4406066357901-React-Native-SDK)
|
|
129
293
|
* Redux [Github](https://github.com/splitio/redux-client) [Docs](https://help.split.io/hc/en-us/articles/360038851551-Redux-SDK)
|
|
130
294
|
* Ruby [Github](https://github.com/splitio/ruby-client) [Docs](https://help.split.io/hc/en-us/articles/360020673251-Ruby-SDK)
|
|
131
|
-
|
|
295
|
+
|
|
132
296
|
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
|
-
|
|
297
|
+
|
|
134
298
|
**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
299
|
|
|
300
|
+
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,15 @@
|
|
|
1
|
+
import { __rest } from "tslib";
|
|
2
|
+
/**
|
|
3
|
+
* Transform OpenFeature evaluation context into a consumer object for the Split API.
|
|
4
|
+
*/
|
|
5
|
+
export function transformContext(context, defaultTrafficType) {
|
|
6
|
+
var targetingKey = context.targetingKey, ttVal = context.trafficType, attributes = __rest(context, ["targetingKey", "trafficType"]);
|
|
7
|
+
var trafficType = ttVal != null && typeof ttVal === 'string' && ttVal.trim() !== ''
|
|
8
|
+
? ttVal
|
|
9
|
+
: defaultTrafficType;
|
|
10
|
+
return {
|
|
11
|
+
targetingKey: targetingKey,
|
|
12
|
+
trafficType: trafficType,
|
|
13
|
+
attributes: JSON.parse(JSON.stringify(attributes)),
|
|
14
|
+
};
|
|
15
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -1,37 +1,18 @@
|
|
|
1
|
-
import { __assign, __awaiter, __generator
|
|
2
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
1
|
+
import { __assign, __awaiter, __generator } from "tslib";
|
|
3
2
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
4
|
-
import { ParseError,
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
import { ParseError, ProviderEvents, OpenFeatureEventEmitter, } from "@openfeature/web-sdk";
|
|
4
|
+
import { transformContext } from "./context";
|
|
5
|
+
import { attachSplitReadyHandlers, attachSplitUpdateHandler } from "./events";
|
|
6
|
+
import { evaluateTreatment } from "./evaluation";
|
|
7
|
+
import { parseBooleanTreatment, parseValidJsonObject, parseValidNumber } from "./parsers";
|
|
7
8
|
var OpenFeatureSplitProvider = /** @class */ (function () {
|
|
8
9
|
function OpenFeatureSplitProvider(splitFactory) {
|
|
9
|
-
this.metadata = {
|
|
10
|
-
name: "split",
|
|
11
|
-
};
|
|
10
|
+
this.metadata = { name: "split" };
|
|
12
11
|
this.events = new OpenFeatureEventEmitter();
|
|
13
|
-
// Asume 'user' as default traffic type'
|
|
14
12
|
this.trafficType = 'user';
|
|
15
13
|
this.factory = splitFactory;
|
|
16
14
|
this.client = splitFactory.client();
|
|
17
15
|
}
|
|
18
|
-
OpenFeatureSplitProvider.prototype.onContextChange = function (oldContext, newContext) {
|
|
19
|
-
var _this = this;
|
|
20
|
-
var oldTargetingKey = oldContext.targetingKey;
|
|
21
|
-
var newTargetingKey = newContext.targetingKey, newTrafficType = newContext.trafficType;
|
|
22
|
-
this.trafficType = newTrafficType && newTrafficType !== this.trafficType ? newTrafficType : this.trafficType;
|
|
23
|
-
if (newTargetingKey && newTargetingKey !== oldTargetingKey) {
|
|
24
|
-
this.client = this.factory.client(newTargetingKey);
|
|
25
|
-
return new Promise(function (resolve, reject) {
|
|
26
|
-
var emitContextChange = function () {
|
|
27
|
-
_this.events.emit(ProviderEvents.ConfigurationChanged);
|
|
28
|
-
resolve();
|
|
29
|
-
};
|
|
30
|
-
_this.eventsHandler(emitContextChange, reject);
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
return Promise.resolve();
|
|
34
|
-
};
|
|
35
16
|
OpenFeatureSplitProvider.prototype.initialize = function () {
|
|
36
17
|
return __awaiter(this, void 0, void 0, function () {
|
|
37
18
|
var _this = this;
|
|
@@ -39,7 +20,8 @@ var OpenFeatureSplitProvider = /** @class */ (function () {
|
|
|
39
20
|
switch (_a.label) {
|
|
40
21
|
case 0: return [4 /*yield*/, new Promise(function (resolve, reject) {
|
|
41
22
|
try {
|
|
42
|
-
_this.
|
|
23
|
+
attachSplitReadyHandlers(_this.client, _this.events, { onSdkReady: resolve, onSdkTimedOut: reject }, _this.metadata.name);
|
|
24
|
+
attachSplitUpdateHandler(_this.client, _this.events, _this.metadata.name);
|
|
43
25
|
}
|
|
44
26
|
catch (_a) {
|
|
45
27
|
reject();
|
|
@@ -52,58 +34,53 @@ var OpenFeatureSplitProvider = /** @class */ (function () {
|
|
|
52
34
|
});
|
|
53
35
|
});
|
|
54
36
|
};
|
|
55
|
-
OpenFeatureSplitProvider.prototype.
|
|
56
|
-
var
|
|
57
|
-
var
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
37
|
+
OpenFeatureSplitProvider.prototype.onContextChange = function (oldContext, newContext) {
|
|
38
|
+
var _this = this;
|
|
39
|
+
var oldTargetingKey = oldContext.targetingKey;
|
|
40
|
+
var newTargetingKey = newContext.targetingKey, newTrafficType = newContext.trafficType;
|
|
41
|
+
this.trafficType =
|
|
42
|
+
newTrafficType && newTrafficType !== this.trafficType
|
|
43
|
+
? newTrafficType
|
|
44
|
+
: this.trafficType;
|
|
45
|
+
if (newTargetingKey && newTargetingKey !== oldTargetingKey) {
|
|
46
|
+
this.client = this.factory.client(newTargetingKey);
|
|
47
|
+
return new Promise(function (resolve, reject) {
|
|
48
|
+
var emitContextChange = function () {
|
|
49
|
+
_this.events.emit(ProviderEvents.ConfigurationChanged);
|
|
50
|
+
resolve();
|
|
51
|
+
};
|
|
52
|
+
attachSplitReadyHandlers(_this.client, _this.events, { onSdkReady: emitContextChange, onSdkTimedOut: reject }, _this.metadata.name);
|
|
53
|
+
attachSplitUpdateHandler(_this.client, _this.events, _this.metadata.name);
|
|
54
|
+
});
|
|
63
55
|
}
|
|
64
|
-
|
|
56
|
+
return Promise.resolve();
|
|
57
|
+
};
|
|
58
|
+
OpenFeatureSplitProvider.prototype.resolveBooleanEvaluation = function (flagKey, _, context, _logger) {
|
|
59
|
+
var details = evaluateTreatment(this.client, flagKey, transformContext(context, this.trafficType));
|
|
60
|
+
var value = parseBooleanTreatment(details.value);
|
|
61
|
+
return __assign(__assign({}, details), { value: value });
|
|
65
62
|
};
|
|
66
63
|
OpenFeatureSplitProvider.prototype.resolveStringEvaluation = function (flagKey, _, context, _logger) {
|
|
67
|
-
|
|
68
|
-
return details;
|
|
64
|
+
return evaluateTreatment(this.client, flagKey, transformContext(context, this.trafficType));
|
|
69
65
|
};
|
|
70
66
|
OpenFeatureSplitProvider.prototype.resolveNumberEvaluation = function (flagKey, _, context, _logger) {
|
|
71
|
-
var details = this.
|
|
72
|
-
return __assign(__assign({}, details), { value:
|
|
67
|
+
var details = evaluateTreatment(this.client, flagKey, transformContext(context, this.trafficType));
|
|
68
|
+
return __assign(__assign({}, details), { value: parseValidNumber(details.value) });
|
|
73
69
|
};
|
|
74
70
|
OpenFeatureSplitProvider.prototype.resolveObjectEvaluation = function (flagKey, _, context, _logger) {
|
|
75
|
-
var details = this.
|
|
76
|
-
return __assign(__assign({}, details), { value:
|
|
77
|
-
};
|
|
78
|
-
OpenFeatureSplitProvider.prototype.evaluateTreatment = function (flagKey, consumer) {
|
|
79
|
-
if (flagKey == null || flagKey === '') {
|
|
80
|
-
throw new FlagNotFoundError('flagKey must be a non-empty string');
|
|
81
|
-
}
|
|
82
|
-
var treatment = this.client.getTreatmentWithConfig(flagKey, consumer.attributes);
|
|
83
|
-
var value = treatment.treatment, config = treatment.config;
|
|
84
|
-
if (value === CONTROL_TREATMENT) {
|
|
85
|
-
throw new FlagNotFoundError(CONTROL_VALUE_ERROR_MESSAGE);
|
|
86
|
-
}
|
|
87
|
-
var flagMetadata = { config: config ? config : '' };
|
|
88
|
-
var details = {
|
|
89
|
-
value: value,
|
|
90
|
-
variant: value,
|
|
91
|
-
flagMetadata: flagMetadata,
|
|
92
|
-
reason: StandardResolutionReasons.TARGETING_MATCH,
|
|
93
|
-
};
|
|
94
|
-
return details;
|
|
71
|
+
var details = evaluateTreatment(this.client, flagKey, transformContext(context, this.trafficType));
|
|
72
|
+
return __assign(__assign({}, details), { value: parseValidJsonObject(details.value) });
|
|
95
73
|
};
|
|
96
74
|
OpenFeatureSplitProvider.prototype.track = function (trackingEventName, context, details) {
|
|
97
|
-
|
|
98
|
-
if (trackingEventName == null || trackingEventName === '')
|
|
75
|
+
if (trackingEventName == null || trackingEventName === '') {
|
|
99
76
|
throw new ParseError('Missing eventName, required to track');
|
|
100
|
-
|
|
77
|
+
}
|
|
78
|
+
var trafficType = transformContext(context, this.trafficType).trafficType;
|
|
101
79
|
var value;
|
|
102
80
|
var properties = {};
|
|
103
81
|
if (details != null) {
|
|
104
|
-
if (details.value != null)
|
|
82
|
+
if (details.value != null)
|
|
105
83
|
value = details.value;
|
|
106
|
-
}
|
|
107
84
|
if (details.properties != null) {
|
|
108
85
|
properties = details.properties;
|
|
109
86
|
}
|
|
@@ -117,81 +94,6 @@ var OpenFeatureSplitProvider = /** @class */ (function () {
|
|
|
117
94
|
});
|
|
118
95
|
});
|
|
119
96
|
};
|
|
120
|
-
//Transform the context into an object useful for the Split API, an key string with arbitrary Split "Attributes".
|
|
121
|
-
OpenFeatureSplitProvider.prototype.transformContext = function (context) {
|
|
122
|
-
var targetingKey = context.targetingKey, ttVal = context.trafficType, attributes = __rest(context, ["targetingKey", "trafficType"]);
|
|
123
|
-
var trafficType = ttVal != null && typeof ttVal === 'string' && ttVal.trim() !== ''
|
|
124
|
-
? ttVal
|
|
125
|
-
: this.trafficType;
|
|
126
|
-
return {
|
|
127
|
-
targetingKey: targetingKey,
|
|
128
|
-
trafficType: trafficType,
|
|
129
|
-
// Stringify context objects include date.
|
|
130
|
-
attributes: JSON.parse(JSON.stringify(attributes)),
|
|
131
|
-
};
|
|
132
|
-
};
|
|
133
|
-
OpenFeatureSplitProvider.prototype.parseValidNumber = function (stringValue) {
|
|
134
|
-
if (stringValue === undefined) {
|
|
135
|
-
throw new ParseError("Invalid 'undefined' value.");
|
|
136
|
-
}
|
|
137
|
-
var result = Number.parseFloat(stringValue);
|
|
138
|
-
if (Number.isNaN(result)) {
|
|
139
|
-
throw new ParseError("Invalid numeric value ".concat(stringValue));
|
|
140
|
-
}
|
|
141
|
-
return result;
|
|
142
|
-
};
|
|
143
|
-
OpenFeatureSplitProvider.prototype.parseValidJsonObject = function (stringValue) {
|
|
144
|
-
if (stringValue === undefined) {
|
|
145
|
-
throw new ParseError("Invalid 'undefined' JSON value.");
|
|
146
|
-
}
|
|
147
|
-
// we may want to allow the parsing to be customized.
|
|
148
|
-
try {
|
|
149
|
-
var value = JSON.parse(stringValue);
|
|
150
|
-
if (typeof value !== "object") {
|
|
151
|
-
throw new ParseError("Flag value ".concat(stringValue, " had unexpected type ").concat(typeof value, ", expected \"object\""));
|
|
152
|
-
}
|
|
153
|
-
return value;
|
|
154
|
-
}
|
|
155
|
-
catch (err) {
|
|
156
|
-
throw new ParseError("Error parsing ".concat(stringValue, " as JSON, ").concat(err));
|
|
157
|
-
}
|
|
158
|
-
};
|
|
159
|
-
OpenFeatureSplitProvider.prototype.eventsHandler = function (onSdkReady, onSdkTimedOut) {
|
|
160
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
161
|
-
var onSdkReadyFromCache, clientStatus;
|
|
162
|
-
var _this = this;
|
|
163
|
-
return __generator(this, function (_a) {
|
|
164
|
-
onSdkReadyFromCache = function () {
|
|
165
|
-
_this.events.emit(ProviderEvents.Stale, {
|
|
166
|
-
message: "Split ready from cache",
|
|
167
|
-
});
|
|
168
|
-
};
|
|
169
|
-
clientStatus = this.client.__getStatus();
|
|
170
|
-
if (clientStatus.isReady) {
|
|
171
|
-
onSdkReady();
|
|
172
|
-
}
|
|
173
|
-
else {
|
|
174
|
-
if (clientStatus.isReadyFromCache) {
|
|
175
|
-
onSdkReadyFromCache();
|
|
176
|
-
}
|
|
177
|
-
else {
|
|
178
|
-
this.client.on(this.client.Event.SDK_READY_FROM_CACHE, onSdkReadyFromCache);
|
|
179
|
-
}
|
|
180
|
-
if (clientStatus.hasTimedout) {
|
|
181
|
-
onSdkTimedOut();
|
|
182
|
-
}
|
|
183
|
-
else {
|
|
184
|
-
this.client.on(this.client.Event.SDK_READY_TIMED_OUT, onSdkTimedOut);
|
|
185
|
-
}
|
|
186
|
-
this.client.on(this.client.Event.SDK_READY, onSdkReady);
|
|
187
|
-
}
|
|
188
|
-
this.client.on(this.client.Event.SDK_UPDATE, function () {
|
|
189
|
-
_this.events.emit(ProviderEvents.ConfigurationChanged);
|
|
190
|
-
});
|
|
191
|
-
return [2 /*return*/];
|
|
192
|
-
});
|
|
193
|
-
});
|
|
194
|
-
};
|
|
195
97
|
return OpenFeatureSplitProvider;
|
|
196
98
|
}());
|
|
197
99
|
export { OpenFeatureSplitProvider };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { ParseError } from "@openfeature/web-sdk";
|
|
2
|
+
export function parseValidNumber(stringValue) {
|
|
3
|
+
if (stringValue === undefined) {
|
|
4
|
+
throw new ParseError("Invalid 'undefined' value.");
|
|
5
|
+
}
|
|
6
|
+
var result = Number.parseFloat(stringValue);
|
|
7
|
+
if (Number.isNaN(result)) {
|
|
8
|
+
throw new ParseError("Invalid numeric value ".concat(stringValue));
|
|
9
|
+
}
|
|
10
|
+
return result;
|
|
11
|
+
}
|
|
12
|
+
export function parseValidJsonObject(stringValue) {
|
|
13
|
+
if (stringValue === undefined) {
|
|
14
|
+
throw new ParseError("Invalid 'undefined' JSON value.");
|
|
15
|
+
}
|
|
16
|
+
try {
|
|
17
|
+
var value = JSON.parse(stringValue);
|
|
18
|
+
if (typeof value !== "object") {
|
|
19
|
+
throw new ParseError("Flag value ".concat(stringValue, " had unexpected type ").concat(typeof value, ", expected \"object\""));
|
|
20
|
+
}
|
|
21
|
+
return value;
|
|
22
|
+
}
|
|
23
|
+
catch (err) {
|
|
24
|
+
throw new ParseError("Error parsing ".concat(stringValue, " as JSON, ").concat(err));
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Map a string treatment to a boolean for boolean flag resolution.
|
|
29
|
+
* @throws ParseError if treatment is not a valid boolean representation
|
|
30
|
+
*/
|
|
31
|
+
export function parseBooleanTreatment(treatment) {
|
|
32
|
+
var lower = treatment.toLowerCase();
|
|
33
|
+
if (lower === 'on' || lower === 'true')
|
|
34
|
+
return true;
|
|
35
|
+
if (lower === 'off' || lower === 'false')
|
|
36
|
+
return false;
|
|
37
|
+
throw new ParseError("Invalid boolean value for ".concat(treatment));
|
|
38
|
+
}
|
package/es/lib/types.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.transformContext = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
/**
|
|
6
|
+
* Transform OpenFeature evaluation context into a consumer object for the Split API.
|
|
7
|
+
*/
|
|
8
|
+
function transformContext(context, defaultTrafficType) {
|
|
9
|
+
var targetingKey = context.targetingKey, ttVal = context.trafficType, attributes = tslib_1.__rest(context, ["targetingKey", "trafficType"]);
|
|
10
|
+
var trafficType = ttVal != null && typeof ttVal === 'string' && ttVal.trim() !== ''
|
|
11
|
+
? ttVal
|
|
12
|
+
: defaultTrafficType;
|
|
13
|
+
return {
|
|
14
|
+
targetingKey: targetingKey,
|
|
15
|
+
trafficType: trafficType,
|
|
16
|
+
attributes: JSON.parse(JSON.stringify(attributes)),
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
exports.transformContext = transformContext;
|