|
3 | 3 | Client,
|
4 | 4 | OpaqueConfigManager,
|
5 | 5 | OptimizelyDecision,
|
| 6 | + OptimizelyUserContext, |
6 | 7 | UserAttributes,
|
7 | 8 | } from '@optimizely/optimizely-sdk';
|
8 | 9 |
|
@@ -33,6 +34,10 @@ type AdapterResponse = {
|
33 | 34 | attributes?: UserAttributes;
|
34 | 35 | },
|
35 | 36 | ) => Adapter<T, UserId>;
|
| 37 | + userContext: ( |
| 38 | + entities: UserId, |
| 39 | + attributes?: UserAttributes, |
| 40 | + ) => Promise<OptimizelyUserContext>; |
36 | 41 | initialize: () => Promise<Client>;
|
37 | 42 | };
|
38 | 43 |
|
@@ -114,8 +119,6 @@ export function createOptimizelyAdapter({
|
114 | 119 | optimizelyInstance = createInstance({
|
115 | 120 | clientEngine: 'javascript-sdk/flags-sdk',
|
116 | 121 | 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 |
119 | 122 | // @ts-expect-error - dispatchEvent runs in `waitUntil` so it's not going to return a response
|
120 | 123 | eventProcessor: createForwardingEventProcessor({ dispatchEvent }),
|
121 | 124 | requestHandler,
|
@@ -171,28 +174,41 @@ export function createOptimizelyAdapter({
|
171 | 174 | };
|
172 | 175 | }
|
173 | 176 |
|
| 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 | + |
174 | 190 | return {
|
175 | 191 | decide,
|
| 192 | + userContext, |
176 | 193 | initialize,
|
177 | 194 | };
|
178 | 195 | }
|
179 | 196 |
|
180 | 197 | function getOrCreateDefaultOptimizelyAdapter(): AdapterResponse {
|
181 | 198 | 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; |
183 | 200 | const edgeConfigItemKey = process.env.OPTIMIZELY_DATAFILE_ITEM_KEY;
|
184 | 201 |
|
185 | 202 | if (!defaultOptimizelyAdapter) {
|
186 |
| - if (edgeConfig && edgeConfigItemKey) { |
| 203 | + if (edgeConfigConnectionString && edgeConfigItemKey) { |
187 | 204 | defaultOptimizelyAdapter = createOptimizelyAdapter({
|
188 | 205 | sdkKey,
|
189 | 206 | edgeConfig: {
|
190 |
| - connectionString: edgeConfig, |
| 207 | + connectionString: edgeConfigConnectionString, |
191 | 208 | itemKey: edgeConfigItemKey,
|
192 | 209 | },
|
193 | 210 | });
|
194 | 211 | } else {
|
195 |
| - // Fallback to polling optimizely SDK |
196 | 212 | defaultOptimizelyAdapter = createOptimizelyAdapter({
|
197 | 213 | sdkKey,
|
198 | 214 | });
|
@@ -223,5 +239,7 @@ function getOrCreateDefaultOptimizelyAdapter(): AdapterResponse {
|
223 | 239 | */
|
224 | 240 | export const optimizelyAdapter: AdapterResponse = {
|
225 | 241 | decide: (...args) => getOrCreateDefaultOptimizelyAdapter().decide(...args),
|
| 242 | + userContext: (...args) => |
| 243 | + getOrCreateDefaultOptimizelyAdapter().userContext(...args), |
226 | 244 | initialize: () => getOrCreateDefaultOptimizelyAdapter().initialize(),
|
227 | 245 | };
|
0 commit comments