@liberfi.io/wallet-connector 0.1.18 → 0.1.20

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 ADDED
@@ -0,0 +1,334 @@
1
+ # @liberfi.io/wallet-connector
2
+
3
+ Base wallet connector abstraction for the Liberfi React SDK. This package defines the **interfaces**, **React context/providers**, and **hooks** for wallet connection and authentication — without binding to any specific wallet provider or identity service. Concrete implementations (e.g. Privy) live in separate packages like `@liberfi.io/wallet-connector-privy`.
4
+
5
+ ## Design Philosophy
6
+
7
+ - **Provider-agnostic abstraction** — Defines a `WalletAdapter` interface and React contexts; the package never imports a concrete wallet SDK. Implementation packages inject behavior via `WalletConnectorProvider` and `AuthProvider`.
8
+ - **Inversion of control** — `connect`, `disconnect`, `signIn`, `signOut`, and `refreshAccessToken` are supplied by the consumer, not hardcoded. This allows swapping wallet providers without changing downstream code.
9
+ - **Layered architecture** — Three clean layers: `types/` (interfaces and domain types) → `providers/` (React context and passthrough providers) → `hooks/` (consumer-facing hooks). No circular dependencies.
10
+ - **Minimal surface area** — Only exports what consumers need: 2 providers, 5 hooks, and a handful of types. No UI, no side effects, no heavy dependencies.
11
+
12
+ ## Installation
13
+
14
+ ```bash
15
+ pnpm add @liberfi.io/wallet-connector
16
+ ```
17
+
18
+ ### Peer Dependencies
19
+
20
+ | Package | Version |
21
+ | ----------- | ------- |
22
+ | `react` | >=18 |
23
+ | `react-dom` | >=18 |
24
+
25
+ ## API Reference
26
+
27
+ ### Types
28
+
29
+ #### `WalletAdapter`
30
+
31
+ The core wallet abstraction. All wallet implementations must conform to this interface.
32
+
33
+ ```typescript
34
+ interface WalletAdapter {
35
+ get chainNamespace(): ChainNamespace;
36
+ get chain(): Chain | undefined;
37
+ get address(): string;
38
+ get isConnected(): boolean;
39
+ get isCustodial(): boolean;
40
+ get connector(): string;
41
+
42
+ signMessage(message: string): Promise<string>;
43
+ signTransaction(serializedTx: Uint8Array): Promise<Uint8Array>;
44
+ sendTransaction(serializedTx: Uint8Array): Promise<string>;
45
+ }
46
+ ```
47
+
48
+ #### `EvmWalletAdapter`
49
+
50
+ Extends `WalletAdapter` with EVM-specific capabilities.
51
+
52
+ ```typescript
53
+ interface EvmWalletAdapter extends WalletAdapter {
54
+ getEip1193Provider(): Promise<Eip1193Provider | undefined>;
55
+ switchChain(chain: Chain): Promise<void>;
56
+ }
57
+ ```
58
+
59
+ #### `Eip1193Provider`
60
+
61
+ Standard [EIP-1193](https://eips.ethereum.org/EIPS/eip-1193) provider interface.
62
+
63
+ ```typescript
64
+ interface Eip1193Provider {
65
+ request(request: {
66
+ method: string;
67
+ params?: Array<unknown> | Record<string, unknown>;
68
+ }): Promise<unknown>;
69
+ }
70
+ ```
71
+
72
+ #### `AuthenticatedUser`
73
+
74
+ Represents the currently authenticated user.
75
+
76
+ ```typescript
77
+ interface AuthenticatedUser {
78
+ id: string;
79
+ wallets: Array<WalletAdapter>;
80
+ accessToken: string;
81
+ }
82
+ ```
83
+
84
+ #### `WalletConnectorContextValue`
85
+
86
+ Shape of the wallet connector context.
87
+
88
+ ```typescript
89
+ interface WalletConnectorContextValue {
90
+ status:
91
+ | "detecting"
92
+ | "connecting"
93
+ | "connected"
94
+ | "disconnecting"
95
+ | "disconnected";
96
+ wallets: Array<WalletAdapter>;
97
+ connect: () => Promise<void>;
98
+ disconnect: () => Promise<void>;
99
+ }
100
+ ```
101
+
102
+ #### `AuthContextValue`
103
+
104
+ Shape of the auth context.
105
+
106
+ ```typescript
107
+ interface AuthContextValue {
108
+ user: AuthenticatedUser | null;
109
+ status: "unauthenticated" | "authenticating" | "authenticated";
110
+ signIn: () => void | Promise<void>;
111
+ signOut: () => void | Promise<void>;
112
+ refreshAccessToken: () => void | Promise<void>;
113
+ }
114
+ ```
115
+
116
+ ### Components
117
+
118
+ #### `WalletConnectorProvider`
119
+
120
+ Provides wallet connection state to the component tree. Typically wrapped by an implementation provider (e.g. `PrivyWalletConnectorProvider`).
121
+
122
+ | Prop | Type | Description |
123
+ | ------------ | --------------------------------------- | -------------------------- |
124
+ | `status` | `WalletConnectorContextValue["status"]` | Current connection status |
125
+ | `wallets` | `Array<WalletAdapter>` | Connected wallet adapters |
126
+ | `connect` | `() => Promise<void>` | Triggers wallet connection |
127
+ | `disconnect` | `() => Promise<void>` | Disconnects all wallets |
128
+ | `children` | `ReactNode` | Child components |
129
+
130
+ #### `AuthProvider`
131
+
132
+ Provides authentication state to the component tree. Typically wrapped by an implementation provider (e.g. `PrivyAuthProvider`).
133
+
134
+ | Prop | Type | Description |
135
+ | -------------------- | ----------------------------- | -------------------------- |
136
+ | `user` | `AuthenticatedUser \| null` | Current authenticated user |
137
+ | `status` | `AuthContextValue["status"]` | Current auth status |
138
+ | `signIn` | `() => void \| Promise<void>` | Triggers sign-in flow |
139
+ | `signOut` | `() => void \| Promise<void>` | Triggers sign-out flow |
140
+ | `refreshAccessToken` | `() => void \| Promise<void>` | Refreshes the access token |
141
+ | `children` | `ReactNode` | Child components |
142
+
143
+ ### Hooks
144
+
145
+ #### `useWalletConnector()`
146
+
147
+ Returns the full `WalletConnectorContextValue`. Throws if used outside `WalletConnectorProvider`.
148
+
149
+ ```typescript
150
+ function useWalletConnector(): WalletConnectorContextValue;
151
+ ```
152
+
153
+ #### `useWallets()`
154
+
155
+ Convenience hook that returns only the `wallets` array from the wallet connector context.
156
+
157
+ ```typescript
158
+ function useWallets(): Array<WalletAdapter>;
159
+ ```
160
+
161
+ #### `useAuth()`
162
+
163
+ Returns the full `AuthContextValue`. Throws if used outside `AuthProvider`.
164
+
165
+ ```typescript
166
+ function useAuth(): AuthContextValue;
167
+ ```
168
+
169
+ #### `useAuthCallback<T>(callback, deps?)`
170
+
171
+ Wraps a callback so it only executes when the user is authenticated. If unauthenticated, it triggers `signIn()` and returns `undefined`. If currently authenticating, it silently returns `undefined` without re-triggering sign-in.
172
+
173
+ ```typescript
174
+ type AuthGuardedCallback<T extends (...args: any[]) => any> = (
175
+ ...args: Parameters<T>
176
+ ) => Promise<Awaited<ReturnType<T>> | undefined>;
177
+
178
+ function useAuthCallback<T extends (...args: any[]) => any>(
179
+ callback: T,
180
+ deps?: DependencyList,
181
+ ): AuthGuardedCallback<T>;
182
+ ```
183
+
184
+ The return type `AuthGuardedCallback<T>` makes it explicit that the wrapped function may resolve to `undefined` when the user is not authenticated.
185
+
186
+ #### `useSwitchChain()`
187
+
188
+ Returns a function that switches all connected EVM wallets to a given chain. Solana chains are automatically skipped.
189
+
190
+ ```typescript
191
+ function useSwitchChain(): (chain: Chain) => Promise<void>;
192
+ ```
193
+
194
+ ### Constants
195
+
196
+ #### `version`
197
+
198
+ The current package version string.
199
+
200
+ ```typescript
201
+ const version: string; // e.g. "0.1.18"
202
+ ```
203
+
204
+ ## Usage Examples
205
+
206
+ ### Basic Setup with an Implementation Provider
207
+
208
+ This package is designed to be used with an implementation provider. Here's an example using `@liberfi.io/wallet-connector-privy`:
209
+
210
+ ```tsx
211
+ import {
212
+ PrivyWalletConnectorProvider,
213
+ PrivyAuthProvider,
214
+ } from "@liberfi.io/wallet-connector-privy";
215
+
216
+ function App() {
217
+ return (
218
+ <PrivyWalletConnectorProvider privyAppId="your-app-id">
219
+ <PrivyAuthProvider>
220
+ <MyApp />
221
+ </PrivyAuthProvider>
222
+ </PrivyWalletConnectorProvider>
223
+ );
224
+ }
225
+ ```
226
+
227
+ ### Consuming Wallet State
228
+
229
+ ```tsx
230
+ import { useWalletConnector, useWallets } from "@liberfi.io/wallet-connector";
231
+
232
+ function WalletStatus() {
233
+ const { status, connect, disconnect } = useWalletConnector();
234
+ const wallets = useWallets();
235
+
236
+ if (status === "disconnected") {
237
+ return <button onClick={connect}>Connect Wallet</button>;
238
+ }
239
+
240
+ return (
241
+ <div>
242
+ <p>Status: {status}</p>
243
+ <ul>
244
+ {wallets.map((w) => (
245
+ <li key={w.address}>
246
+ {w.connector}: {w.address}
247
+ </li>
248
+ ))}
249
+ </ul>
250
+ <button onClick={disconnect}>Disconnect</button>
251
+ </div>
252
+ );
253
+ }
254
+ ```
255
+
256
+ ### Auth-Gated Actions
257
+
258
+ ```tsx
259
+ import { useAuthCallback } from "@liberfi.io/wallet-connector";
260
+
261
+ function TradeButton() {
262
+ const handleTrade = useAuthCallback(async () => {
263
+ // This only runs when the user is authenticated.
264
+ // If not authenticated, signIn() is triggered automatically.
265
+ await executeTrade();
266
+ }, []);
267
+
268
+ return <button onClick={handleTrade}>Trade</button>;
269
+ }
270
+ ```
271
+
272
+ ### Switching Chains
273
+
274
+ ```tsx
275
+ import { Chain } from "@liberfi.io/types";
276
+ import { useSwitchChain } from "@liberfi.io/wallet-connector";
277
+
278
+ function ChainSwitcher() {
279
+ const switchChain = useSwitchChain();
280
+
281
+ return (
282
+ <button onClick={() => switchChain(Chain.ETHEREUM)}>
283
+ Switch to Ethereum
284
+ </button>
285
+ );
286
+ }
287
+ ```
288
+
289
+ ### Building a Custom Implementation
290
+
291
+ To create a new wallet connector implementation, implement `WalletAdapter` (and optionally `EvmWalletAdapter`) and wrap the providers:
292
+
293
+ ```tsx
294
+ import {
295
+ WalletConnectorProvider,
296
+ AuthProvider,
297
+ WalletAdapter,
298
+ } from "@liberfi.io/wallet-connector";
299
+
300
+ class MyWalletAdapter implements WalletAdapter {
301
+ // Implement all WalletAdapter properties and methods...
302
+ }
303
+
304
+ function MyWalletConnectorProvider({ children }: PropsWithChildren) {
305
+ const [wallets, setWallets] = useState<WalletAdapter[]>([]);
306
+ const [status, setStatus] = useState<"disconnected" | "connected">(
307
+ "disconnected",
308
+ );
309
+
310
+ const connect = async () => {
311
+ /* your connect logic */
312
+ };
313
+ const disconnect = async () => {
314
+ /* your disconnect logic */
315
+ };
316
+
317
+ return (
318
+ <WalletConnectorProvider
319
+ status={status}
320
+ wallets={wallets}
321
+ connect={connect}
322
+ disconnect={disconnect}
323
+ >
324
+ {children}
325
+ </WalletConnectorProvider>
326
+ );
327
+ }
328
+ ```
329
+
330
+ ## Future Improvements
331
+
332
+ - **Context default safety** — Switch context defaults from `{} as ContextValue` to `null` with `ContextValue | null` type for type-safe "no provider" detection.
333
+ - **Expand test coverage** — Add tests for status transitions (unauthenticated → authenticated) and error propagation when `switchChain` rejects.
334
+ - **Typed EIP-1193 overloads** — Provide method-specific typed overloads for common EIP-1193 methods (e.g. `eth_sendTransaction`, `personal_sign`).
package/dist/index.d.mts CHANGED
@@ -34,6 +34,13 @@ interface WalletAdapter {
34
34
  sendTransaction(serializedTx: Uint8Array): Promise<string>;
35
35
  }
36
36
 
37
+ /**
38
+ * Represents the currently authenticated user.
39
+ *
40
+ * Implementation providers (e.g. `wallet-connector-privy`) construct this
41
+ * by spreading their own user object, so runtime instances may carry
42
+ * additional provider-specific fields beyond what is declared here.
43
+ */
37
44
  interface AuthenticatedUser {
38
45
  /** user id */
39
46
  id: string;
@@ -41,19 +48,20 @@ interface AuthenticatedUser {
41
48
  wallets: Array<WalletAdapter>;
42
49
  /** user access token */
43
50
  accessToken: string;
44
- /** user profiles */
45
- [key: string]: any;
46
51
  }
47
52
 
48
53
  /**
49
54
  * The interface to an [EIP-1193](https://eips.ethereum.org/EIPS/eip-1193) provider,
50
- * which is a standard used by most injected providers
55
+ * which is a standard used by most injected providers.
56
+ *
57
+ * Uses `unknown` for params and return value — callers must narrow
58
+ * the result to the expected type based on the method being called.
51
59
  */
52
60
  interface Eip1193Provider {
53
61
  request(request: {
54
62
  method: string;
55
- params?: Array<any> | Record<string, any>;
56
- }): Promise<any>;
63
+ params?: Array<unknown> | Record<string, unknown>;
64
+ }): Promise<unknown>;
57
65
  }
