@splitsoftware/openfeature-js-split-provider 1.0.6 → 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 +25 -13
- package/LICENSE +1 -1
- package/README.md +74 -12
- package/es/index.js +1 -0
- package/es/lib/js-split-provider.js +152 -0
- package/lib/index.js +4 -0
- package/lib/lib/js-split-provider.js +156 -0
- package/package.json +37 -29
- package/src/__tests__/mocks/redis-commands.txt +11 -0
- package/src/__tests__/nodeSuites/client.spec.js +181 -139
- package/src/__tests__/nodeSuites/client_redis.spec.js +115 -0
- package/src/__tests__/nodeSuites/provider.spec.js +241 -109
- package/src/__tests__/testUtils/eventSourceMock.js +1 -2
- package/src/__tests__/testUtils/index.js +43 -0
- package/src/lib/js-split-provider.ts +126 -58
- package/types/index.d.ts +1 -0
- package/types/lib/js-split-provider.d.ts +26 -0
- package/src/__tests__/node.spec.js +0 -7
|
@@ -1,109 +1,241 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
1
|
+
/* eslint-disable jest/no-conditional-expect */
|
|
2
|
+
import { getLocalHostSplitClient, getSplitFactory } from '../testUtils';
|
|
3
|
+
import { OpenFeatureSplitProvider } from '../../lib/js-split-provider';
|
|
4
|
+
|
|
5
|
+
const cases = [
|
|
6
|
+
[
|
|
7
|
+
'provider tests mode: splitClient',
|
|
8
|
+
() => ({ splitClient: getLocalHostSplitClient()}),
|
|
9
|
+
|
|
10
|
+
],
|
|
11
|
+
[
|
|
12
|
+
'provider tests mode: splitFactory',
|
|
13
|
+
getSplitFactory
|
|
14
|
+
],
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
describe.each(cases)('%s', (label, getOptions) => {
|
|
18
|
+
|
|
19
|
+
let provider;
|
|
20
|
+
let options;
|
|
21
|
+
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
options = getOptions();
|
|
24
|
+
provider = new OpenFeatureSplitProvider(options);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
afterEach(async () => {
|
|
28
|
+
jest.clearAllMocks()
|
|
29
|
+
await provider.onClose();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test('evaluate Boolean null/empty test', async () => {
|
|
33
|
+
try {
|
|
34
|
+
await provider.resolveBooleanEvaluation('', false, { targetingKey: 'user1' });
|
|
35
|
+
} catch (e) {
|
|
36
|
+
expect(e.message).toBe('flagKey must be a non-empty string');
|
|
37
|
+
expect(e.code).toBe('FLAG_NOT_FOUND');
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test('evaluate Boolean control test', async () => {
|
|
42
|
+
try {
|
|
43
|
+
await provider.resolveBooleanEvaluation('non-existent-feature', false, { targetingKey: 'user1' });
|
|
44
|
+
} catch (e) {
|
|
45
|
+
expect(e.message).toBe('Received the "control" value from Split.');
|
|
46
|
+
expect(e.code).toBe('FLAG_NOT_FOUND');
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test('evaluate Boolean true test', async () => {
|
|
51
|
+
const details = await provider.resolveBooleanEvaluation('my_feature', false, { targetingKey: 'key' });
|
|
52
|
+
expect(details.value).toBe(true);
|
|
53
|
+
expect(details.variant).toBe('on');
|
|
54
|
+
expect(details.reason).toBe('TARGETING_MATCH');
|
|
55
|
+
expect(details.flagMetadata).toEqual({ config: '{"desc" : "this applies only to ON treatment"}' });
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test('evaluate Boolean on test', async () => {
|
|
59
|
+
const details = await provider.resolveBooleanEvaluation('my_feature', true, { targetingKey: 'key' });
|
|
60
|
+
expect(details.value).toBe(true);
|
|
61
|
+
expect(details.variant).toBe('on');
|
|
62
|
+
expect(details.reason).toBe('TARGETING_MATCH');
|
|
63
|
+
expect(details.flagMetadata).toEqual({ config: '{"desc" : "this applies only to ON treatment"}' });
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test('evaluate Boolean false test', async () => {
|
|
67
|
+
const details = await provider.resolveBooleanEvaluation('some_other_feature', true, { targetingKey: 'user1' });
|
|
68
|
+
expect(details.value).toBe(false);
|
|
69
|
+
expect(details.variant).toBe('off');
|
|
70
|
+
expect(details.reason).toBe('TARGETING_MATCH');
|
|
71
|
+
expect(details.flagMetadata).toEqual({ config: '' });
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test('evaluate Boolean off test', async () => {
|
|
75
|
+
const details = await provider.resolveBooleanEvaluation('some_other_feature', false, { targetingKey: 'user1' });
|
|
76
|
+
expect(details.value).toBe(false);
|
|
77
|
+
expect(details.variant).toBe('off');
|
|
78
|
+
expect(details.reason).toBe('TARGETING_MATCH');
|
|
79
|
+
expect(details.flagMetadata).toEqual({ config: '' });
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test('evaluate Boolean error test', async () => {
|
|
83
|
+
try {
|
|
84
|
+
await provider.resolveBooleanEvaluation('int_feature', false, { targetingKey: 'user1' });
|
|
85
|
+
} catch (e) {
|
|
86
|
+
expect(e.message).toBe('Invalid boolean value for 32');
|
|
87
|
+
expect(e.code).toBe('PARSE_ERROR');
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test('evaluate String null/empty test', async () => {
|
|
92
|
+
try {
|
|
93
|
+
await provider.resolveStringEvaluation('', 'default', { targetingKey: 'user1' });
|
|
94
|
+
} catch (e) {
|
|
95
|
+
expect(e.message).toBe('flagKey must be a non-empty string');
|
|
96
|
+
expect(e.code).toBe('FLAG_NOT_FOUND');
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test('evaluate String control test', async () => {
|
|
101
|
+
try {
|
|
102
|
+
await provider.resolveStringEvaluation('non-existent-feature', 'default', { targetingKey: 'user1' });
|
|
103
|
+
} catch (e) {
|
|
104
|
+
expect(e.message).toBe('Received the "control" value from Split.');
|
|
105
|
+
expect(e.code).toBe('FLAG_NOT_FOUND');
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test('evaluate String regular test', async () => {
|
|
110
|
+
try {
|
|
111
|
+
await provider.resolveStringEvaluation('string_feature', 'default', { targetingKey: 'user1' });
|
|
112
|
+
} catch (e) {
|
|
113
|
+
expect(e.message).toBe('Received the "control" value from Split.');
|
|
114
|
+
expect(e.code).toBe('FLAG_NOT_FOUND');
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test('evaluate String error test', async () => {
|
|
119
|
+
try {
|
|
120
|
+
await provider.resolveStringEvaluation('int_feature', 'default', { targetingKey: 'user1' });
|
|
121
|
+
} catch (e) {
|
|
122
|
+
expect(e.message).toBe('Invalid string value for 32');
|
|
123
|
+
expect(e.code).toBe('PARSE_ERROR');
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
test('evaluate Number null/empty test', async () => {
|
|
128
|
+
try {
|
|
129
|
+
await provider.resolveNumberEvaluation('', 0, { targetingKey: 'user1' });
|
|
130
|
+
} catch (e) {
|
|
131
|
+
expect(e.message).toBe('flagKey must be a non-empty string');
|
|
132
|
+
expect(e.code).toBe('FLAG_NOT_FOUND');
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test('evaluate Number control test', async () => {
|
|
137
|
+
try {
|
|
138
|
+
await provider.resolveNumberEvaluation('non-existent-feature', 0, { targetingKey: 'user1' });
|
|
139
|
+
} catch (e) {
|
|
140
|
+
expect(e.message).toBe('Received the "control" value from Split.');
|
|
141
|
+
expect(e.code).toBe('FLAG_NOT_FOUND');
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
test('evaluate Number regular test', async () => {
|
|
146
|
+
const details = await provider.resolveNumberEvaluation('int_feature', 0, { targetingKey: 'user1' });
|
|
147
|
+
expect(details.value).toBe(32);
|
|
148
|
+
expect(details.variant).toBe('32');
|
|
149
|
+
expect(details.reason).toBe('TARGETING_MATCH');
|
|
150
|
+
expect(details.flagMetadata).toEqual({ config: '{"desc" : "this applies only to number treatment"}' });
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test('evaluate Number error test', async () => {
|
|
154
|
+
try {
|
|
155
|
+
await provider.resolveNumberEvaluation('my_feature', 0, { targetingKey: 'user1' });
|
|
156
|
+
} catch (e) {
|
|
157
|
+
expect(e.message).toBe('Invalid numeric value off');
|
|
158
|
+
expect(e.code).toBe('PARSE_ERROR');
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
test('evaluate Structure null/empty test', async () => {
|
|
163
|
+
try {
|
|
164
|
+
await provider.resolveObjectEvaluation('', {}, { targetingKey: 'user1' });
|
|
165
|
+
} catch (e) {
|
|
166
|
+
expect(e.message).toBe('flagKey must be a non-empty string');
|
|
167
|
+
expect(e.code).toBe('FLAG_NOT_FOUND');
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
test('evaluate Structure control test', async () => {
|
|
172
|
+
try {
|
|
173
|
+
await provider.resolveObjectEvaluation('non-existent-feature', {}, { targetingKey: 'user1' });
|
|
174
|
+
} catch (e) {
|
|
175
|
+
expect(e.message).toBe('Received the "control" value from Split.');
|
|
176
|
+
expect(e.code).toBe('FLAG_NOT_FOUND');
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
test('evaluate Structure regular test', async () => {
|
|
181
|
+
const details = await provider.resolveObjectEvaluation('obj_feature', {}, { targetingKey: 'user1' });
|
|
182
|
+
expect(details.value).toEqual({ key: 'value' });
|
|
183
|
+
expect(details.variant).toBe('{"key": "value"}');
|
|
184
|
+
expect(details.reason).toBe('TARGETING_MATCH');
|
|
185
|
+
expect(details.flagMetadata).toEqual({ config: '{"desc" : "this applies only to obj treatment"}' });
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
test('evaluate Structure error test', async () => {
|
|
189
|
+
try {
|
|
190
|
+
await provider.resolveObjectEvaluation('int_feature', {}, { targetingKey: 'user1' });
|
|
191
|
+
} catch (e) {
|
|
192
|
+
expect(e.message).toBe('Error parsing 32 as JSON, ParseError: Flag value 32 had unexpected type number, expected "object"');
|
|
193
|
+
expect(e.code).toBe('PARSE_ERROR');
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
test('track: throws when missing eventName', async () => {
|
|
198
|
+
try {
|
|
199
|
+
await provider.track('', { targetingKey: 'u1', trafficType: 'user' }, {});
|
|
200
|
+
} catch (e) {
|
|
201
|
+
expect(e.message).toBe('Missing eventName, required to track');
|
|
202
|
+
expect(e.code).toBe('PARSE_ERROR');
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
test('track: throws when missing trafficType', async () => {
|
|
207
|
+
try {
|
|
208
|
+
await provider.track('evt', { targetingKey: 'u1' }, {});
|
|
209
|
+
} catch (e) {
|
|
210
|
+
expect(e.message).toBe('Missing trafficType variable, required to track');
|
|
211
|
+
expect(e.code).toBe('INVALID_CONTEXT');
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
test('track: throws when missing targetingKey', async () => {
|
|
216
|
+
try {
|
|
217
|
+
await provider.track('evt', { trafficType: 'user' }, {});
|
|
218
|
+
} catch (e) {
|
|
219
|
+
expect(e.message).toBe('Missing targetingKey, required to track');
|
|
220
|
+
expect(e.code).toBe('TARGETING_KEY_MISSING');
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
test('track: ok without details', async () => {
|
|
225
|
+
const trackSpy = jest.spyOn(options.splitClient ? options.splitClient : options.client(), 'track');
|
|
226
|
+
await provider.track('view', { targetingKey: 'u1', trafficType: 'user' }, null);
|
|
227
|
+
expect(trackSpy).toHaveBeenCalledTimes(1);
|
|
228
|
+
expect(trackSpy).toHaveBeenCalledWith('u1', 'user', 'view', undefined, {});
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
test('track: ok with details', async () => {
|
|
232
|
+
const trackSpy = jest.spyOn(options.splitClient ? options.splitClient : options.client(), 'track');
|
|
233
|
+
await provider.track(
|
|
234
|
+
'purchase',
|
|
235
|
+
{ targetingKey: 'u1', trafficType: 'user' },
|
|
236
|
+
{ value: 9.99, properties: { plan: 'pro', beta: true } }
|
|
237
|
+
);
|
|
238
|
+
expect(trackSpy).toHaveBeenCalledTimes(1);
|
|
239
|
+
expect(trackSpy).toHaveBeenCalledWith('u1', 'user', 'purchase', 9.99, { plan: 'pro', beta: true });
|
|
240
|
+
});
|
|
241
|
+
});
|
|
@@ -32,7 +32,6 @@ export default class EventSource {
|
|
|
32
32
|
this.url = url;
|
|
33
33
|
this.withCredentials = eventSourceInitDict.withCredentials;
|
|
34
34
|
this.readyState = 0;
|
|
35
|
-
// eslint-disable-next-line no-undef
|
|
36
35
|
this.__emitter = new EventEmitter();
|
|
37
36
|
this.__eventSourceInitDict = arguments[1];
|
|
38
37
|
sources[url] = this;
|
|
@@ -82,4 +81,4 @@ export default class EventSource {
|
|
|
82
81
|
|
|
83
82
|
EventSource.CONNECTING = 0;
|
|
84
83
|
EventSource.OPEN = 1;
|
|
85
|
-
EventSource.CLOSED = 2;
|
|
84
|
+
EventSource.CLOSED = 2;
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { SplitFactory } from '@splitsoftware/splitio';
|
|
2
|
+
|
|
1
3
|
const DEFAULT_ERROR_MARGIN = 50; // 0.05 secs
|
|
2
4
|
|
|
3
5
|
/**
|
|
@@ -67,3 +69,44 @@ export function url(settings, target) {
|
|
|
67
69
|
}
|
|
68
70
|
return `${settings.urls.sdk}${target}`;
|
|
69
71
|
}
|
|
72
|
+
|
|
73
|
+
const getRedisConfig = (redisPort) => ({
|
|
74
|
+
core: {
|
|
75
|
+
authorizationKey: 'SOME SDK KEY' // in consumer mode, SDK key is only used to track and log warning regarding duplicated SDK instances
|
|
76
|
+
},
|
|
77
|
+
mode: 'consumer',
|
|
78
|
+
storage: {
|
|
79
|
+
type: 'REDIS',
|
|
80
|
+
prefix: 'REDIS_NODE_UT',
|
|
81
|
+
options: {
|
|
82
|
+
url: `redis://localhost:${redisPort}/0`
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
sync: {
|
|
86
|
+
impressionsMode: 'DEBUG'
|
|
87
|
+
},
|
|
88
|
+
startup: {
|
|
89
|
+
readyTimeout: 36000 // 10hs
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
const config = {
|
|
94
|
+
core: {
|
|
95
|
+
authorizationKey: 'localhost'
|
|
96
|
+
},
|
|
97
|
+
features: './split.yaml',
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* get a Split client in localhost mode for testing purposes
|
|
101
|
+
*/
|
|
102
|
+
export function getLocalHostSplitClient() {
|
|
103
|
+
return SplitFactory(config).client();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function getRedisSplitClient(redisPort) {
|
|
107
|
+
return SplitFactory(getRedisConfig(redisPort)).client();
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function getSplitFactory() {
|
|
111
|
+
return SplitFactory(config);
|
|
112
|
+
}
|
|
@@ -1,17 +1,22 @@
|
|
|
1
1
|
import {
|
|
2
2
|
EvaluationContext,
|
|
3
|
-
Provider,
|
|
4
|
-
ResolutionDetails,
|
|
5
|
-
ParseError,
|
|
6
3
|
FlagNotFoundError,
|
|
4
|
+
InvalidContextError,
|
|
7
5
|
JsonValue,
|
|
8
|
-
|
|
6
|
+
OpenFeatureEventEmitter,
|
|
7
|
+
ParseError,
|
|
8
|
+
Provider,
|
|
9
|
+
ProviderEvents,
|
|
10
|
+
ResolutionDetails,
|
|
9
11
|
StandardResolutionReasons,
|
|
10
|
-
|
|
11
|
-
|
|
12
|
+
TargetingKeyMissingError,
|
|
13
|
+
TrackingEventDetails
|
|
14
|
+
} from '@openfeature/server-sdk';
|
|
15
|
+
import { SplitFactory } from '@splitsoftware/splitio';
|
|
16
|
+
import type SplitIO from '@splitsoftware/splitio/types/splitio';
|
|
12
17
|
|
|
13
|
-
|
|
14
|
-
splitClient: SplitIO.IClient;
|
|
18
|
+
type SplitProviderOptions = {
|
|
19
|
+
splitClient: SplitIO.IClient | SplitIO.IAsyncClient;
|
|
15
20
|
}
|
|
16
21
|
|
|
17
22
|
type Consumer = {
|
|
@@ -19,22 +24,52 @@ type Consumer = {
|
|
|
19
24
|
attributes: SplitIO.Attributes;
|
|
20
25
|
};
|
|
21
26
|
|
|
22
|
-
const CONTROL_VALUE_ERROR_MESSAGE =
|
|
27
|
+
const CONTROL_VALUE_ERROR_MESSAGE = 'Received the "control" value from Split.';
|
|
28
|
+
const CONTROL_TREATMENT = 'control';
|
|
23
29
|
|
|
24
30
|
export class OpenFeatureSplitProvider implements Provider {
|
|
25
31
|
metadata = {
|
|
26
|
-
name:
|
|
32
|
+
name: 'split',
|
|
27
33
|
};
|
|
28
34
|
private initialized: Promise<void>;
|
|
29
|
-
private client: SplitIO.IClient;
|
|
35
|
+
private client: SplitIO.IClient | SplitIO.IAsyncClient;
|
|
36
|
+
|
|
37
|
+
public readonly events = new OpenFeatureEventEmitter();
|
|
30
38
|
|
|
31
|
-
|
|
32
|
-
|
|
39
|
+
private getSplitClient(options: SplitProviderOptions | string | SplitIO.ISDK | SplitIO.IAsyncSDK) {
|
|
40
|
+
if (typeof(options) === 'string') {
|
|
41
|
+
const splitFactory = SplitFactory({core: { authorizationKey: options } });
|
|
42
|
+
return splitFactory.client();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
let splitClient;
|
|
46
|
+
try {
|
|
47
|
+
splitClient = (options as SplitIO.ISDK | SplitIO.IAsyncSDK).client();
|
|
48
|
+
} catch {
|
|
49
|
+
splitClient = (options as SplitProviderOptions).splitClient
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return splitClient;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
constructor(options: SplitProviderOptions | string | SplitIO.ISDK | SplitIO.IAsyncSDK) {
|
|
56
|
+
|
|
57
|
+
this.client = this.getSplitClient(options);
|
|
58
|
+
|
|
59
|
+
this.client.on(this.client.Event.SDK_UPDATE, () => {
|
|
60
|
+
this.events.emit(ProviderEvents.ConfigurationChanged)
|
|
61
|
+
});
|
|
33
62
|
this.initialized = new Promise((resolve) => {
|
|
34
|
-
|
|
63
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
64
|
+
if ((this.client as any).__getStatus().isReady) {
|
|
35
65
|
console.log(`${this.metadata.name} provider initialized`);
|
|
36
66
|
resolve();
|
|
37
|
-
}
|
|
67
|
+
} else {
|
|
68
|
+
this.client.on(this.client.Event.SDK_READY, () => {
|
|
69
|
+
console.log(`${this.metadata.name} provider initialized`);
|
|
70
|
+
resolve();
|
|
71
|
+
});
|
|
72
|
+
}
|
|
38
73
|
});
|
|
39
74
|
}
|
|
40
75
|
|
|
@@ -47,33 +82,17 @@ export class OpenFeatureSplitProvider implements Provider {
|
|
|
47
82
|
flagKey,
|
|
48
83
|
this.transformContext(context)
|
|
49
84
|
);
|
|
85
|
+
const treatment = details.value.toLowerCase();
|
|
50
86
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
case "on":
|
|
54
|
-
value = true;
|
|
55
|
-
break;
|
|
56
|
-
case "off":
|
|
57
|
-
value = false;
|
|
58
|
-
break;
|
|
59
|
-
case "true":
|
|
60
|
-
value = true;
|
|
61
|
-
break;
|
|
62
|
-
case "false":
|
|
63
|
-
value = false;
|
|
64
|
-
break;
|
|
65
|
-
case true:
|
|
66
|
-
value = true;
|
|
67
|
-
break;
|
|
68
|
-
case false:
|
|
69
|
-
value = false;
|
|
70
|
-
break;
|
|
71
|
-
case "control":
|
|
72
|
-
throw new FlagNotFoundError(CONTROL_VALUE_ERROR_MESSAGE);
|
|
73
|
-
default:
|
|
74
|
-
throw new ParseError(`Invalid boolean value for ${details.value}`);
|
|
87
|
+
if ( treatment === 'on' || treatment === 'true' ) {
|
|
88
|
+
return { ...details, value: true };
|
|
75
89
|
}
|
|
76
|
-
|
|
90
|
+
|
|
91
|
+
if ( treatment === 'off' || treatment === 'false' ) {
|
|
92
|
+
return { ...details, value: false };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
throw new ParseError(`Invalid boolean value for ${treatment}`);
|
|
77
96
|
}
|
|
78
97
|
|
|
79
98
|
async resolveStringEvaluation(
|
|
@@ -85,9 +104,6 @@ export class OpenFeatureSplitProvider implements Provider {
|
|
|
85
104
|
flagKey,
|
|
86
105
|
this.transformContext(context)
|
|
87
106
|
);
|
|
88
|
-
if (details.value === "control") {
|
|
89
|
-
throw new FlagNotFoundError(CONTROL_VALUE_ERROR_MESSAGE);
|
|
90
|
-
}
|
|
91
107
|
return details;
|
|
92
108
|
}
|
|
93
109
|
|
|
@@ -121,25 +137,77 @@ export class OpenFeatureSplitProvider implements Provider {
|
|
|
121
137
|
): Promise<ResolutionDetails<string>> {
|
|
122
138
|
if (!consumer.key) {
|
|
123
139
|
throw new TargetingKeyMissingError(
|
|
124
|
-
|
|
140
|
+
'The Split provider requires a targeting key.'
|
|
125
141
|
);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
flagKey,
|
|
131
|
-
consumer.attributes
|
|
142
|
+
}
|
|
143
|
+
if (flagKey == null || flagKey === '') {
|
|
144
|
+
throw new FlagNotFoundError(
|
|
145
|
+
'flagKey must be a non-empty string'
|
|
132
146
|
);
|
|
133
|
-
const details: ResolutionDetails<string> = {
|
|
134
|
-
value: value,
|
|
135
|
-
variant: value,
|
|
136
|
-
reason: StandardResolutionReasons.TARGETING_MATCH,
|
|
137
|
-
};
|
|
138
|
-
return details;
|
|
139
147
|
}
|
|
148
|
+
|
|
149
|
+
await this.initialized;
|
|
150
|
+
const { treatment: value, config }: SplitIO.TreatmentWithConfig = await this.client.getTreatmentWithConfig(
|
|
151
|
+
consumer.key,
|
|
152
|
+
flagKey,
|
|
153
|
+
consumer.attributes
|
|
154
|
+
);
|
|
155
|
+
if (value === CONTROL_TREATMENT) {
|
|
156
|
+
throw new FlagNotFoundError(CONTROL_VALUE_ERROR_MESSAGE);
|
|
157
|
+
}
|
|
158
|
+
const flagMetadata = { config: config ? config : '' };
|
|
159
|
+
const details: ResolutionDetails<string> = {
|
|
160
|
+
value: value,
|
|
161
|
+
variant: value,
|
|
162
|
+
flagMetadata: flagMetadata,
|
|
163
|
+
reason: StandardResolutionReasons.TARGETING_MATCH,
|
|
164
|
+
};
|
|
165
|
+
return details;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
async track(
|
|
169
|
+
trackingEventName: string,
|
|
170
|
+
context: EvaluationContext,
|
|
171
|
+
details: TrackingEventDetails
|
|
172
|
+
): Promise<void> {
|
|
173
|
+
|
|
174
|
+
// targetingKey is always required
|
|
175
|
+
const { targetingKey } = context;
|
|
176
|
+
if (targetingKey == null || targetingKey === '')
|
|
177
|
+
throw new TargetingKeyMissingError('Missing targetingKey, required to track');
|
|
178
|
+
|
|
179
|
+
// eventName is always required
|
|
180
|
+
if (trackingEventName == null || trackingEventName === '')
|
|
181
|
+
throw new ParseError('Missing eventName, required to track');
|
|
182
|
+
|
|
183
|
+
// trafficType is always required
|
|
184
|
+
const ttVal = context['trafficType'];
|
|
185
|
+
const trafficType =
|
|
186
|
+
ttVal != null && typeof ttVal === 'string' && ttVal.trim() !== ''
|
|
187
|
+
? ttVal
|
|
188
|
+
: null;
|
|
189
|
+
if (trafficType == null || trafficType === '')
|
|
190
|
+
throw new InvalidContextError('Missing trafficType variable, required to track');
|
|
191
|
+
|
|
192
|
+
let value;
|
|
193
|
+
let properties: SplitIO.Properties = {};
|
|
194
|
+
if (details != null) {
|
|
195
|
+
if (details.value != null) {
|
|
196
|
+
value = details.value;
|
|
197
|
+
}
|
|
198
|
+
if (details.properties != null) {
|
|
199
|
+
properties = details.properties as SplitIO.Properties;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
this.client.track(targetingKey, trafficType, trackingEventName, value, properties);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
async onClose?(): Promise<void> {
|
|
207
|
+
return this.client.destroy();
|
|
140
208
|
}
|
|
141
209
|
|
|
142
|
-
//Transform the context into an object useful for the Split API, an key string with arbitrary Split
|
|
210
|
+
//Transform the context into an object useful for the Split API, an key string with arbitrary Split 'Attributes'.
|
|
143
211
|
private transformContext(context: EvaluationContext): Consumer {
|
|
144
212
|
const { targetingKey, ...attributes } = context;
|
|
145
213
|
return {
|
|
@@ -169,7 +237,7 @@ export class OpenFeatureSplitProvider implements Provider {
|
|
|
169
237
|
// we may want to allow the parsing to be customized.
|
|
170
238
|
try {
|
|
171
239
|
const value = JSON.parse(stringValue);
|
|
172
|
-
if (typeof value !==
|
|
240
|
+
if (typeof value !== 'object') {
|
|
173
241
|
throw new ParseError(
|
|
174
242
|
`Flag value ${stringValue} had unexpected type ${typeof value}, expected "object"`
|
|
175
243
|
);
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './lib/js-split-provider';
|