@optimizely/optimizely-sdk
Version:
JavaScript SDK for Optimizely Feature Experimentation, Optimizely Full Stack (legacy), and Optimizely Rollouts
409 lines (408 loc) • 13.8 kB
TypeScript
/**
* Copyright 2020-2024, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* This file contains the shared type definitions collected from the SDK.
* These shared type definitions include ones that will be referenced by external consumers via export_types.ts.
*/
import { LoggerFacade } from './logging/logger';
import { NotificationCenter } from './notification_center';
import { IOptimizelyUserContext as OptimizelyUserContext } from './optimizely_user_context';
import { RequestHandler } from './utils/http_request_handler/http';
import { OptimizelySegmentOption } from './odp/segment_manager/optimizely_segment_option';
import { OdpSegmentApiManager } from './odp/segment_manager/odp_segment_api_manager';
import { OdpSegmentManager } from './odp/segment_manager/odp_segment_manager';
import { DefaultOdpEventApiManager } from './odp/event_manager/odp_event_api_manager';
import { OdpEventManager } from './odp/event_manager/odp_event_manager';
import { OdpManager } from './odp/odp_manager';
import { OpaqueConfigManager } from './project_config/config_manager_factory';
import { OpaqueLogger } from './logging/logger_factory';
import { OpaqueErrorNotifier } from './error/error_notifier_factory';
import { OpaqueEventProcessor } from './event_processor/event_processor_factory';
import { OpaqueOdpManager } from './odp/odp_manager_factory';
import { OpaqueVuidManager } from './vuid/vuid_manager_factory';
export { EventDispatcher } from './event_processor/event_dispatcher/event_dispatcher';
export { EventProcessor } from './event_processor/event_processor';
export { NotificationCenter } from './notification_center';
export { VuidManager } from './vuid/vuid_manager';
export { OpaqueLogger } from './logging/logger_factory';
export { OpaqueErrorNotifier } from './error/error_notifier_factory';
export interface BucketerParams {
experimentId: string;
experimentKey: string;
userId: string;
trafficAllocationConfig: TrafficAllocation[];
experimentKeyMap: {
[key: string]: Experiment;
};
experimentIdMap: {
[id: string]: Experiment;
};
groupIdMap: {
[key: string]: Group;
};
variationIdMap: {
[id: string]: Variation;
};
logger?: LoggerFacade;
bucketingId: string;
validateEntity?: boolean;
}
export interface DecisionResponse<T> {
readonly error?: boolean;
readonly result: T;
readonly reasons: [string, ...any[]][];
}
export type UserAttributeValue = string | number | boolean | null | undefined | ExperimentBucketMap;
export type UserAttributes = {
$opt_bucketing_id?: string;
$opt_experiment_bucket_map?: ExperimentBucketMap;
[name: string]: UserAttributeValue;
};
export interface ExperimentBucketMap {
[experiment_id: string]: {
variation_id: string;
};
}
export interface UserProfile {
user_id: string;
experiment_bucket_map: ExperimentBucketMap;
}
export type EventTags = {
revenue?: string | number | null;
value?: string | number | null;
$opt_event_properties?: Record<string, unknown>;
[key: string]: unknown;
};
export interface UserProfileService {
lookup(userId: string): UserProfile;
save(profile: UserProfile): void;
}
export interface UserProfileServiceAsync {
lookup(userId: string): Promise<UserProfile>;
save(profile: UserProfile): Promise<void>;
}
export interface DatafileManagerConfig {
sdkKey: string;
datafile?: string;
}
export interface DatafileOptions {
autoUpdate?: boolean;
updateInterval?: number;
urlTemplate?: string;
datafileAccessToken?: string;
}
export interface ListenerPayload {
userId: string;
attributes?: UserAttributes;
}
export interface Event {
url: string;
httpVerb: 'POST';
params: any;
}
export interface VariationVariable {
id: string;
value: string;
}
export interface Variation {
id: string;
key: string;
featureEnabled?: boolean;
variablesMap: OptimizelyVariablesMap;
variables?: VariationVariable[];
}
export interface ExperimentCore {
id: string;
key: string;
variations: Variation[];
variationKeyMap: {
[key: string]: Variation;
};
audienceConditions: Array<string | string[]>;
audienceIds: string[];
trafficAllocation: TrafficAllocation[];
}
export interface Experiment extends ExperimentCore {
layerId: string;
groupId?: string;
status: string;
forcedVariations?: {
[key: string]: string;
};
isRollout?: boolean;
cmab?: {
trafficAllocation: number;
attributeIds: string[];
};
}
export type HoldoutStatus = 'Draft' | 'Running' | 'Concluded' | 'Archived';
export interface Holdout extends ExperimentCore {
status: HoldoutStatus;
includedFlags: string[];
excludedFlags: string[];
}
export declare function isHoldout(obj: Experiment | Holdout): obj is Holdout;
export declare enum VariableType {
BOOLEAN = "boolean",
DOUBLE = "double",
INTEGER = "integer",
STRING = "string",
JSON = "json"
}
export interface FeatureVariable {
type: VariableType;
key: string;
id: string;
defaultValue: string;
subType?: string;
}
export interface FeatureFlag {
rolloutId: string;
key: string;
id: string;
experimentIds: string[];
variables: FeatureVariable[];
variableKeyMap: {
[key: string]: FeatureVariable;
};
groupId?: string;
}
export type Condition = {
name: string;
type: string;
match?: string;
value: string | number | boolean | null;
};
export interface Audience {
id: string;
name: string;
conditions: unknown[] | string;
}
export interface Integration {
key: string;
host?: string;
publicKey?: string;
pixelUrl?: string;
}
export interface TrafficAllocation {
entityId: string;
endOfRange: number;
}
export interface Group {
id: string;
policy: string;
trafficAllocation: TrafficAllocation[];
experiments: Experiment[];
}
export interface TrafficAllocation {
entityId: string;
endOfRange: number;
}
export interface Group {
id: string;
policy: string;
trafficAllocation: TrafficAllocation[];
experiments: Experiment[];
}
export interface FeatureKeyMap {
[key: string]: FeatureFlag;
}
export interface OnReadyResult {
success: boolean;
reason?: string;
}
export type ObjectWithUnknownProperties = {
[key: string]: unknown;
};
export interface Rollout {
id: string;
experiments: Experiment[];
}
export declare enum OptimizelyDecideOption {
DISABLE_DECISION_EVENT = "DISABLE_DECISION_EVENT",
ENABLED_FLAGS_ONLY = "ENABLED_FLAGS_ONLY",
IGNORE_USER_PROFILE_SERVICE = "IGNORE_USER_PROFILE_SERVICE",
INCLUDE_REASONS = "INCLUDE_REASONS",
EXCLUDE_VARIABLES = "EXCLUDE_VARIABLES",
IGNORE_CMAB_CACHE = "IGNORE_CMAB_CACHE",
RESET_CMAB_CACHE = "RESET_CMAB_CACHE",
INVALIDATE_USER_CMAB_CACHE = "INVALIDATE_USER_CMAB_CACHE"
}
/**
* Optimizely Config Entities
*/
export interface OptimizelyExperiment {
id: string;
key: string;
audiences: string;
variationsMap: {
[variationKey: string]: OptimizelyVariation;
};
}
export type FeatureVariableValue = number | string | boolean | object | null;
export interface OptimizelyVariable {
id: string;
key: string;
type: string;
value: string;
}
export interface Client {
getVuid(): string | undefined;
createUserContext(userId?: string, attributes?: UserAttributes): OptimizelyUserContext;
notificationCenter: NotificationCenter;
activate(experimentKey: string, userId: string, attributes?: UserAttributes): string | null;
track(eventKey: string, userId: string, attributes?: UserAttributes, eventTags?: EventTags): void;
getVariation(experimentKey: string, userId: string, attributes?: UserAttributes): string | null;
setForcedVariation(experimentKey: string, userId: string, variationKey: string | null): boolean;
getForcedVariation(experimentKey: string, userId: string): string | null;
isFeatureEnabled(featureKey: string, userId: string, attributes?: UserAttributes): boolean;
getEnabledFeatures(userId: string, attributes?: UserAttributes): string[];
getFeatureVariable(featureKey: string, variableKey: string, userId: string, attributes?: UserAttributes): FeatureVariableValue;
getFeatureVariableBoolean(featureKey: string, variableKey: string, userId: string, attributes?: UserAttributes): boolean | null;
getFeatureVariableDouble(featureKey: string, variableKey: string, userId: string, attributes?: UserAttributes): number | null;
getFeatureVariableInteger(featureKey: string, variableKey: string, userId: string, attributes?: UserAttributes): number | null;
getFeatureVariableString(featureKey: string, variableKey: string, userId: string, attributes?: UserAttributes): string | null;
getFeatureVariableJSON(featureKey: string, variableKey: string, userId: string, attributes?: UserAttributes): unknown;
getAllFeatureVariables(featureKey: string, userId: string, attributes?: UserAttributes): {
[variableKey: string]: unknown;
} | null;
getOptimizelyConfig(): OptimizelyConfig | null;
onReady(options?: {
timeout?: number;
}): Promise<unknown>;
close(): Promise<unknown>;
sendOdpEvent(action: string, type?: string, identifiers?: Map<string, string>, data?: Map<string, unknown>): void;
isOdpIntegrated(): boolean;
}
export interface ActivateListenerPayload {
[key: string]: any;
}
export interface TrackListenerPayload {
[key: string]: any;
}
/**
* Entry level Config Entities
* For compatibility with the previous declaration file
*/
export interface Config {
projectConfigManager: OpaqueConfigManager;
eventProcessor?: OpaqueEventProcessor;
jsonSchemaValidator?: {
validate(jsonObject: unknown): boolean;
};
logger?: OpaqueLogger;
errorNotifier?: OpaqueErrorNotifier;
userProfileService?: UserProfileService;
userProfileServiceAsync?: UserProfileServiceAsync;
defaultDecideOptions?: OptimizelyDecideOption[];
clientEngine?: string;
clientVersion?: string;
odpManager?: OpaqueOdpManager;
vuidManager?: OpaqueVuidManager;
disposable?: boolean;
}
export type OptimizelyExperimentsMap = {
[experimentKey: string]: OptimizelyExperiment;
};
export type OptimizelyVariablesMap = {
[variableKey: string]: OptimizelyVariable;
};
export type OptimizelyFeaturesMap = {
[featureKey: string]: OptimizelyFeature;
};
export type OptimizelyAttribute = {
id: string;
key: string;
};
export type OptimizelyAudience = {
id: string;
name: string;
conditions: string;
};
export type OptimizelyEvent = {
id: string;
key: string;
experimentIds: string[];
};
export interface OptimizelyFeature {
id: string;
key: string;
experimentRules: OptimizelyExperiment[];
deliveryRules: OptimizelyExperiment[];
variablesMap: OptimizelyVariablesMap;
/**
* @deprecated Use experimentRules and deliveryRules
*/
experimentsMap: OptimizelyExperimentsMap;
}
export interface OptimizelyVariation {
id: string;
key: string;
featureEnabled?: boolean;
variablesMap: OptimizelyVariablesMap;
}
export interface OptimizelyConfig {
environmentKey: string;
sdkKey: string;
revision: string;
/**
* This experimentsMap is for experiments of legacy projects only.
* For flag projects, experiment keys are not guaranteed to be unique
* across multiple flags, so this map may not include all experiments
* when keys conflict.
*/
experimentsMap: OptimizelyExperimentsMap;
featuresMap: OptimizelyFeaturesMap;
attributes: OptimizelyAttribute[];
audiences: OptimizelyAudience[];
events: OptimizelyEvent[];
getDatafile(): string;
}
export { OptimizelyUserContext };
export interface OptimizelyDecision {
variationKey: string | null;
enabled: boolean;
variables: {
[variableKey: string]: unknown;
};
ruleKey: string | null;
flagKey: string;
userContext: OptimizelyUserContext;
reasons: string[];
}
export interface DatafileUpdate {
datafile: string;
}
export interface DatafileUpdateListener {
(datafileUpdate: DatafileUpdate): void;
}
interface Managed {
start(): void;
stop(): Promise<unknown>;
}
export interface DatafileManager extends Managed {
get: () => string;
on(eventName: string, listener: DatafileUpdateListener): () => void;
onReady: () => Promise<void>;
}
export interface OptimizelyDecisionContext {
flagKey: string;
ruleKey?: string;
}
export interface OptimizelyForcedDecision {
variationKey: string;
}
export { RequestHandler, OptimizelySegmentOption, OdpSegmentApiManager, OdpSegmentManager, DefaultOdpEventApiManager, OdpEventManager, OdpManager, };