58
66
  /**
59
67
  * Evm wallet adapter interface
@@ -83,13 +91,46 @@ interface AuthContextValue {
83
91
  refreshAccessToken: () => void | Promise<void>;
84
92
  }
85
93
 
94
+ /**
95
+ * Returns the auth context value.
96
+ *
97
+ * Must be used within an {@link AuthProvider}.
98
+ * Throws if no provider is found in the component tree.
99
+ *
100
+ * @returns The current {@link AuthContextValue} (user, status, signIn, signOut, refreshAccessToken).
101
+ */
86
102
  declare function useAuth(): AuthContextValue;
87
103
 
88
- declare function useAuthCallback<T extends (...args: any[]) => any>(callback: T, deps?: DependencyList): T;
104
+ /**
105
+ * The return type of {@link useAuthCallback}.
106
+ *
107
+ * Accepts the same parameters as `T` but always returns a `Promise` that
108
+ * resolves to `Awaited<ReturnType<T>>` when authenticated, or `undefined`
109
+ * when the user is not yet authenticated.
110
+ */
111
+ type AuthGuardedCallback<T extends (...args: any[]) => any> = (...args: Parameters<T>) => Promise<Awaited<ReturnType<T>> | undefined>;
112
+ /**
113
+ * Wraps a callback so it only executes when the user is authenticated.
114
+ *
115
+ * - **authenticated**: executes the callback and returns its result.
116
+ * - **unauthenticated**: triggers `signIn()` and returns `undefined`.
117
+ * - **authenticating**: returns `undefined` without re-triggering sign-in
118
+ * (avoids conflicting with an in-progress auth flow).
119
+ *
120
+ * @param callback - The function to guard behind authentication.
121
+ * @param deps - Optional dependency list (same semantics as `useCallback`).
122
+ * @returns A wrapped callback that may resolve to `undefined` when not authenticated.
123
+ */
124
+ declare function useAuthCallback<T extends (...args: any[]) => any>(callback: T, deps?: DependencyList): AuthGuardedCallback<T>;
89
125
 
90
126
  /**
91
127
  * Returns a function that switches all connected EVM wallets to the given chain.
92
- * Solana chains are skipped.
128
+ *
129
+ * Solana chains are automatically skipped (no-op). For EVM chains, the returned
130
+ * function calls `switchChain` on every connected wallet that implements
131
+ * {@link EvmWalletAdapter} in parallel.
132
+ *
133
+ * @returns An async function `(chain: Chain) => Promise<void>`.
93
134
  */
94
135
  declare function useSwitchChain(): (chain: Chain) => Promise<void>;
95
136
 
@@ -108,21 +149,46 @@ interface WalletConnectorContextValue {
108
149
  }
109
150
 
110
151
  /**
111
- * use wallet connector hook
112
- * @returns wallet connector
152
+ * Returns the wallet connector context value.
153
+ *
154
+ * Must be used within a {@link WalletConnectorProvider}.
155
+ * Throws if no provider is found in the component tree.
156
+ *
157
+ * @returns The current {@link WalletConnectorContextValue} (status, wallets, connect, disconnect).
113
158
  */
114
159
  declare function useWalletConnector(): WalletConnectorContextValue;
115
160
 
116
161
  /**
117
- * use wallets hook
118
- * @returns all wallets that can be used to take onchain actions
162
+ * Convenience hook that returns the `wallets` array from the wallet connector context.
163
+ *
164
+ * Shorthand for `useWalletConnector().wallets`.
165
+ *
166
+ * @returns All wallets that can be used to take on-chain actions when connected.
119
167
  */
120
168
  declare function useWallets(): WalletAdapter[];
121
169
 
170
+ /** Props for {@link AuthProvider}. */
122
171
  type AuthProviderProps = PropsWithChildren<AuthContextValue>;
172
+ /**
173
+ * Provides authentication state to the component tree.
174
+ *
175
+ * This is a passthrough provider — it accepts the full context value as props
176
+ * and forwards it to React context. Implementation providers (e.g.
177
+ * `PrivyAuthProvider`) typically wrap this component and supply the concrete
178
+ * `user`, `status`, `signIn`, `signOut`, and `refreshAccessToken` values.
179
+ */
123
180
  declare function AuthProvider({ children, ...value }: AuthProviderProps): react_jsx_runtime.JSX.Element;
