UNPKG

statsig-js

Version:

Statsig JavaScript client SDK for single user environments.

200 lines (199 loc) 9.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LogLevel = exports.INIT_TIMEOUT_DEFAULT_MS = void 0; var DEFAULT_FEATURE_GATE_API = 'https://featuregates.org/v1/'; var DEFAULT_EVENT_LOGGING_API = 'https://events.statsigapi.net/v1/'; exports.INIT_TIMEOUT_DEFAULT_MS = 3000; var LogLevel; (function (LogLevel) { LogLevel[LogLevel["NONE"] = 0] = "NONE"; LogLevel[LogLevel["INFO"] = 1] = "INFO"; LogLevel[LogLevel["DEBUG"] = 2] = "DEBUG"; })(LogLevel = exports.LogLevel || (exports.LogLevel = {})); var StatsigSDKOptions = /** @class */ (function () { function StatsigSDKOptions(options) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z; if (options == null) { options = {}; } var api = (_a = options.api) !== null && _a !== void 0 ? _a : DEFAULT_FEATURE_GATE_API; this.api = api.endsWith('/') ? api : api + '/'; this.disableCurrentPageLogging = (_b = options.disableCurrentPageLogging) !== null && _b !== void 0 ? _b : false; this.environment = (_c = options.environment) !== null && _c !== void 0 ? _c : null; this.loggingIntervalMillis = this.normalizeNumberInput(options.loggingIntervalMillis, { default: 10000, min: 1000, max: 60000, }); this.loggingBufferMaxSize = this.normalizeNumberInput(options.loggingBufferMaxSize, { default: 100, min: 2, max: 500, }); this.disableNetworkKeepalive = (_d = options.disableNetworkKeepalive) !== null && _d !== void 0 ? _d : false; this.overrideStableID = (_e = options.overrideStableID) !== null && _e !== void 0 ? _e : null; this.localMode = (_f = options.localMode) !== null && _f !== void 0 ? _f : false; this.initTimeoutMs = options.initTimeoutMs && options.initTimeoutMs >= 0 ? options.initTimeoutMs : exports.INIT_TIMEOUT_DEFAULT_MS; this.disableErrorLogging = (_g = options.disableErrorLogging) !== null && _g !== void 0 ? _g : false; this.disableAutoMetricsLogging = (_h = options.disableAutoMetricsLogging) !== null && _h !== void 0 ? _h : false; this.initializeValues = (_j = options.initializeValues) !== null && _j !== void 0 ? _j : null; var eventLoggingApi = (_l = (_k = options.eventLoggingApi) !== null && _k !== void 0 ? _k : options.api) !== null && _l !== void 0 ? _l : DEFAULT_EVENT_LOGGING_API; this.eventLoggingApi = eventLoggingApi.endsWith('/') ? eventLoggingApi : eventLoggingApi + '/'; this.prefetchUsers = (_m = options.prefetchUsers) !== null && _m !== void 0 ? _m : []; this.disableLocalStorage = (_o = options.disableLocalStorage) !== null && _o !== void 0 ? _o : false; this.initCompletionCallback = (_p = options.initCompletionCallback) !== null && _p !== void 0 ? _p : null; this.updateCompletionCallback = (_q = options.updateUserCompletionCallback) !== null && _q !== void 0 ? _q : null; this.disableDiagnosticsLogging = (_r = options.disableDiagnosticsLogging) !== null && _r !== void 0 ? _r : false; this.logLevel = (_s = options === null || options === void 0 ? void 0 : options.logLevel) !== null && _s !== void 0 ? _s : LogLevel.NONE; this.ignoreWindowUndefined = (_t = options === null || options === void 0 ? void 0 : options.ignoreWindowUndefined) !== null && _t !== void 0 ? _t : false; this.fetchMode = (_u = options.fetchMode) !== null && _u !== void 0 ? _u : 'network-only'; this.disableLocalOverrides = (_v = options === null || options === void 0 ? void 0 : options.disableLocalOverrides) !== null && _v !== void 0 ? _v : false; this.gateEvaluationCallback = (_w = options === null || options === void 0 ? void 0 : options.gateEvaluationCallback) !== null && _w !== void 0 ? _w : null; this.userPersistentStorage = (_x = options === null || options === void 0 ? void 0 : options.userPersistentStorage) !== null && _x !== void 0 ? _x : null; this.disableAllLogging = (_y = options.disableAllLogging) !== null && _y !== void 0 ? _y : false; this.setLoggingCopy(options); this.disableHashing = (_z = options.disableHashing) !== null && _z !== void 0 ? _z : false; } StatsigSDKOptions.prototype.setLoggingCopy = function (options) { if (options == null || this.loggingCopy != null) { return; } var loggingCopy = {}; Object.entries(options).forEach(function (_a) { var _b, _c; var option = _a[0], value = _a[1]; var valueType = typeof value; switch (valueType) { case 'number': case 'bigint': case 'boolean': loggingCopy[String(option)] = value; break; case 'string': if (value.length < 50) { loggingCopy[String(option)] = value; } else { loggingCopy[String(option)] = 'set'; } break; case 'object': if (option === 'environment') { loggingCopy['environment'] = value; } else if (option === 'prefetchUsers') { loggingCopy['prefetchUsers'] = ((_c = (_b = options.prefetchUsers) === null || _b === void 0 ? void 0 : _b.length) !== null && _c !== void 0 ? _c : 0) > 0; } else { loggingCopy[String(option)] = value != null ? 'set' : 'unset'; } break; case 'function': if (option === 'userPersistentStorage') { loggingCopy['userPersistentStorage'] = value != null ? 'set' : 'unset'; } } }); this.loggingCopy = loggingCopy; }; StatsigSDKOptions.prototype.getLoggingCopy = function () { return this.loggingCopy; }; StatsigSDKOptions.prototype.getApi = function () { return this.api; }; StatsigSDKOptions.prototype.getEnvironment = function () { return this.environment; }; StatsigSDKOptions.prototype.getDisableCurrentPageLogging = function () { return this.disableCurrentPageLogging; }; StatsigSDKOptions.prototype.getLoggingIntervalMillis = function () { return this.loggingIntervalMillis; }; StatsigSDKOptions.prototype.getLoggingBufferMaxSize = function () { return this.loggingBufferMaxSize; }; StatsigSDKOptions.prototype.getDisableNetworkKeepalive = function () { return this.disableNetworkKeepalive; }; StatsigSDKOptions.prototype.getOverrideStableID = function () { return this.overrideStableID; }; StatsigSDKOptions.prototype.getLocalModeEnabled = function () { return this.localMode; }; StatsigSDKOptions.prototype.getInitTimeoutMs = function () { return this.initTimeoutMs; }; StatsigSDKOptions.prototype.getDisableErrorLogging = function () { return this.disableErrorLogging; }; StatsigSDKOptions.prototype.getDisableAutoMetricsLogging = function () { return this.disableAutoMetricsLogging; }; StatsigSDKOptions.prototype.getInitializeValues = function () { return this.initializeValues; }; StatsigSDKOptions.prototype.getEventLoggingApi = function () { return this.eventLoggingApi; }; StatsigSDKOptions.prototype.getPrefetchUsers = function () { return this.prefetchUsers; }; StatsigSDKOptions.prototype.getDisableLocalStorage = function () { return this.disableLocalStorage; }; StatsigSDKOptions.prototype.getInitCompletionCallback = function () { return this.initCompletionCallback; }; StatsigSDKOptions.prototype.getUpdateUserCompletionCallback = function () { return this.updateCompletionCallback; }; StatsigSDKOptions.prototype.getDisableDiagnosticsLogging = function () { return this.disableDiagnosticsLogging; }; StatsigSDKOptions.prototype.getLogLevel = function () { return this.logLevel; }; StatsigSDKOptions.prototype.getIgnoreWindowUndefined = function () { return this.ignoreWindowUndefined; }; StatsigSDKOptions.prototype.getFetchMode = function () { return this.fetchMode; }; StatsigSDKOptions.prototype.getDisableLocalOverrides = function () { return this.disableLocalOverrides; }; StatsigSDKOptions.prototype.getGateEvaluationCallback = function () { return this.gateEvaluationCallback; }; StatsigSDKOptions.prototype.getUserPersistentStorage = function () { return this.userPersistentStorage; }; StatsigSDKOptions.prototype.getDisableHashing = function () { return this.disableHashing; }; StatsigSDKOptions.prototype.isAllLoggingDisabled = function () { return this.disableAllLogging; }; StatsigSDKOptions.prototype.reenableAllLogging = function () { this.disableAllLogging = false; }; StatsigSDKOptions.prototype.normalizeNumberInput = function (input, bounds) { if (input == null) { return bounds.default; } return Math.max(Math.min(input, bounds.max), bounds.min); }; return StatsigSDKOptions; }()); exports.default = StatsigSDKOptions;