@statsig/client-core 0.0.1-beta.9 → 1.0.1

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.
Files changed (74) hide show
  1. package/package.json +1 -1
  2. package/src/$_StatsigGlobal.d.ts +9 -1
  3. package/src/$_StatsigGlobal.js +29 -12
  4. package/src/CacheKey.d.ts +4 -0
  5. package/src/CacheKey.js +28 -0
  6. package/src/ClientInterfaces.d.ts +46 -12
  7. package/src/DataAdapterCore.d.ts +31 -0
  8. package/src/DataAdapterCore.js +196 -0
  9. package/src/Diagnostics.d.ts +1 -3
  10. package/src/Diagnostics.js +4 -49
  11. package/src/DownloadConfigSpecsResponse.d.ts +41 -0
  12. package/src/DownloadConfigSpecsResponse.js +2 -0
  13. package/src/ErrorBoundary.d.ts +7 -2
  14. package/src/ErrorBoundary.js +81 -88
  15. package/src/EvaluationOptions.d.ts +20 -0
  16. package/src/EvaluationOptions.js +2 -0
  17. package/src/EvaluationTypes.d.ts +39 -0
  18. package/src/EvaluationTypes.js +2 -0
  19. package/src/EventLogger.d.ts +15 -10
  20. package/src/EventLogger.js +216 -256
  21. package/src/Hashing.d.ts +2 -1
  22. package/src/Hashing.js +26 -7
  23. package/src/InitializeResponse.d.ts +20 -0
  24. package/src/InitializeResponse.js +2 -0
  25. package/src/Log.js +15 -34
  26. package/src/NetworkConfig.d.ts +19 -0
  27. package/src/NetworkConfig.js +19 -0
  28. package/src/NetworkCore.d.ts +17 -7
  29. package/src/NetworkCore.js +173 -196
  30. package/src/OverrideAdapter.d.ts +9 -0
  31. package/src/OverrideAdapter.js +2 -0
  32. package/src/SDKType.d.ts +8 -0
  33. package/src/SDKType.js +19 -0
  34. package/src/SafeJs.d.ts +6 -0
  35. package/src/SafeJs.js +41 -0
  36. package/src/SessionID.d.ts +17 -1
  37. package/src/SessionID.js +89 -8
  38. package/src/StableID.js +24 -53
  39. package/src/StatsigClientBase.d.ts +56 -19
  40. package/src/StatsigClientBase.js +126 -48
  41. package/src/StatsigClientEventEmitter.d.ts +54 -33
  42. package/src/StatsigDataAdapter.d.ts +103 -17
  43. package/src/StatsigDataAdapter.js +2 -0
  44. package/src/StatsigEvent.d.ts +10 -18
  45. package/src/StatsigEvent.js +41 -39
  46. package/src/StatsigMetadata.d.ts +1 -1
  47. package/src/StatsigMetadata.js +7 -18
  48. package/src/StatsigOptionsCommon.d.ts +84 -20
  49. package/src/StatsigTypeFactories.d.ts +6 -0
  50. package/src/StatsigTypeFactories.js +50 -0
  51. package/src/StatsigTypes.d.ts +28 -22
  52. package/src/StatsigTypes.js +0 -29
  53. package/src/StatsigUser.d.ts +7 -8
  54. package/src/StatsigUser.js +11 -24
  55. package/src/StorageProvider.d.ts +13 -8
  56. package/src/StorageProvider.js +66 -85
  57. package/src/TypedJsonParse.d.ts +8 -0
  58. package/src/TypedJsonParse.js +27 -0
  59. package/src/UUID.js +9 -5
  60. package/src/UrlOverrides.d.ts +1 -0
  61. package/src/UrlOverrides.js +15 -0
  62. package/src/UtitlityTypes.d.ts +3 -0
  63. package/src/UtitlityTypes.js +2 -0
  64. package/src/VisibilityObserving.d.ts +9 -0
  65. package/src/VisibilityObserving.js +39 -0
  66. package/src/index.d.ts +20 -4
  67. package/src/index.js +26 -18
  68. package/README.md +0 -8
  69. package/src/Monitoring.d.ts +0 -3
  70. package/src/Monitoring.js +0 -33
  71. package/src/VisibilityChangeObserver.d.ts +0 -13
  72. package/src/VisibilityChangeObserver.js +0 -48
  73. package/src/__tests__/MockLocalStorage.d.ts +0 -9
  74. package/src/__tests__/MockLocalStorage.js +0 -37
@@ -1,40 +1,126 @@
1
- import { StatsigOptionsCommon } from './StatsigOptionsCommon';
1
+ import { NetworkPriority } from './NetworkConfig';
2
+ import type { AnyStatsigOptions, NetworkConfigCommon } from './StatsigOptionsCommon';
2
3
  import { StatsigUser } from './StatsigUser';