124
181
 
182
+ /** Props for {@link WalletConnectorProvider}. */
125
183
  type WalletConnectorProviderProps = PropsWithChildren<WalletConnectorContextValue>;
184
+ /**
185
+ * Provides wallet connection state to the component tree.
186
+ *
187
+ * This is a passthrough provider — it accepts the full context value as props
188
+ * and forwards it to React context. Implementation providers (e.g.
189
+ * `PrivyWalletConnectorProvider`) typically wrap this component and supply
190
+ * the concrete `status`, `wallets`, `connect`, and `disconnect` values.
191
+ */
126
192
  declare function WalletConnectorProvider({ children, ...value }: WalletConnectorProviderProps): react_jsx_runtime.JSX.Element;
127
193
 
128
194
  declare global {
@@ -132,6 +198,6 @@ declare global {
132
198
  };
133
199
  }
134
200
  }
135
- declare const _default: "0.1.18";
201
+ declare const _default: "0.1.20";
136
202
 
137
- export { AuthProvider, type AuthProviderProps, type AuthenticatedUser, type Eip1193Provider, type EvmWalletAdapter, type WalletAdapter, WalletConnectorProvider, type WalletConnectorProviderProps, useAuth, useAuthCallback, useSwitchChain, useWalletConnector, useWallets, _default as version };
203
+ export { type AuthGuardedCallback, AuthProvider, type AuthProviderProps, type AuthenticatedUser, type Eip1193Provider, type EvmWalletAdapter, type WalletAdapter, WalletConnectorProvider, type WalletConnectorProviderProps, useAuth, useAuthCallback, useSwitchChain, useWalletConnector, useWallets, _default as version };
package/dist/index.d.ts CHANGED
@@ -34,6 +34,13 @@ interface WalletAdapter {
34
34
  sendTransaction(serializedTx: Uint8Array): Promise<string>;
35
35
  }
36
36
 
37
+ /**
38
+ * Represents the currently authenticated user.
39
+ *
40
+ * Implementation providers (e.g. `wallet-connector-privy`) construct this
41
+ * by spreading their own user object, so runtime instances may carry
42
+ * additional provider-specific fields beyond what is declared here.
43
+ */
37
44
  interface AuthenticatedUser {
38
45
  /** user id */
39
46
  id: string;
@@ -41,19 +48,20 @@ interface AuthenticatedUser {
41
48
  wallets: Array<WalletAdapter>;
42
49
  /** user access token */
43
50
  accessToken: string;
44
- /** user profiles */
45
- [key: string]: any;
46
51
  }
47
52
 
48
53
  /**
49
54
  * The interface to an [EIP-1193](https://eips.ethereum.org/EIPS/eip-1193) provider,
50
- * which is a standard used by most injected providers
55
+ * which is a standard used by most injected providers.
56
+ *
57
+ * Uses `unknown` for params and return value — callers must narrow
58
+ * the result to the expected type based on the method being called.
51
59
  */
52
60
  interface Eip1193Provider {
53
61
  request(request: {
54
62
  method: string;
55
- params?: Array<any> | Record<string, any>;
56
- }): Promise<any>;
63
+ params?: Array<unknown> | Record<string, unknown>;
64
+ }): Promise<unknown>;
57
65
  }
58
66
  /**
59
67
  * Evm wallet adapter interface
@@ -83,13 +91,46 @@ interface AuthContextValue {
83
91
  refreshAccessToken: () => void | Promise<void>;
84
92
  }
85
93
 
94
+ /**
95
+ * Returns the auth context value.
96
+ *
97
+ * Must be used within an {@link AuthProvider}.
98
+ * Throws if no provider is found in the component tree.
99
+ *
100
+ * @returns The current {@link AuthContextValue} (user, status, signIn, signOut, refreshAccessToken).
101
+ */
86
102
  declare function useAuth(): AuthContextValue;
87
103
 
88
- declare function useAuthCallback<T extends (...args: any[]) => any>(callback: T, deps?: DependencyList): T;
104
+ /**
105
+ * The return type of {@link useAuthCallback}.
106
+ *
107
+ * Accepts the same parameters as `T` but always returns a `Promise` that
108
+ * resolves to `Awaited<ReturnType<T>>` when authenticated, or `undefined`
109
+ * when the user is not yet authenticated.
110
+ */
111
+ type AuthGuardedCallback<T extends (...args: any[]) => any> = (...args: Parameters<T>) => Promise<Awaited<ReturnType<T>> | undefined>;
112
+ /**
113
+ * Wraps a callback so it only executes when the user is authenticated.
114
+ *
115
+ * - **authenticated**: executes the callback and returns its result.
116
+ * - **unauthenticated**: triggers `signIn()` and returns `undefined`.
117
+ * - **authenticating**: returns `undefined` without re-triggering sign-in
118
+ * (avoids conflicting with an in-progress auth flow).
119
+ *
120
+ * @param callback - The function to guard behind authentication.
121
+ * @param deps - Optional dependency list (same semantics as `useCallback`).
122
+ * @returns A wrapped callback that may resolve to `undefined` when not authenticated.
123
+ */
124
+ declare function useAuthCallback<T extends (...args: any[]) => any>(callback: T, deps?: DependencyList): AuthGuardedCallback<T>;
89
125
 
90
126
  /**
91
127
  * Returns a function that switches all connected EVM wallets to the given chain.
92
- * Solana chains are skipped.
128
+ *
129
+ * Solana chains are automatically skipped (no-op). For EVM chains, the returned
130
+ * function calls `switchChain` on every connected wallet that implements
131
+ * {@link EvmWalletAdapter} in parallel.
132
+ *
133
+ * @returns An async function `(chain: Chain) => Promise<void>`.
93
134
  */
94
135
  declare function useSwitchChain(): (chain: Chain) => Promise<void>;
95
136
 
@@ -108,21 +149,46 @@ interface WalletConnectorContextValue {
108
149
  }
109
150
 
110
151
  /**
111
- * use wallet connector hook
112
- * @returns wallet connector
152
+ * Returns the wallet connector context value.
153
+ *
154
+ * Must be used within a {@link WalletConnectorProvider}.
155
+ * Throws if no provider is found in the component tree.
156
+ *
157
+ * @returns The current {@link WalletConnectorContextValue} (status, wallets, connect, disconnect).
113
158
  */
114
159
  declare function useWalletConnector(): WalletConnectorContextValue;
115
160
 
116
161
  /**
117
- * use wallets hook
118
- * @returns all wallets that can be used to take onchain actions
162
+ * Convenience hook that returns the `wallets` array from the wallet connector context.
163
+ *
164
+ * Shorthand for `useWalletConnector().wallets`.
165
+ *
166
+ * @returns All wallets that can be used to take on-chain actions when connected.
119
167
  */
120
168
  declare function useWallets(): WalletAdapter[];
121
169
 
170
+ /** Props for {@link AuthProvider}. */
122
171
  type AuthProviderProps = PropsWithChildren<AuthContextValue>;
172
+ /**
173
+ * Provides authentication state to the component tree.
174
+ *
175
+ * This is a passthrough provider — it accepts the full context value as props
176
+ * and forwards it to React context. Implementation providers (e.g.
177
+ * `PrivyAuthProvider`) typically wrap this component and supply the concrete
178
+ * `user`, `status`, `signIn`, `signOut`, and `refreshAccessToken` values.
179
+ */
123
180
  declare function AuthProvider({ children, ...value }: AuthProviderProps): react_jsx_runtime.JSX.Element;
124
181
 
182
+ /** Props for {@link WalletConnectorProvider}. */
125
183
  type WalletConnectorProviderProps = PropsWithChildren<WalletConnectorContextValue>;
184
+ /**
185
+ * Provides wallet connection state to the component tree.
186
+ *
187
+ * This is a passthrough provider — it accepts the full context value as props
188
+ * and forwards it to React context. Implementation providers (e.g.
189
+ * `PrivyWalletConnectorProvider`) typically wrap this component and supply
190
+ * the concrete `status`, `wallets`, `connect`, and `disconnect` values.
191
+ */
126
192
  declare function WalletConnectorProvider({ children, ...value }: WalletConnectorProviderProps): react_jsx_runtime.JSX.Element;
127
193
 
128
194
  declare global {
@@ -132,6 +198,6 @@ declare global {
132
198
  };
133
199
  }
134
200
  }
135
- declare const _default: "0.1.18";
201
+ declare const _default: "0.1.20";
136
202
 
