wolves-react-client 1.0.5 → 1.0.7
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/WolvesProvider.d.ts +15 -8
- package/dist/WolvesProvider.d.ts.map +1 -1
- package/dist/WolvesProvider.js +17 -135
- package/dist/hooks/useClientAsyncInit.d.ts +49 -0
- package/dist/hooks/useClientAsyncInit.d.ts.map +1 -0
- package/dist/hooks/useClientAsyncInit.js +108 -0
- package/dist/hooks/useClientSyncInit.d.ts +51 -0
- package/dist/hooks/useClientSyncInit.d.ts.map +1 -0
- package/dist/hooks/useClientSyncInit.js +101 -0
- package/dist/index.d.ts +4 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -7
- package/dist/metadata.d.ts +2 -2
- package/dist/metadata.d.ts.map +1 -1
- package/dist/metadata.js +6 -2
- package/dist/types/index.d.ts +2 -71
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/WolvesProvider.d.ts
CHANGED
|
@@ -1,19 +1,26 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import { WolvesClient } from 'wolves-js-client';
|
|
3
|
+
export interface WolvesProviderProps {
|
|
4
|
+
/** The initialized WolvesClient instance */
|
|
5
|
+
client: WolvesClient | null;
|
|
6
|
+
/** Child components that will have access to Wolves context */
|
|
7
|
+
children: React.ReactNode;
|
|
8
|
+
/** Component to display while client is null */
|
|
9
|
+
loadingComponent?: React.ReactNode;
|
|
10
|
+
}
|
|
3
11
|
/**
|
|
4
|
-
* WolvesProvider
|
|
5
|
-
*
|
|
12
|
+
* WolvesProvider provides the Wolves client to child components via React Context.
|
|
13
|
+
* Use with useClientAsyncInit or useClientSyncInit to initialize the client.
|
|
6
14
|
*
|
|
7
15
|
* @example
|
|
8
16
|
* ```tsx
|
|
9
|
-
* import { WolvesProvider } from 'wolves-react-client';
|
|
17
|
+
* import { WolvesProvider, useClientAsyncInit } from 'wolves-react-client';
|
|
10
18
|
*
|
|
11
19
|
* function App() {
|
|
20
|
+
* const { client } = useClientAsyncInit('your-sdk-key', { userID: 'user-123' });
|
|
21
|
+
*
|
|
12
22
|
* return (
|
|
13
|
-
* <WolvesProvider
|
|
14
|
-
* sdkKey="your-sdk-key"
|
|
15
|
-
* user={{ userID: 'user-123' }}
|
|
16
|
-
* >
|
|
23
|
+
* <WolvesProvider client={client} loadingComponent={<div>Loading...</div>}>
|
|
17
24
|
* <YourApp />
|
|
18
25
|
* </WolvesProvider>
|
|
19
26
|
* );
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WolvesProvider.d.ts","sourceRoot":"","sources":["../src/WolvesProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"WolvesProvider.d.ts","sourceRoot":"","sources":["../src/WolvesProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAkB,MAAM,OAAO,CAAC;AACvC,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAIhD,MAAM,WAAW,mBAAmB;IAClC,4CAA4C;IAC5C,MAAM,EAAE,YAAY,GAAG,IAAI,CAAC;IAC5B,+DAA+D;IAC/D,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAC1B,gDAAgD;IAChD,gBAAgB,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;CACpC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,cAAc,EAAE,KAAK,CAAC,EAAE,CAAC,mBAAmB,CA8BxD,CAAC"}
|
package/dist/WolvesProvider.js
CHANGED
|
@@ -44,159 +44,41 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
44
44
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
45
45
|
exports.WolvesProvider = void 0;
|
|
46
46
|
const react_1 = __importStar(require("react"));
|
|
47
|
-
const wolves_js_client_1 = require("wolves-js-client");
|
|
48
47
|
const WolvesContext_1 = require("./WolvesContext");
|
|
49
48
|
const ssrHelpers_1 = require("./utils/ssrHelpers");
|
|
50
|
-
const metadata_1 = require("./metadata");
|
|
51
49
|
/**
|
|
52
|
-
* WolvesProvider
|
|
53
|
-
*
|
|
50
|
+
* WolvesProvider provides the Wolves client to child components via React Context.
|
|
51
|
+
* Use with useClientAsyncInit or useClientSyncInit to initialize the client.
|
|
54
52
|
*
|
|
55
53
|
* @example
|
|
56
54
|
* ```tsx
|
|
57
|
-
* import { WolvesProvider } from 'wolves-react-client';
|
|
55
|
+
* import { WolvesProvider, useClientAsyncInit } from 'wolves-react-client';
|
|
58
56
|
*
|
|
59
57
|
* function App() {
|
|
58
|
+
* const { client } = useClientAsyncInit('your-sdk-key', { userID: 'user-123' });
|
|
59
|
+
*
|
|
60
60
|
* return (
|
|
61
|
-
* <WolvesProvider
|
|
62
|
-
* sdkKey="your-sdk-key"
|
|
63
|
-
* user={{ userID: 'user-123' }}
|
|
64
|
-
* >
|
|
61
|
+
* <WolvesProvider client={client} loadingComponent={<div>Loading...</div>}>
|
|
65
62
|
* <YourApp />
|
|
66
63
|
* </WolvesProvider>
|
|
67
64
|
* );
|
|
68
65
|
* }
|
|
69
66
|
* ```
|
|
70
67
|
*/
|
|
71
|
-
const WolvesProvider = ({
|
|
72
|
-
const [client, setClient] = (0, react_1.useState)(null);
|
|
73
|
-
const [isInitialized, setIsInitialized] = (0, react_1.useState)(false);
|
|
74
|
-
const [isLoading, setIsLoading] = (0, react_1.useState)(true);
|
|
75
|
-
const [initializationDetails, setInitializationDetails] = (0, react_1.useState)(null);
|
|
76
|
-
const [error, setError] = (0, react_1.useState)(null);
|
|
77
|
-
const userRef = (0, react_1.useRef)(user);
|
|
78
|
-
// Skip initialization on server
|
|
79
|
-
const shouldInitialize = !(0, ssrHelpers_1.isServer)();
|
|
80
|
-
// Initialize client on mount
|
|
81
|
-
(0, react_1.useEffect)(() => {
|
|
82
|
-
if (!shouldInitialize) {
|
|
83
|
-
setIsLoading(false);
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
let isMounted = true;
|
|
87
|
-
const initMode = options.initMode || 'async';
|
|
88
|
-
const initializeClient = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
89
|
-
try {
|
|
90
|
-
// Set React SDK metadata before creating client
|
|
91
|
-
wolves_js_client_1.WolvesMetadataProvider.add((0, metadata_1.getReactSDKMetadata)());
|
|
92
|
-
const newClient = new wolves_js_client_1.WolvesClient(sdkKey, user, options.apiEnv || 'prod');
|
|
93
|
-
if (initMode === 'async') {
|
|
94
|
-
// Async initialization
|
|
95
|
-
const details = yield newClient.initializeAsync(options.asyncOptions);
|
|
96
|
-
if (!isMounted)
|
|
97
|
-
return;
|
|
98
|
-
setInitializationDetails(details);
|
|
99
|
-
setIsInitialized(true);
|
|
100
|
-
setIsLoading(false);
|
|
101
|
-
setClient(newClient);
|
|
102
|
-
if (options.onInitialized) {
|
|
103
|
-
options.onInitialized(details);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
else {
|
|
107
|
-
// Sync initialization
|
|
108
|
-
const details = newClient.initializeSync(options.syncOptions);
|
|
109
|
-
if (!isMounted)
|
|
110
|
-
return;
|
|
111
|
-
setInitializationDetails(details);
|
|
112
|
-
setIsInitialized(true);
|
|
113
|
-
setIsLoading(false);
|
|
114
|
-
setClient(newClient);
|
|
115
|
-
if (options.onInitialized) {
|
|
116
|
-
options.onInitialized(details);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
catch (err) {
|
|
121
|
-
if (!isMounted)
|
|
122
|
-
return;
|
|
123
|
-
const error = err instanceof Error ? err : new Error(String(err));
|
|
124
|
-
setError(error);
|
|
125
|
-
setIsLoading(false);
|
|
126
|
-
if (options.onError) {
|
|
127
|
-
options.onError(error);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
});
|
|
131
|
-
initializeClient();
|
|
132
|
-
// Cleanup on unmount
|
|
133
|
-
return () => {
|
|
134
|
-
isMounted = false;
|
|
135
|
-
if (client) {
|
|
136
|
-
client.shutdown().catch((e) => {
|
|
137
|
-
console.error('Failed to shutdown Wolves client:', e);
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
};
|
|
141
|
-
// Intentionally only depend on sdkKey and shouldInitialize
|
|
142
|
-
// User and options changes are handled separately
|
|
143
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
144
|
-
}, [sdkKey, shouldInitialize]);
|
|
145
|
-
// Handle user changes
|
|
146
|
-
(0, react_1.useEffect)(() => {
|
|
147
|
-
if (!client || !isInitialized)
|
|
148
|
-
return;
|
|
149
|
-
// Check if user actually changed
|
|
150
|
-
if (JSON.stringify(userRef.current) === JSON.stringify(user)) {
|
|
151
|
-
return;
|
|
152
|
-
}
|
|
153
|
-
userRef.current = user;
|
|
154
|
-
const updateUser = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
155
|
-
try {
|
|
156
|
-
const details = yield client.updateUserAsync(user, options.asyncOptions);
|
|
157
|
-
setInitializationDetails(details);
|
|
158
|
-
if (options.onInitialized) {
|
|
159
|
-
options.onInitialized(details);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
catch (err) {
|
|
163
|
-
const error = err instanceof Error ? err : new Error(String(err));
|
|
164
|
-
setError(error);
|
|
165
|
-
if (options.onError) {
|
|
166
|
-
options.onError(error);
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
});
|
|
170
|
-
updateUser();
|
|
171
|
-
// Intentionally don't include options in deps as we only want to react to user changes
|
|
172
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
173
|
-
}, [user, client, isInitialized]);
|
|
174
|
-
// Create updateUser function for manual user updates
|
|
175
|
-
const updateUser = (newUser) => __awaiter(void 0, void 0, void 0, function* () {
|
|
176
|
-
if (!client) {
|
|
177
|
-
throw new Error('Wolves client is not initialized');
|
|
178
|
-
}
|
|
179
|
-
userRef.current = newUser;
|
|
180
|
-
const details = yield client.updateUserAsync(newUser, options.asyncOptions);
|
|
181
|
-
setInitializationDetails(details);
|
|
182
|
-
if (options.onInitialized) {
|
|
183
|
-
options.onInitialized(details);
|
|
184
|
-
}
|
|
185
|
-
});
|
|
68
|
+
const WolvesProvider = ({ client, children, loadingComponent = null, }) => {
|
|
186
69
|
// Memoize context value to prevent unnecessary re-renders
|
|
187
70
|
const contextValue = (0, react_1.useMemo)(() => ({
|
|
188
71
|
client,
|
|
189
|
-
isInitialized,
|
|
190
|
-
isLoading,
|
|
191
|
-
initializationDetails,
|
|
192
|
-
error,
|
|
193
|
-
updateUser,
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
if (waitForInitialization && isLoading && !error) {
|
|
72
|
+
isInitialized: !!client,
|
|
73
|
+
isLoading: false,
|
|
74
|
+
initializationDetails: null,
|
|
75
|
+
error: null,
|
|
76
|
+
updateUser: () => __awaiter(void 0, void 0, void 0, function* () {
|
|
77
|
+
throw new Error('updateUser is not available when using client-based initialization. Use client.updateUserAsync() directly.');
|
|
78
|
+
}),
|
|
79
|
+
}), [client]);
|
|
80
|
+
// Show loading component if client is not ready
|
|
81
|
+
if (!client && !(0, ssrHelpers_1.isServer)()) {
|
|
200
82
|
return react_1.default.createElement(react_1.default.Fragment, null, loadingComponent);
|
|
201
83
|
}
|
|
202
84
|
return (react_1.default.createElement(WolvesContext_1.WolvesContext.Provider, { value: contextValue }, children));
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { WolvesClient, WolvesUser, WolvesUpdateDetails, AsyncUpdateOptions } from 'wolves-js-client';
|
|
2
|
+
export interface UseClientAsyncInitOptions {
|
|
3
|
+
/** API environment: 'local', 'dev', or 'prod' (default) */
|
|
4
|
+
apiEnv?: 'local' | 'dev' | 'prod';
|
|
5
|
+
/** Options for async initialization */
|
|
6
|
+
asyncOptions?: AsyncUpdateOptions;
|
|
7
|
+
/** Callback invoked when initialization completes */
|
|
8
|
+
onInitialized?: (details: WolvesUpdateDetails) => void;
|
|
9
|
+
/** Callback invoked when an error occurs */
|
|
10
|
+
onError?: (error: Error) => void;
|
|
11
|
+
}
|
|
12
|
+
export interface UseClientAsyncInitReturn {
|
|
13
|
+
/** The initialized WolvesClient instance (null until initialized) */
|
|
14
|
+
client: WolvesClient | null;
|
|
15
|
+
/** Whether the client is currently initializing */
|
|
16
|
+
isLoading: boolean;
|
|
17
|
+
/** Whether initialization has completed successfully */
|
|
18
|
+
isInitialized: boolean;
|
|
19
|
+
/** Initialization details from the last update */
|
|
20
|
+
initializationDetails: WolvesUpdateDetails | null;
|
|
21
|
+
/** Any error that occurred during initialization */
|
|
22
|
+
error: Error | null;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Hook to initialize Wolves client in async mode.
|
|
26
|
+
* Use this hook before WolvesProvider to control initialization timing.
|
|
27
|
+
*
|
|
28
|
+
* @param sdkKey - Your Wolves SDK key
|
|
29
|
+
* @param user - User identification and attributes
|
|
30
|
+
* @param options - Optional configuration
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```tsx
|
|
34
|
+
* function App() {
|
|
35
|
+
* const { client } = useClientAsyncInit(
|
|
36
|
+
* 'your-sdk-key',
|
|
37
|
+
* { userID: 'user-123' }
|
|
38
|
+
* );
|
|
39
|
+
*
|
|
40
|
+
* return (
|
|
41
|
+
* <WolvesProvider client={client} loadingComponent={<div>Loading...</div>}>
|
|
42
|
+
* <YourApp />
|
|
43
|
+
* </WolvesProvider>
|
|
44
|
+
* );
|
|
45
|
+
* }
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export declare function useClientAsyncInit(sdkKey: string, user: WolvesUser, options?: UseClientAsyncInitOptions): UseClientAsyncInitReturn;
|
|
49
|
+
//# sourceMappingURL=useClientAsyncInit.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useClientAsyncInit.d.ts","sourceRoot":"","sources":["../../src/hooks/useClientAsyncInit.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,mBAAmB,EAAE,kBAAkB,EAA0B,MAAM,kBAAkB,CAAC;AAI7H,MAAM,WAAW,yBAAyB;IACxC,2DAA2D;IAC3D,MAAM,CAAC,EAAE,OAAO,GAAG,KAAK,GAAG,MAAM,CAAC;IAClC,uCAAuC;IACvC,YAAY,CAAC,EAAE,kBAAkB,CAAC;IAClC,qDAAqD;IACrD,aAAa,CAAC,EAAE,CAAC,OAAO,EAAE,mBAAmB,KAAK,IAAI,CAAC;IACvD,4CAA4C;IAC5C,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;CAClC;AAED,MAAM,WAAW,wBAAwB;IACvC,qEAAqE;IACrE,MAAM,EAAE,YAAY,GAAG,IAAI,CAAC;IAC5B,mDAAmD;IACnD,SAAS,EAAE,OAAO,CAAC;IACnB,wDAAwD;IACxD,aAAa,EAAE,OAAO,CAAC;IACvB,kDAAkD;IAClD,qBAAqB,EAAE,mBAAmB,GAAG,IAAI,CAAC;IAClD,oDAAoD;IACpD,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;CACrB;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,UAAU,EAChB,OAAO,GAAE,yBAA8B,GACtC,wBAAwB,CA6E1B"}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.useClientAsyncInit = useClientAsyncInit;
|
|
13
|
+
const react_1 = require("react");
|
|
14
|
+
const wolves_js_client_1 = require("wolves-js-client");
|
|
15
|
+
const metadata_1 = require("../metadata");
|
|
16
|
+
const ssrHelpers_1 = require("../utils/ssrHelpers");
|
|
17
|
+
/**
|
|
18
|
+
* Hook to initialize Wolves client in async mode.
|
|
19
|
+
* Use this hook before WolvesProvider to control initialization timing.
|
|
20
|
+
*
|
|
21
|
+
* @param sdkKey - Your Wolves SDK key
|
|
22
|
+
* @param user - User identification and attributes
|
|
23
|
+
* @param options - Optional configuration
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```tsx
|
|
27
|
+
* function App() {
|
|
28
|
+
* const { client } = useClientAsyncInit(
|
|
29
|
+
* 'your-sdk-key',
|
|
30
|
+
* { userID: 'user-123' }
|
|
31
|
+
* );
|
|
32
|
+
*
|
|
33
|
+
* return (
|
|
34
|
+
* <WolvesProvider client={client} loadingComponent={<div>Loading...</div>}>
|
|
35
|
+
* <YourApp />
|
|
36
|
+
* </WolvesProvider>
|
|
37
|
+
* );
|
|
38
|
+
* }
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
function useClientAsyncInit(sdkKey, user, options = {}) {
|
|
42
|
+
const [client, setClient] = (0, react_1.useState)(null);
|
|
43
|
+
const [isLoading, setIsLoading] = (0, react_1.useState)(true);
|
|
44
|
+
const [isInitialized, setIsInitialized] = (0, react_1.useState)(false);
|
|
45
|
+
const [initializationDetails, setInitializationDetails] = (0, react_1.useState)(null);
|
|
46
|
+
const [error, setError] = (0, react_1.useState)(null);
|
|
47
|
+
const initializingRef = (0, react_1.useRef)(false);
|
|
48
|
+
const clientRef = (0, react_1.useRef)(null);
|
|
49
|
+
// Skip initialization on server
|
|
50
|
+
const shouldInitialize = !(0, ssrHelpers_1.isServer)();
|
|
51
|
+
(0, react_1.useEffect)(() => {
|
|
52
|
+
if (!shouldInitialize || initializingRef.current) {
|
|
53
|
+
if (!shouldInitialize) {
|
|
54
|
+
setIsLoading(false);
|
|
55
|
+
}
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
initializingRef.current = true;
|
|
59
|
+
let isMounted = true;
|
|
60
|
+
const initializeClient = () => __awaiter(this, void 0, void 0, function* () {
|
|
61
|
+
try {
|
|
62
|
+
// Set React SDK metadata
|
|
63
|
+
wolves_js_client_1.WolvesMetadataProvider.add((0, metadata_1.getReactSDKMetadata)());
|
|
64
|
+
const newClient = new wolves_js_client_1.WolvesClient(sdkKey, user, options.apiEnv || 'prod');
|
|
65
|
+
const details = yield newClient.initializeAsync(options.asyncOptions);
|
|
66
|
+
if (!isMounted)
|
|
67
|
+
return;
|
|
68
|
+
clientRef.current = newClient;
|
|
69
|
+
setClient(newClient);
|
|
70
|
+
setInitializationDetails(details);
|
|
71
|
+
setIsInitialized(true);
|
|
72
|
+
setIsLoading(false);
|
|
73
|
+
if (options.onInitialized) {
|
|
74
|
+
options.onInitialized(details);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
catch (err) {
|
|
78
|
+
if (!isMounted)
|
|
79
|
+
return;
|
|
80
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
81
|
+
setError(error);
|
|
82
|
+
setIsLoading(false);
|
|
83
|
+
if (options.onError) {
|
|
84
|
+
options.onError(error);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
initializeClient();
|
|
89
|
+
return () => {
|
|
90
|
+
isMounted = false;
|
|
91
|
+
initializingRef.current = false; // Reset so it can re-initialize if needed
|
|
92
|
+
if (clientRef.current) {
|
|
93
|
+
clientRef.current.shutdown().catch((e) => {
|
|
94
|
+
console.error('Failed to shutdown Wolves client:', e);
|
|
95
|
+
});
|
|
96
|
+
clientRef.current = null;
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
100
|
+
}, [sdkKey, shouldInitialize]);
|
|
101
|
+
return {
|
|
102
|
+
client,
|
|
103
|
+
isLoading,
|
|
104
|
+
isInitialized,
|
|
105
|
+
initializationDetails,
|
|
106
|
+
error,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { WolvesClient, WolvesUser, WolvesUpdateDetails, SyncUpdateOptions } from 'wolves-js-client';
|
|
2
|
+
export interface UseClientSyncInitOptions {
|
|
3
|
+
/** API environment: 'local', 'dev', or 'prod' (default) */
|
|
4
|
+
apiEnv?: 'local' | 'dev' | 'prod';
|
|
5
|
+
/** Options for sync initialization */
|
|
6
|
+
syncOptions?: SyncUpdateOptions;
|
|
7
|
+
/** Callback invoked when initialization completes */
|
|
8
|
+
onInitialized?: (details: WolvesUpdateDetails) => void;
|
|
9
|
+
/** Callback invoked when an error occurs */
|
|
10
|
+
onError?: (error: Error) => void;
|
|
11
|
+
}
|
|
12
|
+
export interface UseClientSyncInitReturn {
|
|
13
|
+
/** The initialized WolvesClient instance (null until initialized) */
|
|
14
|
+
client: WolvesClient | null;
|
|
15
|
+
/** Whether the client is currently initializing */
|
|
16
|
+
isLoading: boolean;
|
|
17
|
+
/** Whether initialization has completed successfully */
|
|
18
|
+
isInitialized: boolean;
|
|
19
|
+
/** Initialization details from the last update */
|
|
20
|
+
initializationDetails: WolvesUpdateDetails | null;
|
|
21
|
+
/** Any error that occurred during initialization */
|
|
22
|
+
error: Error | null;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Hook to initialize Wolves client in sync mode.
|
|
26
|
+
* Use this hook before WolvesProvider to control initialization timing.
|
|
27
|
+
*
|
|
28
|
+
* Sync mode loads from cache immediately and fetches updates in the background.
|
|
29
|
+
*
|
|
30
|
+
* @param sdkKey - Your Wolves SDK key
|
|
31
|
+
* @param user - User identification and attributes
|
|
32
|
+
* @param options - Optional configuration
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```tsx
|
|
36
|
+
* function App() {
|
|
37
|
+
* const { client } = useClientSyncInit(
|
|
38
|
+
* 'your-sdk-key',
|
|
39
|
+
* { userID: 'user-123' }
|
|
40
|
+
* );
|
|
41
|
+
*
|
|
42
|
+
* return (
|
|
43
|
+
* <WolvesProvider client={client}>
|
|
44
|
+
* <YourApp />
|
|
45
|
+
* </WolvesProvider>
|
|
46
|
+
* );
|
|
47
|
+
* }
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export declare function useClientSyncInit(sdkKey: string, user: WolvesUser, options?: UseClientSyncInitOptions): UseClientSyncInitReturn;
|
|
51
|
+
//# sourceMappingURL=useClientSyncInit.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useClientSyncInit.d.ts","sourceRoot":"","sources":["../../src/hooks/useClientSyncInit.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,mBAAmB,EAAE,iBAAiB,EAA0B,MAAM,kBAAkB,CAAC;AAI5H,MAAM,WAAW,wBAAwB;IACvC,2DAA2D;IAC3D,MAAM,CAAC,EAAE,OAAO,GAAG,KAAK,GAAG,MAAM,CAAC;IAClC,sCAAsC;IACtC,WAAW,CAAC,EAAE,iBAAiB,CAAC;IAChC,qDAAqD;IACrD,aAAa,CAAC,EAAE,CAAC,OAAO,EAAE,mBAAmB,KAAK,IAAI,CAAC;IACvD,4CAA4C;IAC5C,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;CAClC;AAED,MAAM,WAAW,uBAAuB;IACtC,qEAAqE;IACrE,MAAM,EAAE,YAAY,GAAG,IAAI,CAAC;IAC5B,mDAAmD;IACnD,SAAS,EAAE,OAAO,CAAC;IACnB,wDAAwD;IACxD,aAAa,EAAE,OAAO,CAAC;IACvB,kDAAkD;IAClD,qBAAqB,EAAE,mBAAmB,GAAG,IAAI,CAAC;IAClD,oDAAoD;IACpD,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;CACrB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,UAAU,EAChB,OAAO,GAAE,wBAA6B,GACrC,uBAAuB,CA6EzB"}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useClientSyncInit = useClientSyncInit;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const wolves_js_client_1 = require("wolves-js-client");
|
|
6
|
+
const metadata_1 = require("../metadata");
|
|
7
|
+
const ssrHelpers_1 = require("../utils/ssrHelpers");
|
|
8
|
+
/**
|
|
9
|
+
* Hook to initialize Wolves client in sync mode.
|
|
10
|
+
* Use this hook before WolvesProvider to control initialization timing.
|
|
11
|
+
*
|
|
12
|
+
* Sync mode loads from cache immediately and fetches updates in the background.
|
|
13
|
+
*
|
|
14
|
+
* @param sdkKey - Your Wolves SDK key
|
|
15
|
+
* @param user - User identification and attributes
|
|
16
|
+
* @param options - Optional configuration
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```tsx
|
|
20
|
+
* function App() {
|
|
21
|
+
* const { client } = useClientSyncInit(
|
|
22
|
+
* 'your-sdk-key',
|
|
23
|
+
* { userID: 'user-123' }
|
|
24
|
+
* );
|
|
25
|
+
*
|
|
26
|
+
* return (
|
|
27
|
+
* <WolvesProvider client={client}>
|
|
28
|
+
* <YourApp />
|
|
29
|
+
* </WolvesProvider>
|
|
30
|
+
* );
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
function useClientSyncInit(sdkKey, user, options = {}) {
|
|
35
|
+
const [client, setClient] = (0, react_1.useState)(null);
|
|
36
|
+
const [isLoading, setIsLoading] = (0, react_1.useState)(true);
|
|
37
|
+
const [isInitialized, setIsInitialized] = (0, react_1.useState)(false);
|
|
38
|
+
const [initializationDetails, setInitializationDetails] = (0, react_1.useState)(null);
|
|
39
|
+
const [error, setError] = (0, react_1.useState)(null);
|
|
40
|
+
const initializingRef = (0, react_1.useRef)(false);
|
|
41
|
+
const clientRef = (0, react_1.useRef)(null);
|
|
42
|
+
// Skip initialization on server
|
|
43
|
+
const shouldInitialize = !(0, ssrHelpers_1.isServer)();
|
|
44
|
+
(0, react_1.useEffect)(() => {
|
|
45
|
+
if (!shouldInitialize || initializingRef.current) {
|
|
46
|
+
if (!shouldInitialize) {
|
|
47
|
+
setIsLoading(false);
|
|
48
|
+
}
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
initializingRef.current = true;
|
|
52
|
+
let isMounted = true;
|
|
53
|
+
const initializeClient = () => {
|
|
54
|
+
try {
|
|
55
|
+
// Set React SDK metadata
|
|
56
|
+
wolves_js_client_1.WolvesMetadataProvider.add((0, metadata_1.getReactSDKMetadata)());
|
|
57
|
+
const newClient = new wolves_js_client_1.WolvesClient(sdkKey, user, options.apiEnv || 'prod');
|
|
58
|
+
const details = newClient.initializeSync(options.syncOptions);
|
|
59
|
+
if (!isMounted)
|
|
60
|
+
return;
|
|
61
|
+
clientRef.current = newClient;
|
|
62
|
+
setClient(newClient);
|
|
63
|
+
setInitializationDetails(details);
|
|
64
|
+
setIsInitialized(true);
|
|
65
|
+
setIsLoading(false);
|
|
66
|
+
if (options.onInitialized) {
|
|
67
|
+
options.onInitialized(details);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
catch (err) {
|
|
71
|
+
if (!isMounted)
|
|
72
|
+
return;
|
|
73
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
74
|
+
setError(error);
|
|
75
|
+
setIsLoading(false);
|
|
76
|
+
if (options.onError) {
|
|
77
|
+
options.onError(error);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
initializeClient();
|
|
82
|
+
return () => {
|
|
83
|
+
isMounted = false;
|
|
84
|
+
initializingRef.current = false; // Reset so it can re-initialize if needed
|
|
85
|
+
if (clientRef.current) {
|
|
86
|
+
clientRef.current.shutdown().catch((e) => {
|
|
87
|
+
console.error('Failed to shutdown Wolves client:', e);
|
|
88
|
+
});
|
|
89
|
+
clientRef.current = null;
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
93
|
+
}, [sdkKey, shouldInitialize]);
|
|
94
|
+
return {
|
|
95
|
+
client,
|
|
96
|
+
isLoading,
|
|
97
|
+
isInitialized,
|
|
98
|
+
initializationDetails,
|
|
99
|
+
error,
|
|
100
|
+
};
|
|
101
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -2,14 +2,13 @@
|
|
|
2
2
|
* Main entry point for the Wolves React SDK
|
|
3
3
|
*/
|
|
4
4
|
export { WolvesProvider } from './WolvesProvider';
|
|
5
|
+
export type { WolvesProviderProps } from './WolvesProvider';
|
|
5
6
|
export { WolvesContext } from './WolvesContext';
|
|
6
7
|
export type { WolvesContextValue } from './WolvesContext';
|
|
7
8
|
export { useWolvesClient } from './hooks/useWolvesClient';
|
|
8
|
-
export {
|
|
9
|
-
export {
|
|
10
|
-
export { useWolvesInitialization } from './hooks/useWolvesInitialization';
|
|
11
|
-
export type { UseWolvesInitializationReturn } from './hooks/useWolvesInitialization';
|
|
9
|
+
export { useClientAsyncInit } from './hooks/useClientAsyncInit';
|
|
10
|
+
export { useClientSyncInit } from './hooks/useClientSyncInit';
|
|
12
11
|
export type { WolvesUser, WolvesUpdateDetails, LoadingStatus, AsyncUpdateOptions, SyncUpdateOptions, } from 'wolves-js-client';
|
|
13
|
-
export type {
|
|
12
|
+
export type { UseClientAsyncInitOptions, UseClientAsyncInitReturn, UseClientSyncInitOptions, UseClientSyncInitReturn, } from './types';
|
|
14
13
|
export { isBrowser, isServer } from './utils/ssrHelpers';
|
|
15
14
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,YAAY,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAG5D,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,YAAY,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAG1D,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC1D,OAAO,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAChE,OAAO,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAG9D,YAAY,EACV,UAAU,EACV,mBAAmB,EACnB,aAAa,EACb,kBAAkB,EAClB,iBAAiB,GAClB,MAAM,kBAAkB,CAAC;AAG1B,YAAY,EACV,yBAAyB,EACzB,wBAAwB,EACxB,wBAAwB,EACxB,uBAAuB,GACxB,MAAM,SAAS,CAAC;AAGjB,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Main entry point for the Wolves React SDK
|
|
4
4
|
*/
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.isServer = exports.isBrowser = exports.
|
|
6
|
+
exports.isServer = exports.isBrowser = exports.useClientSyncInit = exports.useClientAsyncInit = exports.useWolvesClient = exports.WolvesContext = exports.WolvesProvider = void 0;
|
|
7
7
|
// Provider component
|
|
8
8
|
var WolvesProvider_1 = require("./WolvesProvider");
|
|
9
9
|
Object.defineProperty(exports, "WolvesProvider", { enumerable: true, get: function () { return WolvesProvider_1.WolvesProvider; } });
|
|
@@ -13,12 +13,10 @@ Object.defineProperty(exports, "WolvesContext", { enumerable: true, get: functio
|
|
|
13
13
|
// Hooks
|
|
14
14
|
var useWolvesClient_1 = require("./hooks/useWolvesClient");
|
|
15
15
|
Object.defineProperty(exports, "useWolvesClient", { enumerable: true, get: function () { return useWolvesClient_1.useWolvesClient; } });
|
|
16
|
-
var
|
|
17
|
-
Object.defineProperty(exports, "
|
|
18
|
-
var
|
|
19
|
-
Object.defineProperty(exports, "
|
|
20
|
-
var useWolvesInitialization_1 = require("./hooks/useWolvesInitialization");
|
|
21
|
-
Object.defineProperty(exports, "useWolvesInitialization", { enumerable: true, get: function () { return useWolvesInitialization_1.useWolvesInitialization; } });
|
|
16
|
+
var useClientAsyncInit_1 = require("./hooks/useClientAsyncInit");
|
|
17
|
+
Object.defineProperty(exports, "useClientAsyncInit", { enumerable: true, get: function () { return useClientAsyncInit_1.useClientAsyncInit; } });
|
|
18
|
+
var useClientSyncInit_1 = require("./hooks/useClientSyncInit");
|
|
19
|
+
Object.defineProperty(exports, "useClientSyncInit", { enumerable: true, get: function () { return useClientSyncInit_1.useClientSyncInit; } });
|
|
22
20
|
// SSR utilities
|
|
23
21
|
var ssrHelpers_1 = require("./utils/ssrHelpers");
|
|
24
22
|
Object.defineProperty(exports, "isBrowser", { enumerable: true, get: function () { return ssrHelpers_1.isBrowser; } });
|
package/dist/metadata.d.ts
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
* React SDK Metadata
|
|
3
3
|
* This file defines the SDK version and type for the Wolves React SDK
|
|
4
4
|
*/
|
|
5
|
-
export declare const REACT_SDK_VERSION
|
|
6
|
-
export declare const REACT_SDK_TYPE
|
|
5
|
+
export declare const REACT_SDK_VERSION: string;
|
|
6
|
+
export declare const REACT_SDK_TYPE: string;
|
|
7
7
|
/**
|
|
8
8
|
* Get React SDK metadata to override the JS SDK metadata
|
|
9
9
|
*/
|
package/dist/metadata.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"metadata.d.ts","sourceRoot":"","sources":["../src/metadata.ts"],"names":[],"mappings":"AAAA;;;GAGG;
|
|
1
|
+
{"version":3,"file":"metadata.d.ts","sourceRoot":"","sources":["../src/metadata.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,eAAO,MAAM,iBAAiB,QAAsB,CAAC;AACrD,eAAO,MAAM,cAAc,QAAmB,CAAC;AAE/C;;GAEG;AACH,eAAO,MAAM,mBAAmB;;;CAG9B,CAAC"}
|
package/dist/metadata.js
CHANGED
|
@@ -3,10 +3,14 @@
|
|
|
3
3
|
* React SDK Metadata
|
|
4
4
|
* This file defines the SDK version and type for the Wolves React SDK
|
|
5
5
|
*/
|
|
6
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
7
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
8
|
+
};
|
|
6
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
10
|
exports.getReactSDKMetadata = exports.REACT_SDK_TYPE = exports.REACT_SDK_VERSION = void 0;
|
|
8
|
-
|
|
9
|
-
exports.
|
|
11
|
+
const package_json_1 = __importDefault(require("../package.json"));
|
|
12
|
+
exports.REACT_SDK_VERSION = package_json_1.default.version;
|
|
13
|
+
exports.REACT_SDK_TYPE = package_json_1.default.name;
|
|
10
14
|
/**
|
|
11
15
|
* Get React SDK metadata to override the JS SDK metadata
|
|
12
16
|
*/
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,75 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Type definitions for Wolves React SDK
|
|
3
3
|
*/
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Initialization mode for the Wolves client
|
|
8
|
-
*/
|
|
9
|
-
export type InitMode = 'async' | 'sync';
|
|
10
|
-
/**
|
|
11
|
-
* Options for configuring WolvesProvider behavior
|
|
12
|
-
*/
|
|
13
|
-
export interface WolvesProviderOptions {
|
|
14
|
-
/** Initialization mode: 'async' (default) or 'sync' */
|
|
15
|
-
initMode?: InitMode;
|
|
16
|
-
/** API environment: 'local', 'dev', or 'prod' (default) */
|
|
17
|
-
apiEnv?: 'local' | 'dev' | 'prod';
|
|
18
|
-
/** Options for async initialization */
|
|
19
|
-
asyncOptions?: AsyncUpdateOptions;
|
|
20
|
-
/** Options for sync initialization */
|
|
21
|
-
syncOptions?: SyncUpdateOptions;
|
|
22
|
-
/** Callback invoked when initialization completes */
|
|
23
|
-
onInitialized?: (details: WolvesUpdateDetails) => void;
|
|
24
|
-
/** Callback invoked when an error occurs */
|
|
25
|
-
onError?: (error: Error) => void;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Props for the WolvesProvider component
|
|
29
|
-
*/
|
|
30
|
-
export interface WolvesProviderProps {
|
|
31
|
-
/** SDK key for authenticating with Wolves API */
|
|
32
|
-
sdkKey: string;
|
|
33
|
-
/** User object containing user identification and attributes */
|
|
34
|
-
user: WolvesUser;
|
|
35
|
-
/** Child components that will have access to Wolves context */
|
|
36
|
-
children: React.ReactNode;
|
|
37
|
-
/** Optional configuration options */
|
|
38
|
-
options?: WolvesProviderOptions;
|
|
39
|
-
/** Whether to wait for initialization before rendering children */
|
|
40
|
-
waitForInitialization?: boolean;
|
|
41
|
-
/** Component to display while waiting for initialization */
|
|
42
|
-
loadingComponent?: React.ReactNode;
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Options for the useExperiment hook
|
|
46
|
-
*/
|
|
47
|
-
export interface UseExperimentOptions<T = Record<string, any>> {
|
|
48
|
-
/** Whether to skip logging exposure events */
|
|
49
|
-
skipExposure?: boolean;
|
|
50
|
-
/** Default value to return if experiment is not found */
|
|
51
|
-
defaultValue?: T;
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Return type for the useExperiment hook
|
|
55
|
-
*/
|
|
56
|
-
export interface UseExperimentReturn<T = Record<string, any>> {
|
|
57
|
-
/** The experiment configuration value */
|
|
58
|
-
value: T;
|
|
59
|
-
/** The group/variant name assigned to the user */
|
|
60
|
-
groupName: string | null;
|
|
61
|
-
/** The experiment ID */
|
|
62
|
-
experimentID: string;
|
|
63
|
-
/** Get a specific parameter from the experiment with a default fallback */
|
|
64
|
-
get: <K extends keyof T>(key: K, defaultValue: T[K]) => T[K];
|
|
65
|
-
/** Whether the client is still loading */
|
|
66
|
-
isLoading: boolean;
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Return type for the useWolvesEvent hook
|
|
70
|
-
*/
|
|
71
|
-
export interface UseWolvesEventReturn {
|
|
72
|
-
/** Function to log a custom event */
|
|
73
|
-
logEvent: (eventName: string, value?: string | number, metadata?: Record<string, string>) => void;
|
|
74
|
-
}
|
|
4
|
+
export type { UseClientAsyncInitOptions, UseClientAsyncInitReturn } from '../hooks/useClientAsyncInit';
|
|
5
|
+
export type { UseClientSyncInitOptions, UseClientSyncInitReturn } from '../hooks/useClientSyncInit';
|
|
75
6
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA;;GAEG;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAMH,YAAY,EAAE,yBAAyB,EAAE,wBAAwB,EAAE,MAAM,6BAA6B,CAAC;AACvG,YAAY,EAAE,wBAAwB,EAAE,uBAAuB,EAAE,MAAM,4BAA4B,CAAC"}
|