@kheopskit/core 0.0.21 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1,9 +1,21 @@
1
- import { Observable } from 'rxjs';
2
1
  import { AppKit, ThemeMode, ThemeVariables } from '@reown/appkit/core';
3
2
  import { AppKitNetwork } from '@reown/appkit/networks';
4
3
  import { Metadata } from '@walletconnect/universal-provider';
5
4
  import { InjectedExtension, InjectedPolkadotAccount } from 'polkadot-api/pjs-signer';
6
5
  import { EIP1193Provider, WalletClient, CustomTransport, Account } from 'viem';
6
+ import * as rxjs from 'rxjs';
7
+ import { Observable } from 'rxjs';
8
+
9
+ /**
10
+ * Clears an observable from the cache.
11
+ * Use when a wallet disconnects or configuration changes.
12
+ */
13
+ declare const clearCachedObservable: (key: string) => void;
14
+ /**
15
+ * Clears all cached observables.
16
+ * Use when resetting the entire kheopskit state.
17
+ */
18
+ declare const clearAllCachedObservables: () => void;
7
19
 
8
20
  type WalletAccountId = string;
9
21
 
@@ -26,6 +38,30 @@ type KheopskitConfig = {
26
38
  themeVariables?: ThemeVariables;
27
39
  };
28
40
  debug: boolean;
41
+ /**
42
+ * Custom storage key for persisting wallet connection state.
43
+ * Useful when running multiple kheopskit instances on the same domain
44
+ * to prevent state conflicts between different dapps.
45
+ *
46
+ * @default "kheopskit"
47
+ *
48
+ * @example
49
+ * ```ts
50
+ * // For app "MyDapp" to avoid conflicts
51
+ * { storageKey: "kheopskit-mydapp" }
52
+ * ```
53
+ */
54
+ storageKey: string;
55
+ /**
56
+ * Grace period in milliseconds to wait for wallets to inject before
57
+ * syncing to actual state. During this period, cached wallet/account
58
+ * state from storage is preserved to prevent UI flashing.
59
+ *
60
+ * Set to 0 to disable hydration buffering.
61
+ *
62
+ * @default 500
63
+ */
64
+ hydrationGracePeriod: number;
29
65
  };