3
- export type DataSource = 'Uninitialized' | 'Loading' | 'NoValues' | 'Cache' | 'Network' | 'NetworkNotModified' | 'Bootstrap';
4
- export type StatsigDataAdapterResult = {
4
+ export type DataSource = 'Uninitialized' | 'Loading' | 'NoValues' | 'Cache' | 'Network' | 'NetworkNotModified' | 'Bootstrap' | 'Prefetch';
5
+ export type DataAdapterResult = {
5
6
  readonly source: DataSource;
6
7
  readonly data: string;
8
+ readonly receivedAt: number;
9
+ readonly stableID: string | null;
10
+ readonly fullUserHash: string | null;
7
11
  };
12
+ export type DataAdapterAsyncOptions = {
13
+ /**
14
+ * The maximum amount of time (in milliseconds) this operation is permitted to run.
15
+ * If the timeout is hit, null is returned but any in-flight requests are kept alive with results going to cache for future updates.
16
+ *
17
+ * Note: If no timeout is given, the {@link NetworkConfigCommon.networkTimeoutMs|StatsigOptions.networkConfig.networkTimeoutMs} is used.
18
+ */
19
+ readonly timeoutMs?: NetworkConfigCommon['networkTimeoutMs'];
20
+ /**
21
+ * The priority that should be applied to the Http request that is fired.
22
+ *
23
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/fetch#priority
24
+ *
25
+ * default: 'auto'
26
+ */
27
+ readonly priority?: NetworkPriority;
28
+ };
29
+ export declare const DataAdapterCachePrefix = "statsig.cached";
8
30
  /**
9
31
  * Describes a type that is used during intialize/update operations of a Statsig client.
10
32
  *
11
33
  * See below to find the default adapters, but know that it is possible to create your
12
- * own StatsigDataAdapter and provide it via {@link StatsigOptions.dataAdapter}.
34
+ * own StatsigDataAdapter and provide it via `StatsigOptions.dataAdapter`.
13
35
  *
14
- * Defaults:
36
+ * defaults:
15
37
  *
16
- * - {@link PrecomputedEvaluationsClient} uses {@link EvaluationsDataAdapter}
38
+ * - {@link StatsigClient} uses {@link EvaluationsDataAdapter}
17
39
  *
18
- * - {@link OnDeviceEvaluationsClient} uses {@link SpecsDataAdapter}
40
+ * - {@link StatsigOnDeviceEvalClient} uses {@link SpecsDataAdapter}
19
41
  */
20
- export type StatsigDataAdapter = {
42
+ type DataAdapterCommon = {
21
43
  /**
22
44
  * Called when the StatsigDataAdapter is attached to the Statsig client instance during construction.
23
45
  * @param {string} sdkKey The SDK key being used by the Statsig client.
24
46
  * @param {StatsigOptionsCommon | null} options The StatsigOptions being used by the Statsig client.
25
47
  */
26
- readonly attach: (sdkKey: string, options: StatsigOptionsCommon | null) => void;
48
+ readonly attach: (sdkKey: string, options: AnyStatsigOptions | null) => void;
49
+ /**
50
+ * (Internal Use Only) - Used by \@statsig/react-native-bindings to prime the cache from AsyncStorage
51
+ *
52
+ * @param {Record<string, DataAdapterResult>} cache The values to merge into _inMemoryCache
53
+ */
54
+ readonly __primeInMemoryCache: (cache: Record<string, DataAdapterResult>) => void;
55
+ };
56
+ export type EvaluationsDataAdapter = DataAdapterCommon & {
57
+ /**
58
+ * Synchronously get evaluation data for the given user. Called during initializeSync and/or updateUserSync.
59
+ *
60
+ * It is also called during async update operations before StatsigDataAdapter.getDataAsync is called.
61
+ * @param {StatsigUser} user The StatsigUser to get data for.
62
+ * @returns {DataAdapterResult | null} The data that was found for the given StatsigUser.
63
+ */
64
+ readonly getDataSync: (user: StatsigUser) => DataAdapterResult | null;
65
+ /**
66
+ * Asynchronously get evaluation data for the given user. Called during initializeAsync and/or updateUserAsync.
67
+ *
68
+ * @param {DataAdapterResult | null} current The data that was found synchronously (Cache). Will be used as fallback if getDataAsync fails
69
+ * @param {StatsigUser} user The StatsigUser to get data for.
70
+ * @returns {DataAdapterResult | null} The data that was found for the given StatsigUser.
71
+ */
72
+ readonly getDataAsync: (current: DataAdapterResult | null, user: StatsigUser, options?: DataAdapterAsyncOptions) => Promise<DataAdapterResult | null>;
73
+ /**
74
+ * Manually trigger a fetch for new evaluations data for the given user.
75
+ *
76
+ * @param {StatsigUser} user The StatsigUser to get data for.
77
+ */
78
+ readonly prefetchData: (user: StatsigUser, options?: DataAdapterAsyncOptions) => Promise<void>;
79
+ /**
80
+ * Manually set the evaluations data from a JSON string.
81
+ *
82
+ * Statsig Server SDKs supported:
83
+ * - node-js-server-sdk\@5.20.0
84
+ * - java-server-sdk\@1.18.0
85
+ * - python-sdk\@0.32.0
86
+ * - ruby-sdk\@1.34.0
87
+ * - dotnet-sdk\@1.25.0
88
+ * - php-sdk\@3.2.0
89
+ *
90
+ * Note: You can use {@link EvaluationsDataAdapter.setDataLegacy} if your server is running an older Statsig Server SDK.
91
+ */
92
+ readonly setData: (data: string) => void;
93
+ /**
94
+ * Manually set evaluations data for the given user.
95
+ *
96
+ * @param {StatsigUser} user The StatsigUser this data is for.
97
+ *
98
+ * @deprecated This method is provided only to support older versions of Statsig server SDKs.
99
+ * Newer SDKs include the StatsigUser as part of the data string, and can be used with the {@link EvaluationsDataAdapter.setData} method instead.
100
+ */
101
+ readonly setDataLegacy: (data: string, user: StatsigUser) => void;
102
+ };
103
+ export type SpecsDataAdapter = DataAdapterCommon & {
27
104
  /**
28
- * Synchronously get data for the given user (if any). Called during initializeSync and/or updateUserSync.
105
+ * Synchronously get specs data. Called during initializeSync and/or updateUserSync.
29
106
  * It is also called during async update operations before StatsigDataAdapter.getDataAsync is called.
30
- * @param {StatsigUser | undefined} user The StatsigUser to get data for.
31
- * @returns {StatsigDataAdapterResult | null} The data that was found for the given StatsigUser.
107
+ * @returns {DataAdapterResult | null} The data that was found for the given StatsigUser.
108
+ */
109
+ readonly getDataSync: () => DataAdapterResult | null;
110
+ /**
111
+ * Asynchronously get specs data. Called during initializeAsync and/or updateUserAsync.
112
+ *
113
+ * @param {DataAdapterResult | null} current The data that was found synchronously (Cache). Will be used as fallback if getDataAsync fails
114
+ * @returns {DataAdapterResult | null} The data that was found for the given StatsigUser.
115
+ */
116
+ readonly getDataAsync: (current: DataAdapterResult | null, options?: DataAdapterAsyncOptions) => Promise<DataAdapterResult | null>;
117
+ /**
118
+ * Manually trigger a fetch for new specs data.
32
119
  */
33
- readonly getDataSync: (user?: StatsigUser) => StatsigDataAdapterResult | null;
120
+ readonly prefetchData: (options?: DataAdapterAsyncOptions) => Promise<void>;
34
121
  /**
35
- * Asynchronously get data for the given user (if any). Called during initializeAsync and/or updateUserAsync.
36
- * @param {StatsigUser | undefined} user The StatsigUser to get data for.
37
- * @returns {StatsigDataAdapterResult | null} The data that was found for the given StatsigUser.
122
+ * Manually set specs data (Bootstrap).
38
123
  */
39
- readonly getDataAsync: (current: StatsigDataAdapterResult | null, user?: StatsigUser) => Promise<StatsigDataAdapterResult | null>;
124
+ readonly setData: (data: string) => void;
40
125
  };
126
+ export {};
@@ -1,2 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DataAdapterCachePrefix = void 0;
4
+ exports.DataAdapterCachePrefix = 'statsig.cached';
@@ -1,10 +1,6 @@
1
- import { DynamicConfig, EvaluationDetails, FeatureGate } from './StatsigTypes';
1
+ import { SecondaryExposure } from './EvaluationTypes';
2
+ import { DynamicConfig, FeatureGate, Layer } from './StatsigTypes';
2
3
  import { StatsigUser } from './StatsigUser';
3
- export type SecondaryExposure = {
4
- gate: string;
5
- gateValue: string;
6
- ruleID: string;
7
- };
8
4
  export type StatsigEvent = {
9
5
  eventName: string;
10
6
  value?: string | number | null;
@@ -12,19 +8,15 @@ export type StatsigEvent = {
12
8
  [key: string]: string;
13
9
  } | null;
14
10
  };
15
- export type StatsigEventInternal = StatsigEvent & {
11
+ export type StatsigEventInternal = Omit<StatsigEvent, 'metadata'> & {
16
12
  user: StatsigUser | null;
17
13
  time: number;
14
+ metadata?: {
15
+ [key: string]: unknown;
16
+ } | null;
18
17
  secondaryExposures?: SecondaryExposure[];
19
18
  };
20
- export declare function isExposureEvent({ eventName }: StatsigEventInternal): boolean;
21
- export declare function createGateExposure(user: StatsigUser, gate: FeatureGate, secondaryExposures: SecondaryExposure[] | undefined): StatsigEventInternal;
22
- export declare function createConfigExposure(user: StatsigUser, config: DynamicConfig, secondaryExposures: SecondaryExposure[] | undefined): StatsigEventInternal;
23
- export declare function createLayerParameterExposure(user: StatsigUser, layerName: string, parameterName: string, spec: {
24
- rule_id: string;
25
- explicit_parameters: string[];
26
- undelegated_secondary_exposures?: SecondaryExposure[];
27
- secondary_exposures: SecondaryExposure[];
28
- allocated_experiment_name: string;
29
- details: EvaluationDetails;
30
- }): StatsigEventInternal;
19
+ export declare const _isExposureEvent: ({ eventName, }: StatsigEventInternal) => boolean;
20
+ export declare const _createGateExposure: (user: StatsigUser, gate: FeatureGate) => StatsigEventInternal;
21
+ export declare const _createConfigExposure: (user: StatsigUser, config: DynamicConfig) => StatsigEventInternal;
22
+ export declare const _createLayerParameterExposure: (user: StatsigUser, layer: Layer, parameterName: string) => StatsigEventInternal;
@@ -1,58 +1,60 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createLayerParameterExposure = exports.createConfigExposure = exports.createGateExposure = exports.isExposureEvent = void 0;
4
- var CONFIG_EXPOSURE_NAME = 'statsig::config_exposure';
5
- var GATE_EXPOSURE_NAME = 'statsig::gate_exposure';
6
- var LAYER_EXPOSURE_NAME = 'statsig::layer_exposure';
7
- function createExposure(eventName, user, details, metadata, secondaryExposures) {
3
+ exports._createLayerParameterExposure = exports._createConfigExposure = exports._createGateExposure = exports._isExposureEvent = void 0;
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
+ const _createExposure = (eventName, user, details, metadata, secondaryExposures) => {
8
8
  return {
9
- eventName: eventName,
10
- user: user,
9
+ eventName,
10
+ user,
11
11
  value: null,
12
12
  metadata: _addEvaluationDetailsToMetadata(details, metadata),
13
- secondaryExposures: secondaryExposures,
13
+ secondaryExposures,
14
14
  time: Date.now(),
15
15
  };
16
- }
17
- function isExposureEvent(_a) {
18
- var eventName = _a.eventName;
16
+ };
17
+ const _isExposureEvent = ({ eventName, }) => {
19
18
  return eventName === GATE_EXPOSURE_NAME || eventName === CONFIG_EXPOSURE_NAME;
20
- }
21
- exports.isExposureEvent = isExposureEvent;
22
- function createGateExposure(user, gate, secondaryExposures) {
23
- return createExposure(GATE_EXPOSURE_NAME, user, gate.details, {
19
+ };
20
+ exports._isExposureEvent = _isExposureEvent;
21
+ const _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
- }, secondaryExposures !== null && secondaryExposures !== void 0 ? secondaryExposures : []);
28
- }
29
- exports.createGateExposure = createGateExposure;
30
- function createConfigExposure(user, config, secondaryExposures) {
31
- return createExposure(CONFIG_EXPOSURE_NAME, user, config.details, {
27
+ }, (_b = (_a = gate.__evaluation) === null || _a === void 0 ? void 0 : _a.secondary_exposures) !== null && _b !== void 0 ? _b : []);
28
+ };
29
+ exports._createGateExposure = _createGateExposure;
30
+ const _createConfigExposure = (user, config) => {
31
+ var _a, _b;
32
+ return _createExposure(CONFIG_EXPOSURE_NAME, user, config.details, {
32
33
  config: config.name,
33
34
  ruleID: config.ruleID,
34
- }, secondaryExposures !== null && secondaryExposures !== void 0 ? secondaryExposures : []);
35
- }
36
- exports.createConfigExposure = createConfigExposure;
37
- function createLayerParameterExposure(user, layerName, parameterName, spec) {
38
- var _a;
39
- var isExplicit = spec.explicit_parameters.includes(parameterName);
40
- var allocatedExperiment = '';
41
- var secondaryExposures = (_a = spec.undelegated_secondary_exposures) !== null && _a !== void 0 ? _a : [];
35
+ }, (_b = (_a = config.__evaluation) === null || _a === void 0 ? void 0 : _a.secondary_exposures) !== null && _b !== void 0 ? _b : []);
36
+ };
37
+ exports._createConfigExposure = _createConfigExposure;
38
+ const _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 : [];
42
44
  if (isExplicit) {
43
- allocatedExperiment = spec.allocated_experiment_name;
44
- secondaryExposures = spec.secondary_exposures;
45
+ allocatedExperiment = (_c = evaluation.allocated_experiment_name) !== null && _c !== void 0 ? _c : '';
46
+ secondaryExposures = evaluation.secondary_exposures;
45
47
  }
46
- return createExposure(LAYER_EXPOSURE_NAME, user, spec.details, {
47
- config: layerName,
48
- parameterName: parameterName,
49
- ruleID: spec.rule_id,
50
- allocatedExperiment: allocatedExperiment,
48
+ return _createExposure(LAYER_EXPOSURE_NAME, user, layer.details, {
49
+ config: layer.name,
50
+ parameterName,
51
+ ruleID: layer.ruleID,
52
+ allocatedExperiment,
51
53
  isExplicitParameter: String(isExplicit),
52
54
  }, secondaryExposures);
53
- }
54
- exports.createLayerParameterExposure = createLayerParameterExposure;
55
- function _addEvaluationDetailsToMetadata(details, metadata) {
55
+ };
56
+ exports._createLayerParameterExposure = _createLayerParameterExposure;
57
+ const _addEvaluationDetailsToMetadata = (details, metadata) => {
56
58
  metadata['reason'] = details.reason;
57
59
  if (details.lcut) {
58
60
  metadata['lcut'] = String(details.lcut);
@@ -61,4 +63,4 @@ function _addEvaluationDetailsToMetadata(details, metadata) {
61
63
  metadata['receivedAt'] = String(details.receivedAt);
62
64
  }
63
65
  return metadata;
64
- }
66
+ };
@@ -1,10 +1,10 @@
1
+ export declare const SDK_VERSION = "1.0.1";
1
2
  export type StatsigMetadata = {
2
3
  readonly [key: string]: string | undefined;
3
4
  readonly appVersion?: string;
4
5
  readonly deviceModel?: string;
5
6
  readonly deviceModelName?: string;
6
7
  readonly locale?: string;
7
- readonly sdkType: string;
8
8
  readonly sdkVersion: string;
9
9
  readonly stableID?: string;
10
10
  readonly systemName?: string;
@@ -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
- exports.StatsigMetadataProvider = void 0;
15
- var SDK_VERSION = '0.0.1-beta.9';
16
- var metadata = {
17
- sdkVersion: SDK_VERSION,
3
+ exports.StatsigMetadataProvider = exports.SDK_VERSION = void 0;
4
+ exports.SDK_VERSION = '1.0.1';
5
+ let metadata = {
6
+ sdkVersion: exports.SDK_VERSION,
18
7
  sdkType: 'js-mono', // js-mono is overwritten by Precomp and OnDevice clients
19
8
  };
20
9
  exports.StatsigMetadataProvider = {
21
- get: function () { return metadata; },
22
- add: function (additions) {
23
- metadata = __assign(__assign({}, metadata), additions);
10
+ get: () => metadata,
11
+ add: (additions) => {
12
+ metadata = Object.assign(Object.assign({}, metadata), additions);
24
13
  },
25
14
  };
@@ -1,47 +1,111 @@
1
1
  import { LogLevel } from './Log';
2
- import { StatsigDataAdapter } from './StatsigDataAdapter';
3
- /** Common options for configuring the Statsig SDK. */
4
- export type StatsigOptionsCommon = {
2
+ import { NetworkArgs } from './NetworkConfig';
3
+ import { OverrideAdapter } from './OverrideAdapter';
4
+ /** Options that can be set at init and updated during runtime. */
5
+ export type StatsigRuntimeMutableOptions = {
6
+ /**
7
+ * Prevents sending any events over the network.
8
+ */
9
+ disableLogging?: boolean;
10
+ /**
11
+ * Prevents writing anything to storage.
12
+ *
13
+ * Note: caching will not work if storage is disabled
14
+ */
15
+ disableStorage?: boolean;
16
+ };
17
+ export type NetworkConfigCommon = {
5
18
  /**
6
19
  * The API to use for all SDK network requests. You should not need to override this
7
- * unless you have another API that implements the Statsig API endpoints.
20
+ * unless you have a custom API that implements the Statsig endpoints.
8
21
  */
9
22
  api?: string;
10
23
  /**
11
- * An object you can use to set environment variables that apply to all of your users
12
- * in the same session.
24
+ * The URL used to flush queued events via a POST request. Takes precedence over {@link StatsigOptionsCommon.api}.
25
+ *
26
+ * default: `https://featuregates.org/v1/initialize`
13
27
  */
14
- environment?: StatsigEnvironment;
28
+ logEventUrl?: string;
15
29
  /**
16
- * Overrides the auto-generated StableID that is set for the device.
30
+ * The maximum amount of time (in milliseconds) that any network request can take
31
+ * before timing out.
32
+ *
33
+ * default: `10,000 ms` (10 seconds)
17
34
  */
18
- overrideStableID?: string;
35
+ networkTimeoutMs?: number;
19
36
  /**
20
- * How much information is allowed to be printed to the console.
21
- * Default: LogLevel.Warn
37
+ * Intended for testing purposes. Prevents any network requests being made.
22
38
  */
23
- logLevel?: LogLevel;
39
+ preventAllNetworkTraffic?: boolean;
24
40
  /**
25
- * StatsigDataAdapter implementor used to customize the initialization/update flow.
26
- * Default: EvaluationsDataAdapter (Precomputed) or SpecsDataAdapter (OnDevice)
41
+ * Overrides the default networking layer used by the Statsig client.
42
+ * By default, the client use `fetch`, but overriding this
43
+ * you could use `axios` or raw `XMLHttpRequest`
44
+ *
45
+ * default: `Fetch API`
46
+ *
47
+ * @param {string} url Where the request is going.
48
+ * @param {NetworkArgs} args Configuration for the network request.
49
+ * @returns {Response}
27
50
  */
28
- dataAdapter?: StatsigDataAdapter;
51
+ networkOverrideFunc?: (url: string, args: NetworkArgs) => Promise<Response>;
52
+ };
53
+ /** Options for configuring a Statsig client. */
54
+ export type StatsigOptionsCommon<NetworkConfig extends NetworkConfigCommon> = StatsigRuntimeMutableOptions & {
29
55
  /**
30
- * The maximum amount of time (in milliseconds) that any network request can take
31
- * before timing out. Default: 10,000 (10 seconds)
56
+ * Allows for fine grained control over which api or urls are hit for specific Statsig network requests.
57
+ *
58
+ * For defaults see {@link StatsigClientUrlOverrideOptions}
32
59
  */
33
- networkTimeoutMs?: number;
60
+ networkConfig?: NetworkConfig;
61
+ /**
62
+ * An object you can use to set environment variables that apply to all of your users
63
+ * in the same session.
64
+ */
65
+ environment?: StatsigEnvironment;
66
+ /**
67
+ * How much information is allowed to be printed to the console.
68
+ *
69
+ * default: {@link LogLevel.Warn}
70
+ */
71
+ logLevel?: LogLevel;
34
72
  /**
35
73
  * The maximum number of events to batch before flushing logs to Statsig.
36
- * Default: 50
74
+ *
75
+ * default: `50`
37
76
  */
38
77
  loggingBufferMaxSize?: number;
39
78
  /**
40
79
  * How often (in milliseconds) to flush logs to Statsig.
41
- * Default: 10,000 (10 seconds)
80
+ *
81
+ * default: `10,000 ms` (10 seconds)
42
82
  */
43
83
  loggingIntervalMs?: number;
84
+ /**
85
+ * An implementor of {@link OverrideAdapter}, used to alter evaluations before its
86
+ * returned to the caller of a check api (checkGate/getExperiment etc).
87
+ */
88
+ overrideAdapter?: OverrideAdapter;
89
+ /**
90
+ * (Web only) Should the 'current page' url be included with logged events.
91
+ *
92
+ * default: true
93
+ */
94
+ includeCurrentPageUrlWithEvents?: boolean;
95
+ /**
96
+ * Whether or not Statsig should use raw JSON for network requests where possible.
97
+ *
98
+ * default: `false`
99
+ */
100
+ disableStatsigEncoding?: boolean;
101
+ /**
102
+ * Whether or not Statsig should compress JSON bodies for network requests where possible.
103
+ *
104
+ * default: `false`
105
+ */
106
+ disableCompression?: boolean;
44
107
  };
108
+ export type AnyStatsigOptions = StatsigOptionsCommon<NetworkConfigCommon>;
45
109
  export type StatsigEnvironment = {
46
110
  tier?: string;
47
111
  [key: string]: string | undefined;
@@ -0,0 +1,6 @@
1
+ import { DynamicConfigEvaluation, EvaluationDetails, GateEvaluation, LayerEvaluation } from './EvaluationTypes';
2
+ import { AnyConfigBasedStatsigType, DynamicConfig, FeatureGate, Layer } from './StatsigTypes';
3
+ export declare function _makeFeatureGate(name: string, details: EvaluationDetails, evaluation: GateEvaluation | null): FeatureGate;
4
+ export declare function _makeDynamicConfig(name: string, details: EvaluationDetails, evaluation: DynamicConfigEvaluation | null): DynamicConfig;
5
+ export declare function _makeLayer(name: string, details: EvaluationDetails, evaluation: LayerEvaluation | null, exposeFunc?: (param: string) => void): Layer;
6
+ export declare function _mergeOverride<T extends AnyConfigBasedStatsigType>(original: T, overridden: T | null | undefined, value: Record<string, unknown>, exposeFunc?: (param: string) => void): T;
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports._mergeOverride = exports._makeLayer = exports._makeDynamicConfig = exports._makeFeatureGate = void 0;
4
+ const DEFAULT_RULE = 'default';
5
+ function _makeEvaluation(name, details, evaluation, value) {
6
+ var _a;
7
+ return {
8
+ name,
9
+ details,
10
+ ruleID: (_a = evaluation === null || evaluation === void 0 ? void 0 : evaluation.rule_id) !== null && _a !== void 0 ? _a : DEFAULT_RULE,
11
+ __evaluation: evaluation,
12
+ value,
13
+ };
14
+ }
15
+ function _makeFeatureGate(name, details, evaluation) {
16
+ return _makeEvaluation(name, details, evaluation, (evaluation === null || evaluation === void 0 ? void 0 : evaluation.value) === true);
17
+ }
18
+ exports._makeFeatureGate = _makeFeatureGate;
19
+ function _makeDynamicConfig(name, details, evaluation) {
20
+ var _a;
21
+ return Object.assign(Object.assign({}, _makeEvaluation(name, details, evaluation, (_a = evaluation === null || evaluation === void 0 ? void 0 : evaluation.value) !== null && _a !== void 0 ? _a : {})), { groupName: null, get: _makeTypedGet(evaluation === null || evaluation === void 0 ? void 0 : evaluation.value) });
22
+ }
23
+ exports._makeDynamicConfig = _makeDynamicConfig;
24
+ function _makeLayer(name, details, evaluation, exposeFunc) {
25
+ var _a, _b;
26
+ return Object.assign(Object.assign({}, _makeEvaluation(name, details, evaluation, undefined)), { get: _makeTypedGet(evaluation === null || evaluation === void 0 ? void 0 : evaluation.value, exposeFunc), groupName: (_a = evaluation === null || evaluation === void 0 ? void 0 : evaluation.group_name) !== null && _a !== void 0 ? _a : null, __value: (_b = evaluation === null || evaluation === void 0 ? void 0 : evaluation.value) !== null && _b !== void 0 ? _b : {} });
27
+ }
28
+ exports._makeLayer = _makeLayer;
29
+ function _mergeOverride(original, overridden, value, exposeFunc) {
30
+ return Object.assign(Object.assign(Object.assign({}, original), overridden), { get: _makeTypedGet(value, exposeFunc) });
31
+ }
32
+ exports._mergeOverride = _mergeOverride;
33
+ function _isTypeMatch(a, b) {
34
+ const typeOf = (x) => (Array.isArray(x) ? 'array' : typeof x);
35
+ return typeOf(a) === typeOf(b);
36
+ }
37
+ function _makeTypedGet(value, exposeFunc) {
38
+ return (param, fallback) => {
39
+ var _a;
40
+ const found = (_a = value === null || value === void 0 ? void 0 : value[param]) !== null && _a !== void 0 ? _a : null;
41
+ if (found == null) {
42
+ return (fallback !== null && fallback !== void 0 ? fallback : null);
43
+ }
44
+ if (fallback != null && !_isTypeMatch(found, fallback)) {
45
+ return (fallback !== null && fallback !== void 0 ? fallback : null);
46
+ }
47
+ exposeFunc === null || exposeFunc === void 0 ? void 0 : exposeFunc(param);
48
+ return found;
49
+ };
50
+ }
@@ -1,27 +1,33 @@
1
- export type Flatten<T> = {
2
- [K in keyof T]: T[K];
3
- } & {};
4
- export type EvaluationDetails = {
5
- reason: string;
6
- lcut?: number;
7
- receivedAt?: number;
8
- };
9
- type EvaluatedSpec = {
1
+ import { EvaluationDetails, ExperimentEvaluation, GateEvaluation, LayerEvaluation } from './EvaluationTypes';
2
+ import { Flatten } from './UtitlityTypes';
3
+ export type TypedGet = <T = unknown>(key: string, fallback?: T) => TypedReturn<T>;
4
+ export type TypedReturn<T = unknown> = T extends string ? string : T extends number ? number : T extends boolean ? boolean : T extends Array<unknown> ? Array<unknown> : T extends object ? object : unknown;
5
+ export type SpecType = 'gate' | 'dynamic_config' | 'experiment' | 'layer';
6
+ export type FeatureGate = Flatten<{
10
7
  readonly name: string;
11
8
  readonly ruleID: string;
12
9
  readonly details: EvaluationDetails;
13
- };
14
- export type FeatureGate = EvaluatedSpec & {
15
10
  readonly value: boolean;
16
- };
17
- export type DynamicConfig = EvaluatedSpec & {
11
+ readonly __evaluation: GateEvaluation | null;
12
+ }>;
13
+ export type Experiment = Flatten<{
14
+ readonly name: string;
15
+ readonly ruleID: string;
16
+ readonly details: EvaluationDetails;
18
17
  readonly value: Record<string, unknown>;
19
- };
20
- export type Experiment = DynamicConfig;
21
- export type Layer = EvaluatedSpec & {
22
- readonly getValue: (parameterName: string) => unknown;
23
- };
24
- export declare function makeFeatureGate(name: string, details: EvaluationDetails, ruleID?: string, value?: boolean): FeatureGate;
25
- export declare function makeDynamicConfig(name: string, details: EvaluationDetails, ruleID?: string, value?: Record<string, unknown>): DynamicConfig;
26
- export declare function makeLayer(name: string, details: EvaluationDetails, ruleID?: string, getValue?: (param: string) => unknown): Layer;
27
- export {};
18
+ readonly groupName: string | null;
19
+ readonly __evaluation: ExperimentEvaluation | null;
20
+ readonly get: TypedGet;
21
+ }>;
22
+ export type DynamicConfig = Flatten<Experiment>;
23
+ export type Layer = Flatten<{
24
+ readonly name: string;
25
+ readonly ruleID: string;
26
+ readonly details: EvaluationDetails;
27
+ readonly groupName: string | null;
28
+ readonly __value: Record<string, unknown>;
29
+ readonly __evaluation: LayerEvaluation | null;
30
+ readonly get: TypedGet;
31
+ }>;
32
+ export type AnyConfigBasedStatsigType = DynamicConfig | Experiment | Layer;
33
+ export type AnyStatsigType = FeatureGate | AnyConfigBasedStatsigType;
@@ -1,31 +1,2 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.makeLayer = exports.makeDynamicConfig = exports.makeFeatureGate = void 0;
4
- var DEFAULT_RULE = 'default';
5
- function makeFeatureGate(name, details, ruleID, value) {
6
- return {
7
- name: name,
8
- details: details,
9
- ruleID: ruleID !== null && ruleID !== void 0 ? ruleID : DEFAULT_RULE,
10
- value: value === true,
11
- };
12
- }
13
- exports.makeFeatureGate = makeFeatureGate;
14
- function makeDynamicConfig(name, details, ruleID, value) {
15
- return {
16
- name: name,
17
- details: details,
18
- ruleID: ruleID !== null && ruleID !== void 0 ? ruleID : DEFAULT_RULE,
19
- value: value !== null && value !== void 0 ? value : {},
20
- };
21
- }
22
- exports.makeDynamicConfig = makeDynamicConfig;
23
- function makeLayer(name, details, ruleID, getValue) {
24
- return {
25
- name: name,
26
- details: details,
27
- getValue: getValue !== null && getValue !== void 0 ? getValue : (function () { return undefined; }),
28
- ruleID: ruleID !== null && ruleID !== void 0 ? ruleID : DEFAULT_RULE,
29
- };
30
- }
31
- exports.makeLayer = makeLayer;
@@ -1,12 +1,11 @@
1
1
  import type { StatsigEnvironment } from './StatsigOptionsCommon';
2
2
  type StatsigUserPrimitives = string | number | boolean | Array<string> | undefined;
3
- export type StatsigUser = ({
4
- userID: string;
5
- } | {
6
- customIDs: Record<string, string>;
7
- }) & {
3
+ export type StatsigUser = {
8
4
  userID?: string;
9
- customIDs?: Record<string, string>;
5
+ customIDs?: {
6
+ [key: string]: string | undefined;
7
+ stableID?: string;
8
+ };
10
9
  email?: string;
11
10
  ip?: string;
12
11
  userAgent?: string;
@@ -19,6 +18,6 @@ export type StatsigUser = ({
19
18
  export type StatsigUserInternal = StatsigUser & {
20
19
  statsigEnvironment?: StatsigEnvironment;
21
20
  };
22
- export declare function normalizeUser(original: StatsigUser, environment?: StatsigEnvironment): StatsigUser;
23
- export declare function getUserStorageKey(sdkKey: string, user?: StatsigUser): string;
21
+ export declare function _normalizeUser(original: StatsigUser, environment?: StatsigEnvironment): StatsigUser;
22
+ export declare function _getFullUserHash(user: StatsigUser | undefined): string | null;
24
23
  export {};