@statsig/client-core 0.0.1-beta.4 → 0.0.1-beta.40

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 (73) hide show
  1. package/README.md +1 -4
  2. package/package.json +1 -1
  3. package/src/$_StatsigGlobal.d.ts +8 -1
  4. package/src/$_StatsigGlobal.js +22 -12
  5. package/src/ClientInterfaces.d.ts +50 -14
  6. package/src/DataAdapterCore.d.ts +30 -0
  7. package/src/DataAdapterCore.js +169 -0
  8. package/src/Diagnostics.d.ts +1 -3
  9. package/src/Diagnostics.js +4 -49
  10. package/src/DownloadConfigSpecsResponse.d.ts +41 -0
  11. package/src/ErrorBoundary.d.ts +7 -2
  12. package/src/ErrorBoundary.js +79 -87
  13. package/src/EvaluationOptions.d.ts +20 -0
  14. package/src/EvaluationOptions.js +2 -0
  15. package/src/EvaluationTypes.d.ts +39 -0
  16. package/src/EvaluationTypes.js +2 -0
  17. package/src/EventLogger.d.ts +20 -9
  18. package/src/EventLogger.js +216 -232
  19. package/src/Hashing.d.ts +2 -1
  20. package/src/Hashing.js +25 -6
  21. package/src/InitializeResponse.d.ts +18 -0
  22. package/src/InitializeResponse.js +2 -0
  23. package/src/Log.js +15 -34
  24. package/src/NetworkConfig.d.ts +18 -0
  25. package/src/NetworkConfig.js +18 -0
  26. package/src/NetworkCore.d.ts +19 -6
  27. package/src/NetworkCore.js +137 -167
  28. package/src/OverrideAdapter.d.ts +9 -0
  29. package/src/OverrideAdapter.js +2 -0
  30. package/src/SDKType.d.ts +8 -0
  31. package/src/SDKType.js +19 -0
  32. package/src/SafeJs.d.ts +6 -0
  33. package/src/SafeJs.js +41 -0
  34. package/src/SessionID.d.ts +17 -1
  35. package/src/SessionID.js +89 -8
  36. package/src/StableID.js +24 -53
  37. package/src/StatsigClientBase.d.ts +57 -28
  38. package/src/StatsigClientBase.js +115 -238
  39. package/src/StatsigClientEventEmitter.d.ts +69 -28
  40. package/src/StatsigDataAdapter.d.ts +107 -0
  41. package/src/StatsigDataAdapter.js +4 -0
  42. package/src/StatsigEvent.d.ts +10 -19
  43. package/src/StatsigEvent.js +50 -41
  44. package/src/StatsigMetadata.d.ts +1 -1
  45. package/src/StatsigMetadata.js +7 -18
  46. package/src/StatsigOptionsCommon.d.ts +85 -17
  47. package/src/StatsigTypeFactories.d.ts +6 -0
  48. package/src/StatsigTypeFactories.js +50 -0
  49. package/src/StatsigTypes.d.ts +29 -18
  50. package/src/StatsigTypes.js +0 -29
  51. package/src/StatsigUser.d.ts +2 -5
  52. package/src/StatsigUser.js +10 -18
  53. package/src/StorageProvider.d.ts +13 -8
  54. package/src/StorageProvider.js +77 -73
  55. package/src/TypedJsonParse.d.ts +8 -0
  56. package/src/TypedJsonParse.js +27 -0
  57. package/src/UUID.js +9 -5
  58. package/src/UrlOverrides.d.ts +1 -0
  59. package/src/UrlOverrides.js +15 -0
  60. package/src/UtitlityTypes.d.ts +3 -0
  61. package/src/UtitlityTypes.js +2 -0
  62. package/src/VisibilityObserving.d.ts +8 -0
  63. package/src/VisibilityObserving.js +30 -0
  64. package/src/index.d.ts +20 -5
  65. package/src/index.js +30 -18
  66. package/src/Monitoring.d.ts +0 -3
  67. package/src/Monitoring.js +0 -33
  68. package/src/StatsigDataProvider.d.ts +0 -9
  69. package/src/VisibilityChangeObserver.d.ts +0 -13
  70. package/src/VisibilityChangeObserver.js +0 -48
  71. package/src/__tests__/MockLocalStorage.d.ts +0 -9
  72. package/src/__tests__/MockLocalStorage.js +0 -37
  73. /package/src/{StatsigDataProvider.js → DownloadConfigSpecsResponse.js} +0 -0
