@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,105 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import { ProviderEvents } from "@openfeature/web-sdk";
|
|
3
|
+
import type { EventDetails, EventMetadata, OpenFeatureEventEmitter } from "@openfeature/web-sdk";
|
|
4
|
+
import type SplitIO from "@splitsoftware/splitio-browserjs/types/splitio";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Split SDK emits SDK_READY with SdkReadyMetadata:
|
|
8
|
+
* - initialCacheLoad: boolean — true when fresh install / no cache, false when ready from cache
|
|
9
|
+
* - lastUpdateTimestamp?: number — ms since epoch when cache was last updated (undefined if initialCacheLoad is true)
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export type ReadyCallbacks = {
|
|
13
|
+
onSdkReady: () => void;
|
|
14
|
+
onSdkTimedOut: () => void;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/** Map Split SdkReadyMetadata to OpenFeature EventMetadata (string | boolean | number). */
|
|
18
|
+
function toReadyEventMetadata(sdkReadyMetadata: SplitIO.SdkReadyMetadata): EventMetadata {
|
|
19
|
+
const meta: EventMetadata = {
|
|
20
|
+
initialCacheLoad: sdkReadyMetadata.initialCacheLoad,
|
|
21
|
+
};
|
|
22
|
+
if (sdkReadyMetadata.lastUpdateTimestamp != null) {
|
|
23
|
+
meta.lastUpdateTimestamp = sdkReadyMetadata.lastUpdateTimestamp;
|
|
24
|
+
}
|
|
25
|
+
return meta;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Emit OpenFeature ProviderEvents.Ready with Split SDK ready metadata, then invoke the callback.
|
|
30
|
+
* When sdkReadyMetadata is provided (from SDK_READY event), use it; otherwise emit with empty metadata (e.g. when already ready).
|
|
31
|
+
*/
|
|
32
|
+
function emitReadyWithSplitMetadata(
|
|
33
|
+
eventsEmitter: OpenFeatureEventEmitter,
|
|
34
|
+
providerName: string,
|
|
35
|
+
onSdkReady: () => void,
|
|
36
|
+
sdkReadyMetadata?: SplitIO.SdkReadyMetadata
|
|
37
|
+
): void {
|
|
38
|
+
const readyDetails: EventDetails = {
|
|
39
|
+
providerName,
|
|
40
|
+
metadata: sdkReadyMetadata ? toReadyEventMetadata(sdkReadyMetadata) : {},
|
|
41
|
+
};
|
|
42
|
+
eventsEmitter.emit(ProviderEvents.Ready, readyDetails);
|
|
43
|
+
onSdkReady();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Attach Split SDK event listeners and resolve/reject the ready promise based on client status.
|
|
48
|
+
* When the Split SDK becomes ready, emits ProviderEvents.Ready with Split ready metadata before resolving.
|
|
49
|
+
*/
|
|
50
|
+
export function attachSplitReadyHandlers(
|
|
51
|
+
client: SplitIO.IBrowserClient,
|
|
52
|
+
eventsEmitter: OpenFeatureEventEmitter,
|
|
53
|
+
callbacks: ReadyCallbacks,
|
|
54
|
+
providerName: string
|
|
55
|
+
): void {
|
|
56
|
+
const { onSdkReady, onSdkTimedOut } = callbacks;
|
|
57
|
+
|
|
58
|
+
const onSdkReadyFromCache = () => {
|
|
59
|
+
eventsEmitter.emit(ProviderEvents.Stale, { message: 'Split ready from cache' });
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const clientStatus = (client as any).__getStatus();
|
|
63
|
+
if (clientStatus.isReady) {
|
|
64
|
+
emitReadyWithSplitMetadata(eventsEmitter, providerName, onSdkReady);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (clientStatus.isReadyFromCache) {
|
|
69
|
+
onSdkReadyFromCache();
|
|
70
|
+
} else {
|
|
71
|
+
client.on(client.Event.SDK_READY_FROM_CACHE, onSdkReadyFromCache);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (clientStatus.hasTimedout) {
|
|
75
|
+
onSdkTimedOut();
|
|
76
|
+
} else {
|
|
77
|
+
client.on(client.Event.SDK_READY_TIMED_OUT, onSdkTimedOut);
|
|
78
|
+
}
|
|
79
|
+
client.on(client.Event.SDK_READY, (sdkReadyMetadata?: SplitIO.SdkReadyMetadata) => {
|
|
80
|
+
emitReadyWithSplitMetadata(eventsEmitter, providerName, onSdkReady, sdkReadyMetadata);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Subscribe to Split SDK_UPDATE and emit OpenFeature ConfigurationChanged with details.
|
|
86
|
+
*/
|
|
87
|
+
export function attachSplitUpdateHandler(
|
|
88
|
+
client: SplitIO.IBrowserClient,
|
|
89
|
+
eventsEmitter: OpenFeatureEventEmitter,
|
|
90
|
+
providerName: string
|
|
91
|
+
): void {
|
|
92
|
+
client.on(client.Event.SDK_UPDATE, (updateMetadata: SplitIO.SdkUpdateMetadata) => {
|
|
93
|
+
let eventDetails: EventDetails = { providerName };
|
|
94
|
+
if (updateMetadata) {
|
|
95
|
+
eventDetails = {
|
|
96
|
+
...eventDetails,
|
|
97
|
+
metadata: {
|
|
98
|
+
type: updateMetadata.type,
|
|
99
|
+
},
|
|
100
|
+
flagsChanged: updateMetadata.names || []
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
eventsEmitter.emit(ProviderEvents.ConfigurationChanged, eventDetails);
|
|
104
|
+
});
|
|
105
|
+
}
|
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
1
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
3
2
|
import {
|
|
4
3
|
EvaluationContext,
|
|
5
4
|
Provider,
|
|
6
5
|
ResolutionDetails,
|
|
7
6
|
ParseError,
|
|
8
|
-
FlagNotFoundError,
|
|
9
7
|
JsonValue,
|
|
10
|
-
StandardResolutionReasons,
|
|
11
8
|
Logger,
|
|
12
9
|
ProviderEvents,
|
|
13
10
|
OpenFeatureEventEmitter,
|
|
@@ -15,84 +12,82 @@ import {
|
|
|
15
12
|
} from "@openfeature/web-sdk";
|
|
16
13
|
import type SplitIO from "@splitsoftware/splitio-browserjs/types/splitio";
|
|
17
14
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
const CONTROL_VALUE_ERROR_MESSAGE = "Received the 'control' value from Split.";
|
|
25
|
-
const CONTROL_TREATMENT = 'control';
|
|
15
|
+
import { transformContext } from "./context";
|
|
16
|
+
import { attachSplitReadyHandlers, attachSplitUpdateHandler } from "./events";
|
|
17
|
+
import { evaluateTreatment } from "./evaluation";
|
|
18
|
+
import { parseBooleanTreatment, parseValidJsonObject, parseValidNumber } from "./parsers";
|
|
26
19
|
|
|
27
20
|
export class OpenFeatureSplitProvider implements Provider {
|
|
28
|
-
metadata = {
|
|
29
|
-
|
|
30
|
-
};
|
|
21
|
+
metadata = { name: "split" };
|
|
22
|
+
|
|
31
23
|
private client: SplitIO.IBrowserClient;
|
|
32
24
|
private factory: SplitIO.IBrowserSDK;
|
|
33
25
|
private trafficType: string;
|
|
34
26
|
public events = new OpenFeatureEventEmitter();
|
|
35
27
|
|
|
28
|
+
constructor(splitFactory: SplitIO.IBrowserSDK) {
|
|
29
|
+
this.trafficType = 'user';
|
|
30
|
+
this.factory = splitFactory;
|
|
31
|
+
this.client = splitFactory.client();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async initialize(): Promise<void> {
|
|
35
|
+
await new Promise<void>((resolve, reject) => {
|
|
36
|
+
try {
|
|
37
|
+
attachSplitReadyHandlers(
|
|
38
|
+
this.client,
|
|
39
|
+
this.events,
|
|
40
|
+
{ onSdkReady: resolve as () => void, onSdkTimedOut: reject },
|
|
41
|
+
this.metadata.name
|
|
42
|
+
);
|
|
43
|
+
attachSplitUpdateHandler(this.client, this.events, this.metadata.name);
|
|
44
|
+
} catch {
|
|
45
|
+
reject();
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
36
50
|
onContextChange(oldContext: EvaluationContext, newContext: EvaluationContext): Promise<void> {
|
|
37
51
|
const { targetingKey: oldTargetingKey } = oldContext;
|
|
38
52
|
const { targetingKey: newTargetingKey, trafficType: newTrafficType } = newContext;
|
|
39
53
|
|
|
40
|
-
this.trafficType =
|
|
54
|
+
this.trafficType =
|
|
55
|
+
newTrafficType && newTrafficType !== this.trafficType
|
|
56
|
+
? (newTrafficType as string)
|
|
57
|
+
: this.trafficType;
|
|
58
|
+
|
|
41
59
|
if (newTargetingKey && newTargetingKey !== oldTargetingKey) {
|
|
42
60
|
this.client = this.factory.client(newTargetingKey);
|
|
43
|
-
|
|
44
61
|
return new Promise((resolve, reject) => {
|
|
45
62
|
const emitContextChange = () => {
|
|
46
63
|
this.events.emit(ProviderEvents.ConfigurationChanged);
|
|
47
64
|
resolve();
|
|
48
65
|
};
|
|
49
|
-
|
|
50
|
-
|
|
66
|
+
attachSplitReadyHandlers(
|
|
67
|
+
this.client,
|
|
68
|
+
this.events,
|
|
69
|
+
{ onSdkReady: emitContextChange, onSdkTimedOut: reject },
|
|
70
|
+
this.metadata.name
|
|
71
|
+
);
|
|
72
|
+
attachSplitUpdateHandler(this.client, this.events, this.metadata.name);
|
|
51
73
|
});
|
|
52
74
|
}
|
|
53
75
|
return Promise.resolve();
|
|
54
76
|
}
|
|
55
77
|
|
|
56
|
-
constructor(splitFactory: SplitIO.IBrowserSDK) {
|
|
57
|
-
// Asume 'user' as default traffic type'
|
|
58
|
-
this.trafficType = 'user';
|
|
59
|
-
this.factory = splitFactory;
|
|
60
|
-
this.client = splitFactory.client();
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
async initialize(): Promise<void> {
|
|
64
|
-
|
|
65
|
-
await new Promise ((resolve, reject) => {
|
|
66
|
-
try {
|
|
67
|
-
this.eventsHandler(resolve, reject);
|
|
68
|
-
} catch {
|
|
69
|
-
reject();
|
|
70
|
-
}
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
|
|
74
78
|
resolveBooleanEvaluation(
|
|
75
79
|
flagKey: string,
|
|
76
80
|
_: boolean,
|
|
77
81
|
context: EvaluationContext,
|
|
78
82
|
_logger: Logger
|
|
79
83
|
): ResolutionDetails<boolean> {
|
|
80
|
-
const details =
|
|
84
|
+
const details = evaluateTreatment(
|
|
85
|
+
this.client,
|
|
81
86
|
flagKey,
|
|
82
|
-
|
|
87
|
+
transformContext(context, this.trafficType)
|
|
83
88
|
);
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
if ( treatment === 'on' || treatment === 'true' ) {
|
|
88
|
-
return { ...details, value: true };
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
if ( treatment === 'off' || treatment === 'false' ) {
|
|
92
|
-
return { ...details, value: false };
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
throw new ParseError(`Invalid boolean value for ${treatment}`);
|
|
89
|
+
const value = parseBooleanTreatment(details.value);
|
|
90
|
+
return { ...details, value };
|
|
96
91
|
}
|
|
97
92
|
|
|
98
93
|
resolveStringEvaluation(
|
|
@@ -101,11 +96,11 @@ export class OpenFeatureSplitProvider implements Provider {
|
|
|
101
96
|
context: EvaluationContext,
|
|
102
97
|
_logger: Logger
|
|
103
98
|
): ResolutionDetails<string> {
|
|
104
|
-
|
|
99
|
+
return evaluateTreatment(
|
|
100
|
+
this.client,
|
|
105
101
|
flagKey,
|
|
106
|
-
|
|
102
|
+
transformContext(context, this.trafficType)
|
|
107
103
|
);
|
|
108
|
-
return details;
|
|
109
104
|
}
|
|
110
105
|
|
|
111
106
|
resolveNumberEvaluation(
|
|
@@ -114,11 +109,12 @@ export class OpenFeatureSplitProvider implements Provider {
|
|
|
114
109
|
context: EvaluationContext,
|
|
115
110
|
_logger: Logger
|
|
116
111
|
): ResolutionDetails<number> {
|
|
117
|
-
const details =
|
|
112
|
+
const details = evaluateTreatment(
|
|
113
|
+
this.client,
|
|
118
114
|
flagKey,
|
|
119
|
-
|
|
115
|
+
transformContext(context, this.trafficType)
|
|
120
116
|
);
|
|
121
|
-
return { ...details, value:
|
|
117
|
+
return { ...details, value: parseValidNumber(details.value) };
|
|
122
118
|
}
|
|
123
119
|
|
|
124
120
|
resolveObjectEvaluation<U extends JsonValue>(
|
|
@@ -127,39 +123,12 @@ export class OpenFeatureSplitProvider implements Provider {
|
|
|
127
123
|
context: EvaluationContext,
|
|
128
124
|
_logger: Logger
|
|
129
125
|
): ResolutionDetails<U> {
|
|
130
|
-
const details =
|
|
131
|
-
|
|
132
|
-
this.transformContext(context)
|
|
133
|
-
);
|
|
134
|
-
return { ...details, value: this.parseValidJsonObject(details.value) };
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
private evaluateTreatment(
|
|
138
|
-
flagKey: string,
|
|
139
|
-
consumer: Consumer
|
|
140
|
-
): ResolutionDetails<string> {
|
|
141
|
-
if (flagKey == null || flagKey === '') {
|
|
142
|
-
throw new FlagNotFoundError(
|
|
143
|
-
'flagKey must be a non-empty string'
|
|
144
|
-
);
|
|
145
|
-
}
|
|
146
|
-
const treatment: SplitIO.TreatmentWithConfig = this.client.getTreatmentWithConfig(
|
|
126
|
+
const details = evaluateTreatment(
|
|
127
|
+
this.client,
|
|
147
128
|
flagKey,
|
|
148
|
-
|
|
129
|
+
transformContext(context, this.trafficType)
|
|
149
130
|
);
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
if (value === CONTROL_TREATMENT) {
|
|
153
|
-
throw new FlagNotFoundError(CONTROL_VALUE_ERROR_MESSAGE);
|
|
154
|
-
}
|
|
155
|
-
const flagMetadata = { config: config ? config : '' };
|
|
156
|
-
const details: ResolutionDetails<string> = {
|
|
157
|
-
value: value,
|
|
158
|
-
variant: value,
|
|
159
|
-
flagMetadata: flagMetadata,
|
|
160
|
-
reason: StandardResolutionReasons.TARGETING_MATCH,
|
|
161
|
-
};
|
|
162
|
-
return details;
|
|
131
|
+
return { ...details, value: parseValidJsonObject<U>(details.value) };
|
|
163
132
|
}
|
|
164
133
|
|
|
165
134
|
track(
|
|
@@ -167,105 +136,22 @@ export class OpenFeatureSplitProvider implements Provider {
|
|
|
167
136
|
context: EvaluationContext,
|
|
168
137
|
details: TrackingEventDetails
|
|
169
138
|
): void {
|
|
170
|
-
|
|
171
|
-
// eventName is always required
|
|
172
|
-
if (trackingEventName == null || trackingEventName === '')
|
|
139
|
+
if (trackingEventName == null || trackingEventName === '') {
|
|
173
140
|
throw new ParseError('Missing eventName, required to track');
|
|
174
|
-
|
|
175
|
-
const {trafficType} =
|
|
176
|
-
let value;
|
|
141
|
+
}
|
|
142
|
+
const { trafficType } = transformContext(context, this.trafficType);
|
|
143
|
+
let value: unknown;
|
|
177
144
|
let properties: SplitIO.Properties = {};
|
|
178
145
|
if (details != null) {
|
|
179
|
-
if (details.value != null)
|
|
180
|
-
value = details.value;
|
|
181
|
-
}
|
|
146
|
+
if (details.value != null) value = details.value;
|
|
182
147
|
if (details.properties != null) {
|
|
183
148
|
properties = details.properties as SplitIO.Properties;
|
|
184
149
|
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
this.client.track(trafficType, trackingEventName, value, properties);
|
|
150
|
+
}
|
|
151
|
+
this.client.track(trafficType, trackingEventName, value as number | undefined, properties);
|
|
188
152
|
}
|
|
189
153
|
|
|
190
154
|
async onClose?(): Promise<void> {
|
|
191
155
|
return this.factory.destroy();
|
|
192
156
|
}
|
|
193
|
-
|
|
194
|
-
//Transform the context into an object useful for the Split API, an key string with arbitrary Split "Attributes".
|
|
195
|
-
private transformContext(context: EvaluationContext): Consumer {
|
|
196
|
-
const { targetingKey, trafficType: ttVal, ...attributes } = context;
|
|
197
|
-
const trafficType =
|
|
198
|
-
ttVal != null && typeof ttVal === 'string' && ttVal.trim() !== ''
|
|
199
|
-
? ttVal
|
|
200
|
-
: this.trafficType;
|
|
201
|
-
return {
|
|
202
|
-
targetingKey,
|
|
203
|
-
trafficType,
|
|
204
|
-
// Stringify context objects include date.
|
|
205
|
-
attributes: JSON.parse(JSON.stringify(attributes)),
|
|
206
|
-
};
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
private parseValidNumber(stringValue: string | undefined) {
|
|
210
|
-
if (stringValue === undefined) {
|
|
211
|
-
throw new ParseError(`Invalid 'undefined' value.`);
|
|
212
|
-
}
|
|
213
|
-
const result = Number.parseFloat(stringValue);
|
|
214
|
-
if (Number.isNaN(result)) {
|
|
215
|
-
throw new ParseError(`Invalid numeric value ${stringValue}`);
|
|
216
|
-
}
|
|
217
|
-
return result;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
private parseValidJsonObject<T extends JsonValue>(
|
|
221
|
-
stringValue: string | undefined
|
|
222
|
-
): T {
|
|
223
|
-
if (stringValue === undefined) {
|
|
224
|
-
throw new ParseError(`Invalid 'undefined' JSON value.`);
|
|
225
|
-
}
|
|
226
|
-
// we may want to allow the parsing to be customized.
|
|
227
|
-
try {
|
|
228
|
-
const value = JSON.parse(stringValue);
|
|
229
|
-
if (typeof value !== "object") {
|
|
230
|
-
throw new ParseError(
|
|
231
|
-
`Flag value ${stringValue} had unexpected type ${typeof value}, expected "object"`
|
|
232
|
-
);
|
|
233
|
-
}
|
|
234
|
-
return value;
|
|
235
|
-
} catch (err) {
|
|
236
|
-
throw new ParseError(`Error parsing ${stringValue} as JSON, ${err}`);
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
private async eventsHandler(onSdkReady: (params?: any) => void, onSdkTimedOut: () => void): Promise <void> {
|
|
241
|
-
|
|
242
|
-
const onSdkReadyFromCache = () => {
|
|
243
|
-
this.events.emit(ProviderEvents.Stale, {
|
|
244
|
-
message: `Split ready from cache`,
|
|
245
|
-
});
|
|
246
|
-
};
|
|
247
|
-
|
|
248
|
-
const clientStatus = (this.client as any).__getStatus();
|
|
249
|
-
if (clientStatus.isReady) {
|
|
250
|
-
onSdkReady();
|
|
251
|
-
} else {
|
|
252
|
-
|
|
253
|
-
if (clientStatus.isReadyFromCache) {
|
|
254
|
-
onSdkReadyFromCache();
|
|
255
|
-
} else {
|
|
256
|
-
this.client.on(this.client.Event.SDK_READY_FROM_CACHE, onSdkReadyFromCache);
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
if (clientStatus.hasTimedout) {
|
|
260
|
-
onSdkTimedOut();
|
|
261
|
-
} else {
|
|
262
|
-
this.client.on(this.client.Event.SDK_READY_TIMED_OUT, onSdkTimedOut);
|
|
263
|
-
}
|
|
264
|
-
this.client.on(this.client.Event.SDK_READY, onSdkReady);
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
this.client.on(this.client.Event.SDK_UPDATE, () => {
|
|
268
|
-
this.events.emit(ProviderEvents.ConfigurationChanged);
|
|
269
|
-
});
|
|
270
|
-
}
|
|
271
157
|
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { ParseError } from "@openfeature/web-sdk";
|
|
2
|
+
import type { JsonValue } from "@openfeature/web-sdk";
|
|
3
|
+
|
|
4
|
+
export function parseValidNumber(stringValue: string | undefined): number {
|
|
5
|
+
if (stringValue === undefined) {
|
|
6
|
+
throw new ParseError(`Invalid 'undefined' value.`);
|
|
7
|
+
}
|
|
8
|
+
const result = Number.parseFloat(stringValue);
|
|
9
|
+
if (Number.isNaN(result)) {
|
|
10
|
+
throw new ParseError(`Invalid numeric value ${stringValue}`);
|
|
11
|
+
}
|
|
12
|
+
return result;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function parseValidJsonObject<T extends JsonValue>(
|
|
16
|
+
stringValue: string | undefined
|
|
17
|
+
): T {
|
|
18
|
+
if (stringValue === undefined) {
|
|
19
|
+
throw new ParseError(`Invalid 'undefined' JSON value.`);
|
|
20
|
+
}
|
|
21
|
+
try {
|
|
22
|
+
const value = JSON.parse(stringValue);
|
|
23
|
+
if (typeof value !== "object") {
|
|
24
|
+
throw new ParseError(
|
|
25
|
+
`Flag value ${stringValue} had unexpected type ${typeof value}, expected "object"`
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
return value;
|
|
29
|
+
} catch (err) {
|
|
30
|
+
throw new ParseError(`Error parsing ${stringValue} as JSON, ${err}`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Map a string treatment to a boolean for boolean flag resolution.
|
|
36
|
+
* @throws ParseError if treatment is not a valid boolean representation
|
|
37
|
+
*/
|
|
38
|
+
export function parseBooleanTreatment(treatment: string): boolean {
|
|
39
|
+
const lower = treatment.toLowerCase();
|
|
40
|
+
if (lower === 'on' || lower === 'true') return true;
|
|
41
|
+
if (lower === 'off' || lower === 'false') return false;
|
|
42
|
+
throw new ParseError(`Invalid boolean value for ${treatment}`);
|
|
43
|
+
}
|
package/src/lib/types.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type SplitIO from "@splitsoftware/splitio-browserjs/types/splitio";
|
|
2
|
+
|
|
3
|
+
export type Consumer = {
|
|
4
|
+
targetingKey: string | undefined;
|
|
5
|
+
trafficType: string;
|
|
6
|
+
attributes: SplitIO.Attributes;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export const CONTROL_TREATMENT = 'control';
|
|
10
|
+
export const CONTROL_VALUE_ERROR_MESSAGE = "Received the 'control' value from Split.";
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { EvaluationContext } from "@openfeature/web-sdk";
|
|
2
|
+
import type { Consumer } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* Transform OpenFeature evaluation context into a consumer object for the Split API.
|
|
5
|
+
*/
|
|
6
|
+
export declare function transformContext(context: EvaluationContext, defaultTrafficType: string): Consumer;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ResolutionDetails } from "@openfeature/web-sdk";
|
|
2
|
+
import type SplitIO from "@splitsoftware/splitio-browserjs/types/splitio";
|
|
3
|
+
import type { Consumer } from "./types";
|
|
4
|
+
/**
|
|
5
|
+
* Evaluate a flag with the Split client and return string resolution details.
|
|
6
|
+
*/
|
|
7
|
+
export declare function evaluateTreatment(client: SplitIO.IBrowserClient, flagKey: string, consumer: Consumer): ResolutionDetails<string>;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { OpenFeatureEventEmitter } from "@openfeature/web-sdk";
|
|
2
|
+
import type SplitIO from "@splitsoftware/splitio-browserjs/types/splitio";
|
|
3
|
+
/**
|
|
4
|
+
* Split SDK emits SDK_READY with SdkReadyMetadata:
|
|
5
|
+
* - initialCacheLoad: boolean — true when fresh install / no cache, false when ready from cache
|
|
6
|
+
* - lastUpdateTimestamp?: number — ms since epoch when cache was last updated (undefined if initialCacheLoad is true)
|
|
7
|
+
*/
|
|
8
|
+
export type ReadyCallbacks = {
|
|
9
|
+
onSdkReady: () => void;
|
|
10
|
+
onSdkTimedOut: () => void;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Attach Split SDK event listeners and resolve/reject the ready promise based on client status.
|
|
14
|
+
* When the Split SDK becomes ready, emits ProviderEvents.Ready with Split ready metadata before resolving.
|
|
15
|
+
*/
|
|
16
|
+
export declare function attachSplitReadyHandlers(client: SplitIO.IBrowserClient, eventsEmitter: OpenFeatureEventEmitter, callbacks: ReadyCallbacks, providerName: string): void;
|
|
17
|
+
/**
|
|
18
|
+
* Subscribe to Split SDK_UPDATE and emit OpenFeature ConfigurationChanged with details.
|
|
19
|
+
*/
|
|
20
|
+
export declare function attachSplitUpdateHandler(client: SplitIO.IBrowserClient, eventsEmitter: OpenFeatureEventEmitter, providerName: string): void;
|
|
@@ -8,18 +8,13 @@ export declare class OpenFeatureSplitProvider implements Provider {
|
|
|
8
8
|
private factory;
|
|
9
9
|
private trafficType;
|
|
10
10
|
events: OpenFeatureEventEmitter;
|
|
11
|
-
onContextChange(oldContext: EvaluationContext, newContext: EvaluationContext): Promise<void>;
|
|
12
11
|
constructor(splitFactory: SplitIO.IBrowserSDK);
|
|
13
12
|
initialize(): Promise<void>;
|
|
13
|
+
onContextChange(oldContext: EvaluationContext, newContext: EvaluationContext): Promise<void>;
|
|
14
14
|
resolveBooleanEvaluation(flagKey: string, _: boolean, context: EvaluationContext, _logger: Logger): ResolutionDetails<boolean>;
|
|
15
15
|
resolveStringEvaluation(flagKey: string, _: string, context: EvaluationContext, _logger: Logger): ResolutionDetails<string>;
|
|
16
16
|
resolveNumberEvaluation(flagKey: string, _: number, context: EvaluationContext, _logger: Logger): ResolutionDetails<number>;
|
|
17
17
|
resolveObjectEvaluation<U extends JsonValue>(flagKey: string, _: U, context: EvaluationContext, _logger: Logger): ResolutionDetails<U>;
|
|
18
|
-
private evaluateTreatment;
|
|
19
18
|
track(trackingEventName: string, context: EvaluationContext, details: TrackingEventDetails): void;
|
|
20
19
|
onClose?(): Promise<void>;
|
|
21
|
-
private transformContext;
|
|
22
|
-
private parseValidNumber;
|
|
23
|
-
private parseValidJsonObject;
|
|
24
|
-
private eventsHandler;
|
|
25
20
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { JsonValue } from "@openfeature/web-sdk";
|
|
2
|
+
export declare function parseValidNumber(stringValue: string | undefined): number;
|
|
3
|
+
export declare function parseValidJsonObject<T extends JsonValue>(stringValue: string | undefined): T;
|
|
4
|
+
/**
|
|
5
|
+
* Map a string treatment to a boolean for boolean flag resolution.
|
|
6
|
+
* @throws ParseError if treatment is not a valid boolean representation
|
|
7
|
+
*/
|
|
8
|
+
export declare function parseBooleanTreatment(treatment: string): boolean;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type SplitIO from "@splitsoftware/splitio-browserjs/types/splitio";
|
|
2
|
+
export type Consumer = {
|
|
3
|
+
targetingKey: string | undefined;
|
|
4
|
+
trafficType: string;
|
|
5
|
+
attributes: SplitIO.Attributes;
|
|
6
|
+
};
|
|
7
|
+
export declare const CONTROL_TREATMENT = "control";
|
|
8
|
+
export declare const CONTROL_VALUE_ERROR_MESSAGE = "Received the 'control' value from Split.";
|