|
1 | 1 | /**
|
2 |
| - * Copyright 2024, Optimizely |
| 2 | + * Copyright 2024-2025, Optimizely |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
15 | 15 | */
|
16 | 16 |
|
17 | 17 | import { LogEvent } from '../event_processor/event_dispatcher/event_dispatcher';
|
18 |
| -import { EventTags, Experiment, UserAttributes, Variation } from '../shared_types'; |
| 18 | +import { EventTags, Experiment, FeatureVariableValue, UserAttributes, VariableType, Variation } from '../shared_types'; |
| 19 | +import { DecisionSource } from '../utils/enums'; |
| 20 | +import { Nullable } from '../utils/type'; |
19 | 21 |
|
20 | 22 | export type UserEventListenerPayload = {
|
21 | 23 | userId: string;
|
@@ -43,16 +45,75 @@ export const DECISION_NOTIFICATION_TYPES = {
|
43 | 45 | FLAG: 'flag',
|
44 | 46 | } as const;
|
45 | 47 |
|
| 48 | + |
46 | 49 | export type DecisionNotificationType = typeof DECISION_NOTIFICATION_TYPES[keyof typeof DECISION_NOTIFICATION_TYPES];
|
47 | 50 |
|
48 |
| -// TODO: Add more specific types for decision info |
49 |
| -export type OptimizelyDecisionInfo = Record<string, any>; |
| 51 | +export type ExperimentAndVariationInfo = { |
| 52 | + experimentKey: string; |
| 53 | + variationKey: string; |
| 54 | +} |
| 55 | + |
| 56 | +export type DecisionSourceInfo = Partial<ExperimentAndVariationInfo>; |
50 | 57 |
|
51 |
| -export type DecisionListenerPayload = UserEventListenerPayload & { |
52 |
| - type: DecisionNotificationType; |
53 |
| - decisionInfo: OptimizelyDecisionInfo; |
| 58 | +export type AbTestDecisonInfo = Nullable<ExperimentAndVariationInfo, 'variationKey'>; |
| 59 | + |
| 60 | +type FeatureDecisionInfo = { |
| 61 | + featureKey: string, |
| 62 | + featureEnabled: boolean, |
| 63 | + source: DecisionSource, |
| 64 | + sourceInfo: Partial<ExperimentAndVariationInfo>, |
54 | 65 | }
|
55 | 66 |
|
| 67 | +export type FeatureTestDecisionInfo = Nullable<ExperimentAndVariationInfo, 'variationKey'>; |
| 68 | + |
| 69 | +export type FeatureVariableDecisionInfo = { |
| 70 | + featureKey: string, |
| 71 | + featureEnabled: boolean, |
| 72 | + source: DecisionSource, |
| 73 | + variableKey: string, |
| 74 | + variableValue: FeatureVariableValue, |
| 75 | + variableType: VariableType, |
| 76 | + sourceInfo: DecisionSourceInfo, |
| 77 | +}; |
| 78 | + |
| 79 | +export type VariablesMap = { [variableKey: string]: unknown } |
| 80 | + |
| 81 | +export type AllFeatureVariablesDecisionInfo = { |
| 82 | + featureKey: string, |
| 83 | + featureEnabled: boolean, |
| 84 | + source: DecisionSource, |
| 85 | + variableValues: VariablesMap, |
| 86 | + sourceInfo: DecisionSourceInfo, |
| 87 | +}; |
| 88 | + |
| 89 | +export type FlagDecisionInfo = { |
| 90 | + flagKey: string, |
| 91 | + enabled: boolean, |
| 92 | + variationKey: string | null, |
| 93 | + ruleKey: string | null, |
| 94 | + variables: VariablesMap, |
| 95 | + reasons: string[], |
| 96 | + decisionEventDispatched: boolean, |
| 97 | +}; |
| 98 | + |
| 99 | +export type DecisionInfo = { |
| 100 | + [DECISION_NOTIFICATION_TYPES.AB_TEST]: AbTestDecisonInfo; |
| 101 | + [DECISION_NOTIFICATION_TYPES.FEATURE]: FeatureDecisionInfo; |
| 102 | + [DECISION_NOTIFICATION_TYPES.FEATURE_TEST]: FeatureTestDecisionInfo; |
| 103 | + [DECISION_NOTIFICATION_TYPES.FEATURE_VARIABLE]: FeatureVariableDecisionInfo; |
| 104 | + [DECISION_NOTIFICATION_TYPES.ALL_FEATURE_VARIABLES]: AllFeatureVariablesDecisionInfo; |
| 105 | + [DECISION_NOTIFICATION_TYPES.FLAG]: FlagDecisionInfo; |
| 106 | +} |
| 107 | + |
| 108 | +export type DecisionListenerPayloadForType<T extends DecisionNotificationType> = UserEventListenerPayload & { |
| 109 | + type: T; |
| 110 | + decisionInfo: DecisionInfo[T]; |
| 111 | +} |
| 112 | + |
| 113 | +export type DecisionListenerPayload = { |
| 114 | + [T in DecisionNotificationType]: DecisionListenerPayloadForType<T>; |
| 115 | +}[DecisionNotificationType]; |
| 116 | + |
56 | 117 | export type LogEventListenerPayload = LogEvent;
|
57 | 118 |
|
58 | 119 | export type OptimizelyConfigUpdateListenerPayload = undefined;
|
|
0 commit comments