Skip to content

Commit 4da2df3

Browse files
committed
feat: add userContext helper
1 parent 0ad453b commit 4da2df3

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

packages/adapter-optimizely/src/index.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
Client,
44
OpaqueConfigManager,
55
OptimizelyDecision,
6+
OptimizelyUserContext,
67
UserAttributes,
78
} from '@optimizely/optimizely-sdk';
89

@@ -33,6 +34,10 @@ type AdapterResponse = {
3334
attributes?: UserAttributes;
3435
},
3536
) => Adapter<T, UserId>;
37+
userContext: (
38+
entities: UserId,
39+
attributes?: UserAttributes,
40+
) => Promise<OptimizelyUserContext>;
3641
initialize: () => Promise<Client>;
3742
};
3843

@@ -114,8 +119,6 @@ export function createOptimizelyAdapter({
114119
optimizelyInstance = createInstance({
115120
clientEngine: 'javascript-sdk/flags-sdk',
116121
projectConfigManager,
117-
// TODO: Check if batch event processor works here or if we should just force a single `waitUntil` flush of all events
118-
// TODO: Check if running this in a `waitUntil()` doesn't break things
119122
// @ts-expect-error - dispatchEvent runs in `waitUntil` so it's not going to return a response
120123
eventProcessor: createForwardingEventProcessor({ dispatchEvent }),
121124
requestHandler,
@@ -171,28 +174,41 @@ export function createOptimizelyAdapter({
171174
};
172175
}
173176

177+
async function userContext(
178+
entities: UserId,
179+
attributes?: UserAttributes,
180+
): Promise<OptimizelyUserContext> {
181+
await initialize();
182+
if (!optimizelyInstance) {
183+
throw new Error(
184+
'Optimizely Adapter: Optimizely instance not initialized',
185+
);
186+
}
187+
return optimizelyInstance.createUserContext(entities, attributes);
188+
}
189+
174190
return {
175191
decide,
192+
userContext,
176193
initialize,
177194
};
178195
}
179196

180197
function getOrCreateDefaultOptimizelyAdapter(): AdapterResponse {
181198
const sdkKey = process.env.OPTIMIZELY_SDK_KEY;
182-
const edgeConfig = process.env.EDGE_CONFIG_CONNECTION_STRING;
199+
const edgeConfigConnectionString = process.env.EDGE_CONFIG_CONNECTION_STRING;
183200
const edgeConfigItemKey = process.env.OPTIMIZELY_DATAFILE_ITEM_KEY;
184201

185202
if (!defaultOptimizelyAdapter) {
186-
if (edgeConfig && edgeConfigItemKey) {
203+
if (edgeConfigConnectionString && edgeConfigItemKey) {
187204
defaultOptimizelyAdapter = createOptimizelyAdapter({
188205
sdkKey,
189206
edgeConfig: {
190-
connectionString: edgeConfig,
207+
connectionString: edgeConfigConnectionString,
191208
itemKey: edgeConfigItemKey,
192209
},
193210
});
194211
} else {
195-
// Fallback to polling optimizely SDK
196212
defaultOptimizelyAdapter = createOptimizelyAdapter({
197213
sdkKey,
198214
});
@@ -223,5 +239,7 @@ function getOrCreateDefaultOptimizelyAdapter(): AdapterResponse {
223239
*/
224240
export const optimizelyAdapter: AdapterResponse = {
225241
decide: (...args) => getOrCreateDefaultOptimizelyAdapter().decide(...args),
242+
userContext: (...args) =>
243+
getOrCreateDefaultOptimizelyAdapter().userContext(...args),
226244
initialize: () => getOrCreateDefaultOptimizelyAdapter().initialize(),
227245
};

0 commit comments

Comments
 (0)