@@ -1,33 +1,74 @@
1
+ import { DataAdapterResult } from './StatsigDataAdapter';
1
2
  import { DynamicConfig, Experiment, FeatureGate, Layer } from './StatsigTypes';
2
- export type StatsigLoadingStatus = 'Uninitialized' | 'Loading' | 'Ready' | 'Error';
3
- export type StatsigClientEvent = 'status_change' | 'error' | 'logs_flushed' | 'gate_evaluation' | 'dynamic_config_evaluation' | 'experiment_evaluation' | 'layer_evaluation';
4
- export type StatsigClientEventData = {
5
- event: StatsigClientEvent;
6
- } | {
7
- event: 'status_change';
8
- loadingStatus: StatsigLoadingStatus;
9
- } | {
10
- event: 'error';
11
- error: unknown;
12
- } | {
13
- event: 'logs_flushed';
14
- events: Record<string, unknown>[];
15
- } | {
16
- event: 'gate_evaluation';
17
- gate: FeatureGate;
18
- } | {
19
- event: 'dynamic_config_evaluation';
20
- dynamicConfig: DynamicConfig;
21
- } | {
22
- event: 'experiment_evaluation';
23
- experiment: Experiment;
24
- } | {
25
- event: 'layer_evaluation';
26
- layer: Layer;
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
+ session_expired: object;
11
+ error: {
12
+ error: unknown;
13
+ };
14
+ logs_flushed: {
15
+ events: Record<string, unknown>[];
16
+ };
17
+ pre_shutdown: object;
18
+ gate_evaluation: {
19
+ gate: FeatureGate;
20
+ };
21
+ dynamic_config_evaluation: {
22
+ dynamicConfig: DynamicConfig;
23
+ };
24
+ experiment_evaluation: {
25
+ experiment: Experiment;
26
+ };
27
+ layer_evaluation: {
28
+ layer: Layer;
29
+ };
27
30
  };
