UNPKG

statsig-js

Version:

Statsig JavaScript client SDK for single user environments.

168 lines (167 loc) 9.32 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var uuid_1 = require("uuid"); var Constants_1 = require("./utils/Constants"); var StatsigAsyncStorage_1 = __importDefault(require("./utils/StatsigAsyncStorage")); var StatsigLocalStorage_1 = __importDefault(require("./utils/StatsigLocalStorage")); var SDKVersion_1 = require("./SDKVersion"); var Identity = /** @class */ (function () { function Identity(user, overrideStableID, reactNativeUUID) { var _a; this.platform = null; this.nativeModules = null; this.sdkType = 'js-client'; this.reactNativeUUID = reactNativeUUID; this.user = user; this.sdkVersion = SDKVersion_1.version; this.statsigMetadata = { sdkType: this.sdkType, sdkVersion: this.sdkVersion, }; var stableID = overrideStableID; if (!StatsigAsyncStorage_1.default.asyncStorage) { stableID = (_a = stableID !== null && stableID !== void 0 ? stableID : StatsigLocalStorage_1.default.getItem(Constants_1.STATSIG_STABLE_ID_KEY)) !== null && _a !== void 0 ? _a : this.getUUID(); } if (stableID) { this.statsigMetadata.stableID = stableID; } } Identity.prototype.saveStableID = function () { if (this.statsigMetadata.stableID != null) { StatsigLocalStorage_1.default.setItem(Constants_1.STATSIG_STABLE_ID_KEY, this.statsigMetadata.stableID); } }; Identity.prototype.initAsync = function () { return __awaiter(this, void 0, void 0, function () { var stableID; return __generator(this, function (_a) { switch (_a.label) { case 0: stableID = this.statsigMetadata.stableID; if (!!stableID) return [3 /*break*/, 2]; return [4 /*yield*/, StatsigAsyncStorage_1.default.getItemAsync(Constants_1.STATSIG_STABLE_ID_KEY)]; case 1: stableID = _a.sent(); stableID = stableID !== null && stableID !== void 0 ? stableID : this.getUUID(); _a.label = 2; case 2: this.statsigMetadata.stableID = stableID; StatsigAsyncStorage_1.default.setItemAsync(Constants_1.STATSIG_STABLE_ID_KEY, stableID).catch(function () { //noop }); return [2 /*return*/, this]; } }); }); }; Identity.prototype.getSDKType = function () { return this.sdkType; }; Identity.prototype.getSDKVersion = function () { return this.sdkVersion; }; Identity.prototype.getStatsigMetadata = function () { this.statsigMetadata.sdkType = this.sdkType; this.statsigMetadata.sdkVersion = this.sdkVersion; return this.statsigMetadata; }; Identity.prototype.getUser = function () { return this.user; }; Identity.prototype.updateUser = function (user) { this.user = user; }; Identity.prototype.setSDKPackageInfo = function (SDKPackageInfo) { this.sdkType = SDKPackageInfo.sdkType; this.sdkVersion = SDKPackageInfo.sdkVersion; }; Identity.prototype.setPlatform = function (platform) { this.platform = platform; this.updateMetadataFromNativeModules(); }; Identity.prototype.setNativeModules = function (nativeModules) { this.nativeModules = nativeModules; this.updateMetadataFromNativeModules(); }; Identity.prototype.updateMetadataFromNativeModules = function () { var _a, _b, _c, _d, _e, _f, _g; if (this.platform == null || this.nativeModules == null) { return; } if (((_a = this.platform.OS) === null || _a === void 0 ? void 0 : _a.toLocaleLowerCase()) === 'android') { this.statsigMetadata.locale = (_b = this.nativeModules.I18nManager) === null || _b === void 0 ? void 0 : _b.localeIdentifier; } else if (((_c = this.platform.OS) === null || _c === void 0 ? void 0 : _c.toLocaleLowerCase()) === 'ios') { this.statsigMetadata.locale = ((_e = (_d = this.nativeModules.SettingsManager) === null || _d === void 0 ? void 0 : _d.settings) === null || _e === void 0 ? void 0 : _e.AppleLocale) || ((_g = (_f = this.nativeModules.SettingsManager) === null || _f === void 0 ? void 0 : _f.settings) === null || _g === void 0 ? void 0 : _g.AppleLanguages[0]); } }; Identity.prototype.getUUID = function () { var _a, _b; return (_b = (_a = this.reactNativeUUID) === null || _a === void 0 ? void 0 : _a.v4()) !== null && _b !== void 0 ? _b : (0, uuid_1.v4)(); }; Identity.prototype.setRNDeviceInfo = function (deviceInfo) { var _a, _b, _c, _d, _e; this.statsigMetadata.appVersion = (_a = deviceInfo.getVersion()) !== null && _a !== void 0 ? _a : ''; // e.g. 1.0.1 this.statsigMetadata.systemVersion = (_b = deviceInfo.getSystemVersion()) !== null && _b !== void 0 ? _b : ''; // Android: "4.0.3"; iOS: "12.3.1" this.statsigMetadata.systemName = (_c = deviceInfo.getSystemName()) !== null && _c !== void 0 ? _c : ''; // e.g. Android, iOS, iPadOS this.statsigMetadata.deviceModelName = (_d = deviceInfo.getModel()) !== null && _d !== void 0 ? _d : ''; // e.g. Pixel 2, iPhone XS this.statsigMetadata.deviceModel = (_e = deviceInfo.getDeviceId()) !== null && _e !== void 0 ? _e : ''; // e.g. iPhone7,2 }; Identity.prototype.setExpoConstants = function (expoConstants) { var _a, _b; this.statsigMetadata.appVersion = (_b = (_a = expoConstants.nativeAppVersion) !== null && _a !== void 0 ? _a : expoConstants.nativeBuildVersion) !== null && _b !== void 0 ? _b : ''; // e.g. 1.0.1 }; Identity.prototype.setExpoDevice = function (expoDevice) { var _a, _b, _c, _d; this.statsigMetadata.systemVersion = (_a = expoDevice.osVersion) !== null && _a !== void 0 ? _a : ''; // Android: "4.0.3"; iOS: "12.3.1" this.statsigMetadata.systemName = (_b = expoDevice.osName) !== null && _b !== void 0 ? _b : ''; // e.g. Android, iOS, iPadOS this.statsigMetadata.deviceModelName = (_c = expoDevice.modelName) !== null && _c !== void 0 ? _c : ''; // e.g. Pixel 2, iPhone XS this.statsigMetadata.deviceModel = (_d = expoDevice.modelId) !== null && _d !== void 0 ? _d : ''; // e.g. iPhone7,2 }; return Identity; }()); exports.default = Identity;