30
66
  type PolkadotInjectedWallet = {
31
67
  id: WalletId;
@@ -93,14 +129,119 @@ type EthereumAccount = {
93
129
  isWalletDefault: boolean;
94
130
  };
95
131
  type WalletAccount = PolkadotAccount | EthereumAccount;
132
+ /**
133
+ * Serializable wallet data for SSR hydration cache.
134
+ * Contains only the data needed to render wallet UI without flash.
135
+ * Note: icon is NOT stored to save cookie space - it's looked up at hydration time.
136
+ */
137
+ type CachedWallet = {
138
+ id: WalletId;
139
+ platform: WalletPlatform;
140
+ type: "injected" | "appKit";
141
+ name: string;
142
+ isConnected: boolean;
143
+ };
144
+ /**
145
+ * Serializable account data for SSR hydration cache.
146
+ * Contains only the data needed to render account UI without flash.
147
+ */
148
+ type CachedAccount = {
149
+ id: string;
150
+ platform: WalletPlatform;
151
+ address: string;
152
+ name?: string;
153
+ walletId: WalletId;
154
+ walletName: string;
155
+ };
156
+
157
+ /**
158
+ * Converts a CachedWallet to a placeholder Wallet object.
159
+ * The placeholder has the same display properties but connect/disconnect throw errors.
160
+ *
161
+ * @param cached - The cached wallet data from storage
162
+ * @returns A placeholder Wallet object that can be displayed but not interacted with
163
+ */
164
+ declare const hydrateWallet: (cached: CachedWallet) => Wallet;
165
+ /**
166
+ * Converts a CachedAccount to a placeholder WalletAccount object.
167
+ *
168
+ * @param cached - The cached account data from storage
169
+ * @returns A placeholder WalletAccount object that can be displayed
170
+ */
171
+ declare const hydrateAccount: (cached: CachedAccount) => WalletAccount;
172
+
173
+ /**
174
+ * Gets a cached icon for a wallet.
175
+ * @param walletId - The wallet ID (e.g., "ethereum:io.talisman")
176
+ * @returns The cached icon data URI, or undefined if not cached
177
+ */
178
+ declare const getCachedIcon: (walletId: string) => string | undefined;
179
+
180
+ /**
181
+ * Clears the cached AppKit observable.
182
+ * Use when configuration changes or for testing purposes.
183
+ * Note: This does NOT destroy the appKit instance created by Reown.
184
+ */
185
+ declare const resetAppKitCache: () => void;
186
+
187
+ /**
188
+ * Default storage key for persisting wallet connection state.
189
+ * Can be overridden via config.storageKey to avoid conflicts
190
+ * when running multiple dapps on the same domain.
191
+ */
192
+ declare const DEFAULT_STORAGE_KEY = "kheopskit";
193
+ declare const resolveConfig: (config: Partial<KheopskitConfig> | undefined) => KheopskitConfig;
194
+
195
+ type KheopskitStoreData = {
196
+ autoReconnect?: WalletId[];
197
+ /** Cached wallet state for SSR hydration to prevent UI flash */
198
+ cachedWallets?: CachedWallet[];
199
+ /** Cached account state for SSR hydration to prevent UI flash */
200
+ cachedAccounts?: CachedAccount[];
201
+ };
202
+ type CreateKheopskitStoreOptions = {
203
+ /**
204
+ * Cookie string for SSR hydration.
205
+ * When provided, uses cookieStorage instead of localStorage.
206
+ */
207
+ ssrCookies?: string;
208
+ /**
209
+ * Custom storage key to namespace the stored data.
210
+ * @default "kheopskit"
211
+ */
212
+ storageKey?: string;
213
+ };
214
+ /**
215
+ * Creates a kheopskit store with the appropriate storage backend.
216
+ * Uses cookieStorage when ssrCookies is provided (for SSR hydration),
217
+ * otherwise falls back to safeLocalStorage.
218
+ *
219
+ * @param options - Configuration options for the store
220
+ */
221
+ declare const createKheopskitStore: (options?: CreateKheopskitStoreOptions) => {
222
+ observable: rxjs.Observable<KheopskitStoreData>;
223
+ addEnabledWalletId: (walletId: WalletId) => void;
224
+ removeEnabledWalletId: (walletId: WalletId) => void;
225
+ getCachedState: () => {
226
+ wallets: CachedWallet[];
227
+ accounts: CachedAccount[];
228
+ };
229
+ setCachedState: (wallets: CachedWallet[], accounts: CachedAccount[]) => void;
230
+ };
96
231
 
97
232
  type KheopskitState = {
98
233
  wallets: Wallet[];
99
234
  accounts: WalletAccount[];
100
235
  config: KheopskitConfig;
236
+ /**
237
+ * Whether the state is still being hydrated from cache.
238
+ * During hydration, cached wallets/accounts may be displayed
239
+ * before the actual wallet extensions have injected.
240
+ *
241
+ * Use this to show loading indicators or disable certain actions.
242
+ */
243
+ isHydrating: boolean;
101
244
  };
102
- declare const getKheopskit$: (config?: Partial<KheopskitConfig>) => Observable<KheopskitState>;
103
-
104
- declare const resolveConfig: (config: Partial<KheopskitConfig> | undefined) => KheopskitConfig;
245
+ declare const getKheopskit$: (config?: Partial<KheopskitConfig>, ssrCookies?: string, existingStore?: ReturnType<typeof createKheopskitStore>) => Observable<KheopskitState>;
105
246
 
106
- export { type EthereumAccount, type EthereumAppKitWallet, type EthereumInjectedWallet, type EthereumWallet, type KheopskitConfig, type KheopskitState, type PolkadotAccount, type PolkadotAppKitWallet, type PolkadotInjectedWallet, type PolkadotWallet, type Wallet, type WalletAccount, type WalletPlatform, getKheopskit$, resolveConfig };
247
+ export { type CachedAccount, type CachedWallet, DEFAULT_STORAGE_KEY, type EthereumAccount, type EthereumAppKitWallet, type EthereumInjectedWallet, type EthereumWallet, type KheopskitConfig, type KheopskitState, type PolkadotAccount, type PolkadotAppKitWallet, type PolkadotInjectedWallet, type PolkadotWallet, type Wallet, type WalletAccount, type WalletPlatform, clearAllCachedObservables, clearCachedObservable, createKheopskitStore, getCachedIcon, getKheopskit$, hydrateAccount, hydrateWallet, resetAppKitCache, resolveConfig };
package/dist/index.d.ts CHANGED
@@ -1,9 +1,21 @@
1
- import { Observable } from 'rxjs';
2
1
  import { AppKit, ThemeMode, ThemeVariables } from '@reown/appkit/core';
3
2
  import { AppKitNetwork } from '@reown/appkit/networks';
4
3
  import { Metadata } from '@walletconnect/universal-provider';
5
4
  import { InjectedExtension, InjectedPolkadotAccount } from 'polkadot-api/pjs-signer';
6
5
  import { EIP1193Provider, WalletClient, CustomTransport, Account } from 'viem';
6
+ import * as rxjs from 'rxjs';
7
+ import { Observable } from 'rxjs';
8
+
9
+ /**
10
+ * Clears an observable from the cache.
11
+ * Use when a wallet disconnects or configuration changes.
12
+ */
13
+ declare const clearCachedObservable: (key: string) => void;
14
+ /**
15
+ * Clears all cached observables.
16
+ * Use when resetting the entire kheopskit state.
17
+ */
18
+ declare const clearAllCachedObservables: () => void;
7
19
 
8
20
  type WalletAccountId = string;
9
21
 
@@ -26,6 +38,30 @@ type KheopskitConfig = {
26
38
  themeVariables?: ThemeVariables;
27
39
  };
28
40
  debug: boolean;
41
+ /**
42
+ * Custom storage key for persisting wallet connection state.
43
+ * Useful when running multiple kheopskit instances on the same domain
44
+ * to prevent state conflicts between different dapps.
45
+ *
46
+ * @default "kheopskit"
47
+ *
48
+ * @example
49
+ * ```ts
50
+ * // For app "MyDapp" to avoid conflicts
51
+ * { storageKey: "kheopskit-mydapp" }
52
+ * ```
53
+ */
54
+ storageKey: string;
55
+ /**
56
+ * Grace period in milliseconds to wait for wallets to inject before
57
+ * syncing to actual state. During this period, cached wallet/account
58
+ * state from storage is preserved to prevent UI flashing.
59
+ *
60
+ * Set to 0 to disable hydration buffering.
61
+ *
62
+ * @default 500
63
+ */
64
+ hydrationGracePeriod: number;
29
65
  };
