wolves-js-client 1.0.6 → 1.0.7
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/dist/EventLogger.d.ts +20 -1
- package/dist/EventLogger.js +55 -6
- package/dist/Store.d.ts +3 -0
- package/dist/Types.d.ts +2 -0
- package/dist/WolvesClient.d.ts +8 -0
- package/dist/WolvesClient.js +37 -7
- package/dist/WolvesMetadata.d.ts +1 -1
- package/dist/WolvesMetadata.js +1 -1
- package/package.json +1 -1
- package/dist/DeviceInfo.d.ts +0 -22
- package/dist/DeviceInfo.js +0 -98
- package/dist/StatsigClient.d.ts +0 -15
- package/dist/StatsigClient.js +0 -92
- package/dist/StatsigUser.d.ts +0 -6
- package/dist/StatsigUser.js +0 -2
package/dist/EventLogger.d.ts
CHANGED
|
@@ -5,10 +5,29 @@ export declare class EventLogger {
|
|
|
5
5
|
private flushIntervalId;
|
|
6
6
|
private network;
|
|
7
7
|
private maxQueueSize;
|
|
8
|
-
|
|
8
|
+
private sdkKey;
|
|
9
|
+
constructor(sdkKey: string, network: Network);
|
|
10
|
+
/**
|
|
11
|
+
* Safely flush events for a specific sdkKey without throwing.
|
|
12
|
+
* This is used internally and can be called even after client destruction.
|
|
13
|
+
*/
|
|
14
|
+
private static safeFlushAndForget;
|
|
9
15
|
enqueue(event: WolvesEvent): void;
|
|
10
16
|
flush(): Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* Start the event logger and register it in the global map.
|
|
19
|
+
* This should be called when the client is initialized.
|
|
20
|
+
* If an existing logger with the same sdkKey exists, it will be flushed first.
|
|
21
|
+
*/
|
|
11
22
|
start(): void;
|
|
23
|
+
/**
|
|
24
|
+
* Reset the event queue. Any pending events will be lost.
|
|
25
|
+
* Called when switching users to clear any user-specific events.
|
|
26
|
+
*/
|
|
12
27
|
reset(): void;
|
|
28
|
+
/**
|
|
29
|
+
* Stop the event logger, flush all pending events, and remove from global map.
|
|
30
|
+
* This ensures all events are sent before the client is destroyed.
|
|
31
|
+
*/
|
|
13
32
|
stop(): Promise<void>;
|
|
14
33
|
}
|
package/dist/EventLogger.js
CHANGED
|
@@ -13,20 +13,34 @@ exports.EventLogger = void 0;
|
|
|
13
13
|
const Log_1 = require("./Log");
|
|
14
14
|
const DEFAULT_QUEUE_SIZE = 100;
|
|
15
15
|
const DEFAULT_FLUSH_INTERVAL_MS = 10000;
|
|
16
|
+
/**
|
|
17
|
+
* Global map of EventLoggers by sdkKey.
|
|
18
|
+
* This ensures events can still be sent even after a client is destroyed,
|
|
19
|
+
* as long as there are pending events in the queue.
|
|
20
|
+
*/
|
|
21
|
+
const EVENT_LOGGER_MAP = {};
|
|
16
22
|
class EventLogger {
|
|
17
|
-
constructor(network) {
|
|
23
|
+
constructor(sdkKey, network) {
|
|
18
24
|
this.queue = [];
|
|
19
25
|
this.flushIntervalId = null;
|
|
20
26
|
this.maxQueueSize = DEFAULT_QUEUE_SIZE;
|
|
27
|
+
this.sdkKey = sdkKey;
|
|
21
28
|
this.network = network;
|
|
22
|
-
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Safely flush events for a specific sdkKey without throwing.
|
|
32
|
+
* This is used internally and can be called even after client destruction.
|
|
33
|
+
*/
|
|
34
|
+
static safeFlushAndForget(sdkKey) {
|
|
35
|
+
var _a;
|
|
36
|
+
(_a = EVENT_LOGGER_MAP[sdkKey]) === null || _a === void 0 ? void 0 : _a.flush().catch(() => {
|
|
37
|
+
// noop - errors already logged in flush
|
|
38
|
+
});
|
|
23
39
|
}
|
|
24
40
|
enqueue(event) {
|
|
25
41
|
this.queue.push(event);
|
|
26
42
|
if (this.queue.length >= this.maxQueueSize) {
|
|
27
|
-
|
|
28
|
-
Log_1.Log.error('Failed to flush events', e);
|
|
29
|
-
});
|
|
43
|
+
EventLogger.safeFlushAndForget(this.sdkKey);
|
|
30
44
|
}
|
|
31
45
|
}
|
|
32
46
|
flush() {
|
|
@@ -50,24 +64,59 @@ class EventLogger {
|
|
|
50
64
|
}
|
|
51
65
|
});
|
|
52
66
|
}
|
|
67
|
+
/**
|
|
68
|
+
* Start the event logger and register it in the global map.
|
|
69
|
+
* This should be called when the client is initialized.
|
|
70
|
+
* If an existing logger with the same sdkKey exists, it will be flushed first.
|
|
71
|
+
*/
|
|
53
72
|
start() {
|
|
54
73
|
if (this.flushIntervalId) {
|
|
55
74
|
return;
|
|
56
75
|
}
|
|
76
|
+
// If there's an existing logger with the same sdkKey, flush it first
|
|
77
|
+
const existingLogger = EVENT_LOGGER_MAP[this.sdkKey];
|
|
78
|
+
if (existingLogger && existingLogger !== this) {
|
|
79
|
+
// Stop the old logger's interval to prevent it from continuing to flush
|
|
80
|
+
if (existingLogger.flushIntervalId) {
|
|
81
|
+
clearInterval(existingLogger.flushIntervalId);
|
|
82
|
+
existingLogger.flushIntervalId = null;
|
|
83
|
+
}
|
|
84
|
+
// Flush remaining events from the old logger
|
|
85
|
+
existingLogger.flush().catch(() => {
|
|
86
|
+
// noop - errors already logged in flush
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
// Register this logger in the global map (replaces any existing)
|
|
90
|
+
EVENT_LOGGER_MAP[this.sdkKey] = this;
|
|
57
91
|
this.flushIntervalId = setInterval(() => {
|
|
58
|
-
this.
|
|
92
|
+
EventLogger.safeFlushAndForget(this.sdkKey);
|
|
59
93
|
}, DEFAULT_FLUSH_INTERVAL_MS);
|
|
60
94
|
}
|
|
95
|
+
/**
|
|
96
|
+
* Reset the event queue. Any pending events will be lost.
|
|
97
|
+
* Called when switching users to clear any user-specific events.
|
|
98
|
+
*/
|
|
61
99
|
reset() {
|
|
100
|
+
// Attempt to flush any remaining events before resetting
|
|
101
|
+
this.flush().catch(() => {
|
|
102
|
+
// noop - we're resetting anyway
|
|
103
|
+
});
|
|
62
104
|
this.queue = [];
|
|
63
105
|
}
|
|
106
|
+
/**
|
|
107
|
+
* Stop the event logger, flush all pending events, and remove from global map.
|
|
108
|
+
* This ensures all events are sent before the client is destroyed.
|
|
109
|
+
*/
|
|
64
110
|
stop() {
|
|
65
111
|
return __awaiter(this, void 0, void 0, function* () {
|
|
66
112
|
if (this.flushIntervalId) {
|
|
67
113
|
clearInterval(this.flushIntervalId);
|
|
68
114
|
this.flushIntervalId = null;
|
|
69
115
|
}
|
|
116
|
+
// Flush all pending events before stopping
|
|
70
117
|
yield this.flush();
|
|
118
|
+
// Remove from global map after ensuring all events are sent
|
|
119
|
+
delete EVENT_LOGGER_MAP[this.sdkKey];
|
|
71
120
|
});
|
|
72
121
|
}
|
|
73
122
|
}
|
package/dist/Store.d.ts
CHANGED
|
@@ -3,6 +3,9 @@ export type ExperimentConfig = {
|
|
|
3
3
|
value: Record<string, any>;
|
|
4
4
|
experiment_id?: string;
|
|
5
5
|
group?: string;
|
|
6
|
+
group_name?: string;
|
|
7
|
+
is_user_in_experiment?: boolean;
|
|
8
|
+
is_experiment_active?: boolean;
|
|
6
9
|
};
|
|
7
10
|
export type InitializeResponse = {
|
|
8
11
|
dynamic_configs: Record<string, ExperimentConfig>;
|
package/dist/Types.d.ts
CHANGED
package/dist/WolvesClient.d.ts
CHANGED
|
@@ -105,6 +105,14 @@ export declare class WolvesClient {
|
|
|
105
105
|
*/
|
|
106
106
|
getExperimentForTest(experimentName: string, groupName: string): Experiment;
|
|
107
107
|
logEvent(eventName: string, value?: string | number, metadata?: Record<string, string>): void;
|
|
108
|
+
/**
|
|
109
|
+
* Manually flush all pending events to the server.
|
|
110
|
+
* This is useful when you want to ensure events are sent immediately,
|
|
111
|
+
* such as before navigating away from a page.
|
|
112
|
+
*
|
|
113
|
+
* @returns Promise that resolves when all events have been sent
|
|
114
|
+
*/
|
|
115
|
+
flush(): Promise<void>;
|
|
108
116
|
shutdown(): Promise<void>;
|
|
109
117
|
private logExposure;
|
|
110
118
|
}
|
package/dist/WolvesClient.js
CHANGED
|
@@ -29,7 +29,7 @@ class WolvesClient {
|
|
|
29
29
|
this.user = user;
|
|
30
30
|
this.network = new Network_1.Network(sdkKey, apiEnv);
|
|
31
31
|
this.store = new Store_1.Store(sdkKey);
|
|
32
|
-
this.logger = new EventLogger_1.EventLogger(this.network);
|
|
32
|
+
this.logger = new EventLogger_1.EventLogger(sdkKey, this.network);
|
|
33
33
|
}
|
|
34
34
|
/**
|
|
35
35
|
* Returns the current loading status of the client.
|
|
@@ -267,13 +267,23 @@ class WolvesClient {
|
|
|
267
267
|
this.user = user;
|
|
268
268
|
}
|
|
269
269
|
getExperiment(experimentName) {
|
|
270
|
-
var _a, _b, _c;
|
|
270
|
+
var _a, _b, _c, _d, _e;
|
|
271
271
|
const { config, details } = this.store.getExperiment(experimentName);
|
|
272
|
+
// Check if experiment is in prestart state (setup status on server)
|
|
273
|
+
const isExperimentActive = (_a = config === null || config === void 0 ? void 0 : config.is_experiment_active) !== null && _a !== void 0 ? _a : true;
|
|
274
|
+
const isUserInExperiment = (_b = config === null || config === void 0 ? void 0 : config.is_user_in_experiment) !== null && _b !== void 0 ? _b : true;
|
|
275
|
+
const isPrestart = !isExperimentActive && (config === null || config === void 0 ? void 0 : config.group) === 'prestart';
|
|
276
|
+
// For prestart experiments, log the prestart state
|
|
277
|
+
if (isPrestart) {
|
|
278
|
+
Log_1.Log.info(`Experiment "${experimentName}" is in prestart state`);
|
|
279
|
+
}
|
|
272
280
|
const experiment = {
|
|
273
281
|
name: experimentName,
|
|
274
|
-
value: (
|
|
275
|
-
experimentID: (
|
|
276
|
-
groupName: (
|
|
282
|
+
value: (_c = config === null || config === void 0 ? void 0 : config.value) !== null && _c !== void 0 ? _c : {},
|
|
283
|
+
experimentID: (_d = config === null || config === void 0 ? void 0 : config.experiment_id) !== null && _d !== void 0 ? _d : '',
|
|
284
|
+
groupName: (_e = config === null || config === void 0 ? void 0 : config.group) !== null && _e !== void 0 ? _e : null,
|
|
285
|
+
isExperimentActive,
|
|
286
|
+
isUserInExperiment,
|
|
277
287
|
get: (key, defaultValue) => {
|
|
278
288
|
var _a;
|
|
279
289
|
const val = (_a = config === null || config === void 0 ? void 0 : config.value) === null || _a === void 0 ? void 0 : _a[key];
|
|
@@ -293,20 +303,28 @@ class WolvesClient {
|
|
|
293
303
|
* The experiment ID is retrieved from the store config.
|
|
294
304
|
*/
|
|
295
305
|
getExperimentForTest(experimentName, groupName) {
|
|
296
|
-
var _a;
|
|
306
|
+
var _a, _b, _c;
|
|
297
307
|
const { config, details } = this.store.getExperiment(experimentName);
|
|
298
308
|
const experimentId = (_a = config === null || config === void 0 ? void 0 : config.experiment_id) !== null && _a !== void 0 ? _a : '';
|
|
309
|
+
const isExperimentActive = (_b = config === null || config === void 0 ? void 0 : config.is_experiment_active) !== null && _b !== void 0 ? _b : true;
|
|
310
|
+
const isUserInExperiment = (_c = config === null || config === void 0 ? void 0 : config.is_user_in_experiment) !== null && _c !== void 0 ? _c : true;
|
|
299
311
|
const experiment = {
|
|
300
312
|
name: experimentName,
|
|
301
313
|
value: {},
|
|
302
314
|
experimentID: experimentId,
|
|
303
315
|
groupName: groupName,
|
|
316
|
+
isExperimentActive,
|
|
317
|
+
isUserInExperiment,
|
|
304
318
|
get: (_key, defaultValue) => {
|
|
305
319
|
return defaultValue;
|
|
306
320
|
}
|
|
307
321
|
};
|
|
308
322
|
// Log exposure with the specified group and experiment ID from store
|
|
309
|
-
this.logExposure(experimentName, {
|
|
323
|
+
this.logExposure(experimentName, {
|
|
324
|
+
group: groupName,
|
|
325
|
+
experiment_id: experimentId,
|
|
326
|
+
value: {}
|
|
327
|
+
}, details);
|
|
310
328
|
return experiment;
|
|
311
329
|
}
|
|
312
330
|
logEvent(eventName, value, metadata) {
|
|
@@ -319,6 +337,18 @@ class WolvesClient {
|
|
|
319
337
|
};
|
|
320
338
|
this.logger.enqueue(event);
|
|
321
339
|
}
|
|
340
|
+
/**
|
|
341
|
+
* Manually flush all pending events to the server.
|
|
342
|
+
* This is useful when you want to ensure events are sent immediately,
|
|
343
|
+
* such as before navigating away from a page.
|
|
344
|
+
*
|
|
345
|
+
* @returns Promise that resolves when all events have been sent
|
|
346
|
+
*/
|
|
347
|
+
flush() {
|
|
348
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
349
|
+
yield this.logger.flush();
|
|
350
|
+
});
|
|
351
|
+
}
|
|
322
352
|
shutdown() {
|
|
323
353
|
return __awaiter(this, void 0, void 0, function* () {
|
|
324
354
|
yield this.logger.stop();
|
package/dist/WolvesMetadata.d.ts
CHANGED
package/dist/WolvesMetadata.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.WolvesMetadataProvider = exports.SDK_TYPE = exports.SDK_VERSION = void 0;
|
|
4
|
-
exports.SDK_VERSION = '1.0.
|
|
4
|
+
exports.SDK_VERSION = '1.0.7';
|
|
5
5
|
exports.SDK_TYPE = 'wolves-js-client';
|
|
6
6
|
let metadata = {
|
|
7
7
|
sdk_version: exports.SDK_VERSION,
|
package/package.json
CHANGED
package/dist/DeviceInfo.d.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* DeviceInfo - Collects device and environment information for event metadata
|
|
3
|
-
* Aligns with Statsig SDK's approach: sends raw user_agent for server-side parsing
|
|
4
|
-
*
|
|
5
|
-
* Note: Browser/OS parsing is done server-side from user_agent, not client-side
|
|
6
|
-
*/
|
|
7
|
-
export type DeviceInfoData = {
|
|
8
|
-
user_agent: string;
|
|
9
|
-
locale: string;
|
|
10
|
-
screen_width: number | null;
|
|
11
|
-
screen_height: number | null;
|
|
12
|
-
viewport_width: number | null;
|
|
13
|
-
viewport_height: number | null;
|
|
14
|
-
timezone: string;
|
|
15
|
-
timezone_offset: number | null;
|
|
16
|
-
};
|
|
17
|
-
export declare function getDeviceInfo(): DeviceInfoData;
|
|
18
|
-
/**
|
|
19
|
-
* Get device info as a flat record suitable for event metadata
|
|
20
|
-
* Only includes non-null values
|
|
21
|
-
*/
|
|
22
|
-
export declare function getDeviceInfoForMetadata(): Record<string, string | number>;
|
package/dist/DeviceInfo.js
DELETED
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* DeviceInfo - Collects device and environment information for event metadata
|
|
4
|
-
* Aligns with Statsig SDK's approach: sends raw user_agent for server-side parsing
|
|
5
|
-
*
|
|
6
|
-
* Note: Browser/OS parsing is done server-side from user_agent, not client-side
|
|
7
|
-
*/
|
|
8
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.getDeviceInfo = getDeviceInfo;
|
|
10
|
-
exports.getDeviceInfoForMetadata = getDeviceInfoForMetadata;
|
|
11
|
-
function getWindowSafe() {
|
|
12
|
-
if (typeof window !== 'undefined') {
|
|
13
|
-
return window;
|
|
14
|
-
}
|
|
15
|
-
return null;
|
|
16
|
-
}
|
|
17
|
-
function getNavigatorSafe() {
|
|
18
|
-
var _a;
|
|
19
|
-
const win = getWindowSafe();
|
|
20
|
-
return (_a = win === null || win === void 0 ? void 0 : win.navigator) !== null && _a !== void 0 ? _a : null;
|
|
21
|
-
}
|
|
22
|
-
function getSafeTimezone() {
|
|
23
|
-
try {
|
|
24
|
-
return Intl.DateTimeFormat().resolvedOptions().timeZone || '';
|
|
25
|
-
}
|
|
26
|
-
catch (_a) {
|
|
27
|
-
return '';
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
function getSafeTimezoneOffset() {
|
|
31
|
-
try {
|
|
32
|
-
return new Date().getTimezoneOffset();
|
|
33
|
-
}
|
|
34
|
-
catch (_a) {
|
|
35
|
-
return null;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
let cachedDeviceInfo = null;
|
|
39
|
-
function getDeviceInfo() {
|
|
40
|
-
var _a, _b, _c, _d, _e, _f;
|
|
41
|
-
// Return cached info if available
|
|
42
|
-
if (cachedDeviceInfo) {
|
|
43
|
-
return cachedDeviceInfo;
|
|
44
|
-
}
|
|
45
|
-
const win = getWindowSafe();
|
|
46
|
-
const nav = getNavigatorSafe();
|
|
47
|
-
if (!win || !nav) {
|
|
48
|
-
// Return empty values for server-side or non-browser environments
|
|
49
|
-
return {
|
|
50
|
-
user_agent: '',
|
|
51
|
-
locale: '',
|
|
52
|
-
screen_width: null,
|
|
53
|
-
screen_height: null,
|
|
54
|
-
viewport_width: null,
|
|
55
|
-
viewport_height: null,
|
|
56
|
-
timezone: '',
|
|
57
|
-
timezone_offset: null,
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
const userAgent = nav.userAgent || '';
|
|
61
|
-
cachedDeviceInfo = {
|
|
62
|
-
// Send raw user_agent - server will parse browser/os info
|
|
63
|
-
user_agent: userAgent.length > 500 ? userAgent.substring(0, 500) : userAgent,
|
|
64
|
-
locale: nav.language || '',
|
|
65
|
-
screen_width: (_b = (_a = win.screen) === null || _a === void 0 ? void 0 : _a.width) !== null && _b !== void 0 ? _b : null,
|
|
66
|
-
screen_height: (_d = (_c = win.screen) === null || _c === void 0 ? void 0 : _c.height) !== null && _d !== void 0 ? _d : null,
|
|
67
|
-
viewport_width: (_e = win.innerWidth) !== null && _e !== void 0 ? _e : null,
|
|
68
|
-
viewport_height: (_f = win.innerHeight) !== null && _f !== void 0 ? _f : null,
|
|
69
|
-
timezone: getSafeTimezone(),
|
|
70
|
-
timezone_offset: getSafeTimezoneOffset(),
|
|
71
|
-
};
|
|
72
|
-
return cachedDeviceInfo;
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Get device info as a flat record suitable for event metadata
|
|
76
|
-
* Only includes non-null values
|
|
77
|
-
*/
|
|
78
|
-
function getDeviceInfoForMetadata() {
|
|
79
|
-
const info = getDeviceInfo();
|
|
80
|
-
const result = {};
|
|
81
|
-
if (info.user_agent)
|
|
82
|
-
result['user_agent'] = info.user_agent;
|
|
83
|
-
if (info.locale)
|
|
84
|
-
result['locale'] = info.locale;
|
|
85
|
-
if (info.screen_width !== null)
|
|
86
|
-
result['screen_width'] = info.screen_width;
|
|
87
|
-
if (info.screen_height !== null)
|
|
88
|
-
result['screen_height'] = info.screen_height;
|
|
89
|
-
if (info.viewport_width !== null)
|
|
90
|
-
result['viewport_width'] = info.viewport_width;
|
|
91
|
-
if (info.viewport_height !== null)
|
|
92
|
-
result['viewport_height'] = info.viewport_height;
|
|
93
|
-
if (info.timezone)
|
|
94
|
-
result['timezone'] = info.timezone;
|
|
95
|
-
if (info.timezone_offset !== null)
|
|
96
|
-
result['timezone_offset'] = info.timezone_offset;
|
|
97
|
-
return result;
|
|
98
|
-
}
|
package/dist/StatsigClient.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { StatsigUser } from './StatsigUser';
|
|
2
|
-
import { Experiment, StatsigUpdateDetails } from './Types';
|
|
3
|
-
export declare class StatsigClient {
|
|
4
|
-
private sdkKey;
|
|
5
|
-
private user;
|
|
6
|
-
private network;
|
|
7
|
-
private store;
|
|
8
|
-
private initialized;
|
|
9
|
-
constructor(sdkKey: string, user: StatsigUser);
|
|
10
|
-
initializeAsync(): Promise<StatsigUpdateDetails>;
|
|
11
|
-
initializeSync(): StatsigUpdateDetails;
|
|
12
|
-
getExperiment(experimentName: string): Experiment;
|
|
13
|
-
logEvent(eventName: string, value?: string | number, metadata?: Record<string, string>): void;
|
|
14
|
-
private logExposure;
|
|
15
|
-
}
|
package/dist/StatsigClient.js
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
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
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.StatsigClient = void 0;
|
|
13
|
-
const Network_1 = require("./Network");
|
|
14
|
-
const Store_1 = require("./Store");
|
|
15
|
-
class StatsigClient {
|
|
16
|
-
constructor(sdkKey, user) {
|
|
17
|
-
this.initialized = false;
|
|
18
|
-
this.sdkKey = sdkKey;
|
|
19
|
-
this.user = user;
|
|
20
|
-
this.network = new Network_1.Network(sdkKey);
|
|
21
|
-
this.store = new Store_1.Store();
|
|
22
|
-
}
|
|
23
|
-
initializeAsync() {
|
|
24
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
-
const config = yield this.network.fetchConfig(this.user);
|
|
26
|
-
if (config) {
|
|
27
|
-
this.store.setValues(config);
|
|
28
|
-
this.initialized = true;
|
|
29
|
-
return { success: true };
|
|
30
|
-
}
|
|
31
|
-
return { success: false, errorMessage: 'Failed to fetch config' };
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
initializeSync() {
|
|
35
|
-
// In a real implementation, this might load from local storage or cache.
|
|
36
|
-
// For this minimal implementation, we trigger a background fetch.
|
|
37
|
-
this.network.fetchConfig(this.user).then((config) => {
|
|
38
|
-
if (config) {
|
|
39
|
-
this.store.setValues(config);
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
this.initialized = true;
|
|
43
|
-
return { success: true };
|
|
44
|
-
}
|
|
45
|
-
getExperiment(experimentName) {
|
|
46
|
-
var _a, _b, _c;
|
|
47
|
-
const config = this.store.getExperiment(experimentName);
|
|
48
|
-
const experiment = {
|
|
49
|
-
name: experimentName,
|
|
50
|
-
value: (_a = config === null || config === void 0 ? void 0 : config.value) !== null && _a !== void 0 ? _a : {},
|
|
51
|
-
ruleID: (_b = config === null || config === void 0 ? void 0 : config.rule_id) !== null && _b !== void 0 ? _b : '',
|
|
52
|
-
groupName: (_c = config === null || config === void 0 ? void 0 : config.group) !== null && _c !== void 0 ? _c : null,
|
|
53
|
-
get: (key, defaultValue) => {
|
|
54
|
-
var _a;
|
|
55
|
-
const val = (_a = config === null || config === void 0 ? void 0 : config.value) === null || _a === void 0 ? void 0 : _a[key];
|
|
56
|
-
if (val === undefined || val === null) {
|
|
57
|
-
return defaultValue;
|
|
58
|
-
}
|
|
59
|
-
return val;
|
|
60
|
-
}
|
|
61
|
-
};
|
|
62
|
-
// Log exposure
|
|
63
|
-
this.logExposure(experimentName, config);
|
|
64
|
-
return experiment;
|
|
65
|
-
}
|
|
66
|
-
logEvent(eventName, value, metadata) {
|
|
67
|
-
const event = {
|
|
68
|
-
eventName,
|
|
69
|
-
value,
|
|
70
|
-
metadata,
|
|
71
|
-
user: this.user,
|
|
72
|
-
time: Date.now(),
|
|
73
|
-
};
|
|
74
|
-
this.network.sendEvents([event]);
|
|
75
|
-
}
|
|
76
|
-
logExposure(experimentName, experiment) {
|
|
77
|
-
var _a, _b, _c;
|
|
78
|
-
const exposureEvent = {
|
|
79
|
-
eventName: 'statsig::exposure',
|
|
80
|
-
user: this.user,
|
|
81
|
-
time: Date.now(),
|
|
82
|
-
metadata: {
|
|
83
|
-
config: experimentName,
|
|
84
|
-
ruleID: (_a = experiment === null || experiment === void 0 ? void 0 : experiment.rule_id) !== null && _a !== void 0 ? _a : '',
|
|
85
|
-
group: (_b = experiment === null || experiment === void 0 ? void 0 : experiment.group) !== null && _b !== void 0 ? _b : '',
|
|
86
|
-
value: JSON.stringify((_c = experiment === null || experiment === void 0 ? void 0 : experiment.value) !== null && _c !== void 0 ? _c : {}),
|
|
87
|
-
},
|
|
88
|
-
};
|
|
89
|
-
this.network.sendEvents([exposureEvent]);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
exports.StatsigClient = StatsigClient;
|
package/dist/StatsigUser.d.ts
DELETED
package/dist/StatsigUser.js
DELETED