Skip to content

Commit c20678f

Browse files
[FSSDK-12115] Notification center type definition update (#1119)
1 parent 34682ea commit c20678f

File tree

6 files changed

+110
-416
lines changed

6 files changed

+110
-416
lines changed

lib/core/decision_service/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import * as stringValidator from '../../utils/string_value_validator';
4444
import {
4545
BucketerParams,
4646
DecisionResponse,
47+
DecisionSource,
4748
Experiment,
4849
ExperimentBucketMap,
4950
FeatureFlag,
@@ -60,7 +61,7 @@ const MODULE_NAME = 'DECISION_SERVICE';
6061
export interface DecisionObj {
6162
experiment: Experiment | null;
6263
variation: Variation | null;
63-
decisionSource: string;
64+
decisionSource: DecisionSource;
6465
}
6566

6667
interface DecisionServiceOptions {

lib/core/notification_center/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
import { LogHandler, ErrorHandler } from '../../modules/logging';
1717
import { objectValues } from '../../utils/fns';
18-
import { NotificationListener, ListenerPayload } from '../../shared_types';
18+
import { ListenerPayload, NotificationListener, NotificationPayloadMap } from '../../shared_types';
1919

2020
import {
2121
LOG_LEVEL,

lib/export_types.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ export {
3737
UserProfileService,
3838
UserProfile,
3939
ListenerPayload,
40+
DecisionListenerPayload,
41+
LogEventListenerPayload,
42+
NotificationPayloadMap,
4043
OptimizelyDecision,
4144
OptimizelyUserContext,
4245
NotificationListener,
@@ -47,4 +50,18 @@ export {
4750
NotificationCenter,
4851
OptimizelySegmentOption,
4952
ICache,
53+
// Decision info types
54+
DecisionNotificationType,
55+
DecisionSource,
56+
DecisionSourceInfo,
57+
VariablesMap,
58+
// Specific decision info types for type narrowing
59+
AbTestDecisionInfo,
60+
FeatureDecisionInfo,
61+
FeatureTestDecisionInfo,
62+
FeatureVariableDecisionInfo,
63+
AllFeatureVariablesDecisionInfo,
64+
FlagDecisionInfo,
65+
DecisionInfoMap,
66+
DecisionListenerPayloadForType,
5067
} from './shared_types';

lib/shared_types.ts

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { ErrorHandler, LogHandler, LogLevel, LoggerFacade } from './modules/logg
2323
import { EventProcessor } from './modules/event_processor';
2424

2525
import { NotificationCenter as NotificationCenterImpl } from './core/notification_center';
26-
import { NOTIFICATION_TYPES } from './utils/enums';
26+
import { NOTIFICATION_TYPES, DECISION_NOTIFICATION_TYPES, DECISION_SOURCES } from './utils/enums';
2727

2828
import { IOptimizelyUserContext as OptimizelyUserContext } from './optimizely_user_context';
2929

@@ -120,9 +120,93 @@ export interface ListenerPayload {
120120
attributes?: UserAttributes;
121121
}
122122

123-
export type NotificationListener<T extends ListenerPayload> = (notificationData: T) => void;
123+
export type DecisionNotificationType = typeof DECISION_NOTIFICATION_TYPES[keyof typeof DECISION_NOTIFICATION_TYPES];
124+
125+
export type DecisionSource = typeof DECISION_SOURCES[keyof typeof DECISION_SOURCES];
126+
127+
export type DecisionSourceInfo = {
128+
experimentKey?: string;
129+
variationKey?: string;
130+
};
131+
132+
export type VariablesMap = { [variableKey: string]: unknown };
133+
134+
export type AbTestDecisionInfo = {
135+
experimentKey: string;
136+
variationKey: string | null;
137+
};
124138

125-
// NotificationCenter-related types
139+
export type FeatureDecisionInfo = {
140+
featureKey: string;
141+
featureEnabled: boolean;
142+
source: DecisionSource;
143+
sourceInfo: DecisionSourceInfo;
144+
};
145+
146+
export type FeatureTestDecisionInfo = {
147+
experimentKey: string;
148+
variationKey: string | null;
149+
};
150+
151+
export type FeatureVariableDecisionInfo = {
152+
featureKey: string;
153+
featureEnabled: boolean;
154+
source: DecisionSource;
155+
variableKey: string;
156+
variableValue: FeatureVariableValue;
157+
variableType: VariableType;
158+
sourceInfo: DecisionSourceInfo;
159+
};
160+
161+
export type AllFeatureVariablesDecisionInfo = {
162+
featureKey: string;
163+
featureEnabled: boolean;
164+
source: DecisionSource;
165+
variableValues: VariablesMap;
166+
sourceInfo: DecisionSourceInfo;
167+
};
168+
169+
export type FlagDecisionInfo = {
170+
flagKey: string;
171+
enabled: boolean;
172+
variationKey: string | null;
173+
ruleKey: string | null;
174+
variables: VariablesMap;
175+
reasons: string[];
176+
decisionEventDispatched: boolean;
177+
experimentId?: string;
178+
variationId?: string;
179+
};
180+
181+
export type DecisionInfoMap = {
182+
[DECISION_NOTIFICATION_TYPES.AB_TEST]: AbTestDecisionInfo;
183+
[DECISION_NOTIFICATION_TYPES.FEATURE]: FeatureDecisionInfo;
184+
[DECISION_NOTIFICATION_TYPES.FEATURE_TEST]: FeatureTestDecisionInfo;
185+
[DECISION_NOTIFICATION_TYPES.FEATURE_VARIABLE]: FeatureVariableDecisionInfo;
186+
[DECISION_NOTIFICATION_TYPES.ALL_FEATURE_VARIABLES]: AllFeatureVariablesDecisionInfo;
187+
[DECISION_NOTIFICATION_TYPES.FLAG]: FlagDecisionInfo;
188+
};
189+
190+
export type DecisionListenerPayloadForType<T extends DecisionNotificationType> = ListenerPayload & {
191+
type: T;
192+
decisionInfo: DecisionInfoMap[T];
193+
};
194+
195+
export type DecisionListenerPayload = {
196+
[T in DecisionNotificationType]: DecisionListenerPayloadForType<T>;
197+
}[DecisionNotificationType];
198+
199+
export type LogEventListenerPayload = Event;
200+
201+
export type NotificationPayloadMap = {
202+
[NOTIFICATION_TYPES.ACTIVATE]: ActivateListenerPayload;
203+
[NOTIFICATION_TYPES.DECISION]: DecisionListenerPayload;
204+
[NOTIFICATION_TYPES.TRACK]: TrackListenerPayload;
205+
[NOTIFICATION_TYPES.LOG_EVENT]: LogEventListenerPayload;
206+
[NOTIFICATION_TYPES.OPTIMIZELY_CONFIG_UPDATE]: undefined;
207+
};
208+
209+
export type NotificationListener<T extends ListenerPayload> = (notificationData: T) => void;
126210
export interface NotificationCenter {
127211
addNotificationListener<T extends ListenerPayload>(
128212
notificationType: string,

lib/utils/enums/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ export const DECISION_NOTIFICATION_TYPES = {
230230
FEATURE_VARIABLE: 'feature-variable',
231231
ALL_FEATURE_VARIABLES: 'all-feature-variables',
232232
FLAG: 'flag',
233-
};
233+
} as const;
234234

235235
/*
236236
* Represents the source of a decision for feature management. When a feature
@@ -242,7 +242,7 @@ export const DECISION_SOURCES = {
242242
FEATURE_TEST: 'feature-test',
243243
ROLLOUT: 'rollout',
244244
EXPERIMENT: 'experiment',
245-
};
245+
} as const;
246246

247247
export const AUDIENCE_EVALUATION_TYPES = {
248248
RULE: 'rule',

0 commit comments

Comments
 (0)