30
66
  type PolkadotInjectedWallet = {
31
67
  id: WalletId;
@@ -93,14 +129,119 @@ type EthereumAccount = {
93
129
  isWalletDefault: boolean;
94
130
  };
95
131
  type WalletAccount = PolkadotAccount | EthereumAccount;
132
+ /**
133
+ * Serializable wallet data for SSR hydration cache.
134
+ * Contains only the data needed to render wallet UI without flash.
135
+ * Note: icon is NOT stored to save cookie space - it's looked up at hydration time.
136
+ */
137
+ type CachedWallet = {
138
+ id: WalletId;
139
+ platform: WalletPlatform;
140
+ type: "injected" | "appKit";
141
+ name: string;
142
+ isConnected: boolean;
143
+ };
144
+ /**
145
+ * Serializable account data for SSR hydration cache.
146
+ * Contains only the data needed to render account UI without flash.
147
+ */
148
+ type CachedAccount = {
149
+ id: string;
150
+ platform: WalletPlatform;
151
+ address: string;
152
+ name?: string;
153
+ walletId: WalletId;
154
+ walletName: string;
155
+ };
156
+
157
+ /**
158
+ * Converts a CachedWallet to a placeholder Wallet object.
159
+ * The placeholder has the same display properties but connect/disconnect throw errors.
160
+ *
161
+ * @param cached - The cached wallet data from storage
162
+ * @returns A placeholder Wallet object that can be displayed but not interacted with
163
+ */
164
+ declare const hydrateWallet: (cached: CachedWallet) => Wallet;
165
+ /**
166
+ * Converts a CachedAccount to a placeholder WalletAccount object.
167
+ *
168
+ * @param cached - The cached account data from storage
169
+ * @returns A placeholder WalletAccount object that can be displayed
170
+ */
171
+ declare const hydrateAccount: (cached: CachedAccount) => WalletAccount;
172
+
173
+ /**
174
+ * Gets a cached icon for a wallet.
175
+ * @param walletId - The wallet ID (e.g., "ethereum:io.talisman")
176
+ * @returns The cached icon data URI, or undefined if not cached
177
+ */
178
+ declare const getCachedIcon: (walletId: string) => string | undefined;
179
+
180
+ /**
181
+ * Clears the cached AppKit observable.
182
+ * Use when configuration changes or for testing purposes.
183
+ * Note: This does NOT destroy the appKit instance created by Reown.
184
+ */
185
+ declare const resetAppKitCache: () => void;
186
+
187
+ /**
188
+ * Default storage key for persisting wallet connection state.
189
+ * Can be overridden via config.storageKey to avoid conflicts
190
+ * when running multiple dapps on the same domain.
191
+ */
192
+ declare const DEFAULT_STORAGE_KEY = "kheopskit";
193
+ declare const resolveConfig: (config: Partial<KheopskitConfig> | undefined) => KheopskitConfig;
194
+
195
+ type KheopskitStoreData = {
196
+ autoReconnect?: WalletId[];
197
+ /** Cached wallet state for SSR hydration to prevent UI flash */
198
+ cachedWallets?: CachedWallet[];
199
+ /** Cached account state for SSR hydration to prevent UI flash */
200
+ cachedAccounts?: CachedAccount[];
201
+ };
202
+ type CreateKheopskitStoreOptions = {
203
+ /**
204
+ * Cookie string for SSR hydration.
205
+ * When provided, uses cookieStorage instead of localStorage.
206
+ */
207
+ ssrCookies?: string;
208
+ /**
209
+ * Custom storage key to namespace the stored data.
210
+ * @default "kheopskit"
211
+ */
212
+ storageKey?: string;
213
+ };
214
+ /**
215
+ * Creates a kheopskit store with the appropriate storage backend.
216
+ * Uses cookieStorage when ssrCookies is provided (for SSR hydration),
217
+ * otherwise falls back to safeLocalStorage.
218
+ *
219
+ * @param options - Configuration options for the store
220
+ */
221
+ declare const createKheopskitStore: (options?: CreateKheopskitStoreOptions) => {
222
+ observable: rxjs.Observable<KheopskitStoreData>;
223
+ addEnabledWalletId: (walletId: WalletId) => void;
224
+ removeEnabledWalletId: (walletId: WalletId) => void;
225
+ getCachedState: () => {
226
+ wallets: CachedWallet[];
227
+ accounts: CachedAccount[];
228
+ };
229
+ setCachedState: (wallets: CachedWallet[], accounts: CachedAccount[]) => void;
230
+ };
96
231
 
97
232
  type KheopskitState = {
98
233
  wallets: Wallet[];
99
234
  accounts: WalletAccount[];
100
235
  config: KheopskitConfig;
236
+ /**
237
+ * Whether the state is still being hydrated from cache.
238
+ * During hydration, cached wallets/accounts may be displayed
239
+ * before the actual wallet extensions have injected.
240
+ *
241
+ * Use this to show loading indicators or disable certain actions.
242
+ */
243
+ isHydrating: boolean;
101
244
  };
102
- declare const getKheopskit$: (config?: Partial<KheopskitConfig>) => Observable<KheopskitState>;
103
-
104
- declare const resolveConfig: (config: Partial<KheopskitConfig> | undefined) => KheopskitConfig;
245
+ declare const getKheopskit$: (config?: Partial<KheopskitConfig>, ssrCookies?: string, existingStore?: ReturnType<typeof createKheopskitStore>) => Observable<KheopskitState>;
105
246
 
106
- export { type EthereumAccount, type EthereumAppKitWallet, type EthereumInjectedWallet, type EthereumWallet, type KheopskitConfig, type KheopskitState, type PolkadotAccount, type PolkadotAppKitWallet, type PolkadotInjectedWallet, type PolkadotWallet, type Wallet, type WalletAccount, type WalletPlatform, getKheopskit$, resolveConfig };
247
+ export { type CachedAccount, type CachedWallet, DEFAULT_STORAGE_KEY, type EthereumAccount, type EthereumAppKitWallet, type EthereumInjectedWallet, type EthereumWallet, type KheopskitConfig, type KheopskitState, type PolkadotAccount, type PolkadotAppKitWallet, type PolkadotInjectedWallet, type PolkadotWallet, type Wallet, type WalletAccount, type WalletPlatform, clearAllCachedObservables, clearCachedObservable, createKheopskitStore, getCachedIcon, getKheopskit$, hydrateAccount, hydrateWallet, resetAppKitCache, resolveConfig };