UNPKG

@optimizely/optimizely-sdk

Version:

JavaScript SDK for Optimizely Feature Experimentation, Optimizely Full Stack (legacy), and Optimizely Rollouts

379 lines (378 loc) 18.9 kB
import { LoggerFacade } from '../logging/logger'; import { Audience, Experiment, FeatureFlag, FeatureVariable, Group, OptimizelyVariation, Rollout, TrafficAllocation, Variation, VariableType, VariationVariable, Integration, FeatureVariableValue, Holdout } from '../shared_types'; import { OdpIntegrationConfig } from '../odp/odp_config'; import { Transformer } from '../utils/type'; import { Platform } from '../platform_support'; interface TryCreatingProjectConfigConfig { datafile: string | object; jsonSchemaValidator?: Transformer<unknown, boolean>; logger?: LoggerFacade; } interface Event { key: string; id: string; experimentIds: string[]; } interface VariableUsageMap { [id: string]: VariationVariable; } export type Region = 'US' | 'EU'; export interface ProjectConfig { region: Region; revision: string; projectId: string; sdkKey: string; environmentKey: string; sendFlagDecisions?: boolean; experimentKeyMap: { [key: string]: Experiment; }; featureKeyMap: { [key: string]: FeatureFlag; }; rollouts: Rollout[]; featureFlags: FeatureFlag[]; experimentIdMap: { [id: string]: Experiment; }; experimentFeatureMap: { [key: string]: string[]; }; experiments: Experiment[]; eventKeyMap: { [key: string]: Event; }; audiences: Audience[]; attributeKeyMap: { [key: string]: { id: string; }; }; attributeIdMap: { [id: string]: { key: string; }; }; variationIdMap: { [id: string]: OptimizelyVariation; }; variationVariableUsageMap: { [id: string]: VariableUsageMap; }; audiencesById: { [id: string]: Audience; }; __datafileStr: string; groupIdMap: { [id: string]: Group; }; groups: Group[]; events: Event[]; attributes: Array<{ id: string; key: string; }>; typedAudiences: Audience[]; rolloutIdMap: { [id: string]: Rollout; }; anonymizeIP?: boolean | null; botFiltering?: boolean; accountId: string; flagRulesMap: { [key: string]: Experiment[]; }; flagVariationsMap: { [key: string]: Variation[]; }; integrations: Integration[]; integrationKeyMap?: { [key: string]: Integration; }; odpIntegrationConfig: OdpIntegrationConfig; holdouts: Holdout[]; holdoutIdMap?: { [id: string]: Holdout; }; globalHoldouts: Holdout[]; includedHoldouts: { [key: string]: Holdout[]; }; excludedHoldouts: { [key: string]: Holdout[]; }; flagHoldoutsMap: { [key: string]: Holdout[]; }; } /** * Creates projectConfig object to be used for quick project property lookup * @param {Object} datafileObj JSON datafile representing the project * @param {string|null} datafileStr JSON string representation of the datafile * @return {ProjectConfig} Object representing project configuration */ export declare const createProjectConfig: (datafileObj?: JSON, datafileStr?: string | null) => ProjectConfig; export declare const getHoldoutsForFlag: (projectConfig: ProjectConfig, flagKey: string) => Holdout[]; /** * Extract all audience segments used in this audience's conditions * @param {Audience} audience Object representing the audience being parsed * @return {string[]} List of all audience segments */ export declare const getAudienceSegments: (audience: Audience) => string[]; /** * Get experiment ID for the provided experiment key * @param {ProjectConfig} projectConfig Object representing project configuration * @param {string} experimentKey Experiment key for which ID is to be determined * @return {string} Experiment ID corresponding to the provided experiment key * @throws If experiment key is not in datafile */ export declare const getExperimentId: (projectConfig: ProjectConfig, experimentKey: string) => string; /** * Get layer ID for the provided experiment key * @param {ProjectConfig} projectConfig Object representing project configuration * @param {string} experimentId Experiment ID for which layer ID is to be determined * @return {string} Layer ID corresponding to the provided experiment key * @throws If experiment key is not in datafile */ export declare const getLayerId: (projectConfig: ProjectConfig, experimentId: string) => string; /** * Get attribute ID for the provided attribute key * @param {ProjectConfig} projectConfig Object representing project configuration * @param {string} attributeKey Attribute key for which ID is to be determined * @param {LogHandler} logger * @return {string|null} Attribute ID corresponding to the provided attribute key. Attribute key if it is a reserved attribute. */ export declare const getAttributeId: (projectConfig: ProjectConfig, attributeKey: string, logger?: LoggerFacade) => string | null; /** * Get event ID for the provided * @param {ProjectConfig} projectConfig Object representing project configuration * @param {string} eventKey Event key for which ID is to be determined * @return {string|null} Event ID corresponding to the provided event key */ export declare const getEventId: (projectConfig: ProjectConfig, eventKey: string) => string | null; /** * Get experiment status for the provided experiment key * @param {ProjectConfig} projectConfig Object representing project configuration * @param {string} experimentKey Experiment key for which status is to be determined * @return {string} Experiment status corresponding to the provided experiment key * @throws If experiment key is not in datafile */ export declare const getExperimentStatus: (projectConfig: ProjectConfig, experimentKey: string) => string; /** * Returns whether experiment has a status of 'Running' * @param {ProjectConfig} projectConfig Object representing project configuration * @param {string} experimentKey Experiment key for which status is to be compared with 'Running' * @return {boolean} True if experiment status is set to 'Running', false otherwise */ export declare const isActive: (projectConfig: ProjectConfig, experimentKey: string) => boolean; /** * Determine for given experiment if event is running, which determines whether should be dispatched or not * @param {ProjectConfig} configObj Object representing project configuration * @param {string} experimentKey Experiment key for which the status is to be determined * @return {boolean} True if the experiment is running * False if the experiment is not running * */ export declare const isRunning: (projectConfig: ProjectConfig, experimentKey: string) => boolean; /** * Get audience conditions for the experiment * @param {ProjectConfig} projectConfig Object representing project configuration * @param {string} experimentId Experiment id for which audience conditions are to be determined * @return {Array<string|string[]>} Audience conditions for the experiment - can be an array of audience IDs, or a * nested array of conditions * Examples: ["5", "6"], ["and", ["or", "1", "2"], "3"] * @throws If experiment key is not in datafile */ export declare const getExperimentAudienceConditions: (projectConfig: ProjectConfig, experimentId: string) => Array<string | string[]>; /** * Get variation key given experiment key and variation ID * @param {ProjectConfig} projectConfig Object representing project configuration * @param {string} variationId ID of the variation * @return {string|null} Variation key or null if the variation ID is not found */ export declare const getVariationKeyFromId: (projectConfig: ProjectConfig, variationId: string) => string | null; /** * Get variation given variation ID * @param {ProjectConfig} projectConfig Object representing project configuration * @param {string} variationId ID of the variation * @return {Variation|null} Variation or null if the variation ID is not found */ export declare const getVariationFromId: (projectConfig: ProjectConfig, variationId: string) => Variation | null; /** * Get the variation ID given the experiment key and variation key * @param {ProjectConfig} projectConfig Object representing project configuration * @param {string} experimentKey Key of the experiment the variation belongs to * @param {string} variationKey The variation key * @return {string|null} Variation ID or null */ export declare const getVariationIdFromExperimentAndVariationKey: (projectConfig: ProjectConfig, experimentKey: string, variationKey: string) => string | null; /** * Get experiment from provided experiment key * @param {ProjectConfig} projectConfig Object representing project configuration * @param {string} experimentKey Event key for which experiment IDs are to be retrieved * @return {Experiment} Experiment * @throws If experiment key is not in datafile */ export declare const getExperimentFromKey: (projectConfig: ProjectConfig, experimentKey: string) => Experiment; /** * Given an experiment id, returns the traffic allocation within that experiment * @param {ProjectConfig} projectConfig Object representing project configuration * @param {string} experimentId Id representing the experiment * @return {TrafficAllocation[]} Traffic allocation for the experiment * @throws If experiment key is not in datafile */ export declare const getTrafficAllocation: (projectConfig: ProjectConfig, experimentId: string) => TrafficAllocation[]; /** * Get experiment from provided experiment id. Log an error if no experiment * exists in the project config with the given ID. * @param {ProjectConfig} projectConfig Object representing project configuration * @param {string} experimentId ID of desired experiment object * @param {LogHandler} logger * @return {Experiment|null} Experiment object or null */ export declare const getExperimentFromId: (projectConfig: ProjectConfig, experimentId: string, logger?: LoggerFacade) => Experiment | null; /** * Returns flag variation for specified flagKey and variationKey * @param {flagKey} string * @param {variationKey} string * @return {Variation|null} */ export declare const getFlagVariationByKey: (projectConfig: ProjectConfig, flagKey: string, variationKey: string) => Variation | null; /** * Get feature from provided feature key. Log an error if no feature exists in * the project config with the given key. * @param {ProjectConfig} projectConfig * @param {string} featureKey * @param {LogHandler} logger * @return {FeatureFlag|null} Feature object, or null if no feature with the given * key exists */ export declare const getFeatureFromKey: (projectConfig: ProjectConfig, featureKey: string, logger?: LoggerFacade) => FeatureFlag | null; /** * Get the variable with the given key associated with the feature with the * given key. If the feature key or the variable key are invalid, log an error * message. * @param {ProjectConfig} projectConfig * @param {string} featureKey * @param {string} variableKey * @param {LogHandler} logger * @return {FeatureVariable|null} Variable object, or null one or both of the given * feature and variable keys are invalid */ export declare const getVariableForFeature: (projectConfig: ProjectConfig, featureKey: string, variableKey: string, logger?: LoggerFacade) => FeatureVariable | null; /** * Get the value of the given variable for the given variation. If the given * variable has no value for the given variation, return null. Log an error message if the variation is invalid. If the * variable or variation are invalid, return null. * @param {ProjectConfig} projectConfig * @param {FeatureVariable} variable * @param {Variation} variation * @param {LogHandler} logger * @return {string|null} The value of the given variable for the given * variation, or null if the given variable has no value * for the given variation or if the variation or variable are invalid */ export declare const getVariableValueForVariation: (projectConfig: ProjectConfig, variable: FeatureVariable, variation: Variation, logger?: LoggerFacade) => string | null; /** * Given a variable value in string form, try to cast it to the argument type. * If the type cast succeeds, return the type casted value, otherwise log an * error and return null. * @param {string} variableValue Variable value in string form * @param {string} variableType Type of the variable whose value was passed * in the first argument. Must be one of * FEATURE_VARIABLE_TYPES in * lib/utils/enums/index.js. The return value's * type is determined by this argument (boolean * for BOOLEAN, number for INTEGER or DOUBLE, * and string for STRING). * @param {LogHandler} logger Logger instance * @returns {*} Variable value of the appropriate type, or * null if the type cast failed */ export declare const getTypeCastValue: (variableValue: string, variableType: VariableType, logger?: LoggerFacade) => FeatureVariableValue; /** * Returns an object containing all audiences in the project config. Keys are audience IDs * and values are audience objects. * @param {ProjectConfig} projectConfig * @returns {{ [id: string]: Audience }} */ export declare const getAudiencesById: (projectConfig: ProjectConfig) => { [id: string]: Audience; }; /** * Returns true if an event with the given key exists in the datafile, and false otherwise * @param {ProjectConfig} projectConfig * @param {string} eventKey * @returns {boolean} */ export declare const eventWithKeyExists: (projectConfig: ProjectConfig, eventKey: string) => boolean; /** * Returns true if experiment belongs to any feature, false otherwise. * @param {ProjectConfig} projectConfig * @param {string} experimentId * @returns {boolean} */ export declare const isFeatureExperiment: (projectConfig: ProjectConfig, experimentId: string) => boolean; /** * Returns the JSON string representation of the datafile * @param {ProjectConfig} projectConfig * @returns {string} */ export declare const toDatafile: (projectConfig: ProjectConfig) => string; /** * @typedef {Object} * @property {Object|null} configObj * @property {Error|null} error */ /** * Try to create a project config object from the given datafile and * configuration properties. * Returns a ProjectConfig if successful. * Otherwise, throws an error. * @param {Object} config * @param {Object|string} config.datafile * @param {Object} config.jsonSchemaValidator * @param {Object} config.logger * @returns {Object} ProjectConfig * @throws {Error} */ export declare const tryCreatingProjectConfig: (config: TryCreatingProjectConfigConfig) => ProjectConfig; /** * Get the send flag decisions value * @param {ProjectConfig} projectConfig * @return {boolean} A boolean value that indicates if we should send flag decisions */ export declare const getSendFlagDecisionsValue: (projectConfig: ProjectConfig) => boolean; declare const _default: { createProjectConfig: (datafileObj?: JSON | undefined, datafileStr?: string | null) => ProjectConfig; getExperimentId: (projectConfig: ProjectConfig, experimentKey: string) => string; getLayerId: (projectConfig: ProjectConfig, experimentId: string) => string; getAttributeId: (projectConfig: ProjectConfig, attributeKey: string, logger?: LoggerFacade | undefined) => string | null; getEventId: (projectConfig: ProjectConfig, eventKey: string) => string | null; getExperimentStatus: (projectConfig: ProjectConfig, experimentKey: string) => string; isActive: (projectConfig: ProjectConfig, experimentKey: string) => boolean; isRunning: (projectConfig: ProjectConfig, experimentKey: string) => boolean; getExperimentAudienceConditions: (projectConfig: ProjectConfig, experimentId: string) => (string | string[])[]; getVariationFromId: (projectConfig: ProjectConfig, variationId: string) => Variation | null; getVariationKeyFromId: (projectConfig: ProjectConfig, variationId: string) => string | null; getVariationIdFromExperimentAndVariationKey: (projectConfig: ProjectConfig, experimentKey: string, variationKey: string) => string | null; getExperimentFromKey: (projectConfig: ProjectConfig, experimentKey: string) => Experiment; getExperimentFromId: (projectConfig: ProjectConfig, experimentId: string, logger?: LoggerFacade | undefined) => Experiment | null; getFlagVariationByKey: (projectConfig: ProjectConfig, flagKey: string, variationKey: string) => Variation | null; getFeatureFromKey: (projectConfig: ProjectConfig, featureKey: string, logger?: LoggerFacade | undefined) => FeatureFlag | null; getVariableForFeature: (projectConfig: ProjectConfig, featureKey: string, variableKey: string, logger?: LoggerFacade | undefined) => FeatureVariable | null; getVariableValueForVariation: (projectConfig: ProjectConfig, variable: FeatureVariable, variation: Variation, logger?: LoggerFacade | undefined) => string | null; getTypeCastValue: (variableValue: string, variableType: VariableType, logger?: LoggerFacade | undefined) => FeatureVariableValue; getSendFlagDecisionsValue: (projectConfig: ProjectConfig) => boolean; getAudiencesById: (projectConfig: ProjectConfig) => { [id: string]: Audience; }; getAudienceSegments: (audience: Audience) => string[]; eventWithKeyExists: (projectConfig: ProjectConfig, eventKey: string) => boolean; isFeatureExperiment: (projectConfig: ProjectConfig, experimentId: string) => boolean; toDatafile: (projectConfig: ProjectConfig) => string; tryCreatingProjectConfig: (config: TryCreatingProjectConfigConfig) => ProjectConfig; getTrafficAllocation: (projectConfig: ProjectConfig, experimentId: string) => TrafficAllocation[]; }; export default _default; export declare const __platforms: Platform[];