@statsig/client-core 0.0.1-beta.30 → 0.0.1-beta.31

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@statsig/client-core",
3
- "version": "0.0.1-beta.30",
3
+ "version": "0.0.1-beta.31",
4
4
  "dependencies": {},
5
5
  "type": "commonjs",
6
6
  "main": "./src/index.js",
@@ -5,7 +5,7 @@ export type StatsigGlobal = {
5
5
  lastInstance?: StatsigClientInterface;
6
6
  acInstances?: Record<string, unknown>;
7
7
  srInstances?: Record<string, unknown>;
8
- instance?: (sdkKey: string) => StatsigClientInterface | undefined;
8
+ instance: (sdkKey?: string) => StatsigClientInterface | undefined;
9
9
  };
10
10
  declare global {
11
11
  let __STATSIG__: StatsigGlobal | undefined;
@@ -14,3 +14,4 @@ declare global {
14
14
  }
15
15
  }
16
16
  export declare const _getStatsigGlobal: () => StatsigGlobal;
17
+ export declare const _getInstance: (sdkKey: string) => StatsigClientInterface | undefined;
@@ -2,16 +2,23 @@
2
2
  /* eslint-disable @typescript-eslint/no-unsafe-assignment */
3
3
  var _a, _b, _c;
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
5
- exports._getStatsigGlobal = void 0;
5
+ exports._getInstance = exports._getStatsigGlobal = void 0;
6
+ const _getStatsigGlobal = () => {
7
+ return __STATSIG__ !== null && __STATSIG__ !== void 0 ? __STATSIG__ : statsigGlobal;
8
+ };
9
+ exports._getStatsigGlobal = _getStatsigGlobal;
10
+ const _getInstance = (sdkKey) => {
11
+ var _a;
12
+ return sdkKey ? (_a = __STATSIG__ === null || __STATSIG__ === void 0 ? void 0 : __STATSIG__.instances) === null || _a === void 0 ? void 0 : _a[sdkKey] : __STATSIG__ === null || __STATSIG__ === void 0 ? void 0 : __STATSIG__.lastInstance;
13
+ };
14
+ exports._getInstance = _getInstance;
6
15
  const GLOBAL_KEY = '__STATSIG__';
7
16
  const _window = typeof window !== 'undefined' ? window : {};
8
17
  const _global = typeof global !== 'undefined' ? global : {};
9
18
  const _globalThis = typeof globalThis !== 'undefined' ? globalThis : {};
10
- const statsigGlobal = (_c = (_b = (_a = _window[GLOBAL_KEY]) !== null && _a !== void 0 ? _a : _global[GLOBAL_KEY]) !== null && _b !== void 0 ? _b : _globalThis[GLOBAL_KEY]) !== null && _c !== void 0 ? _c : {};
19
+ const statsigGlobal = (_c = (_b = (_a = _window[GLOBAL_KEY]) !== null && _a !== void 0 ? _a : _global[GLOBAL_KEY]) !== null && _b !== void 0 ? _b : _globalThis[GLOBAL_KEY]) !== null && _c !== void 0 ? _c : {
20
+ instance: exports._getInstance,
21
+ };
11
22
  _window[GLOBAL_KEY] = statsigGlobal;
12
23
  _global[GLOBAL_KEY] = statsigGlobal;
13
24
  _globalThis[GLOBAL_KEY] = statsigGlobal;
14
- const _getStatsigGlobal = () => {
15
- return __STATSIG__ !== null && __STATSIG__ !== void 0 ? __STATSIG__ : statsigGlobal;
16
- };
17
- exports._getStatsigGlobal = _getStatsigGlobal;
@@ -42,6 +42,9 @@ class NetworkCore {
42
42
  }
