Skip to content

Commit 7fdb06a

Browse files
yavoronauzair-folio3
andauthored
feat: Create index.d.ts file for decision_service (#568)
* Create index.d.ts file * Define DecisionService and createDecisionService interfaces * Clean up * index.d.ts added for project_config * Fix imports * Fix imports to avoid anti-pattern * Add comments and clean up exports * Add one more comment * Add missing attribute Co-authored-by: uzair-folio3 <unadeem@folio3.com>
1 parent eaf61d0 commit 7fdb06a

File tree

3 files changed

+187
-61
lines changed

3 files changed

+187
-61
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* Copyright 2020, Optimizely
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
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';
20+
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+
interface DecisionService {
29+
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;
66+
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;
75+
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;
84+
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+
}
95+
96+
interface Options {
97+
userProfileService: import('../../shared_types').UserProfileService | null;
98+
logger: LogHandler;
99+
UNSTABLE_conditionEvaluators: unknown;
100+
}
101+
102+
interface Decision {
103+
experiment: import('../../shared_types').Experiment | null;
104+
variation: import('../../shared_types').Variation | null;
105+
decisionSource: string;
106+
}
107+
}

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

Lines changed: 41 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -55,56 +55,81 @@ declare module '@optimizely/optimizely-sdk' {
5555
// TODO[OASIS-6649]: Don't use object type
5656
// eslint-disable-next-line @typescript-eslint/ban-types
5757
jsonSchemaValidator?: object;
58-
userProfileService?: UserProfileService | null;
58+
userProfileService?: import('./shared_types').UserProfileService | null;
5959
eventBatchSize?: number;
6060
eventFlushInterval?: number;
6161
sdkKey?: string;
6262
}
6363

