@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
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.evaluateTreatment = void 0;
|
|
4
|
+
var web_sdk_1 = require("@openfeature/web-sdk");
|
|
5
|
+
var types_1 = require("./types");
|
|
6
|
+
/**
|
|
7
|
+
* Evaluate a flag with the Split client and return string resolution details.
|
|
8
|
+
*/
|
|
9
|
+
function evaluateTreatment(client, flagKey, consumer) {
|
|
10
|
+
if (flagKey == null || flagKey === '') {
|
|
11
|
+
throw new web_sdk_1.FlagNotFoundError('flagKey must be a non-empty string');
|
|
12
|
+
}
|
|
13
|
+
var treatment = client.getTreatmentWithConfig(flagKey, consumer.attributes);
|
|
14
|
+
var value = treatment.treatment, config = treatment.config;
|
|
15
|
+
if (value === types_1.CONTROL_TREATMENT) {
|
|
16
|
+
throw new web_sdk_1.FlagNotFoundError(types_1.CONTROL_VALUE_ERROR_MESSAGE);
|
|
17
|
+
}
|
|
18
|
+
return {
|
|
19
|
+
value: value,
|
|
20
|
+
variant: value,
|
|
21
|
+
flagMetadata: { config: config ? config : '' },
|
|
22
|
+
reason: web_sdk_1.StandardResolutionReasons.TARGETING_MATCH,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
exports.evaluateTreatment = evaluateTreatment;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.attachSplitUpdateHandler = exports.attachSplitReadyHandlers = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
6
|
+
var web_sdk_1 = require("@openfeature/web-sdk");
|
|
7
|
+
/** Map Split SdkReadyMetadata to OpenFeature EventMetadata (string | boolean | number). */
|
|
8
|
+
function toReadyEventMetadata(sdkReadyMetadata) {
|
|
9
|
+
var meta = {
|
|
10
|
+
initialCacheLoad: sdkReadyMetadata.initialCacheLoad,
|
|
11
|
+
};
|
|
12
|
+
if (sdkReadyMetadata.lastUpdateTimestamp != null) {
|
|
13
|
+
meta.lastUpdateTimestamp = sdkReadyMetadata.lastUpdateTimestamp;
|
|
14
|
+
}
|
|
15
|
+
return meta;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Emit OpenFeature ProviderEvents.Ready with Split SDK ready metadata, then invoke the callback.
|
|
19
|
+
* When sdkReadyMetadata is provided (from SDK_READY event), use it; otherwise emit with empty metadata (e.g. when already ready).
|
|
20
|
+
*/
|
|
21
|
+
function emitReadyWithSplitMetadata(eventsEmitter, providerName, onSdkReady, sdkReadyMetadata) {
|
|
22
|
+
var readyDetails = {
|
|
23
|
+
providerName: providerName,
|
|
24
|
+
metadata: sdkReadyMetadata ? toReadyEventMetadata(sdkReadyMetadata) : {},
|
|
25
|
+
};
|
|
26
|
+
eventsEmitter.emit(web_sdk_1.ProviderEvents.Ready, readyDetails);
|
|
27
|
+
onSdkReady();
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Attach Split SDK event listeners and resolve/reject the ready promise based on client status.
|
|
31
|
+
* When the Split SDK becomes ready, emits ProviderEvents.Ready with Split ready metadata before resolving.
|
|
32
|
+
*/
|
|
33
|
+
function attachSplitReadyHandlers(client, eventsEmitter, callbacks, providerName) {
|
|
34
|
+
var onSdkReady = callbacks.onSdkReady, onSdkTimedOut = callbacks.onSdkTimedOut;
|
|
35
|
+
var onSdkReadyFromCache = function () {
|
|
36
|
+
eventsEmitter.emit(web_sdk_1.ProviderEvents.Stale, { message: 'Split ready from cache' });
|
|
37
|
+
};
|
|
38
|
+
var clientStatus = client.__getStatus();
|
|
39
|
+
if (clientStatus.isReady) {
|
|
40
|
+
emitReadyWithSplitMetadata(eventsEmitter, providerName, onSdkReady);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
if (clientStatus.isReadyFromCache) {
|
|
44
|
+
onSdkReadyFromCache();
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
client.on(client.Event.SDK_READY_FROM_CACHE, onSdkReadyFromCache);
|
|
48
|
+
}
|
|
49
|
+
if (clientStatus.hasTimedout) {
|
|
50
|
+
onSdkTimedOut();
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
client.on(client.Event.SDK_READY_TIMED_OUT, onSdkTimedOut);
|
|
54
|
+
}
|
|
55
|
+
client.on(client.Event.SDK_READY, function (sdkReadyMetadata) {
|
|
56
|
+
emitReadyWithSplitMetadata(eventsEmitter, providerName, onSdkReady, sdkReadyMetadata);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
exports.attachSplitReadyHandlers = attachSplitReadyHandlers;
|
|
60
|
+
/**
|
|
61
|
+
* Subscribe to Split SDK_UPDATE and emit OpenFeature ConfigurationChanged with details.
|
|
62
|
+
*/
|
|
63
|
+
function attachSplitUpdateHandler(client, eventsEmitter, providerName) {
|
|
64
|
+
client.on(client.Event.SDK_UPDATE, function (updateMetadata) {
|
|
65
|
+
var eventDetails = { providerName: providerName };
|
|
66
|
+
if (updateMetadata) {
|
|
67
|
+
eventDetails = tslib_1.__assign(tslib_1.__assign({}, eventDetails), { metadata: {
|
|
68
|
+
type: updateMetadata.type,
|
|
69
|
+
}, flagsChanged: updateMetadata.names || [] });
|
|
70
|
+
}
|
|
71
|
+
eventsEmitter.emit(web_sdk_1.ProviderEvents.ConfigurationChanged, eventDetails);
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
exports.attachSplitUpdateHandler = attachSplitUpdateHandler;
|
|
@@ -2,39 +2,20 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.OpenFeatureSplitProvider = void 0;
|
|
4
4
|
var tslib_1 = require("tslib");
|
|
5
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
6
5
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
7
6
|
var web_sdk_1 = require("@openfeature/web-sdk");
|
|
8
|
-
var
|
|
9
|
-
var
|
|
7
|
+
var context_1 = require("./context");
|
|
8
|
+
var events_1 = require("./events");
|
|
9
|
+
var evaluation_1 = require("./evaluation");
|
|
10
|
+
var parsers_1 = require("./parsers");
|
|
10
11
|
var OpenFeatureSplitProvider = /** @class */ (function () {
|
|
11
12
|
function OpenFeatureSplitProvider(splitFactory) {
|
|
12
|
-
this.metadata = {
|
|
13
|
-
name: "split",
|
|
14
|
-
};
|
|
13
|
+
this.metadata = { name: "split" };
|
|
15
14
|
this.events = new web_sdk_1.OpenFeatureEventEmitter();
|
|
16
|
-
// Asume 'user' as default traffic type'
|
|
17
15
|
this.trafficType = 'user';
|
|
18
16
|
this.factory = splitFactory;
|
|
19
17
|
this.client = splitFactory.client();
|
|
20
18
|
}
|
|
21
|
-
OpenFeatureSplitProvider.prototype.onContextChange = function (oldContext, newContext) {
|
|
22
|
-
var _this = this;
|
|
23
|
-
var oldTargetingKey = oldContext.targetingKey;
|
|
24
|
-
var newTargetingKey = newContext.targetingKey, newTrafficType = newContext.trafficType;
|
|
25
|
-
this.trafficType = newTrafficType && newTrafficType !== this.trafficType ? newTrafficType : this.trafficType;
|
|
26
|
-
if (newTargetingKey && newTargetingKey !== oldTargetingKey) {
|
|
27
|
-
this.client = this.factory.client(newTargetingKey);
|
|
28
|
-
return new Promise(function (resolve, reject) {
|
|
29
|
-
var emitContextChange = function () {
|
|
30
|
-
_this.events.emit(web_sdk_1.ProviderEvents.ConfigurationChanged);
|
|
31
|
-
resolve();
|
|
32
|
-
};
|
|
33
|
-
_this.eventsHandler(emitContextChange, reject);
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
return Promise.resolve();
|
|
37
|
-
};
|
|
38
19
|
OpenFeatureSplitProvider.prototype.initialize = function () {
|
|
39
20
|
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
40
21
|
var _this = this;
|
|
@@ -42,7 +23,8 @@ var OpenFeatureSplitProvider = /** @class */ (function () {
|
|
|
42
23
|
switch (_a.label) {
|
|
43
24
|
case 0: return [4 /*yield*/, new Promise(function (resolve, reject) {
|
|
44
25
|
try {
|
|
45
|
-
_this.
|
|
26
|
+
(0, events_1.attachSplitReadyHandlers)(_this.client, _this.events, { onSdkReady: resolve, onSdkTimedOut: reject }, _this.metadata.name);
|
|
27
|
+
(0, events_1.attachSplitUpdateHandler)(_this.client, _this.events, _this.metadata.name);
|
|
46
28
|
}
|
|
47
29
|
catch (_a) {
|
|
48
30
|
reject();
|
|
@@ -55,58 +37,53 @@ var OpenFeatureSplitProvider = /** @class */ (function () {
|
|
|
55
37
|
});
|
|
56
38
|
});
|
|
57
39
|
};
|
|
58
|
-
OpenFeatureSplitProvider.prototype.
|
|
59
|
-
var
|
|
60
|
-
var
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
40
|
+
OpenFeatureSplitProvider.prototype.onContextChange = function (oldContext, newContext) {
|
|
41
|
+
var _this = this;
|
|
42
|
+
var oldTargetingKey = oldContext.targetingKey;
|
|
43
|
+
var newTargetingKey = newContext.targetingKey, newTrafficType = newContext.trafficType;
|
|
44
|
+
this.trafficType =
|
|
45
|
+
newTrafficType && newTrafficType !== this.trafficType
|
|
46
|
+
? newTrafficType
|
|
47
|
+
: this.trafficType;
|
|
48
|
+
if (newTargetingKey && newTargetingKey !== oldTargetingKey) {
|
|
49
|
+
this.client = this.factory.client(newTargetingKey);
|
|
50
|
+
return new Promise(function (resolve, reject) {
|
|
51
|
+
var emitContextChange = function () {
|
|
52
|
+
_this.events.emit(web_sdk_1.ProviderEvents.ConfigurationChanged);
|
|
53
|
+
resolve();
|
|
54
|
+
};
|
|
55
|
+
(0, events_1.attachSplitReadyHandlers)(_this.client, _this.events, { onSdkReady: emitContextChange, onSdkTimedOut: reject }, _this.metadata.name);
|
|
56
|
+
(0, events_1.attachSplitUpdateHandler)(_this.client, _this.events, _this.metadata.name);
|
|
57
|
+
});
|
|
66
58
|
}
|
|
67
|
-
|
|
59
|
+
return Promise.resolve();
|
|
60
|
+
};
|
|
61
|
+
OpenFeatureSplitProvider.prototype.resolveBooleanEvaluation = function (flagKey, _, context, _logger) {
|
|
62
|
+
var details = (0, evaluation_1.evaluateTreatment)(this.client, flagKey, (0, context_1.transformContext)(context, this.trafficType));
|
|
63
|
+
var value = (0, parsers_1.parseBooleanTreatment)(details.value);
|
|
64
|
+
return tslib_1.__assign(tslib_1.__assign({}, details), { value: value });
|
|
68
65
|
};
|
|
69
66
|
OpenFeatureSplitProvider.prototype.resolveStringEvaluation = function (flagKey, _, context, _logger) {
|
|
70
|
-
|
|
71
|
-
return details;
|
|
67
|
+
return (0, evaluation_1.evaluateTreatment)(this.client, flagKey, (0, context_1.transformContext)(context, this.trafficType));
|
|
72
68
|
};
|
|
73
69
|
OpenFeatureSplitProvider.prototype.resolveNumberEvaluation = function (flagKey, _, context, _logger) {
|
|
74
|
-
var details =
|
|
75
|
-
return tslib_1.__assign(tslib_1.__assign({}, details), { value:
|
|
70
|
+
var details = (0, evaluation_1.evaluateTreatment)(this.client, flagKey, (0, context_1.transformContext)(context, this.trafficType));
|
|
71
|
+
return tslib_1.__assign(tslib_1.__assign({}, details), { value: (0, parsers_1.parseValidNumber)(details.value) });
|
|
76
72
|
};
|
|
77
73
|
OpenFeatureSplitProvider.prototype.resolveObjectEvaluation = function (flagKey, _, context, _logger) {
|
|
78
|
-
var details =
|
|
79
|
-
return tslib_1.__assign(tslib_1.__assign({}, details), { value:
|
|
80
|
-
};
|
|
81
|
-
OpenFeatureSplitProvider.prototype.evaluateTreatment = function (flagKey, consumer) {
|
|
82
|
-
if (flagKey == null || flagKey === '') {
|
|
83
|
-
throw new web_sdk_1.FlagNotFoundError('flagKey must be a non-empty string');
|
|
84
|
-
}
|
|
85
|
-
var treatment = this.client.getTreatmentWithConfig(flagKey, consumer.attributes);
|
|
86
|
-
var value = treatment.treatment, config = treatment.config;
|
|
87
|
-
if (value === CONTROL_TREATMENT) {
|
|
88
|
-
throw new web_sdk_1.FlagNotFoundError(CONTROL_VALUE_ERROR_MESSAGE);
|
|
89
|
-
}
|
|
90
|
-
var flagMetadata = { config: config ? config : '' };
|
|
91
|
-
var details = {
|
|
92
|
-
value: value,
|
|
93
|
-
variant: value,
|
|
94
|
-
flagMetadata: flagMetadata,
|
|
95
|
-
reason: web_sdk_1.StandardResolutionReasons.TARGETING_MATCH,
|
|
96
|
-
};
|
|
97
|
-
return details;
|
|
74
|
+
var details = (0, evaluation_1.evaluateTreatment)(this.client, flagKey, (0, context_1.transformContext)(context, this.trafficType));
|
|
75
|
+
return tslib_1.__assign(tslib_1.__assign({}, details), { value: (0, parsers_1.parseValidJsonObject)(details.value) });
|
|
98
76
|
};
|
|
99
77
|
OpenFeatureSplitProvider.prototype.track = function (trackingEventName, context, details) {
|
|
100
|
-
|
|
101
|
-
if (trackingEventName == null || trackingEventName === '')
|
|
78
|
+
if (trackingEventName == null || trackingEventName === '') {
|
|
102
79
|
throw new web_sdk_1.ParseError('Missing eventName, required to track');
|
|
103
|
-
|
|
80
|
+
}
|
|
81
|
+
var trafficType = (0, context_1.transformContext)(context, this.trafficType).trafficType;
|
|
104
82
|
var value;
|
|
105
83
|
var properties = {};
|
|
106
84
|
if (details != null) {
|
|
107
|
-
if (details.value != null)
|
|
85
|
+
if (details.value != null)
|
|
108
86
|
value = details.value;
|
|
109
|
-
}
|
|
110
87
|
if (details.properties != null) {
|
|
111
88
|
properties = details.properties;
|
|
112
89
|
}
|
|
@@ -120,81 +97,6 @@ var OpenFeatureSplitProvider = /** @class */ (function () {
|
|
|
120
97
|
});
|
|
121
98
|
});
|
|
122
99
|
};
|
|
123
|
-
//Transform the context into an object useful for the Split API, an key string with arbitrary Split "Attributes".
|
|
124
|
-
OpenFeatureSplitProvider.prototype.transformContext = function (context) {
|
|
125
|
-
var targetingKey = context.targetingKey, ttVal = context.trafficType, attributes = tslib_1.__rest(context, ["targetingKey", "trafficType"]);
|
|
126
|
-
var trafficType = ttVal != null && typeof ttVal === 'string' && ttVal.trim() !== ''
|
|
127
|
-
? ttVal
|
|
128
|
-
: this.trafficType;
|
|
129
|
-
return {
|
|
130
|
-
targetingKey: targetingKey,
|
|
131
|
-
trafficType: trafficType,
|
|
132
|
-
// Stringify context objects include date.
|
|
133
|
-
attributes: JSON.parse(JSON.stringify(attributes)),
|
|
134
|
-
};
|
|
135
|
-
};
|
|
136
|
-
OpenFeatureSplitProvider.prototype.parseValidNumber = function (stringValue) {
|
|
137
|
-
if (stringValue === undefined) {
|
|
138
|
-
throw new web_sdk_1.ParseError("Invalid 'undefined' value.");
|
|
139
|
-
}
|
|
140
|
-
var result = Number.parseFloat(stringValue);
|
|
141
|
-
if (Number.isNaN(result)) {
|
|
142
|
-
throw new web_sdk_1.ParseError("Invalid numeric value ".concat(stringValue));
|
|
143
|
-
}
|
|
144
|
-
return result;
|
|
145
|
-
};
|
|
146
|
-
OpenFeatureSplitProvider.prototype.parseValidJsonObject = function (stringValue) {
|
|
147
|
-
if (stringValue === undefined) {
|
|
148
|
-
throw new web_sdk_1.ParseError("Invalid 'undefined' JSON value.");
|
|
149
|
-
}
|
|
150
|
-
// we may want to allow the parsing to be customized.
|
|
151
|
-
try {
|
|
152
|
-
var value = JSON.parse(stringValue);
|
|
153
|
-
if (typeof value !== "object") {
|
|
154
|
-
throw new web_sdk_1.ParseError("Flag value ".concat(stringValue, " had unexpected type ").concat(typeof value, ", expected \"object\""));
|
|
155
|
-
}
|
|
156
|
-
return value;
|
|
157
|
-
}
|
|
158
|
-
catch (err) {
|
|
159
|
-
throw new web_sdk_1.ParseError("Error parsing ".concat(stringValue, " as JSON, ").concat(err));
|
|
160
|
-
}
|
|
161
|
-
};
|
|
162
|
-
OpenFeatureSplitProvider.prototype.eventsHandler = function (onSdkReady, onSdkTimedOut) {
|
|
163
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
164
|
-
var onSdkReadyFromCache, clientStatus;
|
|
165
|
-
var _this = this;
|
|
166
|
-
return tslib_1.__generator(this, function (_a) {
|
|
167
|
-
onSdkReadyFromCache = function () {
|
|
168
|
-
_this.events.emit(web_sdk_1.ProviderEvents.Stale, {
|
|
169
|
-
message: "Split ready from cache",
|
|
170
|
-
});
|
|
171
|
-
};
|
|
172
|
-
clientStatus = this.client.__getStatus();
|
|
173
|
-
if (clientStatus.isReady) {
|
|
174
|
-
onSdkReady();
|
|
175
|
-
}
|
|
176
|
-
else {
|
|
177
|
-
if (clientStatus.isReadyFromCache) {
|
|
178
|
-
onSdkReadyFromCache();
|
|
179
|
-
}
|
|
180
|
-
else {
|
|
181
|
-
this.client.on(this.client.Event.SDK_READY_FROM_CACHE, onSdkReadyFromCache);
|
|
182
|
-
}
|
|
183
|
-
if (clientStatus.hasTimedout) {
|
|
184
|
-
onSdkTimedOut();
|
|
185
|
-
}
|
|
186
|
-
else {
|
|
187
|
-
this.client.on(this.client.Event.SDK_READY_TIMED_OUT, onSdkTimedOut);
|
|
188
|
-
}
|
|
189
|
-
this.client.on(this.client.Event.SDK_READY, onSdkReady);
|
|
190
|
-
}
|
|
191
|
-
this.client.on(this.client.Event.SDK_UPDATE, function () {
|
|
192
|
-
_this.events.emit(web_sdk_1.ProviderEvents.ConfigurationChanged);
|
|
193
|
-
});
|
|
194
|
-
return [2 /*return*/];
|
|
195
|
-
});
|
|
196
|
-
});
|
|
197
|
-
};
|
|
198
100
|
return OpenFeatureSplitProvider;
|
|
199
101
|
}());
|
|
200
102
|
exports.OpenFeatureSplitProvider = OpenFeatureSplitProvider;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseBooleanTreatment = exports.parseValidJsonObject = exports.parseValidNumber = void 0;
|
|
4
|
+
var web_sdk_1 = require("@openfeature/web-sdk");
|
|
5
|
+
function parseValidNumber(stringValue) {
|
|
6
|
+
if (stringValue === undefined) {
|
|
7
|
+
throw new web_sdk_1.ParseError("Invalid 'undefined' value.");
|
|
8
|
+
}
|
|
9
|
+
var result = Number.parseFloat(stringValue);
|
|
10
|
+
if (Number.isNaN(result)) {
|
|
11
|
+
throw new web_sdk_1.ParseError("Invalid numeric value ".concat(stringValue));
|
|
12
|
+
}
|
|
13
|
+
return result;
|
|
14
|
+
}
|
|
15
|
+
exports.parseValidNumber = parseValidNumber;
|
|
16
|
+
function parseValidJsonObject(stringValue) {
|
|
17
|
+
if (stringValue === undefined) {
|
|
18
|
+
throw new web_sdk_1.ParseError("Invalid 'undefined' JSON value.");
|
|
19
|
+
}
|
|
20
|
+
try {
|
|
21
|
+
var value = JSON.parse(stringValue);
|
|
22
|
+
if (typeof value !== "object") {
|
|
23
|
+
throw new web_sdk_1.ParseError("Flag value ".concat(stringValue, " had unexpected type ").concat(typeof value, ", expected \"object\""));
|
|
24
|
+
}
|
|
25
|
+
return value;
|
|
26
|
+
}
|
|
27
|
+
catch (err) {
|
|
28
|
+
throw new web_sdk_1.ParseError("Error parsing ".concat(stringValue, " as JSON, ").concat(err));
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.parseValidJsonObject = parseValidJsonObject;
|
|
32
|
+
/**
|
|
33
|
+
* Map a string treatment to a boolean for boolean flag resolution.
|
|
34
|
+
* @throws ParseError if treatment is not a valid boolean representation
|
|
35
|
+
*/
|
|
36
|
+
function parseBooleanTreatment(treatment) {
|
|
37
|
+
var lower = treatment.toLowerCase();
|
|
38
|
+
if (lower === 'on' || lower === 'true')
|
|
39
|
+
return true;
|
|
40
|
+
if (lower === 'off' || lower === 'false')
|
|
41
|
+
return false;
|
|
42
|
+
throw new web_sdk_1.ParseError("Invalid boolean value for ".concat(treatment));
|
|
43
|
+
}
|
|
44
|
+
exports.parseBooleanTreatment = parseBooleanTreatment;
|
package/lib/lib/types.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CONTROL_VALUE_ERROR_MESSAGE = exports.CONTROL_TREATMENT = void 0;
|
|
4
|
+
exports.CONTROL_TREATMENT = 'control';
|
|
5
|
+
exports.CONTROL_VALUE_ERROR_MESSAGE = "Received the 'control' value from Split.";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@splitsoftware/openfeature-web-split-provider",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Split OpenFeature Web Provider",
|
|
5
5
|
"files": [
|
|
6
6
|
"README.md",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@eslint/js": "^9.35.0",
|
|
39
39
|
"@openfeature/web-sdk": "^1.6.1",
|
|
40
|
-
"@splitsoftware/splitio-browserjs": "^1.
|
|
40
|
+
"@splitsoftware/splitio-browserjs": "^1.7.0",
|
|
41
41
|
"copyfiles": "^2.4.1",
|
|
42
42
|
"cross-env": "^7.0.3",
|
|
43
43
|
"eslint": "^9.35.0",
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { transformContext } from '../lib/context';
|
|
2
|
+
|
|
3
|
+
describe('context', () => {
|
|
4
|
+
describe('transformContext', () => {
|
|
5
|
+
const defaultTrafficType = 'user';
|
|
6
|
+
|
|
7
|
+
test('uses defaultTrafficType when context has no trafficType', () => {
|
|
8
|
+
const result = transformContext({ targetingKey: 'key-1' }, defaultTrafficType);
|
|
9
|
+
expect(result.trafficType).toBe('user');
|
|
10
|
+
expect(result.targetingKey).toBe('key-1');
|
|
11
|
+
expect(result.attributes).toEqual({});
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test('uses context trafficType when present and non-empty', () => {
|
|
15
|
+
const result = transformContext(
|
|
16
|
+
{ targetingKey: 'key-1', trafficType: 'account' },
|
|
17
|
+
defaultTrafficType
|
|
18
|
+
);
|
|
19
|
+
expect(result.trafficType).toBe('account');
|
|
20
|
+
expect(result.targetingKey).toBe('key-1');
|
|
21
|
+
expect(result.attributes).toEqual({});
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test('falls back to default when trafficType is empty string', () => {
|
|
25
|
+
const result = transformContext(
|
|
26
|
+
{ targetingKey: 'key-1', trafficType: '' },
|
|
27
|
+
defaultTrafficType
|
|
28
|
+
);
|
|
29
|
+
expect(result.trafficType).toBe('user');
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test('falls back to default when trafficType is whitespace', () => {
|
|
33
|
+
const result = transformContext(
|
|
34
|
+
{ targetingKey: 'key-1', trafficType: ' ' },
|
|
35
|
+
defaultTrafficType
|
|
36
|
+
);
|
|
37
|
+
expect(result.trafficType).toBe('user');
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test('passes remaining context as attributes', () => {
|
|
41
|
+
const result = transformContext(
|
|
42
|
+
{
|
|
43
|
+
targetingKey: 'key-1',
|
|
44
|
+
trafficType: 'user',
|
|
45
|
+
region: 'eu',
|
|
46
|
+
plan: 'pro',
|
|
47
|
+
},
|
|
48
|
+
defaultTrafficType
|
|
49
|
+
);
|
|
50
|
+
expect(result.attributes).toEqual({ region: 'eu', plan: 'pro' });
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test('deep-clones attributes (no reference)', () => {
|
|
54
|
+
const attrs = { nested: { value: 1 } };
|
|
55
|
+
const result = transformContext(
|
|
56
|
+
{ targetingKey: 'k', ...attrs },
|
|
57
|
+
defaultTrafficType
|
|
58
|
+
);
|
|
59
|
+
expect(result.attributes).toEqual({ nested: { value: 1 } });
|
|
60
|
+
expect(result.attributes).not.toBe(attrs);
|
|
61
|
+
expect(result.attributes.nested).not.toBe(attrs.nested);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
});
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
+
import { FlagNotFoundError, StandardResolutionReasons } from '@openfeature/web-sdk';
|
|
3
|
+
import { evaluateTreatment } from '../lib/evaluation';
|
|
4
|
+
import { CONTROL_TREATMENT } from '../lib/types';
|
|
5
|
+
|
|
6
|
+
describe('evaluation', () => {
|
|
7
|
+
describe('evaluateTreatment', () => {
|
|
8
|
+
let mockClient;
|
|
9
|
+
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
mockClient = {
|
|
12
|
+
getTreatmentWithConfig: jest.fn((flagKey, attributes) => ({
|
|
13
|
+
treatment: 'v1',
|
|
14
|
+
config: '{"x":1}',
|
|
15
|
+
})),
|
|
16
|
+
};
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
test('returns resolution details with value, variant, flagMetadata, reason', () => {
|
|
20
|
+
const consumer = { targetingKey: 'u1', trafficType: 'user', attributes: {} };
|
|
21
|
+
const result = evaluateTreatment(mockClient, 'my-flag', consumer);
|
|
22
|
+
|
|
23
|
+
expect(result.value).toBe('v1');
|
|
24
|
+
expect(result.variant).toBe('v1');
|
|
25
|
+
expect(result.flagMetadata).toEqual({ config: '{"x":1}' });
|
|
26
|
+
expect(result.reason).toBe(StandardResolutionReasons.TARGETING_MATCH);
|
|
27
|
+
expect(mockClient.getTreatmentWithConfig).toHaveBeenCalledWith('my-flag', {});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test('calls getTreatmentWithConfig with consumer attributes', () => {
|
|
31
|
+
const consumer = {
|
|
32
|
+
targetingKey: 'u1',
|
|
33
|
+
trafficType: 'account',
|
|
34
|
+
attributes: { region: 'eu', plan: 'pro' },
|
|
35
|
+
};
|
|
36
|
+
evaluateTreatment(mockClient, 'flag', consumer);
|
|
37
|
+
expect(mockClient.getTreatmentWithConfig).toHaveBeenCalledWith('flag', {
|
|
38
|
+
region: 'eu',
|
|
39
|
+
plan: 'pro',
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test('uses empty string for config when config is falsy', () => {
|
|
44
|
+
mockClient.getTreatmentWithConfig.mockReturnValue({ treatment: 'on', config: null });
|
|
45
|
+
const result = evaluateTreatment(mockClient, 'f', {
|
|
46
|
+
targetingKey: undefined,
|
|
47
|
+
trafficType: 'user',
|
|
48
|
+
attributes: {},
|
|
49
|
+
});
|
|
50
|
+
expect(result.flagMetadata.config).toBe('');
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test('throws FlagNotFoundError when flagKey is null', () => {
|
|
54
|
+
const consumer = { targetingKey: 'u1', trafficType: 'user', attributes: {} };
|
|
55
|
+
expect(() => evaluateTreatment(mockClient, null, consumer)).toThrow(FlagNotFoundError);
|
|
56
|
+
expect(() => evaluateTreatment(mockClient, null, consumer)).toThrow(
|
|
57
|
+
/flagKey must be a non-empty string/
|
|
58
|
+
);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test('throws FlagNotFoundError when flagKey is empty string', () => {
|
|
62
|
+
const consumer = { targetingKey: 'u1', trafficType: 'user', attributes: {} };
|
|
63
|
+
expect(() => evaluateTreatment(mockClient, '', consumer)).toThrow(FlagNotFoundError);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test('throws FlagNotFoundError when treatment is control', () => {
|
|
67
|
+
mockClient.getTreatmentWithConfig.mockReturnValue({
|
|
68
|
+
treatment: CONTROL_TREATMENT,
|
|
69
|
+
config: '',
|
|
70
|
+
});
|
|
71
|
+
const consumer = { targetingKey: 'u1', trafficType: 'user', attributes: {} };
|
|
72
|
+
expect(() => evaluateTreatment(mockClient, 'flag', consumer)).toThrow(FlagNotFoundError);
|
|
73
|
+
expect(() => evaluateTreatment(mockClient, 'flag', consumer)).toThrow(/control/);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
});
|