43
43
  beacon(args) {
44
44
  return __awaiter(this, void 0, void 0, function* () {
45
+ if (!_ensureValidSdkKey(args)) {
46
+ return false;
47
+ }
45
48
  const url = yield this._getPopulatedURL(args);
46
49
  const body = yield this._getPopulatedBody(args);
47
50
  return navigator.sendBeacon(url, body);
@@ -50,6 +53,9 @@ class NetworkCore {
50
53
  _sendRequest(args) {
51
54
  var _a;
52
55
  return __awaiter(this, void 0, void 0, function* () {
56
+ if (!_ensureValidSdkKey(args)) {
57
+ return null;
58
+ }
53
59
  const { method, body, retries } = args;
54
60
  const controller = new AbortController();
55
61
  const handle = setTimeout(() => controller.abort(`Timeout of ${this._timeout}ms expired.`), this._timeout);
@@ -131,6 +137,13 @@ class NetworkCore {
131
137
  }
132
138
  }
133
139
  exports.NetworkCore = NetworkCore;
140
+ const _ensureValidSdkKey = (args) => {
141
+ if (!args.sdkKey) {
142
+ Log_1.Log.warn('Unable to make request without an SDK key');
143
+ return false;
144
+ }
145
+ return true;
146
+ };
134
147
  function _getErrorMessage(controller, error) {
135
148
  if (controller.signal.aborted &&
136
149
  typeof controller.signal.reason === 'string') {
@@ -4,11 +4,11 @@ import { EvaluationOptionsCommon } from './EvaluationOptions';
4
4
  import { EventLogger } from './EventLogger';
5
5
  import { NetworkCore } from './NetworkCore';
6
6
  import { OverrideAdapter } from './OverrideAdapter';
7
- import { StatsigClientEvent, StatsigClientEventCallback, StatsigClientEventEmitterInterface, StatsigClientEventName, StatsigLoadingStatus } from './StatsigClientEventEmitter';
7
+ import { AnyStatsigClientEvent, StatsigClientEventCallback, StatsigClientEventEmitterInterface, StatsigClientEventName, StatsigLoadingStatus } from './StatsigClientEventEmitter';
8
8
  import { DataAdapterResult, EvaluationsDataAdapter, SpecsDataAdapter } from './StatsigDataAdapter';
9
9
  import { StatsigEventInternal } from './StatsigEvent';
10
10
  import { AnyStatsigOptions, StatsigRuntimeMutableOptions } from './StatsigOptionsCommon';
11
- export type StatsigClientEmitEventFunc = (event: StatsigClientEvent) => void;
11
+ export type StatsigClientEmitEventFunc = (event: AnyStatsigClientEvent) => void;
12
12
  export type StatsigContext = {
13
13
  sdkKey: string;
14
14
  options: AnyStatsigOptions;
@@ -60,7 +60,7 @@ export declare abstract class StatsigClientBase<TAdapter extends EvaluationsData
60
60
  */
61
61
  off<T extends StatsigClientEventName>(event: T, listener: StatsigClientEventCallback<T>): void;
62
62
  __on<T extends StatsigClientEventName>(event: T, listener: StatsigClientEventCallback<T>): void;
63
- protected _emit(event: StatsigClientEvent): void;
63
+ protected _emit(event: AnyStatsigClientEvent): void;
64
64
  protected _setStatus(newStatus: StatsigLoadingStatus, values: DataAdapterResult | null): void;
65
65
  protected _enqueueExposure(name: string, exposure: StatsigEventInternal, options?: EvaluationOptionsCommon): void;
66
66
  }
@@ -11,6 +11,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.StatsigClientBase = void 0;
13
13
  require("./$_StatsigGlobal");
14
+ const __StatsigGlobal_1 = require("./$_StatsigGlobal");
14
15
  const ErrorBoundary_1 = require("./ErrorBoundary");
15
16
  const EventLogger_1 = require("./EventLogger");
16
17
  const Log_1 = require("./Log");
@@ -34,8 +35,11 @@ class StatsigClientBase {
34
35
  this._emit({ name: 'session_expired' });
35
36
  }, sdkKey);
36
37
  this._errorBoundary = new ErrorBoundary_1.ErrorBoundary(sdkKey);
37
- const statsigGlobal = __STATSIG__ !== null && __STATSIG__ !== void 0 ? __STATSIG__ : {};
38
+ const statsigGlobal = (0, __StatsigGlobal_1._getStatsigGlobal)();
38
39
  const instances = (_c = statsigGlobal.instances) !== null && _c !== void 0 ? _c : {};
40
+ if (instances[sdkKey] != null) {
41
+ Log_1.Log.warn('Creating multiple Statsig clients with the same SDK key can lead to unexpected behavior. Multi-instance support requires different SDK keys.');
42
+ }
39
43
  const inst = this;
40
44
  instances[sdkKey] = inst;
41
45
  statsigGlobal.lastInstance = inst;
@@ -47,16 +47,17 @@ type EventNameToEventDataMap = {
47
47
  *
48
48
  * `layer_evaluation` - Fired when any layer is checked from the Statsig client.
49
49
  */
50
- export type StatsigClientEvent = Flatten<{
50
+ export type AnyStatsigClientEvent = Flatten<{
51
51
  [K in keyof EventNameToEventDataMap]: {
52
52
  name: K;
53
53
  } & EventNameToEventDataMap[K];
54
54
  }[keyof EventNameToEventDataMap]>;
55
- export type AnyStatsigClientEventListener = StatsigClientEventCallback<StatsigClientEventName>;
56
- export type StatsigClientEventName = StatsigClientEvent['name'] | '*';
57
- export type StatsigClientEventCallback<T extends StatsigClientEventName> = (event: T extends '*' ? StatsigClientEvent : Extract<StatsigClientEvent, {
55
+ export type StatsigClientEvent<T> = Extract<AnyStatsigClientEvent, {
58
56
  name: T;
59
- }>) => void;
57
+ }>;
58
+ export type AnyStatsigClientEventListener = StatsigClientEventCallback<StatsigClientEventName>;
59
+ export type StatsigClientEventName = AnyStatsigClientEvent['name'] | '*';
60
+ export type StatsigClientEventCallback<T extends StatsigClientEventName> = (event: T extends '*' ? AnyStatsigClientEvent : StatsigClientEvent<T>) => void;
60
61
  export interface StatsigClientEventEmitterInterface {
61
62
  readonly loadingStatus: StatsigLoadingStatus;
62
63
  on<T extends StatsigClientEventName>(event: T, listener: StatsigClientEventCallback<T>): void;
@@ -1,4 +1,4 @@
1
- export declare const SDK_VERSION = "0.0.1-beta.30";
1
+ export declare const SDK_VERSION = "0.0.1-beta.31";
2
2
  export type StatsigMetadata = {
3
3
  readonly [key: string]: string | undefined;
4
4
  readonly appVersion?: string;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.StatsigMetadataProvider = exports.SDK_VERSION = void 0;
4
- exports.SDK_VERSION = '0.0.1-beta.30';
4
+ exports.SDK_VERSION = '0.0.1-beta.31';
5
5
  let metadata = {
6
6
  sdkVersion: exports.SDK_VERSION,
7
7
  sdkType: 'js-mono', // js-mono is overwritten by Precomp and OnDevice clients
@@ -3,12 +3,12 @@ import { OverrideAdapter } from './OverrideAdapter';
3
3
  /** Options that can be set at init and updated during runtime. */
4
4
  export type StatsigRuntimeMutableOptions = {
5
5
  /**
6
- * Prevents writing anything to storage.
7
- * Note: caching will not work if storage is disabled
6
+ * Prevents sending any events over the network.
8
7
  */
9
8
  disableLogging?: boolean;
10
9
  /**
11
10
  * Prevents writing anything to storage.
11
+ *
12
12
  * Note: caching will not work if storage is disabled
13
13
  */
14
14
  disableStorage?: boolean;