@statsig/react-bindings 0.0.1-beta.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.
- package/README.md +8 -0
- package/package.json +13 -0
- package/src/NoopEvaluationsClient.d.ts +4 -0
- package/src/NoopEvaluationsClient.js +41 -0
- package/src/OnDeviceVsPrecomputedUtils.d.ts +3 -0
- package/src/OnDeviceVsPrecomputedUtils.js +12 -0
- package/src/StatsigContext.d.ts +8 -0
- package/src/StatsigContext.js +8 -0
- package/src/StatsigProvider.d.ts +12 -0
- package/src/StatsigProvider.js +62 -0
- package/src/index.d.ts +7 -0
- package/src/index.js +27 -0
- package/src/useDynamicConfig.d.ts +5 -0
- package/src/useDynamicConfig.js +28 -0
- package/src/useExperiment.d.ts +4 -0
- package/src/useExperiment.js +21 -0
- package/src/useGate.d.ts +5 -0
- package/src/useGate.js +28 -0
- package/src/useLayer.d.ts +5 -0
- package/src/useLayer.js +28 -0
- package/src/useStatsigClient.d.ts +5 -0
- package/src/useStatsigClient.js +8 -0
package/README.md
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# Statsig - React
|
|
2
|
+
|
|
3
|
+
> [!IMPORTANT]
|
|
4
|
+
> This version of the SDK is still in beta. The non-beta version can be found [here](https://github.com/statsig-io/react-sdk).
|
|
5
|
+
|
|
6
|
+
ReactJS specific additions for use with a Statsig Javascript SDK.
|
|
7
|
+
|
|
8
|
+
Learn more by visiting: https://docs.statsig.com/client/jsClientSDK
|
package/package.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@statsig/react-bindings",
|
|
3
|
+
"version": "0.0.1-beta.1",
|
|
4
|
+
"dependencies": {
|
|
5
|
+
"@statsig/client-core": "*"
|
|
6
|
+
},
|
|
7
|
+
"peerDependencies": {
|
|
8
|
+
"react": "^16.6.3 || ^17.0.0 || ^18.0.0"
|
|
9
|
+
},
|
|
10
|
+
"type": "commonjs",
|
|
11
|
+
"main": "./src/index.js",
|
|
12
|
+
"typings": "./src/index.d.ts"
|
|
13
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NoopEvaluationsClient = void 0;
|
|
4
|
+
var client_core_1 = require("@statsig/client-core");
|
|
5
|
+
var noop = function () {
|
|
6
|
+
// noop
|
|
7
|
+
};
|
|
8
|
+
var defaultEvaluation = function (type) {
|
|
9
|
+
return function () {
|
|
10
|
+
var args = [];
|
|
11
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
12
|
+
args[_i] = arguments[_i];
|
|
13
|
+
}
|
|
14
|
+
var name = typeof args[0] === 'string' ? args[0] : args[1];
|
|
15
|
+
switch (type) {
|
|
16
|
+
case 'gate':
|
|
17
|
+
return (0, client_core_1.makeFeatureGate)(name, 'Error');
|
|
18
|
+
case 'config':
|
|
19
|
+
return (0, client_core_1.makeDynamicConfig)(name, 'Error');
|
|
20
|
+
case 'layer':
|
|
21
|
+
return (0, client_core_1.makeLayer)(name, 'Error');
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
var client = {
|
|
26
|
+
isNoop: true,
|
|
27
|
+
loadingStatus: 'Uninitialized',
|
|
28
|
+
initialize: function () { return Promise.resolve(); },
|
|
29
|
+
shutdown: function () { return Promise.resolve(); },
|
|
30
|
+
updateUser: function () { return Promise.resolve(); },
|
|
31
|
+
getCurrentUser: function () { return ({ userID: '' }); },
|
|
32
|
+
checkGate: function () { return false; },
|
|
33
|
+
getFeatureGate: defaultEvaluation('gate'),
|
|
34
|
+
getDynamicConfig: defaultEvaluation('config'),
|
|
35
|
+
getExperiment: defaultEvaluation('config'),
|
|
36
|
+
getLayer: defaultEvaluation('layer'),
|
|
37
|
+
logEvent: noop,
|
|
38
|
+
on: noop,
|
|
39
|
+
off: noop,
|
|
40
|
+
};
|
|
41
|
+
exports.NoopEvaluationsClient = client;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { OnDeviceEvaluationsInterface, PrecomputedEvaluationsInterface } from '@statsig/client-core';
|
|
2
|
+
export declare function isPrecomputedEvaluationsClient(client: OnDeviceEvaluationsInterface | PrecomputedEvaluationsInterface): client is PrecomputedEvaluationsInterface;
|
|
3
|
+
export declare function logMissingStatsigUserWarning(): void;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.logMissingStatsigUserWarning = exports.isPrecomputedEvaluationsClient = void 0;
|
|
4
|
+
var client_core_1 = require("@statsig/client-core");
|
|
5
|
+
function isPrecomputedEvaluationsClient(client) {
|
|
6
|
+
return 'updateUser' in client;
|
|
7
|
+
}
|
|
8
|
+
exports.isPrecomputedEvaluationsClient = isPrecomputedEvaluationsClient;
|
|
9
|
+
function logMissingStatsigUserWarning() {
|
|
10
|
+
client_core_1.Log.warn('StatsigUser not provided for On Device Evaluation. Returning default value.');
|
|
11
|
+
}
|
|
12
|
+
exports.logMissingStatsigUserWarning = logMissingStatsigUserWarning;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { OnDeviceEvaluationsInterface, PrecomputedEvaluationsInterface } from '@statsig/client-core';
|
|
2
|
+
export interface StatsigContext {
|
|
3
|
+
readonly renderVersion: number;
|
|
4
|
+
readonly precomputedClient: PrecomputedEvaluationsInterface;
|
|
5
|
+
readonly onDeviceClient: OnDeviceEvaluationsInterface;
|
|
6
|
+
}
|
|
7
|
+
declare const _default: import("react").Context<StatsigContext>;
|
|
8
|
+
export default _default;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
import { OnDeviceEvaluationsInterface, PrecomputedEvaluationsInterface } from '@statsig/client-core';
|
|
3
|
+
type Props = {
|
|
4
|
+
children: ReactNode | ReactNode[];
|
|
5
|
+
} & ({
|
|
6
|
+
client: OnDeviceEvaluationsInterface | PrecomputedEvaluationsInterface;
|
|
7
|
+
} | {
|
|
8
|
+
precomputedClient: PrecomputedEvaluationsInterface;
|
|
9
|
+
onDeviceClient: OnDeviceEvaluationsInterface;
|
|
10
|
+
});
|
|
11
|
+
export default function StatsigProvider(props: Props): JSX.Element;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var jsx_runtime_1 = require("react/jsx-runtime");
|
|
4
|
+
var react_1 = require("react");
|
|
5
|
+
var client_core_1 = require("@statsig/client-core");
|
|
6
|
+
var NoopEvaluationsClient_1 = require("./NoopEvaluationsClient");
|
|
7
|
+
var OnDeviceVsPrecomputedUtils_1 = require("./OnDeviceVsPrecomputedUtils");
|
|
8
|
+
var StatsigContext_1 = require("./StatsigContext");
|
|
9
|
+
function StatsigProvider(props) {
|
|
10
|
+
var precomputedClient;
|
|
11
|
+
var onDeviceClient;
|
|
12
|
+
if ('client' in props) {
|
|
13
|
+
precomputedClient = (0, OnDeviceVsPrecomputedUtils_1.isPrecomputedEvaluationsClient)(props.client)
|
|
14
|
+
? props.client
|
|
15
|
+
: NoopEvaluationsClient_1.NoopEvaluationsClient;
|
|
16
|
+
onDeviceClient = !(0, OnDeviceVsPrecomputedUtils_1.isPrecomputedEvaluationsClient)(props.client)
|
|
17
|
+
? props.client
|
|
18
|
+
: NoopEvaluationsClient_1.NoopEvaluationsClient;
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
precomputedClient = props.precomputedClient;
|
|
22
|
+
onDeviceClient = props.onDeviceClient;
|
|
23
|
+
}
|
|
24
|
+
var _a = (0, react_1.useState)(0), renderVersion = _a[0], setRenderVersion = _a[1];
|
|
25
|
+
var clients = [precomputedClient, onDeviceClient];
|
|
26
|
+
(0, react_1.useEffect)(function () {
|
|
27
|
+
var onStatusChange = function (data) {
|
|
28
|
+
if (data.event === 'status_change') {
|
|
29
|
+
setRenderVersion(function (v) { return v + 1; });
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
clients.forEach(function (client) {
|
|
33
|
+
client.on('status_change', onStatusChange);
|
|
34
|
+
client.initialize().catch(function (error) {
|
|
35
|
+
client_core_1.Log.error('An error occurred during initialization', error);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
return function () {
|
|
39
|
+
clients.forEach(function (client) {
|
|
40
|
+
client.shutdown().catch(function (error) {
|
|
41
|
+
client_core_1.Log.error('An error occured during shutdown', error);
|
|
42
|
+
});
|
|
43
|
+
client.off('status_change', onStatusChange);
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
}, clients);
|
|
47
|
+
return ((0, jsx_runtime_1.jsx)(StatsigContext_1.default.Provider, { value: { renderVersion: renderVersion, onDeviceClient: onDeviceClient, precomputedClient: precomputedClient }, children: shouldRender(precomputedClient) && shouldRender(onDeviceClient)
|
|
48
|
+
? props.children
|
|
49
|
+
: null }));
|
|
50
|
+
}
|
|
51
|
+
exports.default = StatsigProvider;
|
|
52
|
+
function shouldRender(client) {
|
|
53
|
+
if ('isNoop' in client) {
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
switch (client.loadingStatus) {
|
|
57
|
+
case 'Ready':
|
|
58
|
+
return true;
|
|
59
|
+
default:
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import StatsigContext from './StatsigContext';
|
|
2
|
+
import StatsigProvider from './StatsigProvider';
|
|
3
|
+
import useDynamicConfig from './useDynamicConfig';
|
|
4
|
+
import useExperiment from './useExperiment';
|
|
5
|
+
import useGate from './useGate';
|
|
6
|
+
import useLayer from './useLayer';
|
|
7
|
+
export { StatsigContext, StatsigProvider, useGate, useDynamicConfig, useExperiment, useLayer, };
|
package/src/index.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.useLayer = exports.useExperiment = exports.useDynamicConfig = exports.useGate = exports.StatsigProvider = exports.StatsigContext = void 0;
|
|
15
|
+
var StatsigContext_1 = require("./StatsigContext");
|
|
16
|
+
exports.StatsigContext = StatsigContext_1.default;
|
|
17
|
+
var StatsigProvider_1 = require("./StatsigProvider");
|
|
18
|
+
exports.StatsigProvider = StatsigProvider_1.default;
|
|
19
|
+
var useDynamicConfig_1 = require("./useDynamicConfig");
|
|
20
|
+
exports.useDynamicConfig = useDynamicConfig_1.default;
|
|
21
|
+
var useExperiment_1 = require("./useExperiment");
|
|
22
|
+
exports.useExperiment = useExperiment_1.default;
|
|
23
|
+
var useGate_1 = require("./useGate");
|
|
24
|
+
exports.useGate = useGate_1.default;
|
|
25
|
+
var useLayer_1 = require("./useLayer");
|
|
26
|
+
exports.useLayer = useLayer_1.default;
|
|
27
|
+
__STATSIG__ = __assign(__assign({}, __STATSIG__), { StatsigContext: StatsigContext_1.default, StatsigProvider: StatsigProvider_1.default, useGate: useGate_1.default, useDynamicConfig: useDynamicConfig_1.default, useExperiment: useExperiment_1.default, useLayer: useLayer_1.default });
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { DynamicConfig, EvaluationOptions, StatsigUser } from '@statsig/client-core';
|
|
2
|
+
export type UseDynamicConfigOptions = EvaluationOptions & {
|
|
3
|
+
user: StatsigUser | null;
|
|
4
|
+
};
|
|
5
|
+
export default function (configName: string, options?: UseDynamicConfigOptions): DynamicConfig;
|
|
@@ -0,0 +1,28 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
var react_1 = require("react");
|
|
15
|
+
var client_core_1 = require("@statsig/client-core");
|
|
16
|
+
var StatsigContext_1 = require("./StatsigContext");
|
|
17
|
+
function default_1(configName, options) {
|
|
18
|
+
if (options === void 0) { options = __assign(__assign({}, client_core_1.DEFAULT_EVAL_OPTIONS), { user: null }); }
|
|
19
|
+
var _a = (0, react_1.useContext)(StatsigContext_1.default), precomputedClient = _a.precomputedClient, onDeviceClient = _a.onDeviceClient;
|
|
20
|
+
var config = (0, react_1.useMemo)(function () {
|
|
21
|
+
if (options.user == null) {
|
|
22
|
+
return precomputedClient.getDynamicConfig(configName, options);
|
|
23
|
+
}
|
|
24
|
+
return onDeviceClient.getDynamicConfig(configName, options.user, options);
|
|
25
|
+
}, [precomputedClient.loadingStatus, onDeviceClient.loadingStatus, options]);
|
|
26
|
+
return config;
|
|
27
|
+
}
|
|
28
|
+
exports.default = default_1;
|
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
var client_core_1 = require("@statsig/client-core");
|
|
15
|
+
var useDynamicConfig_1 = require("./useDynamicConfig");
|
|
16
|
+
function default_1(experimentName, options) {
|
|
17
|
+
if (options === void 0) { options = __assign(__assign({}, client_core_1.DEFAULT_EVAL_OPTIONS), { user: null }); }
|
|
18
|
+
var config = (0, useDynamicConfig_1.default)(experimentName, options);
|
|
19
|
+
return config;
|
|
20
|
+
}
|
|
21
|
+
exports.default = default_1;
|
package/src/useGate.d.ts
ADDED
package/src/useGate.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
var react_1 = require("react");
|
|
15
|
+
var client_core_1 = require("@statsig/client-core");
|
|
16
|
+
var StatsigContext_1 = require("./StatsigContext");
|
|
17
|
+
function default_1(gateName, options) {
|
|
18
|
+
if (options === void 0) { options = __assign(__assign({}, client_core_1.DEFAULT_EVAL_OPTIONS), { user: null }); }
|
|
19
|
+
var _a = (0, react_1.useContext)(StatsigContext_1.default), precomputedClient = _a.precomputedClient, onDeviceClient = _a.onDeviceClient;
|
|
20
|
+
var gate = (0, react_1.useMemo)(function () {
|
|
21
|
+
if (options.user == null) {
|
|
22
|
+
return precomputedClient.getFeatureGate(gateName, options);
|
|
23
|
+
}
|
|
24
|
+
return onDeviceClient.getFeatureGate(gateName, options.user, options);
|
|
25
|
+
}, [precomputedClient.loadingStatus, onDeviceClient.loadingStatus, options]);
|
|
26
|
+
return gate;
|
|
27
|
+
}
|
|
28
|
+
exports.default = default_1;
|
package/src/useLayer.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
var react_1 = require("react");
|
|
15
|
+
var client_core_1 = require("@statsig/client-core");
|
|
16
|
+
var StatsigContext_1 = require("./StatsigContext");
|
|
17
|
+
function default_1(layerName, options) {
|
|
18
|
+
if (options === void 0) { options = __assign(__assign({}, client_core_1.DEFAULT_EVAL_OPTIONS), { user: null }); }
|
|
19
|
+
var _a = (0, react_1.useContext)(StatsigContext_1.default), precomputedClient = _a.precomputedClient, onDeviceClient = _a.onDeviceClient;
|
|
20
|
+
var layer = (0, react_1.useMemo)(function () {
|
|
21
|
+
if (options.user == null) {
|
|
22
|
+
return precomputedClient.getLayer(layerName, options);
|
|
23
|
+
}
|
|
24
|
+
return onDeviceClient.getLayer(layerName, options.user, options);
|
|
25
|
+
}, [precomputedClient.loadingStatus, onDeviceClient.loadingStatus, options]);
|
|
26
|
+
return layer;
|
|
27
|
+
}
|
|
28
|
+
exports.default = default_1;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var react_1 = require("react");
|
|
4
|
+
var StatsigContext_1 = require("./StatsigContext");
|
|
5
|
+
function default_1() {
|
|
6
|
+
return (0, react_1.useContext)(StatsigContext_1.default);
|
|
7
|
+
}
|
|
8
|
+
exports.default = default_1;
|