-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjs-split-provider.ts
More file actions
250 lines (220 loc) · 7.21 KB
/
js-split-provider.ts
File metadata and controls
250 lines (220 loc) · 7.21 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import {
EvaluationContext,
FlagNotFoundError,
InvalidContextError,
JsonValue,
OpenFeatureEventEmitter,
ParseError,
Provider,
ProviderEvents,
ResolutionDetails,
StandardResolutionReasons,
TargetingKeyMissingError,
TrackingEventDetails
} from '@openfeature/server-sdk';
import { SplitFactory } from '@splitsoftware/splitio';
import type SplitIO from '@splitsoftware/splitio/types/splitio';
type SplitProviderOptions = {
splitClient: SplitIO.IClient | SplitIO.IAsyncClient;
}
type Consumer = {
key: string | undefined;
attributes: SplitIO.Attributes;
};
const CONTROL_VALUE_ERROR_MESSAGE = 'Received the "control" value from Split.';
const CONTROL_TREATMENT = 'control';
export class OpenFeatureSplitProvider implements Provider {
metadata = {
name: 'split',
};
private initialized: Promise<void>;
private client: SplitIO.IClient | SplitIO.IAsyncClient;
public readonly events = new OpenFeatureEventEmitter();
private getSplitClient(options: SplitProviderOptions | string | SplitIO.ISDK | SplitIO.IAsyncSDK) {
if (typeof(options) === 'string') {
const splitFactory = SplitFactory({core: { authorizationKey: options } });
return splitFactory.client();
}
let splitClient;
try {
splitClient = (options as SplitIO.ISDK | SplitIO.IAsyncSDK).client();
} catch {
splitClient = (options as SplitProviderOptions).splitClient
}
return splitClient;
}
constructor(options: SplitProviderOptions | string | SplitIO.ISDK | SplitIO.IAsyncSDK) {
this.client = this.getSplitClient(options);
this.client.on(this.client.Event.SDK_UPDATE, () => {
this.events.emit(ProviderEvents.ConfigurationChanged)
});
this.initialized = new Promise((resolve) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if ((this.client as any).__getStatus().isReady) {
console.log(`${this.metadata.name} provider initialized`);
resolve();
} else {
this.client.on(this.client.Event.SDK_READY, () => {
console.log(`${this.metadata.name} provider initialized`);
resolve();
});
}
});
}
async resolveBooleanEvaluation(
flagKey: string,
_: boolean,
context: EvaluationContext
): Promise<ResolutionDetails<boolean>> {
const details = await this.evaluateTreatment(
flagKey,
this.transformContext(context)
);
const treatment = details.value.toLowerCase();
if ( treatment === 'on' || treatment === 'true' ) {
return { ...details, value: true };
}
if ( treatment === 'off' || treatment === 'false' ) {
return { ...details, value: false };
}
throw new ParseError(`Invalid boolean value for ${treatment}`);
}
async resolveStringEvaluation(
flagKey: string,
_: string,
context: EvaluationContext
): Promise<ResolutionDetails<string>> {
const details = await this.evaluateTreatment(
flagKey,
this.transformContext(context)
);
return details;
}
async resolveNumberEvaluation(
flagKey: string,
_: number,
context: EvaluationContext
): Promise<ResolutionDetails<number>> {
const details = await this.evaluateTreatment(
flagKey,
this.transformContext(context)
);
return { ...details, value: this.parseValidNumber(details.value) };
}
async resolveObjectEvaluation<U extends JsonValue>(
flagKey: string,
_: U,
context: EvaluationContext
): Promise<ResolutionDetails<U>> {
const details = await this.evaluateTreatment(
flagKey,
this.transformContext(context)
);
return { ...details, value: this.parseValidJsonObject(details.value) };
}
private async evaluateTreatment(
flagKey: string,
consumer: Consumer
): Promise<ResolutionDetails<string>> {
if (!consumer.key) {
throw new TargetingKeyMissingError(
'The Split provider requires a targeting key.'
);
}
if (flagKey == null || flagKey === '') {
throw new FlagNotFoundError(
'flagKey must be a non-empty string'
);
}
await this.initialized;
const { treatment: value, config }: SplitIO.TreatmentWithConfig = await this.client.getTreatmentWithConfig(
consumer.key,
flagKey,
consumer.attributes
);
if (value === CONTROL_TREATMENT) {
throw new FlagNotFoundError(CONTROL_VALUE_ERROR_MESSAGE);
}
const flagMetadata = { config: config ? config : '' };
const details: ResolutionDetails<string> = {
value: value,
variant: value,
flagMetadata: flagMetadata,
reason: StandardResolutionReasons.TARGETING_MATCH,
};
return details;
}
async track(
trackingEventName: string,
context: EvaluationContext,
details: TrackingEventDetails
): Promise<void> {
// targetingKey is always required
const { targetingKey } = context;
if (targetingKey == null || targetingKey === '')
throw new TargetingKeyMissingError('Missing targetingKey, required to track');
// eventName is always required
if (trackingEventName == null || trackingEventName === '')
throw new ParseError('Missing eventName, required to track');
// trafficType is always required
const ttVal = context['trafficType'];
const trafficType =
ttVal != null && typeof ttVal === 'string' && ttVal.trim() !== ''
? ttVal
: null;
if (trafficType == null || trafficType === '')
throw new InvalidContextError('Missing trafficType variable, required to track');
let value;
let properties: SplitIO.Properties = {};
if (details != null) {
if (details.value != null) {
value = details.value;
}
if (details.properties != null) {
properties = details.properties as SplitIO.Properties;
}
}
this.client.track(targetingKey, trafficType, trackingEventName, value, properties);
}
async onClose?(): Promise<void> {
return this.client.destroy();
}
//Transform the context into an object useful for the Split API, an key string with arbitrary Split 'Attributes'.
private transformContext(context: EvaluationContext): Consumer {
const { targetingKey, ...attributes } = context;
return {
key: targetingKey,
// Stringify context objects include date.
attributes: JSON.parse(JSON.stringify(attributes)),
};
}
private parseValidNumber(stringValue: string | undefined) {
if (stringValue === undefined) {
throw new ParseError(`Invalid 'undefined' value.`);
}
const result = Number.parseFloat(stringValue);
if (Number.isNaN(result)) {
throw new ParseError(`Invalid numeric value ${stringValue}`);
}
return result;
}
private parseValidJsonObject<T extends JsonValue>(
stringValue: string | undefined
): T {
if (stringValue === undefined) {
throw new ParseError(`Invalid 'undefined' JSON value.`);
}
// we may want to allow the parsing to be customized.
try {
const value = JSON.parse(stringValue);
if (typeof value !== 'object') {
throw new ParseError(
`Flag value ${stringValue} had unexpected type ${typeof value}, expected "object"`
);
}
return value;
} catch (err) {
throw new ParseError(`Error parsing ${stringValue} as JSON, ${err}`);
}
}
}