wolves-react-client 1.0.5 → 1.0.6

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.
@@ -1,19 +1,26 @@
1
1
  import React from 'react';
2
- import { WolvesProviderProps } from './types';
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 is the root component for integrating Wolves SDK with React.
5
- * It initializes the WolvesClient and provides it to child components via React Context.
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,KAA+C,MAAM,OAAO,CAAC;AAGpE,OAAO,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAI9C;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,cAAc,EAAE,KAAK,CAAC,EAAE,CAAC,mBAAmB,CAuKxD,CAAC"}
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"}
@@ -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 is the root component for integrating Wolves SDK with React.
53
- * It initializes the WolvesClient and provides it to child components via React Context.
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 = ({ sdkKey, user, children, options = {}, waitForInitialization = false, loadingComponent = null, }) => {
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
- // updateUser function is stable (defined outside useEffect)
196
- // eslint-disable-next-line react-hooks/exhaustive-deps
197
- [client, isInitialized, isLoading, initializationDetails, error]);
198
- // Show loading component if waiting for initialization
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,CAyE1B"}
@@ -0,0 +1,104 @@
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
+ // Skip initialization on server
49
+ const shouldInitialize = !(0, ssrHelpers_1.isServer)();
50
+ (0, react_1.useEffect)(() => {
51
+ if (!shouldInitialize || initializingRef.current) {
52
+ if (!shouldInitialize) {
53
+ setIsLoading(false);
54
+ }
55
+ return;
56
+ }
57
+ initializingRef.current = true;
58
+ let isMounted = true;
59
+ const initializeClient = () => __awaiter(this, void 0, void 0, function* () {
60
+ try {
61
+ // Set React SDK metadata
62
+ wolves_js_client_1.WolvesMetadataProvider.add((0, metadata_1.getReactSDKMetadata)());
63
+ const newClient = new wolves_js_client_1.WolvesClient(sdkKey, user, options.apiEnv || 'prod');
64
+ const details = yield newClient.initializeAsync(options.asyncOptions);
65
+ if (!isMounted)
66
+ return;
67
+ setClient(newClient);
68
+ setInitializationDetails(details);
69
+ setIsInitialized(true);
70
+ setIsLoading(false);
71
+ if (options.onInitialized) {
72
+ options.onInitialized(details);
73
+ }
74
+ }
75
+ catch (err) {
76
+ if (!isMounted)
77
+ return;
78
+ const error = err instanceof Error ? err : new Error(String(err));
79
+ setError(error);
80
+ setIsLoading(false);
81
+ if (options.onError) {
82
+ options.onError(error);
83
+ }
84
+ }
85
+ });
86
+ initializeClient();
87
+ return () => {
88
+ isMounted = false;
89
+ if (client) {
90
+ client.shutdown().catch((e) => {
91
+ console.error('Failed to shutdown Wolves client:', e);
92
+ });
93
+ }
94
+ };
95
+ // eslint-disable-next-line react-hooks/exhaustive-deps
96
+ }, [sdkKey, shouldInitialize]);
97
+ return {
98
+ client,
99
+ isLoading,
100
+ isInitialized,
101
+ initializationDetails,
102
+ error,
103
+ };
104
+ }
@@ -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,CAyEzB"}
@@ -0,0 +1,97 @@
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
+ // Skip initialization on server
42
+ const shouldInitialize = !(0, ssrHelpers_1.isServer)();
43
+ (0, react_1.useEffect)(() => {
44
+ if (!shouldInitialize || initializingRef.current) {
45
+ if (!shouldInitialize) {
46
+ setIsLoading(false);
47
+ }
48
+ return;
49
+ }
50
+ initializingRef.current = true;
51
+ let isMounted = true;
52
+ const initializeClient = () => {
53
+ try {
54
+ // Set React SDK metadata
55
+ wolves_js_client_1.WolvesMetadataProvider.add((0, metadata_1.getReactSDKMetadata)());
56
+ const newClient = new wolves_js_client_1.WolvesClient(sdkKey, user, options.apiEnv || 'prod');
57
+ const details = newClient.initializeSync(options.syncOptions);
58
+ if (!isMounted)
59
+ return;
60
+ setClient(newClient);
61
+ setInitializationDetails(details);
62
+ setIsInitialized(true);
63
+ setIsLoading(false);
64
+ if (options.onInitialized) {
65
+ options.onInitialized(details);
66
+ }
67
+ }
68
+ catch (err) {
69
+ if (!isMounted)
70
+ return;
71
+ const error = err instanceof Error ? err : new Error(String(err));
72
+ setError(error);
73
+ setIsLoading(false);
74
+ if (options.onError) {
75
+ options.onError(error);
76
+ }
77
+ }
78
+ };
79
+ initializeClient();
80
+ return () => {
81
+ isMounted = false;
82
+ if (client) {
83
+ client.shutdown().catch((e) => {
84
+ console.error('Failed to shutdown Wolves client:', e);
85
+ });
86
+ }
87
+ };
88
+ // eslint-disable-next-line react-hooks/exhaustive-deps
89
+ }, [sdkKey, shouldInitialize]);
90
+ return {
91
+ client,
92
+ isLoading,
93
+ isInitialized,
94
+ initializationDetails,
95
+ error,
96
+ };
97
+ }
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 { useExperiment } from './hooks/useExperiment';
9
- export { useWolvesEvent } from './hooks/useWolvesEvent';
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 { WolvesProviderProps, WolvesProviderOptions, UseExperimentOptions, UseExperimentReturn, UseWolvesEventReturn, InitMode, } from './types';
12
+ export type { UseClientAsyncInitOptions, UseClientAsyncInitReturn, UseClientSyncInitOptions, UseClientSyncInitReturn, } from './types';
14
13
  export { isBrowser, isServer } from './utils/ssrHelpers';