6464
export interface Client {
6565
notificationCenter: NotificationCenter;
66-
activate(experimentKey: string, userId: string, attributes?: UserAttributes): string | null;
67-
track(eventKey: string, userId: string, attributes?: UserAttributes, eventTags?: EventTags): void;
68-
getVariation(experimentKey: string, userId: string, attributes?: UserAttributes): string | null;
66+
activate(
67+
experimentKey: string,
68+
userId: string,
69+
attributes?: import('./shared_types').UserAttributes
70+
): string | null;
71+
track(
72+
eventKey: string,
73+
userId: string,
74+
attributes?: import('./shared_types').UserAttributes,
75+
eventTags?: EventTags
76+
): void;
77+
getVariation(
78+
experimentKey: string,
79+
userId: string,
80+
attributes?: import('./shared_types').UserAttributes
81+
): string | null;
6982
setForcedVariation(experimentKey: string, userId: string, variationKey: string | null): boolean;
7083
getForcedVariation(experimentKey: string, userId: string): string | null;
71-
isFeatureEnabled(featureKey: string, userId: string, attributes?: UserAttributes): boolean;
72-
getEnabledFeatures(userId: string, attributes?: UserAttributes): string[];
73-
getFeatureVariable(featureKey: string, variableKey: string, userId: string, attributes?: UserAttributes): unknown
84+
isFeatureEnabled(
85+
featureKey: string,
86+
userId: string,
87+
attributes?: import('./shared_types').UserAttributes
88+
): boolean;
89+
getEnabledFeatures(
90+
userId: string,
91+
attributes?: import('./shared_types').UserAttributes
92+
): string[];
93+
getFeatureVariable(
94+
featureKey: string,
95+
variableKey: string,
96+
userId: string,
97+
attributes?: import('./shared_types').UserAttributes
98+
): unknown;
7499
getFeatureVariableBoolean(
75100
featureKey: string,
76101
variableKey: string,
77102
userId: string,
78-
attributes?: UserAttributes
103+
attributes?: import('./shared_types').UserAttributes
79104
): boolean | null;
80105
getFeatureVariableDouble(
81106
featureKey: string,
82107
variableKey: string,
83108
userId: string,
84-
attributes?: UserAttributes
109+
attributes?: import('./shared_types').UserAttributes
85110
): number | null;
86111
getFeatureVariableInteger(
87112
featureKey: string,
88113
variableKey: string,
89114
userId: string,
90-
attributes?: UserAttributes
115+
attributes?: import('./shared_types').UserAttributes
91116
): number | null;
92117
getFeatureVariableString(
93118
featureKey: string,
94119
variableKey: string,
95120
userId: string,
96-
attributes?: UserAttributes
121+
attributes?: import('./shared_types').UserAttributes
97122
): string | null;
98123
getFeatureVariableJSON(
99124
featureKey: string,
100125
variableKey: string,
101126
userId: string,
102-
attributes?: UserAttributes
127+
attributes?: import('./shared_types').UserAttributes
103128
): unknown;
104129
getAllFeatureVariables(
105130
featureKey: string,
106131
userId: string,
107-
attributes?: UserAttributes
132+
attributes?: import('./shared_types').UserAttributes
108133
): { [variableKey: string]: unknown };
109134
getOptimizelyConfig(): OptimizelyConfig | null;
110135
onReady(options?: { timeout?: number }): Promise<{ success: boolean; reason?: string }>;
@@ -135,11 +160,6 @@ declare module '@optimizely/optimizely-sdk' {
135160
dispatchEvent: (event: Event, callback: () => void) => void;
136161
}
137162

138-
export interface UserProfileService {
139-
lookup: (userId: string) => UserProfile;
140-
save: (profile: UserProfile) => void;
141-
}
142-
143163
// NotificationCenter-related types
144164
export interface NotificationCenter {
145165
addNotificationListener<T extends ListenerPayload>(
@@ -155,21 +175,15 @@ declare module '@optimizely/optimizely-sdk' {
155175

156176
export interface ListenerPayload {
157177
userId: string;
158-
attributes: UserAttributes;
178+
attributes: import('./shared_types').UserAttributes;
159179
}
160180

161181
export interface ActivateListenerPayload extends ListenerPayload {
162-
experiment: Experiment;
163-
variation: Variation;
182+
experiment: import('./shared_types').Experiment;
183+
variation: import('./shared_types').Variation;
164184
logEvent: Event;
165185
}
166186

167-
export type UserAttributes = {
168-
// TODO[OASIS-6649]: Don't use any type
169-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
170-
[name: string]: any;
171-
};
172-
173187
export type EventTags = {
174188
[key: string]: string | number | boolean;
175189
};
@@ -180,37 +194,6 @@ declare module '@optimizely/optimizely-sdk' {
180194
logEvent: Event;
181195
}
182196

183-
interface Experiment {
184-
id: string;
185-
key: string;
186-
status: string;
187-
layerId: string;
188-
variations: Variation[];
189-
trafficAllocation: Array<{
190-
entityId: string;
191-
endOfRange: number;
192-
}>;
193-
audienceIds: string[];
194-
// TODO[OASIS-6649]: Don't use object type
195-
// eslint-disable-next-line @typescript-eslint/ban-types
196-
forcedVariations: object;
197-
}
198-
199-
interface Variation {
200-
id: string;
201-
key: string;
202-
}
203-
204-
// Information about past bucketing decisions for a user.
205-
export interface UserProfile {
206-
user_id: string;
207-
experiment_bucket_map: {
208-
[experiment_id: string]: {
209-
variation_id: string;
210-
};
211-
};
212-
}
213-
214197
/**
215198
* Optimizely Config Entities
216199
*/
Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,41 @@
11
export type UserAttributes = {
2-
// TODO[OASIS-6649]: Don't use any type
3-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4-
[name: string]: any;
2+
// TODO[OASIS-6649]: Don't use any type
3+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4+
[name: string]: any;
5+
}
6+
7+
export interface Variation {
8+
id: string;
9+
key: string;
10+
}
11+
12+
export interface Experiment {
13+
id: string;
14+
key: string;
15+
status: string;
16+
layerId: string;
17+
variations: Variation[];
18+
trafficAllocation: Array<{
19+
entityId: string;
20+
endOfRange: number;
21+
}>;
22+
audienceIds: string[];
23+
// TODO[OASIS-6649]: Don't use object type
24+
// eslint-disable-next-line @typescript-eslint/ban-types
25+
forcedVariations: object;
26+
}
27+
28+
// Information about past bucketing decisions for a user.
29+
export interface UserProfile {
30+
user_id: string;
31+
experiment_bucket_map: {
32+
[experiment_id: string]: {
33+
variation_id: string;
34+
};
35+
};
36+
}
37+
38+
export interface UserProfileService {
39+
lookup(userId: string): UserProfile;
40+
save(profile: UserProfile): void;
541
}

0 commit comments

Comments
 (0)