@statsig/client-core 0.0.1-beta.2 → 0.0.1-beta.21
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 +1 -1
- package/package.json +1 -1
- package/src/$_StatsigGlobal.d.ts +2 -1
- package/src/$_StatsigGlobal.js +10 -12
- package/src/ClientInterfaces.d.ts +20 -13
- 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/EvaluationOptions.d.ts +20 -0
- package/src/EvaluationTypes.d.ts +39 -0
- package/src/EvaluationTypes.js +2 -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 +68 -27
- package/src/NetworkCore.d.ts +13 -5
- package/src/NetworkCore.js +96 -164
- package/src/OverrideAdapter.d.ts +16 -0
- package/src/OverrideAdapter.js +24 -0
- package/src/SessionID.js +3 -3
- package/src/StableID.js +23 -52
- package/src/StatsigClientBase.d.ts +20 -29
- package/src/StatsigClientBase.js +58 -248
- package/src/StatsigClientEventEmitter.d.ts +55 -28
- package/src/StatsigDataAdapter.d.ts +89 -0
- package/src/StatsigDataAdapter.js +4 -0
- package/src/StatsigEvent.d.ts +9 -18
- package/src/StatsigEvent.js +40 -31
- package/src/StatsigMetadata.js +5 -16
- package/src/StatsigOptionsCommon.d.ts +42 -12
- package/src/StatsigTypes.d.ts +23 -15
- 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 +9 -5
- package/src/UrlOverrides.d.ts +1 -0
- package/src/UrlOverrides.js +15 -0
- package/src/UtitlityTypes.d.ts +3 -0
- package/src/UtitlityTypes.js +2 -0
- package/src/VisibilityChangeObserver.js +18 -24
- package/src/__tests__/MockLocalStorage.js +18 -19
- package/src/index.d.ts +12 -3
- package/src/index.js +18 -16
- package/src/StatsigDataProvider.d.ts +0 -9
- /package/src/{StatsigDataProvider.js → EvaluationOptions.js} +0 -0
package/src/StatsigClientBase.js
CHANGED
|
@@ -1,272 +1,82 @@
|
|
|
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
|
-
exports.StatsigClientBase =
|
|
3
|
+
exports.StatsigClientBase = void 0;
|
|
47
4
|
require("./$_StatsigGlobal");
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
var
|
|
56
|
-
|
|
57
|
-
var _a, _b;
|
|
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");
|
|
10
|
+
class StatsigClientBase {
|
|
11
|
+
constructor(_sdkKey, adapter, network, options) {
|
|
12
|
+
var _a, _b, _c;
|
|
13
|
+
this._sdkKey = _sdkKey;
|
|
58
14
|
this.loadingStatus = 'Uninitialized';
|
|
59
15
|
this._listeners = {};
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
16
|
+
(options === null || options === void 0 ? void 0 : options.disableStorage) && StorageProvider_1.Storage.setDisabled(true);
|
|
17
|
+
(options === null || options === void 0 ? void 0 : options.overrideStableID) &&
|
|
18
|
+
StableID_1.StableID.setOverride(options.overrideStableID, _sdkKey);
|
|
19
|
+
Log_1.Log.level = (_a = options === null || options === void 0 ? void 0 : options.logLevel) !== null && _a !== void 0 ? _a : Log_1.LogLevel.Warn;
|
|
20
|
+
this._overrideAdapter = (_b = options === null || options === void 0 ? void 0 : options.overrideAdapter) !== null && _b !== void 0 ? _b : null;
|
|
21
|
+
this._logger = new EventLogger_1.EventLogger(_sdkKey, this._emit.bind(this), network, options);
|
|
22
|
+
this._errorBoundary = new ErrorBoundary_1.ErrorBoundary(_sdkKey);
|
|
66
23
|
__STATSIG__ = __STATSIG__ !== null && __STATSIG__ !== void 0 ? __STATSIG__ : {};
|
|
67
|
-
|
|
68
|
-
instances
|
|
24
|
+
const instances = (_c = __STATSIG__.instances) !== null && _c !== void 0 ? _c : {};
|
|
25
|
+
instances[_sdkKey] = this;
|
|
69
26
|
__STATSIG__.instances = instances;
|
|
70
|
-
|
|
71
|
-
this.
|
|
27
|
+
this.dataAdapter = adapter;
|
|
28
|
+
this.dataAdapter.attach(_sdkKey, options);
|
|
72
29
|
}
|
|
73
|
-
|
|
30
|
+
updateRuntimeOptions(options) {
|
|
31
|
+
if (options.disableLogging != null) {
|
|
32
|
+
this._logger.setLoggingDisabled(options.disableLogging);
|
|
33
|
+
}
|
|
34
|
+
if (options.disableStorage != null) {
|
|
35
|
+
StorageProvider_1.Storage.setDisabled(options.disableStorage);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
flush() {
|
|
39
|
+
return this._logger.flush();
|
|
40
|
+
}
|
|
41
|
+
on(event, listener) {
|
|
74
42
|
if (!this._listeners[event]) {
|
|
75
43
|
this._listeners[event] = [];
|
|
76
44
|
}
|
|
77
45
|
this._listeners[event].push(listener);
|
|
78
|
-
}
|
|
79
|
-
|
|
46
|
+
}
|
|
47
|
+
off(event, listener) {
|
|
80
48
|
if (this._listeners[event]) {
|
|
81
|
-
|
|
49
|
+
const index = this._listeners[event].indexOf(listener);
|
|
82
50
|
if (index !== -1) {
|
|
83
51
|
this._listeners[event].splice(index, 1);
|
|
84
52
|
}
|
|
85
53
|
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
var _a;
|
|
89
|
-
if (this._listeners[data.event]) {
|
|
90
|
-
this._listeners[data.event].forEach(function (listener) { return listener(data); });
|
|
91
|
-
}
|
|
92
|
-
(_a = this._listeners['*']) === null || _a === void 0 ? void 0 : _a.forEach(function (listener) { return listener(data); });
|
|
93
|
-
};
|
|
94
|
-
StatsigClientBase.prototype._setStatus = function (newStatus) {
|
|
95
|
-
this.loadingStatus = newStatus;
|
|
96
|
-
this.emit({ event: 'status_change', loadingStatus: newStatus });
|
|
97
|
-
};
|
|
98
|
-
StatsigClientBase.prototype._getDataFromProviders = function (user) {
|
|
54
|
+
}
|
|
55
|
+
_emit(event) {
|
|
99
56
|
var _a;
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
57
|
+
const barrier = (listener) => {
|
|
58
|
+
try {
|
|
59
|
+
listener(event);
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
Log_1.Log.error(`An error occurred in a StatsigClientEvent listener. This is not an issue with Statsig.`, event);
|
|
105
63
|
}
|
|
64
|
+
};
|
|
65
|
+
if (this._listeners[event.name]) {
|
|
66
|
+
this._listeners[event.name].forEach((l) => barrier(l));
|
|
106
67
|
}
|
|
107
|
-
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
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) {
|
|
265
|
-
if (options.disableExposureLog === true) {
|
|
68
|
+
(_a = this._listeners['*']) === null || _a === void 0 ? void 0 : _a.forEach(barrier);
|
|
69
|
+
}
|
|
70
|
+
_setStatus(newStatus, values) {
|
|
71
|
+
this.loadingStatus = newStatus;
|
|
72
|
+
this._emit({ name: 'values_updated', status: newStatus, values });
|
|
73
|
+
}
|
|
74
|
+
_enqueueExposure(name, exposure, options) {
|
|
75
|
+
if ((options === null || options === void 0 ? void 0 : options.disableExposureLog) === true) {
|
|
76
|
+
this._logger.incrementNonExposureCount(name);
|
|
266
77
|
return;
|
|
267
78
|
}
|
|
268
79
|
this._logger.enqueue(exposure);
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
}());
|
|
80
|
+
}
|
|
81
|
+
}
|
|
272
82
|
exports.StatsigClientBase = StatsigClientBase;
|
|
@@ -1,33 +1,60 @@
|
|
|
1
|
+
import { DataAdapterResult } from './StatsigDataAdapter';
|
|
1
2
|
import { DynamicConfig, Experiment, FeatureGate, Layer } from './StatsigTypes';
|
|
2
|
-
|
|
3
|
-
export type
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
3
|
+
import { Flatten } from './UtitlityTypes';
|
|
4
|
+
export type StatsigLoadingStatus = 'Uninitialized' | 'Loading' | 'Ready';
|
|
5
|
+
type EventNameToEventDataMap = {
|
|
6
|
+
values_updated: {
|
|
7
|
+
status: StatsigLoadingStatus;
|
|
8
|
+
values: DataAdapterResult | null;
|
|
9
|
+
};
|
|
10
|
+
error: {
|
|
11
|
+
error: unknown;
|
|
12
|
+
};
|
|
13
|
+
logs_flushed: {
|
|
14
|
+
events: Record<string, unknown>[];
|
|
15
|
+
};
|
|
16
|
+
gate_evaluation: {
|
|
17
|
+
gate: FeatureGate;
|
|
18
|
+
};
|
|
19
|
+
dynamic_config_evaluation: {
|
|
20
|
+
dynamicConfig: DynamicConfig;
|
|
21
|
+
};
|
|
22
|
+
experiment_evaluation: {
|
|
23
|
+
experiment: Experiment;
|
|
24
|
+
};
|
|
25
|
+
layer_evaluation: {
|
|
26
|
+
layer: Layer;
|
|
27
|
+
};
|
|
27
28
|
};
|
|
28
|
-
|
|
29
|
+
/**
|
|
30
|
+
* Type representing various events emitted by a Statsig client.
|
|
31
|
+
*
|
|
32
|
+
* `values_updated` - When the Statsig clients internal values change as the result of an initialize/update operation.
|
|
33
|
+
*
|
|
34
|
+
* `error` - When an unexpected error occurs within the Statsig client.
|
|
35
|
+
*
|
|
36
|
+
* `logs_flushed` - When queued StatsigEvents are flushed to Statsig servers.
|
|
37
|
+
*
|
|
38
|
+
* `gate_evaluation` - Fired when any gate is checked from the Statsig client.
|
|
39
|
+
*
|
|
40
|
+
* `dynamic_config_evaluation` - Fired when any dyanamic config is checked from the Statsig client.
|
|
41
|
+
*
|
|
42
|
+
* `experiment_evaluation` - Fired when any experiment is checked from the Statsig client.
|
|
43
|
+
*
|
|
44
|
+
* `layer_evaluation` - Fired when any layer is checked from the Statsig client.
|
|
45
|
+
*/
|
|
46
|
+
export type StatsigClientEvent = Flatten<{
|
|
47
|
+
[K in keyof EventNameToEventDataMap]: {
|
|
48
|
+
name: K;
|
|
49
|
+
} & EventNameToEventDataMap[K];
|
|
50
|
+
}[keyof EventNameToEventDataMap]>;
|
|
51
|
+
export type StatsigClientEventName = StatsigClientEvent['name'] | '*';
|
|
52
|
+
export type StatsigClientEventCallback<T extends StatsigClientEventName> = (event: T extends '*' ? StatsigClientEvent : Extract<StatsigClientEvent, {
|
|
53
|
+
name: T;
|
|
54
|
+
}>) => void;
|
|
29
55
|
export interface StatsigClientEventEmitterInterface {
|
|
30
56
|
readonly loadingStatus: StatsigLoadingStatus;
|
|
31
|
-
on(event:
|
|
32
|
-
off(event:
|
|
57
|
+
on<T extends StatsigClientEventName>(event: T, listener: StatsigClientEventCallback<T>): void;
|
|
58
|
+
off<T extends StatsigClientEventName>(event: T, listener: StatsigClientEventCallback<T>): void;
|
|
33
59
|
}
|
|
60
|
+
export {};
|
|
@@ -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 merge into _inMemoryCache
|
|
33
|
+
*/
|
|
34
|
+
readonly __primeInMemoryCache: (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,15 @@ 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
|
};
|
|
21
19
|
export declare function isExposureEvent({ eventName }: StatsigEventInternal): boolean;
|
|
22
|
-
export declare function createGateExposure(user: StatsigUser, gate: FeatureGate
|
|
23
|
-
export declare function createConfigExposure(user: StatsigUser, config: DynamicConfig
|
|
24
|
-
export declare function createLayerParameterExposure(user: StatsigUser,
|
|
25
|
-
rule_id: string;
|
|
26
|
-
explicit_parameters: string[];
|
|
27
|
-
undelegated_secondary_exposures?: SecondaryExposure[];
|
|
28
|
-
secondary_exposures: SecondaryExposure[];
|
|
29
|
-
allocated_experiment_name: string;
|
|
30
|
-
source: DataSource;
|
|
31
|
-
}): StatsigEventInternal;
|
|
20
|
+
export declare function createGateExposure(user: StatsigUser, gate: FeatureGate): StatsigEventInternal;
|
|
21
|
+
export declare function createConfigExposure(user: StatsigUser, config: DynamicConfig): StatsigEventInternal;
|
|
22
|
+
export declare function createLayerParameterExposure(user: StatsigUser, layer: Layer, parameterName: string): StatsigEventInternal;
|
package/src/StatsigEvent.js
CHANGED
|
@@ -1,57 +1,66 @@
|
|
|
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
|
-
function createGateExposure(user, gate
|
|
23
|
-
|
|
21
|
+
function createGateExposure(user, gate) {
|
|
22
|
+
var _a, _b;
|
|
23
|
+
return createExposure(GATE_EXPOSURE_NAME, user, gate.details, {
|
|
24
24
|
gate: gate.name,
|
|
25
25
|
gateValue: String(gate.value),
|
|
26
26
|
ruleID: gate.ruleID,
|
|
27
|
-
|
|
28
|
-
}, secondaryExposures !== null && secondaryExposures !== void 0 ? secondaryExposures : []);
|
|
27
|
+
}, (_b = (_a = gate.__evaluation) === null || _a === void 0 ? void 0 : _a.secondary_exposures) !== null && _b !== void 0 ? _b : []);
|
|
29
28
|
}
|
|
30
29
|
exports.createGateExposure = createGateExposure;
|
|
31
|
-
function createConfigExposure(user, config
|
|
32
|
-
|
|
30
|
+
function createConfigExposure(user, config) {
|
|
31
|
+
var _a, _b;
|
|
32
|
+
return createExposure(CONFIG_EXPOSURE_NAME, user, config.details, {
|
|
33
33
|
config: config.name,
|
|
34
34
|
ruleID: config.ruleID,
|
|
35
|
-
|
|
36
|
-
}, secondaryExposures !== null && secondaryExposures !== void 0 ? secondaryExposures : []);
|
|
35
|
+
}, (_b = (_a = config.__evaluation) === null || _a === void 0 ? void 0 : _a.secondary_exposures) !== null && _b !== void 0 ? _b : []);
|
|
37
36
|
}
|
|
38
37
|
exports.createConfigExposure = createConfigExposure;
|
|
39
|
-
function createLayerParameterExposure(user,
|
|
40
|
-
var _a;
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
38
|
+
function createLayerParameterExposure(user, layer, parameterName) {
|
|
39
|
+
var _a, _b, _c;
|
|
40
|
+
const evaluation = layer.__evaluation;
|
|
41
|
+
const isExplicit = ((_a = evaluation === null || evaluation === void 0 ? void 0 : evaluation.explicit_parameters) === null || _a === void 0 ? void 0 : _a.includes(parameterName)) === true;
|
|
42
|
+
let allocatedExperiment = '';
|
|
43
|
+
let secondaryExposures = (_b = evaluation === null || evaluation === void 0 ? void 0 : evaluation.undelegated_secondary_exposures) !== null && _b !== void 0 ? _b : [];
|
|
44
44
|
if (isExplicit) {
|
|
45
|
-
allocatedExperiment =
|
|
46
|
-
secondaryExposures =
|
|
45
|
+
allocatedExperiment = (_c = evaluation.allocated_experiment_name) !== null && _c !== void 0 ? _c : '';
|
|
46
|
+
secondaryExposures = evaluation.secondary_exposures;
|
|
47
47
|
}
|
|
48
|
-
return createExposure(LAYER_EXPOSURE_NAME, user, {
|
|
49
|
-
config:
|
|
50
|
-
parameterName
|
|
51
|
-
ruleID:
|
|
52
|
-
allocatedExperiment
|
|
48
|
+
return createExposure(LAYER_EXPOSURE_NAME, user, layer.details, {
|
|
49
|
+
config: layer.name,
|
|
50
|
+
parameterName,
|
|
51
|
+
ruleID: layer.ruleID,
|
|
52
|
+
allocatedExperiment,
|
|
53
53
|
isExplicitParameter: String(isExplicit),
|
|
54
|
-
reason: spec.source,
|
|
55
54
|
}, secondaryExposures);
|
|
56
55
|
}
|
|
57
56
|
exports.createLayerParameterExposure = createLayerParameterExposure;
|
|
57
|
+
function _addEvaluationDetailsToMetadata(details, metadata) {
|
|
58
|
+
metadata['reason'] = details.reason;
|
|
59
|
+
if (details.lcut) {
|
|
60
|
+
metadata['lcut'] = String(details.lcut);
|
|
61
|
+
}
|
|
62
|
+
if (details.receivedAt) {
|
|
63
|
+
metadata['receivedAt'] = String(details.receivedAt);
|
|
64
|
+
}
|
|
65
|
+
return metadata;
|
|
66
|
+
}
|