15
14
  //# sourceMappingURL=index.d.ts.map
@@ -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;AAGlD,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,aAAa,EAAE,MAAM,uBAAuB,CAAC;AACtD,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AACxD,OAAO,EAAE,uBAAuB,EAAE,MAAM,iCAAiC,CAAC;AAC1E,YAAY,EAAE,6BAA6B,EAAE,MAAM,iCAAiC,CAAC;AAGrF,YAAY,EACV,UAAU,EACV,mBAAmB,EACnB,aAAa,EACb,kBAAkB,EAClB,iBAAiB,GAClB,MAAM,kBAAkB,CAAC;AAG1B,YAAY,EACV,mBAAmB,EACnB,qBAAqB,EACrB,oBAAoB,EACpB,mBAAmB,EACnB,oBAAoB,EACpB,QAAQ,GACT,MAAM,SAAS,CAAC;AAGjB,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,oBAAoB,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.useWolvesInitialization = exports.useWolvesEvent = exports.useExperiment = exports.useWolvesClient = exports.WolvesContext = exports.WolvesProvider = void 0;
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 useExperiment_1 = require("./hooks/useExperiment");
17
- Object.defineProperty(exports, "useExperiment", { enumerable: true, get: function () { return useExperiment_1.useExperiment; } });
18
- var useWolvesEvent_1 = require("./hooks/useWolvesEvent");
19
- Object.defineProperty(exports, "useWolvesEvent", { enumerable: true, get: function () { return useWolvesEvent_1.useWolvesEvent; } });
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; } });
@@ -2,7 +2,7 @@
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 = "1.0.4";
5
+ export declare const REACT_SDK_VERSION = "1.0.6";
6
6
  export declare const REACT_SDK_TYPE = "wolves-react-client";
7
7
  /**
8
8
  * Get React SDK metadata to override the JS SDK metadata
package/dist/metadata.js CHANGED
@@ -5,7 +5,7 @@
5
5
  */
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
7
  exports.getReactSDKMetadata = exports.REACT_SDK_TYPE = exports.REACT_SDK_VERSION = void 0;
8
- exports.REACT_SDK_VERSION = '1.0.4';
8
+ exports.REACT_SDK_VERSION = '1.0.6';
9
9
  exports.REACT_SDK_TYPE = 'wolves-react-client';
10
10
  /**
11
11
  * Get React SDK metadata to override the JS SDK metadata
@@ -1,75 +1,6 @@
1
1
  /**
2
2
  * Type definitions for Wolves React SDK
3
3
  */
4
- import { WolvesUser, WolvesUpdateDetails, AsyncUpdateOptions, SyncUpdateOptions } from 'wolves-js-client';
5
- import React from 'react';
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;AAEH,OAAO,EAAE,UAAU,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAC1G,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B;;GAEG;AACH,MAAM,MAAM,QAAQ,GAAG,OAAO,GAAG,MAAM,CAAC;AAExC;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC,uDAAuD;IACvD,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB,2DAA2D;IAC3D,MAAM,CAAC,EAAE,OAAO,GAAG,KAAK,GAAG,MAAM,CAAC;IAClC,uCAAuC;IACvC,YAAY,CAAC,EAAE,kBAAkB,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;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC,iDAAiD;IACjD,MAAM,EAAE,MAAM,CAAC;IACf,gEAAgE;IAChE,IAAI,EAAE,UAAU,CAAC;IACjB,+DAA+D;IAC/D,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAC1B,qCAAqC;IACrC,OAAO,CAAC,EAAE,qBAAqB,CAAC;IAChC,mEAAmE;IACnE,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAChC,4DAA4D;IAC5D,gBAAgB,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;CACpC;AAED;;GAEG;AACH,MAAM,WAAW,oBAAoB,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAC3D,8CAA8C;IAC9C,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,yDAAyD;IACzD,YAAY,CAAC,EAAE,CAAC,CAAC;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,mBAAmB,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAC1D,yCAAyC;IACzC,KAAK,EAAE,CAAC,CAAC;IACT,kDAAkD;IAClD,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,wBAAwB;IACxB,YAAY,EAAE,MAAM,CAAC;IACrB,2EAA2E;IAC3E,GAAG,EAAE,CAAC,CAAC,SAAS,MAAM,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,YAAY,EAAE,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;IAC7D,0CAA0C;IAC1C,SAAS,EAAE,OAAO,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACnC,qCAAqC;IACrC,QAAQ,EAAE,CACR,SAAS,EAAE,MAAM,EACjB,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,EACvB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,KAC9B,IAAI,CAAC;CACX"}
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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wolves-react-client",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "description": "React SDK for Wolves A/B Testing Platform",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",