Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 3226575

Browse files
authored
feat: Add typescript types. (optimizely#199)
1 parent 9d249a2 commit 3226575

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/**
2+
* Copyright 2018, 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' {
18+
import enums = require('@optimizely/optimizely-sdk/lib/utils/enums');
19+
20+
export function createInstance(config: Config): Client;
21+
22+
// The options object given to Optimizely.createInstance.
23+
export interface Config {
24+
datafile: object;
25+
errorHandler?: object;
26+
eventDispatcher?: object;
27+
logger?: object;
28+
logLevel?: enums.LOG_LEVEL.DEBUG | enums.LOG_LEVEL.ERROR | enums.LOG_LEVEL.INFO | enums.LOG_LEVEL.NOTSET | enums.LOG_LEVEL.WARNING;
29+
skipJSONValidation?: boolean;
30+
jsonSchemaValidator?: object;
31+
userProfileService?: UserProfileService | null;
32+
}
33+
34+
export interface Client {
35+
notificationCenter: NotificationCenter;
36+
activate(experimentKey: string, userId: string, attributes?: UserAttributes): string | null;
37+
track(eventKey: string, userId: string, attributes?: UserAttributes, eventTags?: EventTags): void;
38+
getVariation(experimentKey: string, userId: string, attributes?: UserAttributes): string | null;
39+
setForcedVariation(experimentKey: string, userId: string, variationKey: string | null): boolean;
40+
getForcedVariation(experimentKey: string, userId: string): string | null;
41+
isFeatureEnabled(featureKey: string, userId: string, attributes?: UserAttributes): boolean;
42+
getEnabledFeatures(userId: string, attributes?: UserAttributes): string[];
43+
getFeatureVariableBoolean(featureKey: string, variableKey: string, userId: string, attributes?: UserAttributes): boolean | null;
44+
getFeatureVariableDouble(featureKey: string, variableKey: string, userId: string, attributes?: UserAttributes): number | null;
45+
getFeatureVariableInteger(featureKey: string, variableKey: string, userId: string, attributes?: UserAttributes): number | null;
46+
getFeatureVariableString(featureKey: string, variableKey: string, userId: string, attributes?: UserAttributes): string | null;
47+
}
48+
49+
// An event to be submitted to Optimizely, enabling tracking the reach and impact of
50+
// tests and feature rollouts.
51+
export interface Event {
52+
// URL to which to send the HTTP request.
53+
url: string,
54+
// HTTP method with which to send the event.
55+
httpVerb: 'POST',
56+
// Value to send in the request body, JSON-serialized.
57+
params: any,
58+
}
59+
60+
export interface EventDispatcher {
61+
/**
62+
* @param event
63+
* Event being submitted for eventual dispatch.
64+
* @param callback
65+
* After the event has at least been queued for dispatch, call this function to return
66+
* control back to the Client.
67+
*/
68+
dispatchEvent: (event: Event, callback: () => void) => void,
69+
}
70+
71+
export interface UserProfileService {
72+
lookup: (userId: string) => UserProfile,
73+
save: (profile: UserProfile) => void,
74+
}
75+
76+
// NotificationCenter-related types
77+
export interface NotificationCenter {
78+
addNotificationListener<T extends ListenerPayload>(notificationType: string, callback: NotificationListener<T>): number;
79+
removeNotificationListener(listenerId: number): boolean;
80+
clearAllNotificationListeners(): void;
81+
clearNotificationListeners(notificationType: enums.NOTIFICATION_TYPES): void;
82+
}
83+
84+
export type NotificationListener<T extends ListenerPayload> = (notificationData: T) => void;
85+
86+
export interface ListenerPayload {
87+
userId: string;
88+
attributes: UserAttributes;
89+
}
90+
91+
export interface ActivateListenerPayload extends ListenerPayload {
92+
experiment: Experiment;
93+
variation: Variation;
94+
logEvent: Event;
95+
}
96+
97+
export type UserAttributes = {
98+
[name: string]: string
99+
};
100+
101+
export type EventTags = {
102+
[key: string]: string | number | boolean,
103+
};
104+
105+
export interface TrackListenerPayload extends ListenerPayload {
106+
eventKey: string;
107+
eventTags: EventTags;
108+
logEvent: Event;
109+
}
110+
111+
interface Experiment {
112+
id: string,
113+
key: string,
114+
status: string,
115+
layerId: string,
116+
variations: Variation[],
117+
trafficAllocation: Array<{
118+
entityId: string,
119+
endOfRange: number,
120+
}>,
121+
audienceIds: string[],
122+
forcedVariations: object,
123+
}
124+
125+
interface Variation {
126+
id: string,
127+
key: string,
128+
}
129+
130+
export interface Logger {
131+
log: (logLevel: enums.LOG_LEVEL, message: string) => void,
132+
}
133+
134+
// Information about past bucketing decisions for a user.
135+
export interface UserProfile {
136+
user_id: string,
137+
experiment_bucket_map: {
138+
[experiment_id: string]: {
139+
variation_id: string,
140+
},
141+
},
142+
}
143+
}
144+
145+
declare module '@optimizely/optimizely-sdk/lib/utils/enums'{
146+
export enum LOG_LEVEL{
147+
NOTSET = 0,
148+
DEBUG = 1,
149+
INFO = 2,
150+
WARNING = 3,
151+
ERROR = 4,
152+
}
153+
export enum NOTIFICATION_TYPES {
154+
ACTIVATE = 'ACTIVATE:experiment, user_id, attributes, variation, events',
155+
TRACK = 'TRACK:event_key, user_id, attributes, event_tags, event',
156+
}
157+
}
158+
159+
declare module '@optimizely/optimizely-sdk/lib/plugins/event_dispatcher/index.node.js' {
160+
161+
}
162+
163+
declare module '@optimizely/optimizely-sdk/lib/utils/json_schema_validator' {
164+
165+
}
166+
167+
declare module '@optimizely/optimizely-sdk/lib/plugins/error_handler' {
168+
}
169+
170+
declare module '@optimizely/optimizely-sdk/lib/plugins/logger' {
171+
import * as Optimizely from '@optimizely/optimizely-sdk';
172+
import * as enums from '@optimizely/optimizely-sdk/lib/utils/enums';
173+
174+
export interface Config {
175+
logLevel?: enums.LOG_LEVEL,
176+
logToConsole?: boolean,
177+
prefix?: string,
178+
}
179+
export function createLogger(config: Config): Optimizely.Logger;
180+
export function createNoOpLogger(): Optimizely.Logger;
181+
}

packages/optimizely-sdk/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "JavaScript SDK for Optimizely X Full Stack",
55
"main": "lib/index.node.js",
66
"browser": "lib/index.browser.js",
7+
"typings": "lib/index.d.ts",
78
"scripts": {
89
"test": "mocha ./lib/*.tests.js ./lib/**/*.tests.js ./lib/**/**/*tests.js --recursive",
910
"test-xbrowser": "karma start karma.bs.conf.js --single-run",

0 commit comments

Comments
 (0)