137
- export { AuthProvider, type AuthProviderProps, type AuthenticatedUser, type Eip1193Provider, type EvmWalletAdapter, type WalletAdapter, WalletConnectorProvider, type WalletConnectorProviderProps, useAuth, useAuthCallback, useSwitchChain, useWalletConnector, useWallets, _default as version };
203
+ export { type AuthGuardedCallback, AuthProvider, type AuthProviderProps, type AuthenticatedUser, type Eip1193Provider, type EvmWalletAdapter, type WalletAdapter, WalletConnectorProvider, type WalletConnectorProviderProps, useAuth, useAuthCallback, useSwitchChain, useWalletConnector, useWallets, _default as version };
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- 'use strict';var react=require('react'),types=require('@liberfi.io/types'),utils=require('@liberfi.io/utils'),jsxRuntime=require('react/jsx-runtime');var n=react.createContext({});function s(){let t=react.useContext(n);if(!t)throw new Error("useAuth must be used within a AuthProvider");return t}function L(t,e){let{status:r,signIn:o}=s(),a=react.useRef(r);return a.current=r,react.useCallback(async(...c)=>{if(a.current!=="authenticated"){a.current==="unauthenticated"&&o();return}return t(...c)},[...e||[],o,t])}var i=react.createContext({});function u(){let t=react.useContext(i);if(!t)throw new Error("useWalletConnector must be used within a WalletConnectorProvider");return t}function l(){let{wallets:t}=u();return t}function K(){let t=l();return react.useCallback(async e=>{if(utils.chainToNamespace(e)===types.ChainNamespace.SOLANA)return;let r=t.filter(o=>"switchChain"in o&&o.isConnected);await Promise.all(r.map(o=>o.switchChain(e)));},[t])}function ot({children:t,...e}){return jsxRuntime.jsx(n.Provider,{value:e,children:t})}function st({children:t,...e}){return jsxRuntime.jsx(i.Provider,{value:e,children:t})}typeof window<"u"&&(window.__LIBERFI_VERSION__=window.__LIBERFI_VERSION__||{},window.__LIBERFI_VERSION__["@liberfi.io/wallet-connector"]="0.1.18");var w="0.1.18";exports.AuthProvider=ot;exports.WalletConnectorProvider=st;exports.useAuth=s;exports.useAuthCallback=L;exports.useSwitchChain=K;exports.useWalletConnector=u;exports.useWallets=l;exports.version=w;//# sourceMappingURL=index.js.map
1
+ 'use strict';var react=require('react'),types=require('@liberfi.io/types'),utils=require('@liberfi.io/utils'),jsxRuntime=require('react/jsx-runtime');var n=react.createContext({});function u(){let t=react.useContext(n);if(!t)throw new Error("useAuth must be used within a AuthProvider");return t}function b(t,e){let{status:r,signIn:o}=u(),a=react.useRef(r);return a.current=r,react.useCallback(async(...c)=>{if(a.current!=="authenticated"){a.current==="unauthenticated"&&o();return}return t(...c)},[...e||[],o,t])}var i=react.createContext({});function s(){let t=react.useContext(i);if(!t)throw new Error("useWalletConnector must be used within a WalletConnectorProvider");return t}function l(){let{wallets:t}=s();return t}function J(){let t=l();return react.useCallback(async e=>{if(utils.chainToNamespace(e)===types.ChainNamespace.SOLANA)return;let r=t.filter(o=>"switchChain"in o&&o.isConnected);await Promise.all(r.map(o=>o.switchChain(e)));},[t])}function et({children:t,...e}){return jsxRuntime.jsx(n.Provider,{value:e,children:t})}function at({children:t,...e}){return jsxRuntime.jsx(i.Provider,{value:e,children:t})}typeof window<"u"&&(window.__LIBERFI_VERSION__=window.__LIBERFI_VERSION__||{},window.__LIBERFI_VERSION__["@liberfi.io/wallet-connector"]="0.1.20");var w="0.1.20";exports.AuthProvider=et;exports.WalletConnectorProvider=at;exports.useAuth=u;exports.useAuthCallback=b;exports.useSwitchChain=J;exports.useWalletConnector=s;exports.useWallets=l;exports.version=w;//# sourceMappingURL=index.js.map
2
2
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/providers/AuthContext.ts","../src/hooks/useAuth.ts","../src/hooks/useAuthCallback.ts","../src/providers/WalletConnectorContext.ts","../src/hooks/useWalletConnector.ts","../src/hooks/useWallets.ts","../src/hooks/useSwitchChain.ts","../src/providers/AuthProvider.tsx","../src/providers/WalletConnectorProvider.tsx","../src/version.ts"],"names":["AuthContext","createContext","useAuth","context","useContext","useAuthCallback","callback","deps","status","signIn","statusRef","useRef","useCallback","args","WalletConnectorContext","useWalletConnector","useWallets","wallets","useSwitchChain","chain","chainToNamespace","ChainNamespace","evmWallets","w","AuthProvider","children","value","jsx","WalletConnectorProvider","version_default"],"mappings":"sJAgBO,IAAMA,CAAAA,CAAcC,mBAAAA,CACzB,EACF,CAAA,CCfO,SAASC,CAAAA,EAAU,CACxB,IAAMC,CAAAA,CAAUC,gBAAAA,CAAWJ,CAAW,CAAA,CACtC,GAAI,CAACG,CAAAA,CACH,MAAM,IAAI,KAAA,CAAM,4CAA4C,CA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Q,GAAA,CAAID,CAAAA,CAAW,GAAA,CAAKC,CAAAA,EAAMA,CAAAA,CAAE,WAAA,CAAYJ,CAAK,CAAC,CAAC,EAC/D,EACA,CAACF,CAAO,CACV,CACF,CCnBO,SAASO,EAAAA,CAAa,CAAE,QAAA,CAAAC,CAAAA,CAAU,GAAGC,CAAM,CAAA,CAAsB,CACtE,OAAOC,cAAAA,CAAC3B,CAAAA,CAAY,QAAA,CAAZ,CAAqB,KAAA,CAAO0B,CAAAA,CAAQ,QAAA,CAAAD,CAAAA,CAAS,CACvD,CCEO,SAASG,GAAwB,CACtC,QAAA,CAAAH,CAAAA,CACA,GAAGC,CACL,CAAA,CAAiC,CAC/B,OACEC,cAAAA,CAACb,CAAAA,CAAuB,QAAA,CAAvB,CAAgC,KAAA,CAAOY,CAAAA,CACrC,QAAA,CAAAD,CAAAA,CACH,CAEJ,CCXI,OAAO,MAAA,CAAW,GAAA,GACpB,MAAA,CAAO,mBAAA,CAAsB,MAAA,CAAO,mBAAA,EAAuB,EAAC,CAC5D,MAAA,CAAO,mBAAA,CAAoB,8BAA8B,CAAA,CAAI,QAAA,CAAA,KAGxDI,CAAAA,CAAQ","file":"index.js","sourcesContent":["import { createContext } from \"react\";\nimport { AuthenticatedUser } from \"../types\";\n\nexport interface AuthContextValue {\n /** authenticated user profile */\n user: AuthenticatedUser | null;\n /** authentication status */\n status: \"unauthenticated\" | \"authenticating\" | \"authenticated\";\n /** sign in to the IdP */\n signIn: () => void | Promise<void>;\n /** sign out from the IdP */\n signOut: () => void | Promise<void>;\n /** refresh the access token */\n refreshAccessToken: () => void | Promise<void>;\n}\n\nexport const AuthContext = createContext<AuthContextValue>(\n {} as AuthContextValue,\n);\n","import { useContext } from \"react\";\nimport { AuthContext } from \"../providers/AuthContext\";\n\nexport function useAuth() {\n const context = useContext(AuthContext);\n if (!context) {\n throw new Error(\"useAuth must be used within a AuthProvider\");\n }\n return context;\n}\n","/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { DependencyList, useCallback, useRef } from \"react\";\nimport { useAuth } from \"./useAuth\";\n\nexport function useAuthCallback<T extends (...args: any[]) => any>(\n callback: T,\n deps?: DependencyList,\n): T {\n const { status, signIn } = useAuth();\n\n // statusRef will be updated to the latest on each render\n // avoid callback rebuild on status changes\n const statusRef = useRef<string>(status);\n statusRef.current = status;\n\n const cb = useCallback(\n async (...args: Parameters<T>): Promise<ReturnType<T> | undefined> => {\n if (statusRef.current !== \"authenticated\") {\n // Only trigger sign-in when truly unauthenticated.\n // When \"authenticating\" (wallet detecting/connecting), just wait —\n // calling signIn() again may conflict with the in-progress auth flow.\n if (statusRef.current === \"unauthenticated\") {\n signIn();\n }\n return;\n }\n return callback(...args);\n },\n [...(deps || []), signIn, callback],\n );\n\n return cb as T;\n}\n","import { createContext } from \"react\";\nimport { WalletAdapter } from \"../types\";\n\nexport interface WalletConnectorContextValue {\n /**\n * detecting: is detecting connected wallets\n * connecting: is connecting to the first wallet\n * connected: is connected to at least one wallet\n * disconnecting: is disconnecting from the last connected wallet\n * disconnected: is disconnected from all wallets\n */\n status:\n | \"detecting\"\n | \"connecting\"\n | \"connected\"\n | \"disconnecting\"\n | \"disconnected\";\n // all wallets that can be used to take onchain actions when connected\n wallets: Array<WalletAdapter>;\n // if no wallets are connected, connect one or multiple wallets\n connect: () => Promise<void>;\n // disconnect all wallets\n disconnect: () => Promise<void>;\n}\n\nexport const WalletConnectorContext =\n createContext<WalletConnectorContextValue>({} as WalletConnectorContextValue);\n","import { useContext } from \"react\";\nimport { WalletConnectorContext } from \"../providers/WalletConnectorContext\";\n\n/**\n * use wallet connector hook\n * @returns wallet connector\n */\nexport function useWalletConnector() {\n const context = useContext(WalletConnectorContext);\n if (!context) {\n throw new Error(\n \"useWalletConnector must be used within a WalletConnectorProvider\",\n );\n }\n return context;\n}\n","import { useWalletConnector } from \"./useWalletConnector\";\n\n/**\n * use wallets hook\n * @returns all wallets that can be used to take onchain actions\n */\nexport function useWallets() {\n const { wallets } = useWalletConnector();\n return wallets;\n}\n","import { useCallback } from \"react\";\nimport { Chain, ChainNamespace } from \"@liberfi.io/types\";\nimport { chainToNamespace } from \"@liberfi.io/utils\";\nimport { EvmWalletAdapter } from \"../types\";\nimport { useWallets } from \"./useWallets\";\n\n/**\n * Returns a function that switches all connected EVM wallets to the given chain.\n * Solana chains are skipped.\n */\nexport function useSwitchChain() {\n const wallets = useWallets();\n\n return useCallback(\n async (chain: Chain) => {\n if (chainToNamespace(chain) === ChainNamespace.SOLANA) return;\n\n const evmWallets = wallets.filter(\n (w): w is EvmWalletAdapter => \"switchChain\" in w && w.isConnected,\n );\n await Promise.all(evmWallets.map((w) => w.switchChain(chain)));\n },\n [wallets],\n );\n}\n","import { PropsWithChildren } from \"react\";\nimport { AuthContext, AuthContextValue } from \"./AuthContext\";\n\nexport type AuthProviderProps = PropsWithChildren<AuthContextValue>;\n\nexport function AuthProvider({ children, ...value }: AuthProviderProps) {\n return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;\n}\n","import { PropsWithChildren } from \"react\";\nimport {\n WalletConnectorContext,\n WalletConnectorContextValue,\n} from \"./WalletConnectorContext\";\n\nexport type WalletConnectorProviderProps =\n PropsWithChildren<WalletConnectorContextValue>;\n\nexport function WalletConnectorProvider({\n children,\n ...value\n}: WalletConnectorProviderProps) {\n return (\n <WalletConnectorContext.Provider value={value}>\n {children}\n </WalletConnectorContext.Provider>\n );\n}\n","declare global {\n interface Window {\n __LIBERFI_VERSION__?: {\n [key: string]: string;\n };\n }\n}\nif (typeof window !== \"undefined\") {\n window.__LIBERFI_VERSION__ = window.__LIBERFI_VERSION__ || {};\n window.__LIBERFI_VERSION__[\"@liberfi.io/wallet-connector\"] = \"0.1.18\";\n}\n\nexport default \"0.1.18\";\n"]}
1
+ {"version":3,"sources":["../src/providers/AuthContext.ts","../src/hooks/useAuth.ts","../src/hooks/useAuthCallback.ts","../src/providers/WalletConnectorContext.ts","../src/hooks/useWalletConnector.ts","../src/hooks/useWallets.ts","../src/hooks/useSwitchChain.ts","../src/providers/AuthProvider.tsx","../src/providers/WalletConnectorProvider.tsx","../src/version.ts"],"names":["AuthContext","createContext","useAuth","context","useContext","useAuthCallback","callback","deps","status","signIn","statusRef","useRef","useCallback","args","WalletConnectorContext","useWalletConnector","useWallets","wallets","useSwitchChain","chain","chainToNamespace","ChainNamespace","evmWallets","w","AuthProvider","children","value","jsx","WalletConnectorProvider","version_default"],"mappings":"sJAgBO,IAAMA,CAAAA,CAAcC,mBAAAA,CACzB,EACF,CAAA,CCPO,SAASC,CAAAA,EAAU,CACxB,IAAMC,CAAAA,CAAUC,gBAAAA,CAAWJ,CAAW,CAAA,CACtC,GAAI,CAACG,CAAAA,CACH,MAAM,IAAI,KAAA,CAAM,4CAA4C,CAAA,CAE9D,OAAOA,CACT,CCUO,SAASE,CAAAA,CACdC,CAAAA,CACAC,CAAAA,CACwB,CACxB,GAAM,CAAE,MAAA,CAAAC,CAAAA,CAAQ,MAAA,CAAAC,CAAO,CAAA,CAAIP,CAAAA,EAAQ,CAE7BQ,CAAAA,CAAYC,YAAAA,CAAeH,CAAM,CAAA,CACvC,OAAAE,CAAAA,CAAU,OAAA,CAAUF,CAAAA,CAEbI,kBACL,MAAA,GACKC,CAAAA,GAC6C,CAChD,GAAIH,CAAAA,CAAU,OAAA,GAAY,eAAA,CAAiB,CACrCA,CAAAA,CAAU,OAAA,GAAY,iBAAA,EACxBD,CAAAA,EAAO,CAET,MACF,CACA,OAAOH,CAAAA,CAAS,GAAGO,CAAI,CACzB,CAAA,CACA,CAAC,GAAIN,CAAAA,EAAQ,EAAC,CAAIE,CAAAA,CAAQH,CAAQ,CACpC,CACF,CCzBO,IAAMQ,CAAAA,CACXb,mBAAAA,CAA2C,EAAiC,CAAA,CCfvE,SAASc,CAAAA,EAAqB,CACnC,IAAMZ,CAAAA,CAAUC,gBAAAA,CAAWU,CAAsB,CAAA,CACjD,GAAI,CAACX,CAAAA,CACH,MAAM,IAAI,KAAA,CACR,kEACF,CAAA,CA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uthenticatedUser } from \"../types\";\n\nexport interface AuthContextValue {\n /** authenticated user profile */\n user: AuthenticatedUser | null;\n /** authentication status */\n status: \"unauthenticated\" | \"authenticating\" | \"authenticated\";\n /** sign in to the IdP */\n signIn: () => void | Promise<void>;\n /** sign out from the IdP */\n signOut: () => void | Promise<void>;\n /** refresh the access token */\n refreshAccessToken: () => void | Promise<void>;\n}\n\nexport const AuthContext = createContext<AuthContextValue>(\n {} as AuthContextValue,\n);\n","import { useContext } from \"react\";\nimport { AuthContext } from \"../providers/AuthContext\";\n\n/**\n * Returns the auth context value.\n *\n * Must be used within an {@link AuthProvider}.\n * Throws if no provider is found in the component tree.\n *\n * @returns The current {@link AuthContextValue} (user, status, signIn, signOut, refreshAccessToken).\n */\nexport function useAuth() {\n const context = useContext(AuthContext);\n if (!context) {\n throw new Error(\"useAuth must be used within a AuthProvider\");\n }\n return context;\n}\n","/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { DependencyList, useCallback, useRef } from \"react\";\nimport { useAuth } from \"./useAuth\";\n\n/**\n * The return type of {@link useAuthCallback}.\n *\n * Accepts the same parameters as `T` but always returns a `Promise` that\n * resolves to `Awaited<ReturnType<T>>` when authenticated, or `undefined`\n * when the user is not yet authenticated.\n */\nexport type AuthGuardedCallback<T extends (...args: any[]) => any> = (\n ...args: Parameters<T>\n) => Promise<Awaited<ReturnType<T>> | undefined>;\n\n/**\n * Wraps a callback so it only executes when the user is authenticated.\n *\n * - **authenticated**: executes the callback and returns its result.\n * - **unauthenticated**: triggers `signIn()` and returns `undefined`.\n * - **authenticating**: returns `undefined` without re-triggering sign-in\n * (avoids conflicting with an in-progress auth flow).\n *\n * @param callback - The function to guard behind authentication.\n * @param deps - Optional dependency list (same semantics as `useCallback`).\n * @returns A wrapped callback that may resolve to `undefined` when not authenticated.\n */\nexport function useAuthCallback<T extends (...args: any[]) => any>(\n callback: T,\n deps?: DependencyList,\n): AuthGuardedCallback<T> {\n const { status, signIn } = useAuth();\n\n const statusRef = useRef<string>(status);\n statusRef.current = status;\n\n return useCallback(\n async (\n ...args: Parameters<T>\n ): Promise<Awaited<ReturnType<T>> | undefined> => {\n if (statusRef.current !== \"authenticated\") {\n if (statusRef.current === \"unauthenticated\") {\n signIn();\n }\n return;\n }\n return callback(...args);\n },\n [...(deps || []), signIn, callback],\n );\n}\n","import { createContext } from \"react\";\nimport { WalletAdapter } from \"../types\";\n\nexport interface WalletConnectorContextValue {\n /**\n * detecting: is detecting connected wallets\n * connecting: is connecting to the first wallet\n * connected: is connected to at least one wallet\n * disconnecting: is disconnecting from the last connected wallet\n * disconnected: is disconnected from all wallets\n */\n status:\n | \"detecting\"\n | \"connecting\"\n | \"connected\"\n | \"disconnecting\"\n | \"disconnected\";\n // all wallets that can be used to take onchain actions when connected\n wallets: Array<WalletAdapter>;\n // if no wallets are connected, connect one or multiple wallets\n connect: () => Promise<void>;\n // disconnect all wallets\n disconnect: () => Promise<void>;\n}\n\nexport const WalletConnectorContext =\n createContext<WalletConnectorContextValue>({} as WalletConnectorContextValue);\n","import { useContext } from \"react\";\nimport { WalletConnectorContext } from \"../providers/WalletConnectorContext\";\n\n/**\n * Returns the wallet connector context value.\n *\n * Must be used within a {@link WalletConnectorProvider}.\n * Throws if no provider is found in the component tree.\n *\n * @returns The current {@link WalletConnectorContextValue} (status, wallets, connect, disconnect).\n */\nexport function useWalletConnector() {\n const context = useContext(WalletConnectorContext);\n if (!context) {\n throw new Error(\n \"useWalletConnector must be used within a WalletConnectorProvider\",\n );\n }\n return context;\n}\n","import { useWalletConnector } from \"./useWalletConnector\";\n\n/**\n * Convenience hook that returns the `wallets` array from the wallet connector context.\n *\n * Shorthand for `useWalletConnector().wallets`.\n *\n * @returns All wallets that can be used to take on-chain actions when connected.\n */\nexport function useWallets() {\n const { wallets } = useWalletConnector();\n return wallets;\n}\n","import { useCallback } from \"react\";\nimport { Chain, ChainNamespace } from \"@liberfi.io/types\";\nimport { chainToNamespace } from \"@liberfi.io/utils\";\nimport { EvmWalletAdapter } from \"../types\";\nimport { useWallets } from \"./useWallets\";\n\n/**\n * Returns a function that switches all connected EVM wallets to the given chain.\n *\n * Solana chains are automatically skipped (no-op). For EVM chains, the returned\n * function calls `switchChain` on every connected wallet that implements\n * {@link EvmWalletAdapter} in parallel.\n *\n * @returns An async function `(chain: Chain) => Promise<void>`.\n */\nexport function useSwitchChain() {\n const wallets = useWallets();\n\n return useCallback(\n async (chain: Chain) => {\n if (chainToNamespace(chain) === ChainNamespace.SOLANA) return;\n\n const evmWallets = wallets.filter(\n (w): w is EvmWalletAdapter => \"switchChain\" in w && w.isConnected,\n );\n await Promise.all(evmWallets.map((w) => w.switchChain(chain)));\n },\n [wallets],\n );\n}\n","import { PropsWithChildren } from \"react\";\nimport { AuthContext, AuthContextValue } from \"./AuthContext\";\n\n/** Props for {@link AuthProvider}. */\nexport type AuthProviderProps = PropsWithChildren<AuthContextValue>;\n\n/**\n * Provides authentication state to the component tree.\n *\n * This is a passthrough provider — it accepts the full context value as props\n * and forwards it to React context. Implementation providers (e.g.\n * `PrivyAuthProvider`) typically wrap this component and supply the concrete\n * `user`, `status`, `signIn`, `signOut`, and `refreshAccessToken` values.\n */\nexport function AuthProvider({ children, ...value }: AuthProviderProps) {\n return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;\n}\n","import { PropsWithChildren } from \"react\";\nimport {\n WalletConnectorContext,\n WalletConnectorContextValue,\n} from \"./WalletConnectorContext\";\n\n/** Props for {@link WalletConnectorProvider}. */\nexport type WalletConnectorProviderProps =\n PropsWithChildren<WalletConnectorContextValue>;\n\n/**\n * Provides wallet connection state to the component tree.\n *\n * This is a passthrough provider — it accepts the full context value as props\n * and forwards it to React context. Implementation providers (e.g.\n * `PrivyWalletConnectorProvider`) typically wrap this component and supply\n * the concrete `status`, `wallets`, `connect`, and `disconnect` values.\n */\nexport function WalletConnectorProvider({\n children,\n ...value\n}: WalletConnectorProviderProps) {\n return (\n <WalletConnectorContext.Provider value={value}>\n {children}\n </WalletConnectorContext.Provider>\n );\n}\n","declare global {\n interface Window {\n __LIBERFI_VERSION__?: {\n [key: string]: string;\n };\n }\n}\nif (typeof window !== \"undefined\") {\n window.__LIBERFI_VERSION__ = window.__LIBERFI_VERSION__ || {};\n window.__LIBERFI_VERSION__[\"@liberfi.io/wallet-connector\"] = \"0.1.20\";\n}\n\nexport default \"0.1.20\";\n"]}
package/dist/index.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import {createContext,useContext,useRef,useCallback}from'react';import {ChainNamespace}from'@liberfi.io/types';import {chainToNamespace}from'@liberfi.io/utils';import {jsx}from'react/jsx-runtime';var n=createContext({});function s(){let t=useContext(n);if(!t)throw new Error("useAuth must be used within a AuthProvider");return t}function L(t,e){let{status:r,signIn:o}=s(),a=useRef(r);return a.current=r,useCallback(async(...c)=>{if(a.current!=="authenticated"){a.current==="unauthenticated"&&o();return}return t(...c)},[...e||[],o,t])}var i=createContext({});function u(){let t=useContext(i);if(!t)throw new Error("useWalletConnector must be used within a WalletConnectorProvider");return t}function l(){let{wallets:t}=u();return t}function K(){let t=l();return useCallback(async e=>{if(chainToNamespace(e)===ChainNamespace.SOLANA)return;let r=t.filter(o=>"switchChain"in o&&o.isConnected);await Promise.all(r.map(o=>o.switchChain(e)));},[t])}function ot({children:t,...e}){return jsx(n.Provider,{value:e,children:t})}function st({children:t,...e}){return jsx(i.Provider,{value:e,children:t})}typeof window<"u"&&(window.__LIBERFI_VERSION__=window.__LIBERFI_VERSION__||{},window.__LIBERFI_VERSION__["@liberfi.io/wallet-connector"]="0.1.18");var w="0.1.18";export{ot as AuthProvider,st as WalletConnectorProvider,s as useAuth,L as useAuthCallback,K as useSwitchChain,u as useWalletConnector,l as useWallets,w as version};//# sourceMappingURL=index.mjs.map
1
+ import {createContext,useContext,useRef,useCallback}from'react';import {ChainNamespace}from'@liberfi.io/types';import {chainToNamespace}from'@liberfi.io/utils';import {jsx}from'react/jsx-runtime';var n=createContext({});function u(){let t=useContext(n);if(!t)throw new Error("useAuth must be used within a AuthProvider");return t}function b(t,e){let{status:r,signIn:o}=u(),a=useRef(r);return a.current=r,useCallback(async(...c)=>{if(a.current!=="authenticated"){a.current==="unauthenticated"&&o();return}return t(...c)},[...e||[],o,t])}var i=createContext({});function s(){let t=useContext(i);if(!t)throw new Error("useWalletConnector must be used within a WalletConnectorProvider");return t}function l(){let{wallets:t}=s();return t}function J(){let t=l();return useCallback(async e=>{if(chainToNamespace(e)===ChainNamespace.SOLANA)return;let r=t.filter(o=>"switchChain"in o&&o.isConnected);await Promise.all(r.map(o=>o.switchChain(e)));},[t])}function et({children:t,...e}){return jsx(n.Provider,{value:e,children:t})}function at({children:t,...e}){return jsx(i.Provider,{value:e,children:t})}typeof window<"u"&&(window.__LIBERFI_VERSION__=window.__LIBERFI_VERSION__||{},window.__LIBERFI_VERSION__["@liberfi.io/wallet-connector"]="0.1.20");var w="0.1.20";export{et as AuthProvider,at as WalletConnectorProvider,u as useAuth,b as useAuthCallback,J as useSwitchChain,s as useWalletConnector,l as useWallets,w as version};//# sourceMappingURL=index.mjs.map
2
2
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/providers/AuthContext.ts","../src/hooks/useAuth.ts","../src/hooks/useAuthCallback.ts","../src/providers/WalletConnectorContext.ts","../src/hooks/useWalletConnector.ts","../src/hooks/useWallets.ts","../src/hooks/useSwitchChain.ts","../src/providers/AuthProvider.tsx","../src/providers/WalletConnectorProvider.tsx","../src/version.ts"],"names":["AuthContext","createContext","useAuth","context","useContext","useAuthCallback","callback","deps","status","signIn","statusRef","useRef","useCallback","args","WalletConnectorContext","useWalletConnector","useWallets","wallets","useSwitchChain","chain","chainToNamespace","ChainNamespace","evmWallets","w","AuthProvider","children","value","jsx","WalletConnectorProvider","version_default"],"mappings":"oMAgBO,IAAMA,CAAAA,CAAcC,aAAAA,CACzB,EACF,CAAA,CCfO,SAASC,CAAAA,EAAU,CACxB,IAAMC,CAAAA,CAAUC,UAAAA,CAAWJ,CAAW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AiBA,CAAAA,EAAKA,CAAAA,CAAE,WACxD,CAAA,CACA,MAAM,OAAA,CAAQ,GAAA,CAAID,CAAAA,CAAW,GAAA,CAAKC,CAAAA,EAAMA,CAAAA,CAAE,WAAA,CAAYJ,CAAK,CAAC,CAAC,EAC/D,EACA,CAACF,CAAO,CACV,CACF,CCnBO,SAASO,EAAAA,CAAa,CAAE,QAAA,CAAAC,CAAAA,CAAU,GAAGC,CAAM,CAAA,CAAsB,CACtE,OAAOC,GAAAA,CAAC3B,CAAAA,CAAY,QAAA,CAAZ,CAAqB,KAAA,CAAO0B,CAAAA,CAAQ,QAAA,CAAAD,CAAAA,CAAS,CACvD,CCEO,SAASG,GAAwB,CACtC,QAAA,CAAAH,CAAAA,CACA,GAAGC,CACL,CAAA,CAAiC,CAC/B,OACEC,GAAAA,CAACb,CAAAA,CAAuB,QAAA,CAAvB,CAAgC,KAAA,CAAOY,CAAAA,CACrC,QAAA,CAAAD,CAAAA,CACH,CAEJ,CCXI,OAAO,MAAA,CAAW,GAAA,GACpB,MAAA,CAAO,mBAAA,CAAsB,MAAA,CAAO,mBAAA,EAAuB,EAAC,CAC5D,MAAA,CAAO,mBAAA,CAAoB,8BAA8B,CAAA,CAAI,QAAA,CAAA,KAGxDI,CAAAA,CAAQ","file":"index.mjs","sourcesContent":["import { createContext } from \"react\";\nimport { AuthenticatedUser } from \"../types\";\n\nexport interface AuthContextValue {\n /** authenticated user profile */\n user: AuthenticatedUser | null;\n /** authentication status */\n status: \"unauthenticated\" | \"authenticating\" | \"authenticated\";\n /** sign in to the IdP */\n signIn: () => void | Promise<void>;\n /** sign out from the IdP */\n signOut: () => void | Promise<void>;\n /** refresh the access token */\n refreshAccessToken: () => void | Promise<void>;\n}\n\nexport const AuthContext = createContext<AuthContextValue>(\n {} as AuthContextValue,\n);\n","import { useContext } from \"react\";\nimport { AuthContext } from \"../providers/AuthContext\";\n\nexport function useAuth() {\n const context = useContext(AuthContext);\n if (!context) {\n throw new Error(\"useAuth must be used within a AuthProvider\");\n }\n return context;\n}\n","/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { DependencyList, useCallback, useRef } from \"react\";\nimport { useAuth } from \"./useAuth\";\n\nexport function useAuthCallback<T extends (...args: any[]) => any>(\n callback: T,\n deps?: DependencyList,\n): T {\n const { status, signIn } = useAuth();\n\n // statusRef will be updated to the latest on each render\n // avoid callback rebuild on status changes\n const statusRef = useRef<string>(status);\n statusRef.current = status;\n\n const cb = useCallback(\n async (...args: Parameters<T>): Promise<ReturnType<T> | undefined> => {\n if (statusRef.current !== \"authenticated\") {\n // Only trigger sign-in when truly unauthenticated.\n // When \"authenticating\" (wallet detecting/connecting), just wait —\n // calling signIn() again may conflict with the in-progress auth flow.\n if (statusRef.current === \"unauthenticated\") {\n signIn();\n }\n return;\n }\n return callback(...args);\n },\n [...(deps || []), signIn, callback],\n );\n\n return cb as T;\n}\n","import { createContext } from \"react\";\nimport { WalletAdapter } from \"../types\";\n\nexport interface WalletConnectorContextValue {\n /**\n * detecting: is detecting connected wallets\n * connecting: is connecting to the first wallet\n * connected: is connected to at least one wallet\n * disconnecting: is disconnecting from the last connected wallet\n * disconnected: is disconnected from all wallets\n */\n status:\n | \"detecting\"\n | \"connecting\"\n | \"connected\"\n | \"disconnecting\"\n | \"disconnected\";\n // all wallets that can be used to take onchain actions when connected\n wallets: Array<WalletAdapter>;\n // if no wallets are connected, connect one or multiple wallets\n connect: () => Promise<void>;\n // disconnect all wallets\n disconnect: () => Promise<void>;\n}\n\nexport const WalletConnectorContext =\n createContext<WalletConnectorContextValue>({} as WalletConnectorContextValue);\n","import { useContext } from \"react\";\nimport { WalletConnectorContext } from \"../providers/WalletConnectorContext\";\n\n/**\n * use wallet connector hook\n * @returns wallet connector\n */\nexport function useWalletConnector() {\n const context = useContext(WalletConnectorContext);\n if (!context) {\n throw new Error(\n \"useWalletConnector must be used within a WalletConnectorProvider\",\n );\n }\n return context;\n}\n","import { useWalletConnector } from \"./useWalletConnector\";\n\n/**\n * use wallets hook\n * @returns all wallets that can be used to take onchain actions\n */\nexport function useWallets() {\n const { wallets } = useWalletConnector();\n return wallets;\n}\n","import { useCallback } from \"react\";\nimport { Chain, ChainNamespace } from \"@liberfi.io/types\";\nimport { chainToNamespace } from \"@liberfi.io/utils\";\nimport { EvmWalletAdapter } from \"../types\";\nimport { useWallets } from \"./useWallets\";\n\n/**\n * Returns a function that switches all connected EVM wallets to the given chain.\n * Solana chains are skipped.\n */\nexport function useSwitchChain() {\n const wallets = useWallets();\n\n return useCallback(\n async (chain: Chain) => {\n if (chainToNamespace(chain) === ChainNamespace.SOLANA) return;\n\n const evmWallets = wallets.filter(\n (w): w is EvmWalletAdapter => \"switchChain\" in w && w.isConnected,\n );\n await Promise.all(evmWallets.map((w) => w.switchChain(chain)));\n },\n [wallets],\n );\n}\n","import { PropsWithChildren } from \"react\";\nimport { AuthContext, AuthContextValue } from \"./AuthContext\";\n\nexport type AuthProviderProps = PropsWithChildren<AuthContextValue>;\n\nexport function AuthProvider({ children, ...value }: AuthProviderProps) {\n return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;\n}\n","import { PropsWithChildren } from \"react\";\nimport {\n WalletConnectorContext,\n WalletConnectorContextValue,\n} from \"./WalletConnectorContext\";\n\nexport type WalletConnectorProviderProps =\n PropsWithChildren<WalletConnectorContextValue>;\n\nexport function WalletConnectorProvider({\n children,\n ...value\n}: WalletConnectorProviderProps) {\n return (\n <WalletConnectorContext.Provider value={value}>\n {children}\n </WalletConnectorContext.Provider>\n );\n}\n","declare global {\n interface Window {\n __LIBERFI_VERSION__?: {\n [key: string]: string;\n };\n }\n}\nif (typeof window !== \"undefined\") {\n window.__LIBERFI_VERSION__ = window.__LIBERFI_VERSION__ || {};\n window.__LIBERFI_VERSION__[\"@liberfi.io/wallet-connector\"] = \"0.1.18\";\n}\n\nexport default \"0.1.18\";\n"]}
1
+ {"version":3,"sources":["../src/providers/AuthContext.ts","../src/hooks/useAuth.ts","../src/hooks/useAuthCallback.ts","../src/providers/WalletConnectorContext.ts","../src/hooks/useWalletConnector.ts","../src/hooks/useWallets.ts","../src/hooks/useSwitchChain.ts","../src/providers/AuthProvider.tsx","../src/providers/WalletConnectorProvider.tsx","../src/version.ts"],"names":["AuthContext","createContext","useAuth","context","useContext","useAuthCallback","callback","deps","status","signIn","statusRef","useRef","useCallback","args","WalletConnectorContext","useWalletConnector","useWallets","wallets","useSwitchChain","chain","chainToNamespace","ChainNamespace","evmWallets","w","AuthProvider","children","value","jsx","WalletConnectorProvider","version_default"],"mappings":"oMAgBO,IAAMA,CAAAA,CAAcC,aAAAA,CACzB,EACF,CAAA,CCPO,SAASC,CAAAA,EAAU,CACxB,IAAMC,CAAAA,CAAUC,UAAAA,CAAWJ,CAAW,CAAA,CACtC,GAAI,CAACG,CAAAA,CACH,MAAM,IAAI,KAAA,CAAM,4CAA4C,CAAA,CAE9D,OAAOA,CACT,CCUO,SAASE,CAAAA,CACdC,CAAAA,CACAC,CAAAA,CACwB,CACxB,GAAM,CAAE,MAAA,CAAAC,CAAAA,CAAQ,MAAA,CAAAC,CAAO,CAAA,CAAIP,CAAAA,EAAQ,CAE7BQ,CAAAA,CAAYC,MAAAA,CAAeH,CAAM,CAAA,CACvC,OAAAE,CAAAA,CAAU,OAAA,CAAUF,CAAAA,CAEbI,YACL,MAAA,GACKC,CAAAA,GAC6C,CAChD,GAAIH,CAAAA,CAAU,OAAA,GAAY,eAAA,CAAiB,CACrCA,CAAAA,CAAU,OAAA,GAAY,iBAAA,EACxBD,CAAAA,EAAO,CAET,MACF,CACA,OAAOH,CAAAA,CAAS,GAAGO,CAAI,CACzB,CAAA,CACA,CAAC,GAAIN,CAAAA,EAAQ,EAAC,CAAIE,CAAAA,CAAQH,CAAQ,CACpC,CACF,CCzBO,IAAMQ,CAAAA,CACXb,aAAAA,CAA2C,EAAiC,CAAA,CCfvE,SAASc,CAAAA,EAAqB,CACnC,IAAMZ,CAAAA,CAAUC,UAAAA,CAAWU,CAAsB,CAAA,CACjD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GxDI,CAAAA,CAAQ","file":"index.mjs","sourcesContent":["import { createContext } from \"react\";\nimport { AuthenticatedUser } from \"../types\";\n\nexport interface AuthContextValue {\n /** authenticated user profile */\n user: AuthenticatedUser | null;\n /** authentication status */\n status: \"unauthenticated\" | \"authenticating\" | \"authenticated\";\n /** sign in to the IdP */\n signIn: () => void | Promise<void>;\n /** sign out from the IdP */\n signOut: () => void | Promise<void>;\n /** refresh the access token */\n refreshAccessToken: () => void | Promise<void>;\n}\n\nexport const AuthContext = createContext<AuthContextValue>(\n {} as AuthContextValue,\n);\n","import { useContext } from \"react\";\nimport { AuthContext } from \"../providers/AuthContext\";\n\n/**\n * Returns the auth context value.\n *\n * Must be used within an {@link AuthProvider}.\n * Throws if no provider is found in the component tree.\n *\n * @returns The current {@link AuthContextValue} (user, status, signIn, signOut, refreshAccessToken).\n */\nexport function useAuth() {\n const context = useContext(AuthContext);\n if (!context) {\n throw new Error(\"useAuth must be used within a AuthProvider\");\n }\n return context;\n}\n","/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { DependencyList, useCallback, useRef } from \"react\";\nimport { useAuth } from \"./useAuth\";\n\n/**\n * The return type of {@link useAuthCallback}.\n *\n * Accepts the same parameters as `T` but always returns a `Promise` that\n * resolves to `Awaited<ReturnType<T>>` when authenticated, or `undefined`\n * when the user is not yet authenticated.\n */\nexport type AuthGuardedCallback<T extends (...args: any[]) => any> = (\n ...args: Parameters<T>\n) => Promise<Awaited<ReturnType<T>> | undefined>;\n\n/**\n * Wraps a callback so it only executes when the user is authenticated.\n *\n * - **authenticated**: executes the callback and returns its result.\n * - **unauthenticated**: triggers `signIn()` and returns `undefined`.\n * - **authenticating**: returns `undefined` without re-triggering sign-in\n * (avoids conflicting with an in-progress auth flow).\n *\n * @param callback - The function to guard behind authentication.\n * @param deps - Optional dependency list (same semantics as `useCallback`).\n * @returns A wrapped callback that may resolve to `undefined` when not authenticated.\n */\nexport function useAuthCallback<T extends (...args: any[]) => any>(\n callback: T,\n deps?: DependencyList,\n): AuthGuardedCallback<T> {\n const { status, signIn } = useAuth();\n\n const statusRef = useRef<string>(status);\n statusRef.current = status;\n\n return useCallback(\n async (\n ...args: Parameters<T>\n ): Promise<Awaited<ReturnType<T>> | undefined> => {\n if (statusRef.current !== \"authenticated\") {\n if (statusRef.current === \"unauthenticated\") {\n signIn();\n }\n return;\n }\n return callback(...args);\n },\n [...(deps || []), signIn, callback],\n );\n}\n","import { createContext } from \"react\";\nimport { WalletAdapter } from \"../types\";\n\nexport interface WalletConnectorContextValue {\n /**\n * detecting: is detecting connected wallets\n * connecting: is connecting to the first wallet\n * connected: is connected to at least one wallet\n * disconnecting: is disconnecting from the last connected wallet\n * disconnected: is disconnected from all wallets\n */\n status:\n | \"detecting\"\n | \"connecting\"\n | \"connected\"\n | \"disconnecting\"\n | \"disconnected\";\n // all wallets that can be used to take onchain actions when connected\n wallets: Array<WalletAdapter>;\n // if no wallets are connected, connect one or multiple wallets\n connect: () => Promise<void>;\n // disconnect all wallets\n disconnect: () => Promise<void>;\n}\n\nexport const WalletConnectorContext =\n createContext<WalletConnectorContextValue>({} as WalletConnectorContextValue);\n","import { useContext } from \"react\";\nimport { WalletConnectorContext } from \"../providers/WalletConnectorContext\";\n\n/**\n * Returns the wallet connector context value.\n *\n * Must be used within a {@link WalletConnectorProvider}.\n * Throws if no provider is found in the component tree.\n *\n * @returns The current {@link WalletConnectorContextValue} (status, wallets, connect, disconnect).\n */\nexport function useWalletConnector() {\n const context = useContext(WalletConnectorContext);\n if (!context) {\n throw new Error(\n \"useWalletConnector must be used within a WalletConnectorProvider\",\n );\n }\n return context;\n}\n","import { useWalletConnector } from \"./useWalletConnector\";\n\n/**\n * Convenience hook that returns the `wallets` array from the wallet connector context.\n *\n * Shorthand for `useWalletConnector().wallets`.\n *\n * @returns All wallets that can be used to take on-chain actions when connected.\n */\nexport function useWallets() {\n const { wallets } = useWalletConnector();\n return wallets;\n}\n","import { useCallback } from \"react\";\nimport { Chain, ChainNamespace } from \"@liberfi.io/types\";\nimport { chainToNamespace } from \"@liberfi.io/utils\";\nimport { EvmWalletAdapter } from \"../types\";\nimport { useWallets } from \"./useWallets\";\n\n/**\n * Returns a function that switches all connected EVM wallets to the given chain.\n *\n * Solana chains are automatically skipped (no-op). For EVM chains, the returned\n * function calls `switchChain` on every connected wallet that implements\n * {@link EvmWalletAdapter} in parallel.\n *\n * @returns An async function `(chain: Chain) => Promise<void>`.\n */\nexport function useSwitchChain() {\n const wallets = useWallets();\n\n return useCallback(\n async (chain: Chain) => {\n if (chainToNamespace(chain) === ChainNamespace.SOLANA) return;\n\n const evmWallets = wallets.filter(\n (w): w is EvmWalletAdapter => \"switchChain\" in w && w.isConnected,\n );\n await Promise.all(evmWallets.map((w) => w.switchChain(chain)));\n },\n [wallets],\n );\n}\n","import { PropsWithChildren } from \"react\";\nimport { AuthContext, AuthContextValue } from \"./AuthContext\";\n\n/** Props for {@link AuthProvider}. */\nexport type AuthProviderProps = PropsWithChildren<AuthContextValue>;\n\n/**\n * Provides authentication state to the component tree.\n *\n * This is a passthrough provider — it accepts the full context value as props\n * and forwards it to React context. Implementation providers (e.g.\n * `PrivyAuthProvider`) typically wrap this component and supply the concrete\n * `user`, `status`, `signIn`, `signOut`, and `refreshAccessToken` values.\n */\nexport function AuthProvider({ children, ...value }: AuthProviderProps) {\n return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;\n}\n","import { PropsWithChildren } from \"react\";\nimport {\n WalletConnectorContext,\n WalletConnectorContextValue,\n} from \"./WalletConnectorContext\";\n\n/** Props for {@link WalletConnectorProvider}. */\nexport type WalletConnectorProviderProps =\n PropsWithChildren<WalletConnectorContextValue>;\n\n/**\n * Provides wallet connection state to the component tree.\n *\n * This is a passthrough provider — it accepts the full context value as props\n * and forwards it to React context. Implementation providers (e.g.\n * `PrivyWalletConnectorProvider`) typically wrap this component and supply\n * the concrete `status`, `wallets`, `connect`, and `disconnect` values.\n */\nexport function WalletConnectorProvider({\n children,\n ...value\n}: WalletConnectorProviderProps) {\n return (\n <WalletConnectorContext.Provider value={value}>\n {children}\n </WalletConnectorContext.Provider>\n );\n}\n","declare global {\n interface Window {\n __LIBERFI_VERSION__?: {\n [key: string]: string;\n };\n }\n}\nif (typeof window !== \"undefined\") {\n window.__LIBERFI_VERSION__ = window.__LIBERFI_VERSION__ || {};\n window.__LIBERFI_VERSION__[\"@liberfi.io/wallet-connector\"] = \"0.1.20\";\n}\n\nexport default \"0.1.20\";\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@liberfi.io/wallet-connector",
3
- "version": "0.1.18",
3
+ "version": "0.1.20",
4
4
  "description": "Base Wallet Connector for Liberfi React SDK",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -14,18 +14,24 @@
14
14
  "access": "public"
15
15
  },
16
16
  "dependencies": {
17
- "@liberfi.io/types": "0.1.27",
18
- "@liberfi.io/utils": "0.1.24"
17
+ "@liberfi.io/types": "0.1.29",
18
+ "@liberfi.io/utils": "0.1.26"
19
19
  },
20
20
  "devDependencies": {
21
+ "@testing-library/dom": "^10.4.1",
22
+ "@testing-library/react": "^16.3.0",
23
+ "@types/jest": "^29.5.3",
21
24
  "@types/react": "^19.1.13",
22
25
  "@types/react-dom": "^19.1.9",
26
+ "jest": "^29.6.1",
27
+ "jest-environment-jsdom": "^29.7.0",
23
28
  "react": "^19.1.1",
24
29
  "react-dom": "^19.1.1",
25
30
  "rimraf": "^5.0.5",
31
+ "ts-jest": "^29.4.6",
26
32
  "tsup": "^8.5.0",
27
33
  "typescript": "^5.9.2",
28
- "tsconfig": "0.1.15"
34
+ "tsconfig": "0.1.17"
29
35
  },
30
36
  "peerDependencies": {
31
37
  "react": ">=18",
@@ -33,6 +39,6 @@
33
39
  },
34
40
  "scripts": {
35
41
  "build": "rimraf -rf dist && tsup",
36
- "test": "echo \"Error: no test specified\" && exit 1"
42
+ "test": "jest"
37
43
  }
38
44
  }