@oai-statsig/statsig-node-core 0.19.5-beta.2604230102

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.
@@ -0,0 +1,45 @@
1
+ import { EvaluationDetails, SecondaryExposure } from './statsig-generated';
2
+ export type TypedGet = <T = unknown>(key: string, defaultValue: T, typeGuard?: ((value: unknown) => value is T | null) | null) => T;
3
+ export type UnknownGet = (key: string, fallback?: boolean | number | string | object | Array<any> | null) => unknown | null;
4
+ type RawEvaluation = Record<string, unknown> | string | null;
5
+ declare class BaseEvaluation {
6
+ readonly name: string;
7
+ readonly ruleID: string;
8
+ readonly idType: string;
9
+ readonly details: EvaluationDetails;
10
+ readonly secondaryExposures: SecondaryExposure[];
11
+ constructor(name: string, data: Record<string, unknown>);
12
+ getEvaluationDetails(): EvaluationDetails;
13
+ getRuleId(): string;
14
+ getIdType(): string;
15
+ getSecondaryExposures(): SecondaryExposure[];
16
+ }
17
+ export declare class FeatureGate extends BaseEvaluation {
18
+ readonly value: boolean;
19
+ constructor(name: string, raw: RawEvaluation);
20
+ }
21
+ export declare class DynamicConfig extends BaseEvaluation {
22
+ readonly value: Record<string, unknown>;
23
+ readonly get: TypedGet;
24
+ readonly getValue: UnknownGet;
25
+ constructor(name: string, raw: RawEvaluation);
26
+ }
27
+ export declare class Experiment extends BaseEvaluation {
28
+ readonly groupName: string | null;
29
+ readonly value: Record<string, unknown>;
30
+ readonly get: TypedGet;
31
+ readonly getValue: UnknownGet;
32
+ constructor(name: string, raw: RawEvaluation);
33
+ getGroupName(): string | null;
34
+ }
35
+ export declare class Layer extends BaseEvaluation {
36
+ readonly groupName: string | null;
37
+ readonly allocatedExperimentName: string | null;
38
+ readonly __value: Record<string, unknown>;
39
+ readonly get: TypedGet;
40
+ readonly getValue: UnknownGet;
41
+ constructor(exposeFn: (param: string) => void, name: string, raw: RawEvaluation);
42
+ getGroupName(): string | null;
43
+ getAllocatedExperimentName(): string | null;
44
+ }
45
+ export {};
@@ -0,0 +1,167 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Layer = exports.Experiment = exports.DynamicConfig = exports.FeatureGate = void 0;
4
+ function _getTypeOf(x) {
5
+ return Array.isArray(x) ? 'array' : x === null ? 'null' : typeof x;
6
+ }
7
+ class BaseEvaluation {
8
+ constructor(name, data) {
9
+ var _a, _b, _c;
10
+ this.secondaryExposures = [];
11
+ this.name = name;
12
+ this.ruleID = (_a = data.ruleID) !== null && _a !== void 0 ? _a : '';
13
+ this.idType = (_b = data.idType) !== null && _b !== void 0 ? _b : '';
14
+ this.details = _extractEvaluationDetails(data.details);
15
+ this.secondaryExposures =
16
+ (_c = data.secondaryExposures) !== null && _c !== void 0 ? _c : [];
17
+ }
18
+ getEvaluationDetails() {
19
+ return this.details;
20
+ }
21
+ getRuleId() {
22
+ return this.ruleID;
23
+ }
24
+ getIdType() {
25
+ return this.idType;
26
+ }
27
+ getSecondaryExposures() {
28
+ return this.secondaryExposures;
29
+ }
30
+ }
31
+ class FeatureGate extends BaseEvaluation {
32
+ constructor(name, raw) {
33
+ const data = _parseRawEvaluation(raw);
34
+ super(name, data);
35
+ this.value = data.value === true;
36
+ }
37
+ }
38
+ exports.FeatureGate = FeatureGate;
39
+ class DynamicConfig extends BaseEvaluation {
40
+ constructor(name, raw) {
41
+ var _a;
42
+ const data = _parseRawEvaluation(raw);
43
+ super(name, data);
44
+ this.value = {};
45
+ this.value = (_a = data.value) !== null && _a !== void 0 ? _a : {};
46
+ this.get = _makeTypedGet(name, this.value);
47
+ this.getValue = _makeUnknownGet(this.value);
48
+ }
49
+ }
50
+ exports.DynamicConfig = DynamicConfig;
51
+ class Experiment extends BaseEvaluation {
52
+ constructor(name, raw) {
53
+ var _a, _b;
54
+ const data = _parseRawEvaluation(raw);
55
+ super(name, data);
56
+ this.groupName = null;
57
+ this.value = {};
58
+ this.groupName = (_a = data.groupName) !== null && _a !== void 0 ? _a : null;
59
+ this.value = (_b = data.value) !== null && _b !== void 0 ? _b : {};
60
+ this.get = _makeTypedGet(name, this.value);
61
+ this.getValue = _makeUnknownGet(this.value);
62
+ }
63
+ getGroupName() {
64
+ return this.groupName;
65
+ }
66
+ }
67
+ exports.Experiment = Experiment;
68
+ class Layer extends BaseEvaluation {
69
+ constructor(exposeFn, name, raw) {
70
+ var _a, _b, _c;
71
+ const data = _parseRawEvaluation(raw);
72
+ super(name, data);
73
+ this.groupName = null;
74
+ this.allocatedExperimentName = null;
75
+ this.__value = {};
76
+ this.__value = (_a = data.value) !== null && _a !== void 0 ? _a : {};
77
+ this.groupName = (_b = data.groupName) !== null && _b !== void 0 ? _b : null;
78
+ this.allocatedExperimentName =
79
+ (_c = data.allocatedExperimentName) !== null && _c !== void 0 ? _c : null;
80
+ this.get = _makeTypedGet(name, this.__value, exposeFn);
81
+ this.getValue = _makeUnknownGet(this.__value, exposeFn);
82
+ }
83
+ getGroupName() {
84
+ return this.groupName;
85
+ }
86
+ getAllocatedExperimentName() {
87
+ return this.allocatedExperimentName;
88
+ }
89
+ }
90
+ exports.Layer = Layer;
91
+ function _parseRawEvaluation(raw) {
92
+ if (raw == null) {
93
+ return {};
94
+ }
95
+ if (typeof raw === 'object' && !Array.isArray(raw)) {
96
+ return raw;
97
+ }
98
+ if (typeof raw !== 'string') {
99
+ return {};
100
+ }
101
+ try {
102
+ return JSON.parse(raw);
103
+ }
104
+ catch (error) {
105
+ console.error(`[Statsig] Error parsing BaseEvaluation: ${error}`);
106
+ return {};
107
+ }
108
+ }
109
+ function _makeTypedGet(name, value, exposeFunc) {
110
+ return (key, defaultValue, typeGuard = null) => {
111
+ var _a;
112
+ // @ts-ignore - intentionally matches legacy behavior exactly
113
+ defaultValue = defaultValue !== null && defaultValue !== void 0 ? defaultValue : null;
114
+ // Equivalent to legacy `this.getValue(key, defaultValue)`
115
+ const val = ((_a = value[key]) !== null && _a !== void 0 ? _a : defaultValue);
116
+ if (val == null) {
117
+ return defaultValue;
118
+ }
119
+ const expectedType = _getTypeOf(defaultValue);
120
+ const actualType = _getTypeOf(val);
121
+ if (typeGuard != null) {
122
+ if (typeGuard(val)) {
123
+ exposeFunc === null || exposeFunc === void 0 ? void 0 : exposeFunc(key);
124
+ return val;
125
+ }
126
+ console.warn(`[Statsig] Parameter type mismatch. '${name}.${key}' failed typeGuard. Expected '${expectedType}', got '${actualType}'`);
127
+ return defaultValue;
128
+ }
129
+ if (defaultValue == null || expectedType === actualType) {
130
+ exposeFunc === null || exposeFunc === void 0 ? void 0 : exposeFunc(key);
131
+ return val;
132
+ }
133
+ console.warn(`[Statsig] Parameter type mismatch. '${name}.${key}' was found to be type '${actualType}' but fallback/return type is '${expectedType}'`);
134
+ return defaultValue;
135
+ };
136
+ }
137
+ function _makeUnknownGet(value, exposeFunc) {
138
+ return (param, fallback) => {
139
+ var _a;
140
+ if (fallback === undefined) {
141
+ fallback = null;
142
+ }
143
+ if (param == null) {
144
+ return fallback;
145
+ }
146
+ if (value[param] != null) {
147
+ exposeFunc === null || exposeFunc === void 0 ? void 0 : exposeFunc(param);
148
+ }
149
+ return (_a = value[param]) !== null && _a !== void 0 ? _a : fallback;
150
+ };
151
+ }
152
+ function _extractEvaluationDetails(data) {
153
+ if (data == null) {
154
+ return {
155
+ reason: '',
156
+ lcut: 0,
157
+ receivedAt: 0,
158
+ version: 0,
159
+ };
160
+ }
161
+ return {
162
+ reason: data.reason,
163
+ lcut: data.lcut,
164
+ receivedAt: data.received_at,
165
+ version: data.version,
166
+ };
167
+ }