Skip to content

Commit f3c51c4

Browse files
committed
Convert Optimizely Module
1 parent 7e2636d commit f3c51c4

File tree

13 files changed

+1799
-1643
lines changed

13 files changed

+1799
-1643
lines changed

packages/optimizely-sdk/lib/core/decision_service/index.d.ts

Lines changed: 80 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -13,95 +13,94 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
import { LogHandler } from '@optimizely/js-sdk-logging';
17+
import { ProjectConfig } from '../project_config';
18+
import { UserAttributes, UserProfileService, Experiment, Variation } from '../../shared_types';
19+
import { FeatureFlag } from '../project_config/entities';
1620

17-
declare module '@optimizely/optimizely-sdk/lib/core/decision_service' {
18-
import { LogHandler } from '@optimizely/js-sdk-logging';
19-
import { ProjectConfig } from '@optimizely/optimizely-sdk/lib/core/project_config';
21+
/**
22+
* Creates an instance of the DecisionService.
23+
* @param {Options} options Configuration options
24+
* @return {DecisionService} An instance of the DecisionService
25+
*/
26+
export function createDecisionService(options: Options): DecisionService;
27+
28+
export interface DecisionService {
2029

2130
/**
22-
* Creates an instance of the DecisionService.
23-
* @param {Options} options Configuration options
24-
* @return {DecisionService} An instance of the DecisionService
31+
* Gets variation where visitor will be bucketed.
32+
* @param {ProjectConfig} configObj The parsed project configuration object
33+
* @param {string} experimentKey
34+
* @param {string} userId
35+
* @param {UserAttributes} attributes
36+
* @return {string|null} The variation the user is bucketed into.
2537
*/
26-
export function createDecisionService(options: Options): DecisionService;
27-
28-
interface DecisionService {
38+
getVariation(
39+
configObj: ProjectConfig,
40+
experimentKey: string,
41+
userId: string,
42+
attributes?: UserAttributes
43+
): string | null;
2944

30-
/**
31-
* Gets variation where visitor will be bucketed.
32-
* @param {ProjectConfig} configObj The parsed project configuration object
33-
* @param {string} experimentKey
34-
* @param {string} userId
35-
* @param {UserAttributes} attributes
36-
* @return {string|null} The variation the user is bucketed into.
37-
*/
38-
getVariation(
39-
configObj: ProjectConfig,
40-
experimentKey: string,
41-
userId: string,
42-
attributes?: import('../../shared_types').UserAttributes
43-
): string | null;
44-
45-
/**
46-
* Given a feature, user ID, and attributes, returns an object representing a
47-
* decision. If the user was bucketed into a variation for the given feature
48-
* and attributes, the returned decision object will have variation and
49-
* experiment properties (both objects), as well as a decisionSource property.
50-
* decisionSource indicates whether the decision was due to a rollout or an
51-
* experiment.
52-
* @param {ProjectConfig} configObj The parsed project configuration object
53-
* @param {FeatureFlag} feature A feature flag object from project configuration
54-
* @param {string} userId A string identifying the user, for bucketing
55-
* @param {unknown} attributes Optional user attributes
56-
* @return {Decision} An object with experiment, variation, and decisionSource
57-
* properties. If the user was not bucketed into a variation, the variation
58-
* property is null.
59-
*/
60-
getVariationForFeature(
61-
configObj: ProjectConfig,
62-
feature: import('../project_config/entities').FeatureFlag,
63-
userId: string,
64-
attributes: unknown
65-
): Decision;
45+
/**
46+
* Given a feature, user ID, and attributes, returns an object representing a
47+
* decision. If the user was bucketed into a variation for the given feature
48+
* and attributes, the returned decision object will have variation and
49+
* experiment properties (both objects), as well as a decisionSource property.
50+
* decisionSource indicates whether the decision was due to a rollout or an
51+
* experiment.
52+
* @param {ProjectConfig} configObj The parsed project configuration object
53+
* @param {FeatureFlag} feature A feature flag object from project configuration
54+
* @param {string} userId A string identifying the user, for bucketing
55+
* @param {unknown} attributes Optional user attributes
56+
* @return {Decision} An object with experiment, variation, and decisionSource
57+
* properties. If the user was not bucketed into a variation, the variation
58+
* property is null.
59+
*/
60+
getVariationForFeature(
61+
configObj: ProjectConfig,
62+
feature: FeatureFlag,
63+
userId: string,
64+
attributes: unknown
65+
): Decision;
6666

67-
/**
68-
* Removes forced variation for given userId and experimentKey
69-
* @param {unknown} userId String representing the user id
70-
* @param {string} experimentId Number representing the experiment id
71-
* @param {string} experimentKey Key representing the experiment id
72-
* @throws If the user id is not valid or not in the forced variation map
73-
*/
74-
removeForcedVariation(userId: unknown, experimentId: string, experimentKey: string): void;
67+
/**
68+
* Removes forced variation for given userId and experimentKey
69+
* @param {unknown} userId String representing the user id
70+
* @param {string} experimentId Number representing the experiment id
71+
* @param {string} experimentKey Key representing the experiment id
72+
* @throws If the user id is not valid or not in the forced variation map
73+
*/
74+
removeForcedVariation(userId: unknown, experimentId: string, experimentKey: string): void;
7575

76-
/**
77-
* Gets the forced variation key for the given user and experiment.
78-
* @param {ProjectConfig} configObj Object representing project configuration
79-
* @param {string} experimentKey Key for experiment.
80-
* @param {string} userId The user Id.
81-
* @return {string|null} Variation key that specifies the variation which the given user and experiment should be forced into.
82-
*/
83-
getForcedVariation(configObj: ProjectConfig, experimentKey: string, userId: string): string | null;
76+
/**
77+
* Gets the forced variation key for the given user and experiment.
78+
* @param {ProjectConfig} configObj Object representing project configuration
79+
* @param {string} experimentKey Key for experiment.
80+
* @param {string} userId The user Id.
81+
* @return {string|null} Variation key that specifies the variation which the given user and experiment should be forced into.
82+
*/
83+
getForcedVariation(configObj: ProjectConfig, experimentKey: string, userId: string): string | null;
8484

85-
/**
86-
* Sets the forced variation for a user in a given experiment
87-
* @param {ProjectConfig} configObj Object representing project configuration
88-
* @param {string} experimentKey Key for experiment.
89-
* @param {string} userId The user Id.
90-
* @param {unknown} variationKey Key for variation. If null, then clear the existing experiment-to-variation mapping
91-
* @return {boolean} A boolean value that indicates if the set completed successfully.
92-
*/
93-
setForcedVariation(configObj: ProjectConfig, experimentKey: string, userId: string, variationKey: unknown): boolean;
94-
}
85+
/**
86+
* Sets the forced variation for a user in a given experiment
87+
* @param {ProjectConfig} configObj Object representing project configuration
88+
* @param {string} experimentKey Key for experiment.
89+
* @param {string} userId The user Id.
90+
* @param {unknown} variationKey Key for variation. If null, then clear the existing experiment-to-variation mapping
91+
* @return {boolean} A boolean value that indicates if the set completed successfully.
92+
*/
93+
setForcedVariation(configObj: ProjectConfig, experimentKey: string, userId: string, variationKey: unknown): boolean;
94+
}
9595

96-
interface Options {
97-
userProfileService: import('../../shared_types').UserProfileService | null;
98-
logger: LogHandler;
99-
UNSTABLE_conditionEvaluators: unknown;
100-
}
96+
interface Options {
97+
userProfileService: UserProfileService | null;
98+
logger: LogHandler;
99+
UNSTABLE_conditionEvaluators: unknown;
100+
}
101101

102-
interface Decision {
103-
experiment: import('../../shared_types').Experiment | null;
104-
variation: import('../../shared_types').Variation | null;
105-
decisionSource: string;
106-
}
102+
interface Decision {
103+
experiment: Experiment | null;
104+
variation: Variation | null;
105+
decisionSource: string;
107106
}

packages/optimizely-sdk/lib/core/notification_center/index.d.ts

Lines changed: 70 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -14,79 +14,84 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17+
import { LogHandler, ErrorHandler } from '@optimizely/js-sdk-logging';
18+
import {
19+
Event,
20+
EventTags,
21+
UserAttributes,
22+
Experiment,
23+
Variation
24+
} from '../../shared_types';
1725

18-
declare module '@optimizely/optimizely-sdk/lib/core/notification_center' {
19-
import { LogHandler, ErrorHandler } from '@optimizely/js-sdk-logging';
20-
21-
export enum NOTIFICATION_TYPES {
22-
ACTIVATE = 'ACTIVATE:experiment, user_id,attributes, variation, event',
23-
DECISION = 'DECISION:type, userId, attributes, decisionInfo',
24-
LOG_EVENT = 'LOG_EVENT:logEvent',
25-
OPTIMIZELY_CONFIG_UPDATE = 'OPTIMIZELY_CONFIG_UPDATE',
26-
TRACK = 'TRACK:event_key, user_id, attributes, event_tags, event',
27-
}
28-
29-
export enum DECISION_NOTIFICATION_TYPES {
30-
AB_TEST = 'ab-test',
31-
FEATURE = 'feature',
32-
FEATURE_TEST = 'feature-test',
33-
FEATURE_VARIABLE = 'feature-variable',
34-
ALL_FEATURE_VARIABLES = 'all-feature-variables',
35-
}
26+
export enum NOTIFICATION_TYPES {
27+
ACTIVATE = 'ACTIVATE:experiment, user_id,attributes, variation, event',
28+
DECISION = 'DECISION:type, userId, attributes, decisionInfo',
29+
LOG_EVENT = 'LOG_EVENT:logEvent',
30+
OPTIMIZELY_CONFIG_UPDATE = 'OPTIMIZELY_CONFIG_UPDATE',
31+
TRACK = 'TRACK:event_key, user_id, attributes, event_tags, event',
32+
}
3633

37-
export type Options = {
38-
logger: LogHandler;
39-
errorHandler: ErrorHandler;
40-
};
34+
export enum DECISION_NOTIFICATION_TYPES {
35+
AB_TEST = 'ab-test',
36+
FEATURE = 'feature',
37+
FEATURE_TEST = 'feature-test',
38+
FEATURE_VARIABLE = 'feature-variable',
39+
ALL_FEATURE_VARIABLES = 'all-feature-variables',
40+
}
4141

42-
export type SourceInfo = {
43-
experimentKey?: string;
44-
variationKey?: string;
45-
};
42+
export type Options = {
43+
logger: LogHandler;
44+
errorHandler: ErrorHandler;
45+
};
4646

47-
export type VariableValues = {
48-
[name: string]: unknown;
49-
};
47+
export type SourceInfo = {
48+
experimentKey?: string;
49+
variationKey?: string;
50+
};
5051

51-
export type DecisionInfo = {
52-
experimentKey?: string;
53-
variationKey?: string;
54-
featureKey?: string;
55-
featureEnabled?: boolean;
56-
source?: string;
57-
sourceInfo?: SourceInfo;
58-
variableKey?: string;
59-
variableValue?: unknown;
60-
variableValues?: VariableValues;
61-
variableType?: string;
62-
};
52+
export type VariableValues = {
53+
[name: string]: unknown;
54+
};
6355

64-
export interface NotificationData {
65-
type?: DECISION_NOTIFICATION_TYPES;
66-
userId?: string;
67-
attributes?: import('../../shared_types').UserAttributes;
68-
decisionInfo?: DecisionInfo;
69-
experiment?: import('../../shared_types').Experiment;
70-
variation?: import('../../shared_types').Variation;
71-
logEvent?: string;
72-
eventKey?: string;
73-
eventTags?: string;
74-
}
56+
export type DecisionInfo = {
57+
experimentKey?: string;
58+
variationKey?: string | null;
59+
featureKey?: string;
60+
featureEnabled?: boolean;
61+
source?: string;
62+
sourceInfo?: SourceInfo;
63+
variableKey?: string;
64+
variableValue?: unknown;
65+
variableValues?: VariableValues;
66+
variableType?: string;
67+
};
7568

76-
export interface NotificationCenter {
77-
/**
78-
* Fires notifications for the argument type. All registered callbacks for this type will be
79-
* called. The notificationData object will be passed on to callbacks called.
80-
* @param {NOTIFICATION_TYPES} notificationType One of NOTIFICATION_TYPES
81-
* @param {NotificationData} notificationData Will be passed to callbacks called
82-
*/
83-
sendNotifications(notificationType: NOTIFICATION_TYPES, notificationData: NotificationData): void;
84-
}
69+
export interface NotificationData {
70+
// type?: DECISION_NOTIFICATION_TYPES;
71+
type?: string;
72+
userId?: string;
73+
attributes?: UserAttributes;
74+
decisionInfo?: DecisionInfo;
75+
experiment?: Experiment;
76+
variation?: Variation;
77+
logEvent?: Event;
78+
eventKey?: string;
79+
eventTags?: EventTags;
80+
}
8581

82+
export interface NotificationCenter {
8683
/**
87-
* Create an instance of NotificationCenter
88-
* @param {Options} options
89-
* @returns {NotificationCenter} An instance of NotificationCenter
84+
* Fires notifications for the argument type. All registered callbacks for this type will be
85+
* called. The notificationData object will be passed on to callbacks called.
86+
* @param {NOTIFICATION_TYPES} notificationType One of NOTIFICATION_TYPES
87+
* @param {NotificationData} notificationData Will be passed to callbacks called
9088
*/
91-
export function createNotificationCenter(options: Options): NotificationCenter;
89+
sendNotifications(notificationType: NOTIFICATION_TYPES, notificationData?: NotificationData): void;
9290
}
91+
92+
/**
93+
* Create an instance of NotificationCenter
94+
* @param {Options} options
95+
* @returns {NotificationCenter} An instance of NotificationCenter
96+
*/
97+
export function createNotificationCenter(options: Options): NotificationCenter;

packages/optimizely-sdk/lib/core/project_config/entities.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,20 @@
1616

1717
export interface FeatureVariable {
1818
type: string;
19+
key: string;
20+
id: string;
21+
defaultValue: string;
1922
}
2023

2124
export interface FeatureFlag {
22-
variables: FeatureVariable[];
25+
rolloutId: string;
26+
key: string;
27+
id: string;
28+
experimentIds: string[],
29+
variables: FeatureVariable[],
30+
variableKeyMap?: {[key: string]: FeatureVariable}
2331
}
2432

33+
export interface FeatureKeyMap {
34+
[key: string]: FeatureFlag
35+
}

0 commit comments

Comments
 (0)