Skip to content

[FSSDK-11395] add specific type for decision notification #1025

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 15, 2025
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
4 changes: 2 additions & 2 deletions lib/common_exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export { createErrorNotifier } from './error/error_notifier_factory';

export {
DECISION_SOURCES,
DECISION_NOTIFICATION_TYPES,
NOTIFICATION_TYPES,
} from './utils/enums';

export { NOTIFICATION_TYPES, DECISION_NOTIFICATION_TYPES } from './notification_center/type';

export { OptimizelyDecideOption } from './shared_types';
3 changes: 2 additions & 1 deletion lib/core/decision_service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
AUDIENCE_EVALUATION_TYPES,
CONTROL_ATTRIBUTES,
DECISION_SOURCES,
DecisionSource,
} from '../../utils/enums';
import {
getAudiencesById,
Expand Down Expand Up @@ -114,7 +115,7 @@ export const CMAB_FETCHED_VARIATION_INVALID = 'Fetched variation %s for cmab exp
export interface DecisionObj {
experiment: Experiment | null;
variation: Variation | null;
decisionSource: string;
decisionSource: DecisionSource;
cmabUuid?: string;
}

Expand Down
6 changes: 3 additions & 3 deletions lib/entrypoint.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ import {

import {
DECISION_SOURCES,
DECISION_NOTIFICATION_TYPES,
NOTIFICATION_TYPES,
} from './utils/enums';

import { NOTIFICATION_TYPES, DECISION_NOTIFICATION_TYPES } from './notification_center/type';

import { LogLevel } from './logging/logger';

import { OptimizelyDecideOption } from './shared_types';
Expand Down Expand Up @@ -89,8 +89,8 @@ export type Entrypoint = {

// enums
DECISION_SOURCES: typeof DECISION_SOURCES;
DECISION_NOTIFICATION_TYPES: typeof DECISION_NOTIFICATION_TYPES;
NOTIFICATION_TYPES: typeof NOTIFICATION_TYPES;
DECISION_NOTIFICATION_TYPES: typeof DECISION_NOTIFICATION_TYPES;

// decide options
OptimizelyDecideOption: typeof OptimizelyDecideOption;
Expand Down
6 changes: 3 additions & 3 deletions lib/entrypoint.universal.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ import { RequestHandler } from './utils/http_request_handler/http';
import { UniversalBatchEventProcessorOptions } from './event_processor/event_processor_factory.universal';
import {
DECISION_SOURCES,
DECISION_NOTIFICATION_TYPES,
NOTIFICATION_TYPES,
} from './utils/enums';

import { NOTIFICATION_TYPES, DECISION_NOTIFICATION_TYPES } from './notification_center/type';

import { LogLevel } from './logging/logger';

import { OptimizelyDecideOption } from './shared_types';
Expand Down Expand Up @@ -82,8 +82,8 @@ export type UniversalEntrypoint = {

// enums
DECISION_SOURCES: typeof DECISION_SOURCES;
DECISION_NOTIFICATION_TYPES: typeof DECISION_NOTIFICATION_TYPES;
NOTIFICATION_TYPES: typeof NOTIFICATION_TYPES;
DECISION_NOTIFICATION_TYPES: typeof DECISION_NOTIFICATION_TYPES;

// decide options
OptimizelyDecideOption: typeof OptimizelyDecideOption;
Expand Down
75 changes: 68 additions & 7 deletions lib/notification_center/type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2024, Optimizely
* Copyright 2024-2025, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,7 +15,9 @@
*/

import { LogEvent } from '../event_processor/event_dispatcher/event_dispatcher';
import { EventTags, Experiment, UserAttributes, Variation } from '../shared_types';
import { EventTags, Experiment, FeatureVariableValue, UserAttributes, VariableType, Variation } from '../shared_types';
import { DecisionSource } from '../utils/enums';
import { Nullable } from '../utils/type';

export type UserEventListenerPayload = {
userId: string;
Expand Down Expand Up @@ -43,16 +45,75 @@ export const DECISION_NOTIFICATION_TYPES = {
FLAG: 'flag',
} as const;


export type DecisionNotificationType = typeof DECISION_NOTIFICATION_TYPES[keyof typeof DECISION_NOTIFICATION_TYPES];

// TODO: Add more specific types for decision info
export type OptimizelyDecisionInfo = Record<string, any>;
export type ExperimentAndVariationInfo = {
experimentKey: string;
variationKey: string;
}

export type DecisionSourceInfo = Partial<ExperimentAndVariationInfo>;

export type DecisionListenerPayload = UserEventListenerPayload & {
type: DecisionNotificationType;
decisionInfo: OptimizelyDecisionInfo;
export type AbTestDecisonInfo = Nullable<ExperimentAndVariationInfo, 'variationKey'>;

type FeatureDecisionInfo = {
featureKey: string,
featureEnabled: boolean,
source: DecisionSource,
sourceInfo: DecisionSourceInfo,
}

export type FeatureTestDecisionInfo = Nullable<ExperimentAndVariationInfo, 'variationKey'>;

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

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

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,
};

export type DecisionInfo = {
[DECISION_NOTIFICATION_TYPES.AB_TEST]: AbTestDecisonInfo;
[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> = UserEventListenerPayload & {
type: T;
decisionInfo: DecisionInfo[T];
}

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

export type LogEventListenerPayload = LogEvent;

export type OptimizelyConfigUpdateListenerPayload = undefined;
Expand Down
6 changes: 3 additions & 3 deletions lib/tests/test_data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2021, 2024 Optimizely
* Copyright 2016-2021, 2024-2025 Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -3555,7 +3555,7 @@ export var getMutexFeatureTestsConfig = function() {
export var rolloutDecisionObj = {
experiment: null,
variation: null,
decisionSource: 'rollout',
decisionSource: 'rollout' as const,
};

export var featureTestDecisionObj = {
Expand Down Expand Up @@ -3611,7 +3611,7 @@ export var featureTestDecisionObj = {
variables: [],
variablesMap: {}
},
decisionSource: 'feature-test',
decisionSource: 'feature-test' as const,
};

var similarRuleKeyConfig = {
Expand Down
17 changes: 4 additions & 13 deletions lib/utils/enums/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2024, Optimizely
* Copyright 2016-2025, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -44,15 +44,6 @@ export const NODE_CLIENT_ENGINE = 'node-sdk';
export const REACT_NATIVE_JS_CLIENT_ENGINE = 'react-native-js-sdk';
export const CLIENT_VERSION = '5.3.4';

export const DECISION_NOTIFICATION_TYPES = {
AB_TEST: 'ab-test',
FEATURE: 'feature',
FEATURE_TEST: 'feature-test',
FEATURE_VARIABLE: 'feature-variable',
ALL_FEATURE_VARIABLES: 'all-feature-variables',
FLAG: 'flag',
};

/*
* Represents the source of a decision for feature management. When a feature
* is accessed through isFeatureEnabled or getVariableValue APIs, the decision
Expand All @@ -63,7 +54,9 @@ export const DECISION_SOURCES = {
FEATURE_TEST: 'feature-test',
ROLLOUT: 'rollout',
EXPERIMENT: 'experiment',
};
} as const;

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

export const AUDIENCE_EVALUATION_TYPES = {
RULE: 'rule',
Expand Down Expand Up @@ -104,8 +97,6 @@ export const DECISION_MESSAGES = {
VARIABLE_VALUE_INVALID: 'Variable value for key "%s" is invalid or wrong type.',
};

export { NOTIFICATION_TYPES } from '../../notification_center/type';

/**
* Default milliseconds before request timeout
*/
Expand Down
8 changes: 7 additions & 1 deletion lib/utils/type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2024, Optimizely
* Copyright 2024-2025, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,3 +31,9 @@ export type Either<A, B> = { type: 'left', value: A } | { type: 'right', value:

export type OpType = 'sync' | 'async';
export type OpValue<O extends OpType, V> = O extends 'sync' ? V : Promise<V>;

export type OrNull<T> = T | null;

export type Nullable<T, K extends keyof T> = {
[P in keyof T]: P extends K ? OrNull<T[P]> : T[P];
}
Loading