UNPKG

statsig-js

Version:

Statsig JavaScript client SDK for single user environments.

136 lines (135 loc) 5.93 kB
import DynamicConfig from './DynamicConfig'; import Layer, { LogParameterFunction } from './Layer'; import { IHasStatsigInternal, StatsigOverrides } from './StatsigClient'; import { StatsigUser } from './StatsigUser'; import { UserCacheKey } from './utils/Hashing'; import { EvaluationReason } from './utils/EvaluationReason'; export declare type EvaluationDetails = { time: number; reason: EvaluationReason; }; declare type APIFeatureGate = { name: string; value: boolean; rule_id: string; group_name?: string; id_type?: string; secondary_exposures: []; }; export declare type StoreGateFetchResult = { gate: APIFeatureGate; evaluationDetails: EvaluationDetails; }; declare type APIDynamicConfig = { name: string; value: { [key: string]: unknown; }; rule_id: string; group_name?: string; id_type?: string; secondary_exposures: []; is_device_based?: boolean; is_user_in_experiment?: boolean; is_experiment_active?: boolean; allocated_experiment_name: string | null; undelegated_secondary_exposures?: []; explicit_parameters?: string[]; }; declare type APIInitializeData = { dynamic_configs: Record<string, APIDynamicConfig | undefined>; feature_gates: Record<string, APIFeatureGate | undefined>; layer_configs: Record<string, APIDynamicConfig | undefined>; has_updates?: boolean; time: number; hash_used?: 'djb2' | 'sha256' | 'none'; derived_fields?: Record<string, string>; }; export declare type APIInitializeDataWithPrefetchedUsers = APIInitializeData & { prefetched_user_values?: Record<string, APIInitializeData>; }; export declare type UserCacheValues = APIInitializeDataWithPrefetchedUsers & { sticky_experiments: Record<string, APIDynamicConfig | undefined>; evaluation_time?: number; user_hash?: string; stableIDUsed?: string; }; export default class StatsigStore { private sdkInternal; private overrides; private loaded; private values; private userValues; private stickyDeviceExperiments; private userCacheKey; private reason; private userPersistentStorageAdapter; private userPersistentStorageData; constructor(sdkInternal: IHasStatsigInternal, initializeValues: Record<string, unknown> | null); load(): void; loadAsync(): Promise<void>; updateUser(isUserPrefetched: boolean): number | null; getInitializeResponseJson(): string; private loadFromAsyncStorage; bootstrap(initializeValues: Record<string, unknown>): void; private loadFromLocalStorage; private partialLoadFromPersistentStorageAdapter; private saveStickyExperimentsToPersistentStorageAdapter; isLoaded(): boolean; getLastUpdateTime(user: StatsigUser | null, stableID: string): number | null; getPreviousDerivedFields(user: StatsigUser | null, stableID: string): Record<string, string> | undefined; private parseCachedValues; private getUserValues; private setUserValueFromCache; private removeFromStorage; private loadOverrides; setEvaluationReason(evalReason: EvaluationReason): void; save(user: StatsigUser | null, response: Record<string, unknown>, prefetchUsers?: Record<string, StatsigUser>): Promise<void>; /** * Persists the init values to storage, but DOES NOT update the state of the store. */ saveWithoutUpdatingClientState(user: StatsigUser | null, response: Record<string, unknown>, prefetchUsers?: Record<string, StatsigUser>): Promise<void>; getDeltasMergeFunction(mergedValues: Record<string, UserCacheValues>): (user: UserCacheValues, key: UserCacheKey) => UserCacheValues; saveInitDeltas(user: StatsigUser | null, response: Record<string, unknown>, updateState: boolean, prefetchUsers?: Record<string, StatsigUser>): Promise<void>; private refetchAndSaveValues; private getStableID; /** * Merges the provided init configs into the provided config map, according to the provided merge function */ mergeInitializeResponseIntoUserMap(data: APIInitializeDataWithPrefetchedUsers, configMap: Record<string, UserCacheValues | undefined>, requestedUserCacheKey: UserCacheKey, user: StatsigUser | null, mergeFn: (user: UserCacheValues, key: UserCacheKey) => UserCacheValues, prefetchUsers?: Record<string, StatsigUser>): void; private getDefaultUserCacheValues; private mergeUserCacheValues; /** * Writes the provided values to storage, truncating down to * MAX_USER_VALUE_CACHED number entries. * @returns The truncated entry list */ private writeValuesToStorage; checkGate(gateName: string, ignoreOverrides?: boolean): StoreGateFetchResult; getConfig(configName: string, ignoreOverrides?: boolean): DynamicConfig; getExperiment(expName: string, keepDeviceValue?: boolean, ignoreOverrides?: boolean): DynamicConfig; getLayer(logParameterFunction: LogParameterFunction | null, layerName: string, keepDeviceValue: boolean): Layer; overrideConfig(configName: string, value: Record<string, unknown>): void; overrideLayer(layerName: string, value: Record<string, unknown>): void; overrideGate(gateName: string, value: boolean): void; removeGateOverride(gateName?: string): void; removeConfigOverride(configName?: string): void; removeLayerOverride(layerName?: string): void; getAllOverrides(): StatsigOverrides; private saveOverrides; private getLatestValue; private getPossiblyStickyValue; private createDynamicConfig; private getStickyValue; private attemptToSaveStickyValue; private removeStickyValue; private saveStickyValuesToStorage; getGlobalEvaluationDetails(): EvaluationDetails; private getEvaluationDetails; private resetUserValues; private getHashedSpecName; private convertAPIDataToCacheValues; private setItemToStorage; private makeOnConfigDefaultValueFallback; } export {};