28
- export type StatsigClientEventCallback = (data: StatsigClientEventData) => void;
31
+ /**
32
+ * Type representing various events emitted by a Statsig client.
33
+ *
34
+ * `values_updated` - When the Statsig clients internal values change as the result of an initialize/update operation.
35
+ *
36
+ * `error` - When an unexpected error occurs within the Statsig client.
37
+ *
38
+ * `logs_flushed` - When queued StatsigEvents are flushed to Statsig servers.
39
+ *
40
+ * `pre_shutdown` - Fired just before the SDK is shutdown
41
+ *
42
+ * `gate_evaluation` - Fired when any gate is checked from the Statsig client.
43
+ *
44
+ * `dynamic_config_evaluation` - Fired when any dyanamic config is checked from the Statsig client.
45
+ *
46
+ * `experiment_evaluation` - Fired when any experiment is checked from the Statsig client.
47
+ *
48
+ * `layer_evaluation` - Fired when any layer is checked from the Statsig client.
49
+ */
50
+ export type AnyStatsigClientEvent = Flatten<{
51
+ [K in keyof EventNameToEventDataMap]: {
52
+ name: K;
53
+ } & EventNameToEventDataMap[K];
54
+ }[keyof EventNameToEventDataMap]>;
55
+ export type StatsigClientEvent<T> = Extract<AnyStatsigClientEvent, {
56
+ name: T;
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;
29
61
  export interface StatsigClientEventEmitterInterface {
30
62
  readonly loadingStatus: StatsigLoadingStatus;
31
- on(event: StatsigClientEvent, listener: StatsigClientEventCallback): void;
32
- off(event: StatsigClientEvent, listener: StatsigClientEventCallback): void;
63
+ on<T extends StatsigClientEventName>(event: T, listener: StatsigClientEventCallback<T>): void;
64
+ off<T extends StatsigClientEventName>(event: T, listener: StatsigClientEventCallback<T>): void;
65
+ /**
66
+ * (Statsig Use Only) - Same as .on() but logs errors to sdk_exception
67
+ */
68
+ $on<T extends StatsigClientEventName>(event: T, listener: StatsigClientEventCallback<T>): void;
69
+ /**
70
+ * (Statsig Use Only) - Emit StatsigClientEvents
71
+ */
72
+ $emt(event: AnyStatsigClientEvent): void;
33
73
  }
74
+ export {};
@@ -0,0 +1,107 @@
1
+ import { NetworkPriority } from './NetworkConfig';
2
+ import type { AnyStatsigOptions, NetworkConfigCommon } from './StatsigOptionsCommon';
3
+ import { StatsigUser } from './StatsigUser';
4
+ export type DataSource = 'Uninitialized' | 'Loading' | 'NoValues' | 'Cache' | 'Network' | 'NetworkNotModified' | 'Bootstrap' | 'Prefetch';
5
+ export type DataAdapterResult = {
6
+ readonly source: DataSource;
7
+ readonly data: string;
8
+ readonly receivedAt: number;
9
+ };
10
+ export type DataAdapterAsyncOptions = {
11
+ /**
12
+ * The maximum amount of time (in milliseconds) this operation is permitted to run.
13
+ * If the timeout is hit, null is returned but any in-flight requests are kept alive with results going to cache for future updates.
14
+ *
15
+ * Note: If no timeout is given, the {@link NetworkConfigCommon.networkTimeoutMs|StatsigOptions.networkConfig.networkTimeoutMs} is used.
16
+ */
17
+ readonly timeoutMs?: NetworkConfigCommon['networkTimeoutMs'];
18
+ /**
19
+ * The priority that should be applied to the Http request that is fired.
20
+ *
21
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/fetch#priority
22
+ *
23
+ * default: 'auto'
24
+ */
25
+ readonly priority?: NetworkPriority;
26
+ };
27
+ export declare const DataAdapterCachePrefix = "statsig.cached";
28
+ /**
29
+ * Describes a type that is used during intialize/update operations of a Statsig client.
30
+ *
31
+ * See below to find the default adapters, but know that it is possible to create your
32
+ * own StatsigDataAdapter and provide it via `StatsigOptions.dataAdapter`.
33
+ *
34
+ * defaults:
35
+ *
36
+ * - {@link StatsigClient} uses {@link EvaluationsDataAdapter}
37
+ *
38
+ * - {@link StatsigOnDeviceEvalClient} uses {@link SpecsDataAdapter}
39
+ */
40
+ type DataAdapterCommon = {
41
+ /**
42
+ * Called when the StatsigDataAdapter is attached to the Statsig client instance during construction.
43
+ * @param {string} sdkKey The SDK key being used by the Statsig client.
44
+ * @param {StatsigOptionsCommon | null} options The StatsigOptions being used by the Statsig client.
45
+ */
46
+ readonly attach: (sdkKey: string, options: AnyStatsigOptions | null) => void;
47
+ /**
48
+ * (Internal Use Only) - Used by \@statsig/react-native-bindings to prime the cache from AsyncStorage
49
+ *
50
+ * @param {Record<string, DataAdapterResult>} cache The values to merge into _inMemoryCache
51
+ */
52
+ readonly __primeInMemoryCache: (cache: Record<string, DataAdapterResult>) => void;
53
+ };
54
+ export type EvaluationsDataAdapter = DataAdapterCommon & {
55
+ /**
56
+ * Synchronously get evaluation data for the given user. Called during initializeSync and/or updateUserSync.
57
+ *
58
+ * It is also called during async update operations before StatsigDataAdapter.getDataAsync is called.
59
+ * @param {StatsigUser} user The StatsigUser to get data for.
60
+ * @returns {DataAdapterResult | null} The data that was found for the given StatsigUser.
61
+ */
62
+ readonly getDataSync: (user: StatsigUser) => DataAdapterResult | null;
63
+ /**
64
+ * Asynchronously get evaluation data for the given user. Called during initializeAsync and/or updateUserAsync.
65
+ *
66
+ * @param {DataAdapterResult | null} current The data that was found synchronously (Cache). Will be used as fallback if getDataAsync fails
67
+ * @param {StatsigUser} user The StatsigUser to get data for.
68
+ * @returns {DataAdapterResult | null} The data that was found for the given StatsigUser.
69
+ */
70
+ readonly getDataAsync: (current: DataAdapterResult | null, user: StatsigUser, options?: DataAdapterAsyncOptions) => Promise<DataAdapterResult | null>;
71
+ /**
72
+ * Manually trigger a fetch for new evaluations data for the given user.
73
+ *
74
+ * @param {StatsigUser} user The StatsigUser to get data for.
75
+ */
76
+ readonly prefetchData: (user: StatsigUser, options?: DataAdapterAsyncOptions) => Promise<void>;
77
+ /**
78
+ * Manually set evaluations data for the given user.
79
+ *
80
+ * @param {StatsigUser} user The StatsigUser this data is for.
81
+ */
82
+ readonly setData: (data: string, user: StatsigUser) => void;
83
+ };
84
+ export type SpecsDataAdapter = DataAdapterCommon & {
85
+ /**
86
+ * Synchronously get specs data. Called during initializeSync and/or updateUserSync.
87
+ * It is also called during async update operations before StatsigDataAdapter.getDataAsync is called.
88
+ * @returns {DataAdapterResult | null} The data that was found for the given StatsigUser.
89
+ */
90
+ readonly getDataSync: () => DataAdapterResult | null;
91
+ /**
92
+ * Asynchronously get specs data. Called during initializeAsync and/or updateUserAsync.
93
+ *
94
+ * @param {DataAdapterResult | null} current The data that was found synchronously (Cache). Will be used as fallback if getDataAsync fails
95
+ * @returns {DataAdapterResult | null} The data that was found for the given StatsigUser.
96
+ */
97
+ readonly getDataAsync: (current: DataAdapterResult | null, options?: DataAdapterAsyncOptions) => Promise<DataAdapterResult | null>;
98
+ /**
99
+ * Manually trigger a fetch for new specs data.
100
+ */
101
+ readonly prefetchData: (options?: DataAdapterAsyncOptions) => Promise<void>;
102
+ /**
103
+ * Manually set specs data (Bootstrap).
104
+ */
105
+ readonly setData: (data: string) => void;
106
+ };
107
+ export {};
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DataAdapterCachePrefix = void 0;
4
+ exports.DataAdapterCachePrefix = 'statsig.cached';
@@ -1,11 +1,6 @@
1
- import { DataSource } from './StatsigDataProvider';
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
- export declare function isExposureEvent({ eventName }: StatsigEventInternal): boolean;
22
- export declare function createGateExposure(user: StatsigUser, gate: FeatureGate, secondaryExposures: SecondaryExposure[] | undefined): StatsigEventInternal;
23
- export declare function createConfigExposure(user: StatsigUser, config: DynamicConfig, secondaryExposures: SecondaryExposure[] | undefined): StatsigEventInternal;
24
- export declare function createLayerParameterExposure(user: StatsigUser, layerName: string, parameterName: string, spec: {
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;
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,57 +1,66 @@
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, 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
- metadata: metadata,
13
- secondaryExposures: secondaryExposures,
12
+ metadata: _addEvaluationDetailsToMetadata(details, metadata),
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, {
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
- reason: gate.source,
28
- }, secondaryExposures !== null && secondaryExposures !== void 0 ? secondaryExposures : []);
29
- }
30
- exports.createGateExposure = createGateExposure;
31
- function createConfigExposure(user, config, secondaryExposures) {
32
- return createExposure(CONFIG_EXPOSURE_NAME, user, {
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, {
33
33
  config: config.name,
34
34
  ruleID: config.ruleID,
35
- reason: config.source,
36
- }, secondaryExposures !== null && secondaryExposures !== void 0 ? secondaryExposures : []);
37
- }
38
- exports.createConfigExposure = createConfigExposure;
39
- function createLayerParameterExposure(user, layerName, parameterName, spec) {
40
- var _a;
41
- var isExplicit = spec.explicit_parameters.includes(parameterName);
42
- var allocatedExperiment = '';
43
- 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 : [];
44
44
  if (isExplicit) {
45
- allocatedExperiment = spec.allocated_experiment_name;
46
- secondaryExposures = spec.secondary_exposures;
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: layerName,
50
- parameterName: parameterName,
51
- ruleID: spec.rule_id,
52
- allocatedExperiment: 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
- }
57
- exports.createLayerParameterExposure = createLayerParameterExposure;
55
+ };
56
+ exports._createLayerParameterExposure = _createLayerParameterExposure;
57
+ const _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
+ };
@@ -1,10 +1,10 @@
1
+ export declare const SDK_VERSION = "0.0.1-beta.40";
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.4';
16
- var metadata = {
17
- sdkVersion: SDK_VERSION,
3
+ exports.StatsigMetadataProvider = exports.SDK_VERSION = void 0;
4
+ exports.SDK_VERSION = '0.0.1-beta.40';
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,12 +1,69 @@
1
1
  import { LogLevel } from './Log';
2
- import { StatsigDataProvider } from './StatsigDataProvider';
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;
23
+ /**
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`
27
+ */
28
+ logEventUrl?: string;
29
+ /**
30
+ * The URL used to flush queued events via {@link window.navigator.sendBeacon} (web only). Takes precedence over {@link StatsigOptionsCommon.api}.
31
+ *
32
+ * default: `https://featuregates.org/v1/initialize`
33
+ */
34
+ logEventBeaconUrl?: string;
35
+ /**
36
+ * The maximum amount of time (in milliseconds) that any network request can take
37
+ * before timing out.
38
+ *
39
+ * default: `10,000 ms` (10 seconds)
40
+ */
41
+ networkTimeoutMs?: number;
42
+ /**
43
+ * Intended for testing purposes. Prevents any network requests being made.
44
+ */
45
+ preventAllNetworkTraffic?: boolean;
46
+ /**
47
+ * Overrides the default networking layer used by the Statsig client.
48
+ * By default, the client use `fetch`, but overriding this
49
+ * you could use `axios` or raw `XMLHttpRequest`
50
+ *
51
+ * default: `Fetch API`
52
+ *
53
+ * @param {string} url Where the request is going.
54
+ * @param {NetworkArgs} args Configuration for the network request.
55
+ * @returns {Response}
56
+ */
57
+ networkOverrideFunc?: (url: string, args: NetworkArgs) => Promise<Response>;
58
+ };
59
+ /** Options for configuring a Statsig client. */
60
+ export type StatsigOptionsCommon<NetworkConfig extends NetworkConfigCommon> = StatsigRuntimeMutableOptions & {
61
+ /**
62
+ * Allows for fine grained control over which api or urls are hit for specific Statsig network requests.
63
+ *
64
+ * For defaults see {@link StatsigClientUrlOverrideOptions}
65
+ */
66
+ networkConfig?: NetworkConfig;
10
67
  /**
11
68
  * An object you can use to set environment variables that apply to all of your users
12
69
  * in the same session.
@@ -18,30 +75,41 @@ export type StatsigOptionsCommon = {
18
75
  overrideStableID?: string;
19
76
  /**
20
77
  * How much information is allowed to be printed to the console.
21
- * Default: LogLevel.Warn
78
+ *
79
+ * default: {@link LogLevel.Warn}
22
80
  */
23
81
  logLevel?: LogLevel;
24
- /**
25
- * StatsigDataProvider implementors used to customize the initialization flow.
26
- * Default: LocalStorageCache then Network
27
- */
28
- dataProviders?: StatsigDataProvider[];
29
- /**
30
- * The maximum amount of time (in milliseconds) that any network request can take
31
- * before timing out. Default: 10,000 (10 seconds)
32
- */
33
- networkTimeoutMs?: number;
34
82
  /**
35
83
  * The maximum number of events to batch before flushing logs to Statsig.
36
- * Default: 50
84
+ *
85
+ * default: `50`
37
86
  */
38
87
  loggingBufferMaxSize?: number;
39
88
  /**
40
89
  * How often (in milliseconds) to flush logs to Statsig.
41
- * Default: 10,000 (10 seconds)
90
+ *
91
+ * default: `10,000 ms` (10 seconds)
42
92
  */
43
93
  loggingIntervalMs?: number;
94
+ /**
95
+ * An implementor of {@link OverrideAdapter}, used to alter evaluations before its
96
+ * returned to the caller of a check api (checkGate/getExperiment etc).
97
+ */
98
+ overrideAdapter?: OverrideAdapter;
99
+ /**
100
+ * (Web only) Should the 'current page' url be included with logged events.
101
+ *
102
+ * default: true
103
+ */
104
+ includeCurrentPageUrlWithEvents?: boolean;
105
+ /**
106
+ * Whether or not Statsig should use raw JSON for network requests where possible.
107
+ *
108
+ * default: `false`
109
+ */
110
+ disableStatsigEncoding?: boolean;
44
111
  };
112
+ export type AnyStatsigOptions = StatsigOptionsCommon<NetworkConfigCommon>;
45
113
  export type StatsigEnvironment = {
46
114
  tier?: string;
47
115
  [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,22 +1,33 @@
1
- export type Flatten<T> = {
2
- [K in keyof T]: T[K];
3
- } & {};
4
- 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<{
5
7
  readonly name: string;
6
8
  readonly ruleID: string;
7
- readonly source: string;
8
- };
9
- export type FeatureGate = EvaluatedSpec & {
9
+ readonly details: EvaluationDetails;
10
10
  readonly value: boolean;
11
- };
12
- 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;
13
17
  readonly value: Record<string, unknown>;
14
- };
15
- export type Experiment = DynamicConfig;
16
- export type Layer = EvaluatedSpec & {
17
- readonly getValue: (parameterName: string) => unknown;
18
- };
19
- export declare function makeFeatureGate(name: string, source: string, ruleID?: string, value?: boolean): FeatureGate;
20
- export declare function makeDynamicConfig(name: string, source: string, ruleID?: string, value?: Record<string, unknown>): DynamicConfig;
21
- export declare function makeLayer(name: string, source: string, ruleID?: string, getValue?: (param: string) => unknown): Layer;
22
- 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;