@statsig/client-core 0.0.1-beta.1 → 0.0.1-beta.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -1
- package/package.json +1 -1
- package/src/$_StatsigGlobal.js +8 -12
- package/src/ClientInterfaces.d.ts +9 -2
- package/src/DataAdapterCore.d.ts +32 -0
- package/src/DataAdapterCore.js +167 -0
- package/src/Diagnostics.js +21 -26
- package/src/ErrorBoundary.d.ts +1 -0
- package/src/ErrorBoundary.js +39 -86
- package/src/EvaluationTypes.d.ts +38 -0
- package/src/EventLogger.d.ts +17 -4
- package/src/EventLogger.js +186 -225
- package/src/Hashing.d.ts +1 -0
- package/src/Hashing.js +23 -4
- package/src/Log.js +15 -34
- package/src/Monitoring.d.ts +1 -2
- package/src/Monitoring.js +65 -27
- package/src/NetworkCore.d.ts +13 -5
- package/src/NetworkCore.js +96 -164
- package/src/OverrideAdapter.d.ts +15 -0
- package/src/OverrideAdapter.js +24 -0
- package/src/SessionID.js +3 -3
- package/src/StableID.js +23 -52
- package/src/StatsigClientBase.d.ts +15 -21
- package/src/StatsigClientBase.js +46 -241
- package/src/StatsigClientEventEmitter.d.ts +28 -6
- package/src/StatsigDataAdapter.d.ts +89 -0
- package/src/StatsigDataAdapter.js +4 -0
- package/src/StatsigEvent.d.ts +14 -16
- package/src/StatsigEvent.js +33 -27
- package/src/StatsigMetadata.js +5 -16
- package/src/StatsigOptionsCommon.d.ts +42 -12
- package/src/StatsigTypes.d.ts +22 -12
- package/src/StatsigTypes.js +23 -16
- package/src/StatsigUser.d.ts +1 -0
- package/src/StatsigUser.js +12 -20
- package/src/StorageProvider.d.ts +7 -2
- package/src/StorageProvider.js +53 -62
- package/src/TypedJsonParse.d.ts +8 -0
- package/src/TypedJsonParse.js +27 -0
- package/src/UUID.js +5 -5
- package/src/UrlOverrides.d.ts +1 -0
- package/src/UrlOverrides.js +15 -0
- package/src/VisibilityChangeObserver.js +18 -24
- package/src/__tests__/MockLocalStorage.js +18 -19
- package/src/index.d.ts +10 -3
- package/src/index.js +16 -16
- package/src/StatsigDataProvider.d.ts +0 -9
- /package/src/{StatsigDataProvider.js → EvaluationTypes.js} +0 -0
package/src/StatsigClientBase.js
CHANGED
|
@@ -1,272 +1,77 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
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;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
39
|
-
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
40
|
-
var m = o[Symbol.asyncIterator], i;
|
|
41
|
-
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
42
|
-
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
43
|
-
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
44
|
-
};
|
|
45
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
46
3
|
exports.StatsigClientBase = exports.DEFAULT_EVAL_OPTIONS = void 0;
|
|
47
4
|
require("./$_StatsigGlobal");
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
5
|
+
const ErrorBoundary_1 = require("./ErrorBoundary");
|
|
6
|
+
const EventLogger_1 = require("./EventLogger");
|
|
7
|
+
const Log_1 = require("./Log");
|
|
8
|
+
const StableID_1 = require("./StableID");
|
|
9
|
+
const StorageProvider_1 = require("./StorageProvider");
|
|
52
10
|
exports.DEFAULT_EVAL_OPTIONS = {
|
|
53
11
|
disableExposureLog: false,
|
|
54
12
|
};
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
var _a, _b;
|
|
13
|
+
class StatsigClientBase {
|
|
14
|
+
constructor(_sdkKey, adapter, network, options) {
|
|
15
|
+
var _a, _b, _c;
|
|
16
|
+
this._sdkKey = _sdkKey;
|
|
58
17
|
this.loadingStatus = 'Uninitialized';
|
|
59
18
|
this._listeners = {};
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
19
|
+
(options === null || options === void 0 ? void 0 : options.disableStorage) && StorageProvider_1.Storage.setDisabled(true);
|
|
20
|
+
(options === null || options === void 0 ? void 0 : options.overrideStableID) &&
|
|
21
|
+
StableID_1.StableID.setOverride(options.overrideStableID, _sdkKey);
|
|
22
|
+
Log_1.Log.level = (_a = options === null || options === void 0 ? void 0 : options.logLevel) !== null && _a !== void 0 ? _a : Log_1.LogLevel.Warn;
|
|
23
|
+
this._overrideAdapter = (_b = options === null || options === void 0 ? void 0 : options.overrideAdapter) !== null && _b !== void 0 ? _b : null;
|
|
24
|
+
this._logger = new EventLogger_1.EventLogger(_sdkKey, this._emit.bind(this), network, options);
|
|
25
|
+
this._errorBoundary = new ErrorBoundary_1.ErrorBoundary(_sdkKey);
|
|
66
26
|
__STATSIG__ = __STATSIG__ !== null && __STATSIG__ !== void 0 ? __STATSIG__ : {};
|
|
67
|
-
|
|
27
|
+
const instances = (_c = __STATSIG__.instances) !== null && _c !== void 0 ? _c : new Set();
|
|
68
28
|
instances.add(this);
|
|
69
29
|
__STATSIG__.instances = instances;
|
|
70
|
-
|
|
71
|
-
this.
|
|
30
|
+
this.dataAdapter = adapter;
|
|
31
|
+
this.dataAdapter.attach(_sdkKey, options);
|
|
32
|
+
}
|
|
33
|
+
updateRuntimeOptions(options) {
|
|
34
|
+
if (options.disableLogging != null) {
|
|
35
|
+
this._logger.setLoggingDisabled(options.disableLogging);
|
|
36
|
+
}
|
|
37
|
+
if (options.disableStorage != null) {
|
|
38
|
+
StorageProvider_1.Storage.setDisabled(options.disableStorage);
|
|
39
|
+
}
|
|
72
40
|
}
|
|
73
|
-
|
|
41
|
+
flush() {
|
|
42
|
+
return this._logger.flush();
|
|
43
|
+
}
|
|
44
|
+
on(event, listener) {
|
|
74
45
|
if (!this._listeners[event]) {
|
|
75
46
|
this._listeners[event] = [];
|
|
76
47
|
}
|
|
77
48
|
this._listeners[event].push(listener);
|
|
78
|
-
}
|
|
79
|
-
|
|
49
|
+
}
|
|
50
|
+
off(event, listener) {
|
|
80
51
|
if (this._listeners[event]) {
|
|
81
|
-
|
|
52
|
+
const index = this._listeners[event].indexOf(listener);
|
|
82
53
|
if (index !== -1) {
|
|
83
54
|
this._listeners[event].splice(index, 1);
|
|
84
55
|
}
|
|
85
56
|
}
|
|
86
|
-
}
|
|
87
|
-
|
|
57
|
+
}
|
|
58
|
+
_emit(data) {
|
|
88
59
|
var _a;
|
|
89
60
|
if (this._listeners[data.event]) {
|
|
90
|
-
this._listeners[data.event].forEach(
|
|
61
|
+
this._listeners[data.event].forEach((listener) => listener(data));
|
|
91
62
|
}
|
|
92
|
-
(_a = this._listeners['*']) === null || _a === void 0 ? void 0 : _a.forEach(
|
|
93
|
-
}
|
|
94
|
-
|
|
63
|
+
(_a = this._listeners['*']) === null || _a === void 0 ? void 0 : _a.forEach((listener) => listener(data));
|
|
64
|
+
}
|
|
65
|
+
_setStatus(newStatus, values) {
|
|
95
66
|
this.loadingStatus = newStatus;
|
|
96
|
-
this.
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
var _a;
|
|
100
|
-
for (var _i = 0, _b = this._dataProviders; _i < _b.length; _i++) {
|
|
101
|
-
var provider = _b[_i];
|
|
102
|
-
var data = (_a = provider.getData) === null || _a === void 0 ? void 0 : _a.call(provider, this._sdkKey, user);
|
|
103
|
-
if (data) {
|
|
104
|
-
return { data: data, source: provider.source };
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
return { data: null, source: 'NoValues' };
|
|
108
|
-
};
|
|
109
|
-
StatsigClientBase.prototype._getDataFromProvidersAsync = function (user) {
|
|
110
|
-
var _a, e_1, _b, _c;
|
|
111
|
-
var _d;
|
|
112
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
113
|
-
var _e, _f, _g, provider, data, e_1_1;
|
|
114
|
-
return __generator(this, function (_h) {
|
|
115
|
-
switch (_h.label) {
|
|
116
|
-
case 0:
|
|
117
|
-
_h.trys.push([0, 6, 7, 12]);
|
|
118
|
-
_e = true, _f = __asyncValues(this._dataProviders);
|
|
119
|
-
_h.label = 1;
|
|
120
|
-
case 1: return [4 /*yield*/, _f.next()];
|
|
121
|
-
case 2:
|
|
122
|
-
if (!(_g = _h.sent(), _a = _g.done, !_a)) return [3 /*break*/, 5];
|
|
123
|
-
_c = _g.value;
|
|
124
|
-
_e = false;
|
|
125
|
-
provider = _c;
|
|
126
|
-
return [4 /*yield*/, ((_d = provider.getDataAsync) === null || _d === void 0 ? void 0 : _d.call(provider, this._sdkKey, user))];
|
|
127
|
-
case 3:
|
|
128
|
-
data = _h.sent();
|
|
129
|
-
if (data) {
|
|
130
|
-
return [2 /*return*/, { data: data, source: provider.source }];
|
|
131
|
-
}
|
|
132
|
-
_h.label = 4;
|
|
133
|
-
case 4:
|
|
134
|
-
_e = true;
|
|
135
|
-
return [3 /*break*/, 1];
|
|
136
|
-
case 5: return [3 /*break*/, 12];
|
|
137
|
-
case 6:
|
|
138
|
-
e_1_1 = _h.sent();
|
|
139
|
-
e_1 = { error: e_1_1 };
|
|
140
|
-
return [3 /*break*/, 12];
|
|
141
|
-
case 7:
|
|
142
|
-
_h.trys.push([7, , 10, 11]);
|
|
143
|
-
if (!(!_e && !_a && (_b = _f.return))) return [3 /*break*/, 9];
|
|
144
|
-
return [4 /*yield*/, _b.call(_f)];
|
|
145
|
-
case 8:
|
|
146
|
-
_h.sent();
|
|
147
|
-
_h.label = 9;
|
|
148
|
-
case 9: return [3 /*break*/, 11];
|
|
149
|
-
case 10:
|
|
150
|
-
if (e_1) throw e_1.error;
|
|
151
|
-
return [7 /*endfinally*/];
|
|
152
|
-
case 11: return [7 /*endfinally*/];
|
|
153
|
-
case 12: return [2 /*return*/, { data: null, source: 'NoValues' }];
|
|
154
|
-
}
|
|
155
|
-
});
|
|
156
|
-
});
|
|
157
|
-
};
|
|
158
|
-
StatsigClientBase.prototype._getDataPostInitFromProviders = function (user) {
|
|
159
|
-
var _a, e_2, _b, _c;
|
|
160
|
-
var _d;
|
|
161
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
162
|
-
var _e, _f, _g, provider, data, e_2_1;
|
|
163
|
-
return __generator(this, function (_h) {
|
|
164
|
-
switch (_h.label) {
|
|
165
|
-
case 0:
|
|
166
|
-
_h.trys.push([0, 6, 7, 12]);
|
|
167
|
-
_e = true, _f = __asyncValues(this._dataProviders);
|
|
168
|
-
_h.label = 1;
|
|
169
|
-
case 1: return [4 /*yield*/, _f.next()];
|
|
170
|
-
case 2:
|
|
171
|
-
if (!(_g = _h.sent(), _a = _g.done, !_a)) return [3 /*break*/, 5];
|
|
172
|
-
_c = _g.value;
|
|
173
|
-
_e = false;
|
|
174
|
-
provider = _c;
|
|
175
|
-
return [4 /*yield*/, ((_d = provider.getDataPostInit) === null || _d === void 0 ? void 0 : _d.call(provider, this._sdkKey, user))];
|
|
176
|
-
case 3:
|
|
177
|
-
data = _h.sent();
|
|
178
|
-
if (data) {
|
|
179
|
-
return [2 /*return*/, { data: data, source: provider.source }];
|
|
180
|
-
}
|
|
181
|
-
_h.label = 4;
|
|
182
|
-
case 4:
|
|
183
|
-
_e = true;
|
|
184
|
-
return [3 /*break*/, 1];
|
|
185
|
-
case 5: return [3 /*break*/, 12];
|
|
186
|
-
case 6:
|
|
187
|
-
e_2_1 = _h.sent();
|
|
188
|
-
e_2 = { error: e_2_1 };
|
|
189
|
-
return [3 /*break*/, 12];
|
|
190
|
-
case 7:
|
|
191
|
-
_h.trys.push([7, , 10, 11]);
|
|
192
|
-
if (!(!_e && !_a && (_b = _f.return))) return [3 /*break*/, 9];
|
|
193
|
-
return [4 /*yield*/, _b.call(_f)];
|
|
194
|
-
case 8:
|
|
195
|
-
_h.sent();
|
|
196
|
-
_h.label = 9;
|
|
197
|
-
case 9: return [3 /*break*/, 11];
|
|
198
|
-
case 10:
|
|
199
|
-
if (e_2) throw e_2.error;
|
|
200
|
-
return [7 /*endfinally*/];
|
|
201
|
-
case 11: return [7 /*endfinally*/];
|
|
202
|
-
case 12: return [2 /*return*/, { data: null, source: 'NoValues' }];
|
|
203
|
-
}
|
|
204
|
-
});
|
|
205
|
-
});
|
|
206
|
-
};
|
|
207
|
-
StatsigClientBase.prototype._saveToDataProviders = function (data, user) {
|
|
208
|
-
var _this = this;
|
|
209
|
-
(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
210
|
-
var localResult, _a, _b, _c, provider, e_3_1;
|
|
211
|
-
var _d, e_3, _e, _f;
|
|
212
|
-
var _g, _h;
|
|
213
|
-
return __generator(this, function (_j) {
|
|
214
|
-
switch (_j.label) {
|
|
215
|
-
case 0: return [4 /*yield*/, this._getDataPostInitFromProviders(user)];
|
|
216
|
-
case 1:
|
|
217
|
-
localResult = _j.sent();
|
|
218
|
-
data = (_g = localResult.data) !== null && _g !== void 0 ? _g : data;
|
|
219
|
-
if (!data) {
|
|
220
|
-
return [2 /*return*/];
|
|
221
|
-
}
|
|
222
|
-
_j.label = 2;
|
|
223
|
-
case 2:
|
|
224
|
-
_j.trys.push([2, 8, 9, 14]);
|
|
225
|
-
_a = true, _b = __asyncValues(this._dataProviders);
|
|
226
|
-
_j.label = 3;
|
|
227
|
-
case 3: return [4 /*yield*/, _b.next()];
|
|
228
|
-
case 4:
|
|
229
|
-
if (!(_c = _j.sent(), _d = _c.done, !_d)) return [3 /*break*/, 7];
|
|
230
|
-
_f = _c.value;
|
|
231
|
-
_a = false;
|
|
232
|
-
provider = _f;
|
|
233
|
-
return [4 /*yield*/, ((_h = provider.setDataPostInit) === null || _h === void 0 ? void 0 : _h.call(provider, this._sdkKey, data, user))];
|
|
234
|
-
case 5:
|
|
235
|
-
_j.sent();
|
|
236
|
-
_j.label = 6;
|
|
237
|
-
case 6:
|
|
238
|
-
_a = true;
|
|
239
|
-
return [3 /*break*/, 3];
|
|
240
|
-
case 7: return [3 /*break*/, 14];
|
|
241
|
-
case 8:
|
|
242
|
-
e_3_1 = _j.sent();
|
|
243
|
-
e_3 = { error: e_3_1 };
|
|
244
|
-
return [3 /*break*/, 14];
|
|
245
|
-
case 9:
|
|
246
|
-
_j.trys.push([9, , 12, 13]);
|
|
247
|
-
if (!(!_a && !_d && (_e = _b.return))) return [3 /*break*/, 11];
|
|
248
|
-
return [4 /*yield*/, _e.call(_b)];
|
|
249
|
-
case 10:
|
|
250
|
-
_j.sent();
|
|
251
|
-
_j.label = 11;
|
|
252
|
-
case 11: return [3 /*break*/, 13];
|
|
253
|
-
case 12:
|
|
254
|
-
if (e_3) throw e_3.error;
|
|
255
|
-
return [7 /*endfinally*/];
|
|
256
|
-
case 13: return [7 /*endfinally*/];
|
|
257
|
-
case 14: return [2 /*return*/];
|
|
258
|
-
}
|
|
259
|
-
});
|
|
260
|
-
}); })().catch(function (error) {
|
|
261
|
-
_this.emit({ event: 'error', error: error });
|
|
262
|
-
});
|
|
263
|
-
};
|
|
264
|
-
StatsigClientBase.prototype._enqueueExposure = function (options, exposure) {
|
|
67
|
+
this._emit({ event: 'values_updated', status: newStatus, values });
|
|
68
|
+
}
|
|
69
|
+
_enqueueExposure(name, options, exposure) {
|
|
265
70
|
if (options.disableExposureLog === true) {
|
|
71
|
+
this._logger.incrementNonExposureCount(name);
|
|
266
72
|
return;
|
|
267
73
|
}
|
|
268
74
|
this._logger.enqueue(exposure);
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
}());
|
|
75
|
+
}
|
|
76
|
+
}
|
|
272
77
|
exports.StatsigClientBase = StatsigClientBase;
|
|
@@ -1,11 +1,33 @@
|
|
|
1
|
+
import { DataAdapterResult } from './StatsigDataAdapter';
|
|
1
2
|
import { DynamicConfig, Experiment, FeatureGate, Layer } from './StatsigTypes';
|
|
2
|
-
export type StatsigLoadingStatus = 'Uninitialized' | 'Loading' | 'Ready'
|
|
3
|
-
|
|
3
|
+
export type StatsigLoadingStatus = 'Uninitialized' | 'Loading' | 'Ready';
|
|
4
|
+
/**
|
|
5
|
+
* All the possible events emitted from a Statsig client.
|
|
6
|
+
*
|
|
7
|
+
* `values_updated` - When the Statsig clients internal values change as the result of an initialize/update operation.
|
|
8
|
+
*
|
|
9
|
+
* `error` - When an unexpected error occurs within the Statsig client.
|
|
10
|
+
*
|
|
11
|
+
* `logs_flushed` - When queued StatsigEvents are flushed to Statsig servers.
|
|
12
|
+
*
|
|
13
|
+
* `gate_evaluation` - Fired when any gate is checked from the Statsig client.
|
|
14
|
+
*
|
|
15
|
+
* `dynamic_config_evaluation` - Fired when any dyanamic config is checked from the Statsig client.
|
|
16
|
+
*
|
|
17
|
+
* `experiment_evaluation` - Fired when any experiment is checked from the Statsig client.
|
|
18
|
+
*
|
|
19
|
+
* `layer_evaluation` - Fired when any layer is checked from the Statsig client.
|
|
20
|
+
*/
|
|
21
|
+
export type StatsigClientEvent = 'values_updated' | 'error' | 'logs_flushed' | 'gate_evaluation' | 'dynamic_config_evaluation' | 'experiment_evaluation' | 'layer_evaluation';
|
|
22
|
+
/**
|
|
23
|
+
* Type representing various events emitted by a Statsig client.
|
|
24
|
+
*/
|
|
4
25
|
export type StatsigClientEventData = {
|
|
5
26
|
event: StatsigClientEvent;
|
|
6
|
-
}
|
|
7
|
-
event: '
|
|
8
|
-
|
|
27
|
+
} & ({
|
|
28
|
+
event: 'values_updated';
|
|
29
|
+
status: StatsigLoadingStatus;
|
|
30
|
+
values: DataAdapterResult | null;
|
|
9
31
|
} | {
|
|
10
32
|
event: 'error';
|
|
11
33
|
error: unknown;
|
|
@@ -24,7 +46,7 @@ export type StatsigClientEventData = {
|
|
|
24
46
|
} | {
|
|
25
47
|
event: 'layer_evaluation';
|
|
26
48
|
layer: Layer;
|
|
27
|
-
};
|
|
49
|
+
});
|
|
28
50
|
export type StatsigClientEventCallback = (data: StatsigClientEventData) => void;
|
|
29
51
|
export interface StatsigClientEventEmitterInterface {
|
|
30
52
|
readonly loadingStatus: StatsigLoadingStatus;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { StatsigOptionsCommon } from './StatsigOptionsCommon';
|
|
2
|
+
import { StatsigUser } from './StatsigUser';
|
|
3
|
+
export type DataSource = 'Uninitialized' | 'Loading' | 'NoValues' | 'Cache' | 'Network' | 'NetworkNotModified' | 'Bootstrap' | 'Prefetch';
|
|
4
|
+
export type DataAdapterResult = {
|
|
5
|
+
readonly source: DataSource;
|
|
6
|
+
readonly data: string;
|
|
7
|
+
readonly receivedAt: number;
|
|
8
|
+
};
|
|
9
|
+
export declare const DataAdapterCachePrefix = "statsig.cached";
|
|
10
|
+
/**
|
|
11
|
+
* Describes a type that is used during intialize/update operations of a Statsig client.
|
|
12
|
+
*
|
|
13
|
+
* See below to find the default adapters, but know that it is possible to create your
|
|
14
|
+
* own StatsigDataAdapter and provide it via {@link StatsigOptionsCommon.dataAdapter}.
|
|
15
|
+
*
|
|
16
|
+
* Defaults:
|
|
17
|
+
*
|
|
18
|
+
* - {@link StatsigClient} uses {@link EvaluationsDataAdapter}
|
|
19
|
+
*
|
|
20
|
+
* - {@link StatsigOnDeviceEvalClient} uses {@link SpecsDataAdapter}
|
|
21
|
+
*/
|
|
22
|
+
type DataAdapterCommon = {
|
|
23
|
+
/**
|
|
24
|
+
* Called when the StatsigDataAdapter is attached to the Statsig client instance during construction.
|
|
25
|
+
* @param {string} sdkKey The SDK key being used by the Statsig client.
|
|
26
|
+
* @param {StatsigOptionsCommon | null} options The StatsigOptions being used by the Statsig client.
|
|
27
|
+
*/
|
|
28
|
+
readonly attach: (sdkKey: string, options: StatsigOptionsCommon | null) => void;
|
|
29
|
+
/**
|
|
30
|
+
* (Internal Use Only) - Used by \@statsig/react-native-bindings to prime the cache from AsyncStorage
|
|
31
|
+
*
|
|
32
|
+
* @param {Record<string, DataAdapterResult>} cache The values to set for _inMemoryCache
|
|
33
|
+
*/
|
|
34
|
+
readonly __setInMemoryCache: (cache: Record<string, DataAdapterResult>) => void;
|
|
35
|
+
};
|
|
36
|
+
export type EvaluationsDataAdapter = DataAdapterCommon & {
|
|
37
|
+
/**
|
|
38
|
+
* Synchronously get evaluation data for the given user. Called during initializeSync and/or updateUserSync.
|
|
39
|
+
*
|
|
40
|
+
* It is also called during async update operations before StatsigDataAdapter.getDataAsync is called.
|
|
41
|
+
* @param {StatsigUser} user The StatsigUser to get data for.
|
|
42
|
+
* @returns {DataAdapterResult | null} The data that was found for the given StatsigUser.
|
|
43
|
+
*/
|
|
44
|
+
readonly getDataSync: (user: StatsigUser) => DataAdapterResult | null;
|
|
45
|
+
/**
|
|
46
|
+
* Asynchronously get evaluation data for the given user. Called during initializeAsync and/or updateUserAsync.
|
|
47
|
+
*
|
|
48
|
+
* @param {DataAdapterResult | null} current The data that was found synchronously (Cache). Will be used as fallback if getDataAsync fails
|
|
49
|
+
* @param {StatsigUser} user The StatsigUser to get data for.
|
|
50
|
+
* @returns {DataAdapterResult | null} The data that was found for the given StatsigUser.
|
|
51
|
+
*/
|
|
52
|
+
readonly getDataAsync: (current: DataAdapterResult | null, user: StatsigUser) => Promise<DataAdapterResult | null>;
|
|
53
|
+
/**
|
|
54
|
+
* Manually trigger a fetch for new evaluations data for the given user.
|
|
55
|
+
*
|
|
56
|
+
* @param {StatsigUser} user The StatsigUser to get data for.
|
|
57
|
+
*/
|
|
58
|
+
readonly prefetchData: (user: StatsigUser) => Promise<void>;
|
|
59
|
+
/**
|
|
60
|
+
* Manually set evaluations data for the given user.
|
|
61
|
+
*
|
|
62
|
+
* @param {StatsigUser} user The StatsigUser this data is for.
|
|
63
|
+
*/
|
|
64
|
+
readonly setData: (data: string, user: StatsigUser) => void;
|
|
65
|
+
};
|
|
66
|
+
export type SpecsDataAdapter = DataAdapterCommon & {
|
|
67
|
+
/**
|
|
68
|
+
* Synchronously get specs data. Called during initializeSync and/or updateUserSync.
|
|
69
|
+
* It is also called during async update operations before StatsigDataAdapter.getDataAsync is called.
|
|
70
|
+
* @returns {DataAdapterResult | null} The data that was found for the given StatsigUser.
|
|
71
|
+
*/
|
|
72
|
+
readonly getDataSync: () => DataAdapterResult | null;
|
|
73
|
+
/**
|
|
74
|
+
* Asynchronously get specs data. Called during initializeAsync and/or updateUserAsync.
|
|
75
|
+
*
|
|
76
|
+
* @param {DataAdapterResult | null} current The data that was found synchronously (Cache). Will be used as fallback if getDataAsync fails
|
|
77
|
+
* @returns {DataAdapterResult | null} The data that was found for the given StatsigUser.
|
|
78
|
+
*/
|
|
79
|
+
readonly getDataAsync: (current: DataAdapterResult | null) => Promise<DataAdapterResult | null>;
|
|
80
|
+
/**
|
|
81
|
+
* Manually trigger a fetch for new specs data.
|
|
82
|
+
*/
|
|
83
|
+
readonly prefetchData: () => Promise<void>;
|
|
84
|
+
/**
|
|
85
|
+
* Manually set specs data (Bootstrap).
|
|
86
|
+
*/
|
|
87
|
+
readonly setData: (data: string) => void;
|
|
88
|
+
};
|
|
89
|
+
export {};
|
package/src/StatsigEvent.d.ts
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { DynamicConfig, FeatureGate } from './StatsigTypes';
|
|
1
|
+
import { SecondaryExposure } from './EvaluationTypes';
|
|
2
|
+
import { DynamicConfig, FeatureGate, Layer } from './StatsigTypes';
|
|
3
3
|
import { StatsigUser } from './StatsigUser';
|
|
4
|
-
export type SecondaryExposure = {
|
|
5
|
-
gate: string;
|
|
6
|
-
gateValue: string;
|
|
7
|
-
ruleID: string;
|
|
8
|
-
};
|
|
9
4
|
export type StatsigEvent = {
|
|
10
5
|
eventName: string;
|
|
11
6
|
value?: string | number | null;
|
|
@@ -13,19 +8,22 @@ export type StatsigEvent = {
|
|
|
13
8
|
[key: string]: string;
|
|
14
9
|
} | null;
|
|
15
10
|
};
|
|
16
|
-
export type StatsigEventInternal = StatsigEvent & {
|
|
11
|
+
export type StatsigEventInternal = Omit<StatsigEvent, 'metadata'> & {
|
|
17
12
|
user: StatsigUser | null;
|
|
18
13
|
time: number;
|
|
14
|
+
metadata?: {
|
|
15
|
+
[key: string]: unknown;
|
|
16
|
+
} | null;
|
|
19
17
|
secondaryExposures?: SecondaryExposure[];
|
|
20
18
|
};
|
|
19
|
+
type LayerEvaluation = {
|
|
20
|
+
explicit_parameters: string[] | null;
|
|
21
|
+
undelegated_secondary_exposures?: SecondaryExposure[];
|
|
22
|
+
secondary_exposures: SecondaryExposure[];
|
|
23
|
+
allocated_experiment_name: string | null;
|
|
24
|
+
};
|
|
21
25
|
export declare function isExposureEvent({ eventName }: StatsigEventInternal): boolean;
|
|
22
26
|
export declare function createGateExposure(user: StatsigUser, gate: FeatureGate, secondaryExposures: SecondaryExposure[] | undefined): StatsigEventInternal;
|
|
23
27
|
export declare function createConfigExposure(user: StatsigUser, config: DynamicConfig, secondaryExposures: SecondaryExposure[] | undefined): StatsigEventInternal;
|
|
24
|
-
export declare function createLayerParameterExposure(user: StatsigUser,
|
|
25
|
-
|
|
26
|
-
explicit_parameters: string[];
|
|
27
|
-
undelegated_secondary_exposures?: SecondaryExposure[];
|
|
28
|
-
secondary_exposures: SecondaryExposure[];
|
|
29
|
-
allocated_experiment_name: string;
|
|
30
|
-
source: DataSource;
|
|
31
|
-
}): StatsigEventInternal;
|
|
28
|
+
export declare function createLayerParameterExposure(user: StatsigUser, layer: Layer, parameterName: string, evaluation: LayerEvaluation | null): StatsigEventInternal;
|
|
29
|
+
export {};
|
package/src/StatsigEvent.js
CHANGED
|
@@ -1,57 +1,63 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createLayerParameterExposure = exports.createConfigExposure = exports.createGateExposure = exports.isExposureEvent = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
function createExposure(eventName, user, metadata, secondaryExposures) {
|
|
4
|
+
const CONFIG_EXPOSURE_NAME = 'statsig::config_exposure';
|
|
5
|
+
const GATE_EXPOSURE_NAME = 'statsig::gate_exposure';
|
|
6
|
+
const LAYER_EXPOSURE_NAME = 'statsig::layer_exposure';
|
|
7
|
+
function createExposure(eventName, user, details, metadata, secondaryExposures) {
|
|
8
8
|
return {
|
|
9
|
-
eventName
|
|
10
|
-
user
|
|
9
|
+
eventName,
|
|
10
|
+
user,
|
|
11
11
|
value: null,
|
|
12
|
-
metadata: metadata,
|
|
13
|
-
secondaryExposures
|
|
12
|
+
metadata: _addEvaluationDetailsToMetadata(details, metadata),
|
|
13
|
+
secondaryExposures,
|
|
14
14
|
time: Date.now(),
|
|
15
15
|
};
|
|
16
16
|
}
|
|
17
|
-
function isExposureEvent(
|
|
18
|
-
var eventName = _a.eventName;
|
|
17
|
+
function isExposureEvent({ eventName }) {
|
|
19
18
|
return eventName === GATE_EXPOSURE_NAME || eventName === CONFIG_EXPOSURE_NAME;
|
|
20
19
|
}
|
|
21
20
|
exports.isExposureEvent = isExposureEvent;
|
|
22
21
|
function createGateExposure(user, gate, secondaryExposures) {
|
|
23
|
-
return createExposure(GATE_EXPOSURE_NAME, user, {
|
|
22
|
+
return createExposure(GATE_EXPOSURE_NAME, user, gate.details, {
|
|
24
23
|
gate: gate.name,
|
|
25
24
|
gateValue: String(gate.value),
|
|
26
25
|
ruleID: gate.ruleID,
|
|
27
|
-
reason: gate.source,
|
|
28
26
|
}, secondaryExposures !== null && secondaryExposures !== void 0 ? secondaryExposures : []);
|
|
29
27
|
}
|
|
30
28
|
exports.createGateExposure = createGateExposure;
|
|
31
29
|
function createConfigExposure(user, config, secondaryExposures) {
|
|
32
|
-
return createExposure(CONFIG_EXPOSURE_NAME, user, {
|
|
30
|
+
return createExposure(CONFIG_EXPOSURE_NAME, user, config.details, {
|
|
33
31
|
config: config.name,
|
|
34
32
|
ruleID: config.ruleID,
|
|
35
|
-
reason: config.source,
|
|
36
33
|
}, secondaryExposures !== null && secondaryExposures !== void 0 ? secondaryExposures : []);
|
|
37
34
|
}
|
|
38
35
|
exports.createConfigExposure = createConfigExposure;
|
|
39
|
-
function createLayerParameterExposure(user,
|
|
40
|
-
var _a;
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
36
|
+
function createLayerParameterExposure(user, layer, parameterName, evaluation) {
|
|
37
|
+
var _a, _b, _c;
|
|
38
|
+
const isExplicit = ((_a = evaluation === null || evaluation === void 0 ? void 0 : evaluation.explicit_parameters) === null || _a === void 0 ? void 0 : _a.includes(parameterName)) === true;
|
|
39
|
+
let allocatedExperiment = '';
|
|
40
|
+
let secondaryExposures = (_b = evaluation === null || evaluation === void 0 ? void 0 : evaluation.undelegated_secondary_exposures) !== null && _b !== void 0 ? _b : [];
|
|
44
41
|
if (isExplicit) {
|
|
45
|
-
allocatedExperiment =
|
|
46
|
-
secondaryExposures =
|
|
42
|
+
allocatedExperiment = (_c = evaluation.allocated_experiment_name) !== null && _c !== void 0 ? _c : '';
|
|
43
|
+
secondaryExposures = evaluation.secondary_exposures;
|
|
47
44
|
}
|
|
48
|
-
return createExposure(LAYER_EXPOSURE_NAME, user, {
|
|
49
|
-
config:
|
|
50
|
-
parameterName
|
|
51
|
-
ruleID:
|
|
52
|
-
allocatedExperiment
|
|
45
|
+
return createExposure(LAYER_EXPOSURE_NAME, user, layer.details, {
|
|
46
|
+
config: layer.name,
|
|
47
|
+
parameterName,
|
|
48
|
+
ruleID: layer.ruleID,
|
|
49
|
+
allocatedExperiment,
|
|
53
50
|
isExplicitParameter: String(isExplicit),
|
|
54
|
-
reason: spec.source,
|
|
55
51
|
}, secondaryExposures);
|
|
56
52
|
}
|
|
57
53
|
exports.createLayerParameterExposure = createLayerParameterExposure;
|
|
54
|
+
function _addEvaluationDetailsToMetadata(details, metadata) {
|
|
55
|
+
metadata['reason'] = details.reason;
|
|
56
|
+
if (details.lcut) {
|
|
57
|
+
metadata['lcut'] = String(details.lcut);
|
|
58
|
+
}
|
|
59
|
+
if (details.receivedAt) {
|
|
60
|
+
metadata['receivedAt'] = String(details.receivedAt);
|
|
61
|
+
}
|
|
62
|
+
return metadata;
|
|
63
|
+
}
|
package/src/StatsigMetadata.js
CHANGED
|
@@ -1,25 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
3
|
exports.StatsigMetadataProvider = void 0;
|
|
15
|
-
|
|
16
|
-
|
|
4
|
+
const SDK_VERSION = '0.0.1-beta.11';
|
|
5
|
+
let metadata = {
|
|
17
6
|
sdkVersion: SDK_VERSION,
|
|
18
7
|
sdkType: 'js-mono', // js-mono is overwritten by Precomp and OnDevice clients
|
|
19
8
|
};
|
|
20
9
|
exports.StatsigMetadataProvider = {
|
|
21
|
-
get:
|
|
22
|
-
add:
|
|
23
|
-
metadata =
|
|
10
|
+
get: () => metadata,
|
|
11
|
+
add: (additions) => {
|
|
12
|
+
metadata = Object.assign(Object.assign({}, metadata), additions);
|
|
24
13
|
},
|
|
25
14
|
};
|