-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathClientEntity.ts
More file actions
199 lines (174 loc) · 5.55 KB
/
ClientEntity.ts
File metadata and controls
199 lines (174 loc) · 5.55 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
'use client';
import { useEffect } from 'react';
import {
CommandParams,
CommandType,
makeLogger,
SDKConfigParams,
ClientSideTestHook as TestHook,
ValueType,
} from '@launchdarkly/js-contract-test-utils/client';
import { LDOptions, LDReactClient, useLDClient } from '@launchdarkly/react-sdk';
export const badCommandError = new Error('unsupported command');
export const malformedCommand = new Error('command was malformed');
export function makeSdkConfig(options: SDKConfigParams, tag: string): LDOptions {
if (!options.clientSide) {
throw new Error('configuration did not include clientSide options');
}
const isSet = (x?: unknown) => x !== null && x !== undefined;
const maybeTime = (seconds?: number) => (isSet(seconds) ? seconds! / 1000 : undefined);
const cf: LDOptions = {
withReasons: options.clientSide.evaluationReasons,
logger: makeLogger(`${tag}.sdk`),
useReport: options.clientSide.useReport,
};
if (options.serviceEndpoints) {
cf.streamUri = options.serviceEndpoints.streaming;
cf.baseUri = options.serviceEndpoints.polling;
cf.eventsUri = options.serviceEndpoints.events;
}
if (options.polling) {
if (options.polling.baseUri) {
cf.baseUri = options.polling.baseUri;
}
}
if (options.streaming) {
if (options.streaming.baseUri) {
cf.streamUri = options.streaming.baseUri;
}
cf.streaming = true;
cf.streamInitialReconnectDelay = maybeTime(options.streaming.initialRetryDelayMs);
}
if (options.events) {
if (options.events.baseUri) {
cf.eventsUri = options.events.baseUri;
}
cf.allAttributesPrivate = options.events.allAttributesPrivate;
cf.capacity = options.events.capacity;
cf.diagnosticOptOut = !options.events.enableDiagnostics;
cf.flushInterval = maybeTime(options.events.flushIntervalMs);
cf.privateAttributes = options.events.globalPrivateAttributes;
} else {
cf.sendEvents = false;
}
if (options.tags) {
cf.applicationInfo = {
id: options.tags.applicationId,
version: options.tags.applicationVersion,
};
}
if (options.hooks) {
cf.hooks = TestHook.forClient(options.hooks.hooks);
}
cf.fetchGoals = false;
return cf;
}
export async function doCommand(client: LDReactClient, params: CommandParams): Promise<unknown> {
const logger = makeLogger('doCommand');
logger.info(`Received command: ${params.command}`);
switch (params.command) {
case CommandType.EvaluateFlag: {
const evaluationParams = params.evaluate;
if (!evaluationParams) {
throw malformedCommand;
}
if (evaluationParams.detail) {
switch (evaluationParams.valueType) {
case ValueType.Bool:
return client.boolVariationDetail(
evaluationParams.flagKey,
evaluationParams.defaultValue as boolean,
);
case ValueType.Int: // Intentional fallthrough.
case ValueType.Double:
return client.numberVariationDetail(
evaluationParams.flagKey,
evaluationParams.defaultValue as number,
);
case ValueType.String:
return client.stringVariationDetail(
evaluationParams.flagKey,
evaluationParams.defaultValue as string,
);
default:
return client.variationDetail(evaluationParams.flagKey, evaluationParams.defaultValue);
}
}
switch (evaluationParams.valueType) {
case ValueType.Bool:
return {
value: client.boolVariation(
evaluationParams.flagKey,
evaluationParams.defaultValue as boolean,
),
};
case ValueType.Int: // Intentional fallthrough.
case ValueType.Double:
return {
value: client.numberVariation(
evaluationParams.flagKey,
evaluationParams.defaultValue as number,
),
};
case ValueType.String:
return {
value: client.stringVariation(
evaluationParams.flagKey,
evaluationParams.defaultValue as string,
),
};
default:
return {
value: client.variation(evaluationParams.flagKey, evaluationParams.defaultValue),
};
}
}
case CommandType.EvaluateAllFlags:
return { state: client.allFlags() };
case CommandType.IdentifyEvent: {
const identifyParams = params.identifyEvent;
if (!identifyParams) {
throw malformedCommand;
}
await client.identify(identifyParams.user || identifyParams.context);
return undefined;
}
case CommandType.CustomEvent: {
const customEventParams = params.customEvent;
if (!customEventParams) {
throw malformedCommand;
}
client.track(
customEventParams.eventKey,
customEventParams.data,
customEventParams.metricValue,
);
return undefined;
}
case CommandType.FlushEvents:
client.flush();
return undefined;
default:
throw badCommandError;
}
}
export type CommandHandler = (params: CommandParams) => Promise<unknown>;
export function ClientInstance({
clientId,
handlers,
onReady,
}: {
clientId: string;
handlers: Map<string, CommandHandler>;
onReady: (id: string) => void;
}) {
const client = useLDClient();
useEffect(() => {
handlers.set(clientId, (params) => doCommand(client, params));
onReady(clientId);
return () => {
handlers.delete(clientId);
};
}, [client, clientId, handlers, onReady]);
return null;
}