Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/core/decision_service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import {
BucketerParams,
DecisionResponse,
DecisionSource,
Experiment,
ExperimentBucketMap,
FeatureFlag,
Expand All @@ -60,7 +61,7 @@
export interface DecisionObj {
experiment: Experiment | null;
variation: Variation | null;
decisionSource: string;
decisionSource: DecisionSource;
}

interface DecisionServiceOptions {
Expand Down Expand Up @@ -484,7 +485,7 @@

try {
return this.userProfileService.lookup(userId);
} catch (ex: any) {

Check warning on line 488 in lib/core/decision_service/index.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 488 in lib/core/decision_service/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (14)

Unexpected any. Specify a different type

Check warning on line 488 in lib/core/decision_service/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (16)

Unexpected any. Specify a different type

Check warning on line 488 in lib/core/decision_service/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (20)

Unexpected any. Specify a different type

Check warning on line 488 in lib/core/decision_service/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (18)

Unexpected any. Specify a different type
this.logger.log(
LOG_LEVEL.ERROR,
ERROR_MESSAGES.USER_PROFILE_LOOKUP_ERROR,
Expand Down Expand Up @@ -532,7 +533,7 @@
experiment.key,
userId,
);
} catch (ex: any) {

Check warning on line 536 in lib/core/decision_service/index.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 536 in lib/core/decision_service/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (14)

Unexpected any. Specify a different type

Check warning on line 536 in lib/core/decision_service/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (16)

Unexpected any. Specify a different type

Check warning on line 536 in lib/core/decision_service/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (20)

Unexpected any. Specify a different type

Check warning on line 536 in lib/core/decision_service/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (18)

Unexpected any. Specify a different type
this.logger.log(LOG_LEVEL.ERROR, ERROR_MESSAGES.USER_PROFILE_SAVE_ERROR, MODULE_NAME, userId, ex.message);
}
}
Expand Down Expand Up @@ -972,7 +973,7 @@
reasons: decideReasons,
};
}
} catch (ex: any) {

Check warning on line 976 in lib/core/decision_service/index.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 976 in lib/core/decision_service/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (14)

Unexpected any. Specify a different type

Check warning on line 976 in lib/core/decision_service/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (16)

Unexpected any. Specify a different type

Check warning on line 976 in lib/core/decision_service/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (20)

Unexpected any. Specify a different type

Check warning on line 976 in lib/core/decision_service/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (18)

Unexpected any. Specify a different type
// catching experiment not in datafile
this.logger.log(LOG_LEVEL.ERROR, ex.message);
decideReasons.push(ex.message);
Expand Down Expand Up @@ -1065,7 +1066,7 @@
);
return false;
}
} catch (ex: any) {

Check warning on line 1069 in lib/core/decision_service/index.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 1069 in lib/core/decision_service/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (14)

Unexpected any. Specify a different type

Check warning on line 1069 in lib/core/decision_service/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (16)

Unexpected any. Specify a different type

Check warning on line 1069 in lib/core/decision_service/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (20)

Unexpected any. Specify a different type

Check warning on line 1069 in lib/core/decision_service/index.ts

View workflow job for this annotation

GitHub Actions / unit_tests (18)

Unexpected any. Specify a different type
// catching experiment not in datafile
this.logger.log(LOG_LEVEL.ERROR, ex.message);
return false;
Expand Down
2 changes: 1 addition & 1 deletion lib/core/notification_center/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
import { LogHandler, ErrorHandler } from '../../modules/logging';
import { objectValues } from '../../utils/fns';
import { NotificationListener, ListenerPayload } from '../../shared_types';
import { ListenerPayload, NotificationListener, NotificationPayloadMap } from '../../shared_types';

import {
LOG_LEVEL,
Expand Down
17 changes: 17 additions & 0 deletions lib/export_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export {
UserProfileService,
UserProfile,
ListenerPayload,
DecisionListenerPayload,
LogEventListenerPayload,
NotificationPayloadMap,
OptimizelyDecision,
OptimizelyUserContext,
NotificationListener,
Expand All @@ -47,4 +50,18 @@ export {
NotificationCenter,
OptimizelySegmentOption,
ICache,
// Decision info types
DecisionNotificationType,
DecisionSource,
DecisionSourceInfo,
VariablesMap,
// Specific decision info types for type narrowing
AbTestDecisionInfo,
FeatureDecisionInfo,
FeatureTestDecisionInfo,
FeatureVariableDecisionInfo,
AllFeatureVariablesDecisionInfo,
FlagDecisionInfo,
DecisionInfoMap,
DecisionListenerPayloadForType,
} from './shared_types';
90 changes: 87 additions & 3 deletions lib/shared_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { ErrorHandler, LogHandler, LogLevel, LoggerFacade } from './modules/logg
import { EventProcessor } from './modules/event_processor';

import { NotificationCenter as NotificationCenterImpl } from './core/notification_center';
import { NOTIFICATION_TYPES } from './utils/enums';
import { NOTIFICATION_TYPES, DECISION_NOTIFICATION_TYPES, DECISION_SOURCES } from './utils/enums';

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

Expand Down Expand Up @@ -120,9 +120,93 @@ export interface ListenerPayload {
attributes?: UserAttributes;
}

export type NotificationListener<T extends ListenerPayload> = (notificationData: T) => void;
export type DecisionNotificationType = typeof DECISION_NOTIFICATION_TYPES[keyof typeof DECISION_NOTIFICATION_TYPES];

export type DecisionSource = typeof DECISION_SOURCES[keyof typeof DECISION_SOURCES];

export type DecisionSourceInfo = {
experimentKey?: string;
variationKey?: string;
};

export type VariablesMap = { [variableKey: string]: unknown };

export type AbTestDecisionInfo = {
experimentKey: string;
variationKey: string | null;
};

// NotificationCenter-related types
export type FeatureDecisionInfo = {
featureKey: string;
featureEnabled: boolean;
source: DecisionSource;
sourceInfo: DecisionSourceInfo;
};

export type FeatureTestDecisionInfo = {
experimentKey: string;
variationKey: string | null;
};

export type FeatureVariableDecisionInfo = {
featureKey: string;
featureEnabled: boolean;
source: DecisionSource;
variableKey: string;
variableValue: FeatureVariableValue;
variableType: VariableType;
sourceInfo: DecisionSourceInfo;
};

export type AllFeatureVariablesDecisionInfo = {
featureKey: string;
featureEnabled: boolean;
source: DecisionSource;
variableValues: VariablesMap;
sourceInfo: DecisionSourceInfo;
};

export type FlagDecisionInfo = {
flagKey: string;
enabled: boolean;
variationKey: string | null;
ruleKey: string | null;
variables: VariablesMap;
reasons: string[];
decisionEventDispatched: boolean;
experimentId?: string;
variationId?: string;
};

export type DecisionInfoMap = {
[DECISION_NOTIFICATION_TYPES.AB_TEST]: AbTestDecisionInfo;
[DECISION_NOTIFICATION_TYPES.FEATURE]: FeatureDecisionInfo;
[DECISION_NOTIFICATION_TYPES.FEATURE_TEST]: FeatureTestDecisionInfo;
[DECISION_NOTIFICATION_TYPES.FEATURE_VARIABLE]: FeatureVariableDecisionInfo;
[DECISION_NOTIFICATION_TYPES.ALL_FEATURE_VARIABLES]: AllFeatureVariablesDecisionInfo;
[DECISION_NOTIFICATION_TYPES.FLAG]: FlagDecisionInfo;
};

export type DecisionListenerPayloadForType<T extends DecisionNotificationType> = ListenerPayload & {
type: T;
decisionInfo: DecisionInfoMap[T];
};

export type DecisionListenerPayload = {
[T in DecisionNotificationType]: DecisionListenerPayloadForType<T>;
}[DecisionNotificationType];

export type LogEventListenerPayload = Event;

export type NotificationPayloadMap = {
[NOTIFICATION_TYPES.ACTIVATE]: ActivateListenerPayload;
[NOTIFICATION_TYPES.DECISION]: DecisionListenerPayload;
[NOTIFICATION_TYPES.TRACK]: TrackListenerPayload;
[NOTIFICATION_TYPES.LOG_EVENT]: LogEventListenerPayload;
[NOTIFICATION_TYPES.OPTIMIZELY_CONFIG_UPDATE]: undefined;
};

export type NotificationListener<T extends ListenerPayload> = (notificationData: T) => void;
export interface NotificationCenter {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot modify this interface, it would be a breaking change

addNotificationListener<T extends ListenerPayload>(
notificationType: string,
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/enums/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ export const DECISION_NOTIFICATION_TYPES = {
FEATURE_VARIABLE: 'feature-variable',
ALL_FEATURE_VARIABLES: 'all-feature-variables',
FLAG: 'flag',
};
} as const;

/*
* Represents the source of a decision for feature management. When a feature
Expand All @@ -242,7 +242,7 @@ export const DECISION_SOURCES = {
FEATURE_TEST: 'feature-test',
ROLLOUT: 'rollout',
EXPERIMENT: 'experiment',
};
} as const;

export const AUDIENCE_EVALUATION_TYPES = {
RULE: 'rule',
Expand Down
Loading
Loading