@cadence-ab/react 1.0.0
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/dist/index.d.mts +41 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.js +141 -0
- package/dist/index.mjs +109 -0
- package/package.json +32 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
3
|
+
import { CadenceClient } from '@cadence/sdk';
|
|
4
|
+
export { CadenceClient } from '@cadence/sdk';
|
|
5
|
+
|
|
6
|
+
interface CadenceProviderProps {
|
|
7
|
+
sdkKey: string;
|
|
8
|
+
apiUrl?: string;
|
|
9
|
+
userId?: string;
|
|
10
|
+
attributes?: Record<string, unknown>;
|
|
11
|
+
children: ReactNode;
|
|
12
|
+
}
|
|
13
|
+
declare function CadenceProvider({ sdkKey, apiUrl, userId, attributes, children, }: CadenceProviderProps): react_jsx_runtime.JSX.Element;
|
|
14
|
+
|
|
15
|
+
interface UseCadenceVariantResult {
|
|
16
|
+
variant: string;
|
|
17
|
+
isLoading: boolean;
|
|
18
|
+
}
|
|
19
|
+
declare function useCadenceVariant(experimentName: string): UseCadenceVariantResult;
|
|
20
|
+
|
|
21
|
+
interface UseTrackingResult {
|
|
22
|
+
trackConversion: (name: string, properties?: Record<string, unknown>) => void;
|
|
23
|
+
track: (name: string, properties?: Record<string, unknown>) => void;
|
|
24
|
+
}
|
|
25
|
+
declare function useTrackConversion(): UseTrackingResult;
|
|
26
|
+
|
|
27
|
+
interface UseFeatureFlagResult {
|
|
28
|
+
enabled: boolean;
|
|
29
|
+
value: unknown;
|
|
30
|
+
isLoading: boolean;
|
|
31
|
+
}
|
|
32
|
+
declare function useFeatureFlag(featureKey: string, defaultValue?: unknown): UseFeatureFlagResult;
|
|
33
|
+
|
|
34
|
+
interface CadenceContextValue {
|
|
35
|
+
client: CadenceClient | null;
|
|
36
|
+
isReady: boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
declare function useCadence(): CadenceContextValue;
|
|
40
|
+
|
|
41
|
+
export { type CadenceContextValue, CadenceProvider, type CadenceProviderProps, type UseCadenceVariantResult, type UseFeatureFlagResult, type UseTrackingResult, useCadence, useCadenceVariant, useFeatureFlag, useTrackConversion };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
3
|
+
import { CadenceClient } from '@cadence/sdk';
|
|
4
|
+
export { CadenceClient } from '@cadence/sdk';
|
|
5
|
+
|
|
6
|
+
interface CadenceProviderProps {
|
|
7
|
+
sdkKey: string;
|
|
8
|
+
apiUrl?: string;
|
|
9
|
+
userId?: string;
|
|
10
|
+
attributes?: Record<string, unknown>;
|
|
11
|
+
children: ReactNode;
|
|
12
|
+
}
|
|
13
|
+
declare function CadenceProvider({ sdkKey, apiUrl, userId, attributes, children, }: CadenceProviderProps): react_jsx_runtime.JSX.Element;
|
|
14
|
+
|
|
15
|
+
interface UseCadenceVariantResult {
|
|
16
|
+
variant: string;
|
|
17
|
+
isLoading: boolean;
|
|
18
|
+
}
|
|
19
|
+
declare function useCadenceVariant(experimentName: string): UseCadenceVariantResult;
|
|
20
|
+
|
|
21
|
+
interface UseTrackingResult {
|
|
22
|
+
trackConversion: (name: string, properties?: Record<string, unknown>) => void;
|
|
23
|
+
track: (name: string, properties?: Record<string, unknown>) => void;
|
|
24
|
+
}
|
|
25
|
+
declare function useTrackConversion(): UseTrackingResult;
|
|
26
|
+
|
|
27
|
+
interface UseFeatureFlagResult {
|
|
28
|
+
enabled: boolean;
|
|
29
|
+
value: unknown;
|
|
30
|
+
isLoading: boolean;
|
|
31
|
+
}
|
|
32
|
+
declare function useFeatureFlag(featureKey: string, defaultValue?: unknown): UseFeatureFlagResult;
|
|
33
|
+
|
|
34
|
+
interface CadenceContextValue {
|
|
35
|
+
client: CadenceClient | null;
|
|
36
|
+
isReady: boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
declare function useCadence(): CadenceContextValue;
|
|
40
|
+
|
|
41
|
+
export { type CadenceContextValue, CadenceProvider, type CadenceProviderProps, type UseCadenceVariantResult, type UseFeatureFlagResult, type UseTrackingResult, useCadence, useCadenceVariant, useFeatureFlag, useTrackConversion };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
CadenceClient: () => import_sdk2.CadenceClient,
|
|
24
|
+
CadenceProvider: () => CadenceProvider,
|
|
25
|
+
useCadence: () => useCadence,
|
|
26
|
+
useCadenceVariant: () => useCadenceVariant,
|
|
27
|
+
useFeatureFlag: () => useFeatureFlag,
|
|
28
|
+
useTrackConversion: () => useTrackConversion
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(index_exports);
|
|
31
|
+
|
|
32
|
+
// src/CadenceProvider.tsx
|
|
33
|
+
var import_react2 = require("react");
|
|
34
|
+
var import_sdk = require("@cadence/sdk");
|
|
35
|
+
|
|
36
|
+
// src/context.ts
|
|
37
|
+
var import_react = require("react");
|
|
38
|
+
var CadenceContext = (0, import_react.createContext)({
|
|
39
|
+
client: null,
|
|
40
|
+
isReady: false
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
// src/CadenceProvider.tsx
|
|
44
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
45
|
+
function CadenceProvider({
|
|
46
|
+
sdkKey,
|
|
47
|
+
apiUrl,
|
|
48
|
+
userId,
|
|
49
|
+
attributes,
|
|
50
|
+
children
|
|
51
|
+
}) {
|
|
52
|
+
const [state, setState] = (0, import_react2.useState)({
|
|
53
|
+
client: null,
|
|
54
|
+
isReady: false
|
|
55
|
+
});
|
|
56
|
+
(0, import_react2.useEffect)(() => {
|
|
57
|
+
const newClient = new import_sdk.CadenceClient({ sdkKey, apiUrl, userId, attributes });
|
|
58
|
+
let cancelled = false;
|
|
59
|
+
newClient.ready().then(() => {
|
|
60
|
+
if (!cancelled) {
|
|
61
|
+
setState({ client: newClient, isReady: true });
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
return () => {
|
|
65
|
+
cancelled = true;
|
|
66
|
+
newClient.destroy();
|
|
67
|
+
};
|
|
68
|
+
}, [sdkKey, apiUrl, userId, attributes]);
|
|
69
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CadenceContext.Provider, { value: state, children });
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// src/useCadenceVariant.ts
|
|
73
|
+
var import_react3 = require("react");
|
|
74
|
+
function useCadenceVariant(experimentName) {
|
|
75
|
+
const { client, isReady } = (0, import_react3.useContext)(CadenceContext);
|
|
76
|
+
const variant = (0, import_react3.useMemo)(() => {
|
|
77
|
+
if (!isReady || !client) return "control";
|
|
78
|
+
return client.getVariant(experimentName);
|
|
79
|
+
}, [isReady, client, experimentName]);
|
|
80
|
+
return {
|
|
81
|
+
variant,
|
|
82
|
+
isLoading: !isReady
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// src/useTrackConversion.ts
|
|
87
|
+
var import_react4 = require("react");
|
|
88
|
+
function useTrackConversion() {
|
|
89
|
+
const { client } = (0, import_react4.useContext)(CadenceContext);
|
|
90
|
+
const trackConversion = (0, import_react4.useCallback)(
|
|
91
|
+
(name, properties) => {
|
|
92
|
+
client?.trackConversion(name, properties);
|
|
93
|
+
},
|
|
94
|
+
[client]
|
|
95
|
+
);
|
|
96
|
+
const track = (0, import_react4.useCallback)(
|
|
97
|
+
(name, properties) => {
|
|
98
|
+
client?.track(name, properties);
|
|
99
|
+
},
|
|
100
|
+
[client]
|
|
101
|
+
);
|
|
102
|
+
return { trackConversion, track };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// src/useFeatureFlag.ts
|
|
106
|
+
var import_react5 = require("react");
|
|
107
|
+
function useFeatureFlag(featureKey, defaultValue = null) {
|
|
108
|
+
const { client, isReady } = (0, import_react5.useContext)(CadenceContext);
|
|
109
|
+
const { enabled, value } = (0, import_react5.useMemo)(() => {
|
|
110
|
+
if (!isReady || !client) {
|
|
111
|
+
return { enabled: false, value: defaultValue };
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
enabled: client.isFeatureEnabled(featureKey),
|
|
115
|
+
value: client.getFeatureValue(featureKey, defaultValue)
|
|
116
|
+
};
|
|
117
|
+
}, [isReady, client, featureKey, defaultValue]);
|
|
118
|
+
return {
|
|
119
|
+
enabled,
|
|
120
|
+
value,
|
|
121
|
+
isLoading: !isReady
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// src/useCadence.ts
|
|
126
|
+
var import_react6 = require("react");
|
|
127
|
+
function useCadence() {
|
|
128
|
+
return (0, import_react6.useContext)(CadenceContext);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// src/index.ts
|
|
132
|
+
var import_sdk2 = require("@cadence/sdk");
|
|
133
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
134
|
+
0 && (module.exports = {
|
|
135
|
+
CadenceClient,
|
|
136
|
+
CadenceProvider,
|
|
137
|
+
useCadence,
|
|
138
|
+
useCadenceVariant,
|
|
139
|
+
useFeatureFlag,
|
|
140
|
+
useTrackConversion
|
|
141
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// src/CadenceProvider.tsx
|
|
2
|
+
import { useEffect, useState } from "react";
|
|
3
|
+
import { CadenceClient } from "@cadence/sdk";
|
|
4
|
+
|
|
5
|
+
// src/context.ts
|
|
6
|
+
import { createContext } from "react";
|
|
7
|
+
var CadenceContext = createContext({
|
|
8
|
+
client: null,
|
|
9
|
+
isReady: false
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
// src/CadenceProvider.tsx
|
|
13
|
+
import { jsx } from "react/jsx-runtime";
|
|
14
|
+
function CadenceProvider({
|
|
15
|
+
sdkKey,
|
|
16
|
+
apiUrl,
|
|
17
|
+
userId,
|
|
18
|
+
attributes,
|
|
19
|
+
children
|
|
20
|
+
}) {
|
|
21
|
+
const [state, setState] = useState({
|
|
22
|
+
client: null,
|
|
23
|
+
isReady: false
|
|
24
|
+
});
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
const newClient = new CadenceClient({ sdkKey, apiUrl, userId, attributes });
|
|
27
|
+
let cancelled = false;
|
|
28
|
+
newClient.ready().then(() => {
|
|
29
|
+
if (!cancelled) {
|
|
30
|
+
setState({ client: newClient, isReady: true });
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
return () => {
|
|
34
|
+
cancelled = true;
|
|
35
|
+
newClient.destroy();
|
|
36
|
+
};
|
|
37
|
+
}, [sdkKey, apiUrl, userId, attributes]);
|
|
38
|
+
return /* @__PURE__ */ jsx(CadenceContext.Provider, { value: state, children });
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// src/useCadenceVariant.ts
|
|
42
|
+
import { useContext, useMemo } from "react";
|
|
43
|
+
function useCadenceVariant(experimentName) {
|
|
44
|
+
const { client, isReady } = useContext(CadenceContext);
|
|
45
|
+
const variant = useMemo(() => {
|
|
46
|
+
if (!isReady || !client) return "control";
|
|
47
|
+
return client.getVariant(experimentName);
|
|
48
|
+
}, [isReady, client, experimentName]);
|
|
49
|
+
return {
|
|
50
|
+
variant,
|
|
51
|
+
isLoading: !isReady
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// src/useTrackConversion.ts
|
|
56
|
+
import { useContext as useContext2, useCallback } from "react";
|
|
57
|
+
function useTrackConversion() {
|
|
58
|
+
const { client } = useContext2(CadenceContext);
|
|
59
|
+
const trackConversion = useCallback(
|
|
60
|
+
(name, properties) => {
|
|
61
|
+
client?.trackConversion(name, properties);
|
|
62
|
+
},
|
|
63
|
+
[client]
|
|
64
|
+
);
|
|
65
|
+
const track = useCallback(
|
|
66
|
+
(name, properties) => {
|
|
67
|
+
client?.track(name, properties);
|
|
68
|
+
},
|
|
69
|
+
[client]
|
|
70
|
+
);
|
|
71
|
+
return { trackConversion, track };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// src/useFeatureFlag.ts
|
|
75
|
+
import { useContext as useContext3, useMemo as useMemo2 } from "react";
|
|
76
|
+
function useFeatureFlag(featureKey, defaultValue = null) {
|
|
77
|
+
const { client, isReady } = useContext3(CadenceContext);
|
|
78
|
+
const { enabled, value } = useMemo2(() => {
|
|
79
|
+
if (!isReady || !client) {
|
|
80
|
+
return { enabled: false, value: defaultValue };
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
enabled: client.isFeatureEnabled(featureKey),
|
|
84
|
+
value: client.getFeatureValue(featureKey, defaultValue)
|
|
85
|
+
};
|
|
86
|
+
}, [isReady, client, featureKey, defaultValue]);
|
|
87
|
+
return {
|
|
88
|
+
enabled,
|
|
89
|
+
value,
|
|
90
|
+
isLoading: !isReady
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// src/useCadence.ts
|
|
95
|
+
import { useContext as useContext4 } from "react";
|
|
96
|
+
function useCadence() {
|
|
97
|
+
return useContext4(CadenceContext);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// src/index.ts
|
|
101
|
+
import { CadenceClient as CadenceClient2 } from "@cadence/sdk";
|
|
102
|
+
export {
|
|
103
|
+
CadenceClient2 as CadenceClient,
|
|
104
|
+
CadenceProvider,
|
|
105
|
+
useCadence,
|
|
106
|
+
useCadenceVariant,
|
|
107
|
+
useFeatureFlag,
|
|
108
|
+
useTrackConversion
|
|
109
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cadence-ab/react",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "React hooks and provider for CADENCE A/B Testing",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": ["dist"],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
18
|
+
"dev": "tsup src/index.ts --format cjs,esm --dts --watch"
|
|
19
|
+
},
|
|
20
|
+
"peerDependencies": {
|
|
21
|
+
"react": ">=18.0.0"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@cadence/sdk": "file:../sdk"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"react": "^19.0.0",
|
|
28
|
+
"@types/react": "^19.0.0",
|
|
29
|
+
"tsup": "^8.0.0",
|
|
30
|
+
"typescript": "^5.0.0"
|
|
31
|
+
}
|
|
32
|
+
}
|