@talken/talkenkit 2.5.1 → 2.5.2

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.
@@ -0,0 +1,873 @@
1
+ "use client";
2
+ import {
3
+ TalkenApiError
4
+ } from "./chunk-4P3SPC44.js";
5
+ import {
6
+ getCredentialManager
7
+ } from "./chunk-QKK3OPQA.js";
8
+ import {
9
+ DEFAULT_TALKEN_API_CONFIG,
10
+ TALKEN_API_ENDPOINTS,
11
+ TOKEN_EXPIRY
12
+ } from "./chunk-RX2VOIUB.js";
13
+
14
+ // src/wallets/walletConnectors/abcWallet/api/TalkenApiClient.ts
15
+ var STORAGE_KEY_PREFIX = "talken_api_";
16
+ function loadToken(key) {
17
+ if (typeof window === "undefined")
18
+ return null;
19
+ try {
20
+ return localStorage.getItem(`${STORAGE_KEY_PREFIX}${key}`);
21
+ } catch {
22
+ return null;
23
+ }
24
+ }
25
+ function saveToken(key, value) {
26
+ if (typeof window === "undefined")
27
+ return;
28
+ try {
29
+ localStorage.setItem(`${STORAGE_KEY_PREFIX}${key}`, value);
30
+ } catch {
31
+ }
32
+ }
33
+ function removeToken(key) {
34
+ if (typeof window === "undefined")
35
+ return;
36
+ try {
37
+ localStorage.removeItem(`${STORAGE_KEY_PREFIX}${key}`);
38
+ } catch {
39
+ }
40
+ }
41
+ function normalizeAuthResponse(raw) {
42
+ const d = raw.data || raw;
43
+ const loginData = d.login || d;
44
+ return {
45
+ accessToken: loginData.access_token || loginData.accessToken || "",
46
+ refreshToken: loginData.refresh_token || loginData.refreshToken || "",
47
+ expiresIn: loginData.expires_in || loginData.expire_in || loginData.expiresIn || 3600,
48
+ uid: d.uid || loginData.uid || "",
49
+ email: d.email || loginData.email || "",
50
+ isNewUser: d.isNewUser
51
+ };
52
+ }
53
+ var TalkenAuthModule = class {
54
+ constructor(client) {
55
+ this.client = client;
56
+ }
57
+ /**
58
+ * Login with email and password
59
+ * POST /wallet/auth { action: 'login', method: 'password', email, password }
60
+ */
61
+ async loginWithPassword(email, password) {
62
+ const raw = await this.client.post(
63
+ TALKEN_API_ENDPOINTS.AUTH,
64
+ { action: "login", method: "password", email, password },
65
+ { skipAuth: true }
66
+ );
67
+ const res = normalizeAuthResponse(raw);
68
+ this.client.handleAuthResponse(res);
69
+ return res;
70
+ }
71
+ /**
72
+ * Send OTP code to email
73
+ * POST /wallet/auth { action: 'otp.send', email }
74
+ */
75
+ async sendOtp(email) {
76
+ const raw = await this.client.post(
77
+ TALKEN_API_ENDPOINTS.AUTH,
78
+ { action: "otp.send", email },
79
+ { skipAuth: true }
80
+ );
81
+ const res = raw;
82
+ if (res.success === false) {
83
+ const data = res.data || {};
84
+ throw new Error(data.msg || res.error || "OTP send failed");
85
+ }
86
+ return res.data || raw;
87
+ }
88
+ /**
89
+ * Verify OTP code
90
+ * POST /wallet/auth { action: 'otp.verify', email, otpCode }
91
+ */
92
+ async verifyOtp(email, otpCode) {
93
+ const raw = await this.client.post(
94
+ TALKEN_API_ENDPOINTS.AUTH,
95
+ { action: "otp.verify", email, otpCode },
96
+ { skipAuth: true }
97
+ );
98
+ const res = raw;
99
+ if (res.success === false) {
100
+ const data = res.data || {};
101
+ throw new Error(data.msg || res.error || "OTP verification failed");
102
+ }
103
+ return res.data || raw;
104
+ }
105
+ /**
106
+ * Check if email is already registered
107
+ * POST /wallet/auth { action: 'email.check', email }
108
+ * Returns code 606 if email exists
109
+ */
110
+ async checkEmail(email) {
111
+ const raw = await this.client.post(
112
+ TALKEN_API_ENDPOINTS.AUTH,
113
+ { action: "email.check", email },
114
+ { skipAuth: true }
115
+ );
116
+ const d = raw.data || raw;
117
+ return {
118
+ code: d.code,
119
+ message: d.message,
120
+ exists: d.code === 606
121
+ };
122
+ }
123
+ /**
124
+ * Register a new user (creates account + auto-login)
125
+ * POST /wallet/auth { action: 'register', email, otpCode, password, name? }
126
+ */
127
+ async register(params) {
128
+ const raw = await this.client.post(
129
+ TALKEN_API_ENDPOINTS.AUTH,
130
+ { action: "register", ...params },
131
+ { skipAuth: true }
132
+ );
133
+ const res = normalizeAuthResponse(raw);
134
+ this.client.handleAuthResponse(res);
135
+ return res;
136
+ }
137
+ /**
138
+ * Reset password for existing user
139
+ * POST /wallet/auth { action: 'password.reset', email, newPassword, emailCode }
140
+ */
141
+ async resetPassword(email, newPassword, emailCode) {
142
+ const raw = await this.client.post(
143
+ TALKEN_API_ENDPOINTS.AUTH,
144
+ { action: "password.reset", email, newPassword, emailCode },
145
+ { skipAuth: true }
146
+ );
147
+ return raw.data || raw;
148
+ }
149
+ /**
150
+ * Login with social provider (Google, Apple, Kakao, etc.)
151
+ * POST /wallet/auth { action: 'login', method: 'sns', token, service, audience? }
152
+ */
153
+ async loginWithSns(params) {
154
+ const raw = await this.client.post(
155
+ TALKEN_API_ENDPOINTS.AUTH,
156
+ {
157
+ action: "login",
158
+ method: "sns",
159
+ token: params.token,
160
+ service: params.service,
161
+ ...params.audience && { audience: params.audience }
162
+ },
163
+ { skipAuth: true }
164
+ );
165
+ const res = normalizeAuthResponse(raw);
166
+ this.client.handleAuthResponse(res);
167
+ return res;
168
+ }
169
+ /**
170
+ * Refresh access token
171
+ * POST /wallet/auth { action: 'token.refresh', refreshToken }
172
+ */
173
+ async refresh() {
174
+ const refreshToken = this.client.getRefreshToken();
175
+ if (!refreshToken) {
176
+ throw new TalkenApiError(
177
+ "TOKEN_EXPIRED",
178
+ "No refresh token available",
179
+ 401
180
+ );
181
+ }
182
+ const raw = await this.client.post(
183
+ TALKEN_API_ENDPOINTS.AUTH,
184
+ { action: "token.refresh", refreshToken },
185
+ { skipAuth: true }
186
+ );
187
+ const res = normalizeAuthResponse(raw);
188
+ this.client.handleAuthResponse(res);
189
+ return res;
190
+ }
191
+ /**
192
+ * Logout (clear local session only — no server endpoint)
193
+ */
194
+ logout() {
195
+ this.client.clearSession();
196
+ }
197
+ };
198
+ var TalkenWalletModule = class {
199
+ constructor(client) {
200
+ this.client = client;
201
+ }
202
+ /**
203
+ * Generate/recover MPC wallets (create = idempotent generate/recover)
204
+ * POST /wallet/mpc { action: 'create', email?, pin }
205
+ *
206
+ * @param pin - SHA-256 hashed PIN
207
+ * @param email - User email (optional; server resolves from bearer token if omitted)
208
+ */
209
+ async generate(pin, email) {
210
+ return this.client.post(TALKEN_API_ENDPOINTS.WALLET_MPC, {
211
+ action: "create",
212
+ ...email && { email },
213
+ pin
214
+ });
215
+ }
216
+ /**
217
+ * Recover is the same as generate (create is idempotent)
218
+ */
219
+ async recover(pin, email) {
220
+ return this.generate(pin, email);
221
+ }
222
+ /**
223
+ * Get wallet info
224
+ * POST /wallet/mpc { action: 'info' }
225
+ */
226
+ async getInfo() {
227
+ return this.client.post(TALKEN_API_ENDPOINTS.WALLET_MPC, {
228
+ action: "info"
229
+ });
230
+ }
231
+ /**
232
+ * Get addresses for all chains
233
+ * POST /wallet/address { chain: 'all' }
234
+ */
235
+ async getAddresses() {
236
+ return this.client.post(TALKEN_API_ENDPOINTS.WALLET_ADDRESS, {
237
+ chain: "all"
238
+ });
239
+ }
240
+ /**
241
+ * Get addresses for a specific chain
242
+ * POST /wallet/address { chain, publicKey?, pin? }
243
+ */
244
+ async getAddressForChain(chain, publicKey, pin) {
245
+ return this.client.post(TALKEN_API_ENDPOINTS.WALLET_ADDRESS, {
246
+ chain,
247
+ ...publicKey && { publicKey },
248
+ ...pin && { pin }
249
+ });
250
+ }
251
+ };
252
+ function withCachedEvmCreds(params) {
253
+ if (typeof window === "undefined")
254
+ return params;
255
+ try {
256
+ const creds = getCredentialManager().getEvmSigningCredentials();
257
+ if (creds)
258
+ return { ...params, evmCreds: creds };
259
+ } catch {
260
+ }
261
+ return params;
262
+ }
263
+ var TalkenEvmModule = class {
264
+ constructor(client) {
265
+ this.client = client;
266
+ }
267
+ /**
268
+ * Sign EVM transaction (sign only, no broadcast)
269
+ * POST /wallet/sign/evm { network, to, from?, value?, data?, ... }
270
+ * Server handles SecureChannel internally
271
+ */
272
+ async signTransaction(params) {
273
+ return this.client.post(
274
+ TALKEN_API_ENDPOINTS.SIGN_EVM,
275
+ withCachedEvmCreds(params)
276
+ );
277
+ }
278
+ /**
279
+ * Sign EIP-712 typed data
280
+ * POST /wallet/sign/evm/typed { network, typedData }
281
+ */
282
+ async signTypedData(params) {
283
+ return this.client.post(
284
+ TALKEN_API_ENDPOINTS.SIGN_TYPED,
285
+ withCachedEvmCreds(params)
286
+ );
287
+ }
288
+ /**
289
+ * Sign personal message (EIP-191)
290
+ * POST /wallet/sign/evm/personal { network, message, address? }
291
+ */
292
+ async signPersonal(params) {
293
+ const res = await this.client.post(
294
+ TALKEN_API_ENDPOINTS.SIGN_PERSONAL,
295
+ withCachedEvmCreds(params)
296
+ );
297
+ return {
298
+ signature: res?.signature ?? res?.serializedTx ?? "",
299
+ txHash: res?.txHash ?? res?.rawTx
300
+ };
301
+ }
302
+ /**
303
+ * Sign EIP-7702 authorization payload
304
+ * POST /wallet/sign/authorization { network, authorization, pin? }
305
+ */
306
+ async signAuthorization(params) {
307
+ return this.client.post(
308
+ TALKEN_API_ENDPOINTS.SIGN_AUTHORIZATION,
309
+ withCachedEvmCreds(params)
310
+ );
311
+ }
312
+ /**
313
+ * Broadcast signed transaction
314
+ * POST /wallet/tx/raw { network, signedSerializeTx }
315
+ */
316
+ async sendRawTransaction(params) {
317
+ return this.client.post(
318
+ TALKEN_API_ENDPOINTS.TX_RAW,
319
+ params
320
+ );
321
+ }
322
+ /**
323
+ * Integrated EVM transfer (sign + broadcast)
324
+ * POST /wallet/tx/evm { chainKey, to, amountWei, pin, ... }
325
+ */
326
+ async sendTransaction(params) {
327
+ return this.client.post(
328
+ TALKEN_API_ENDPOINTS.TX_EVM,
329
+ withCachedEvmCreds(params)
330
+ );
331
+ }
332
+ /**
333
+ * Get gas price
334
+ * GET /wallet/gas/price?network=
335
+ */
336
+ async getGasPrice(network) {
337
+ return this.client.get(
338
+ `${TALKEN_API_ENDPOINTS.GAS_PRICE}?network=${encodeURIComponent(network)}`
339
+ );
340
+ }
341
+ /**
342
+ * Get suggested gas fees (EIP-1559)
343
+ * GET /wallet/gas/suggested?network=
344
+ */
345
+ async getGasSuggested(network) {
346
+ return this.client.get(
347
+ `${TALKEN_API_ENDPOINTS.GAS_SUGGESTED}?network=${encodeURIComponent(network)}`
348
+ );
349
+ }
350
+ /**
351
+ * Estimate gas
352
+ * POST /wallet/gas/estimate { network, from, to, value?, data? }
353
+ */
354
+ async estimateGas(params) {
355
+ return this.client.post(
356
+ TALKEN_API_ENDPOINTS.GAS_ESTIMATE,
357
+ params
358
+ );
359
+ }
360
+ /**
361
+ * Get nonce for address
362
+ * GET /wallet/nonce?network=&address=
363
+ */
364
+ async getNonce(network, address) {
365
+ const qs = new URLSearchParams({ network, address }).toString();
366
+ return this.client.get(`${TALKEN_API_ENDPOINTS.NONCE}?${qs}`);
367
+ }
368
+ /**
369
+ * Get native balance for address
370
+ * GET /wallet/address/balance?network=&address=
371
+ */
372
+ async getBalance(network, address) {
373
+ const qs = new URLSearchParams({ network, address }).toString();
374
+ return this.client.get(`${TALKEN_API_ENDPOINTS.ADDRESS_BALANCE}?${qs}`);
375
+ }
376
+ /**
377
+ * Execute read-only eth_call
378
+ * POST /wallet/contract/eth-call { network, to, data, from? }
379
+ */
380
+ async ethCall(params) {
381
+ return this.client.post(TALKEN_API_ENDPOINTS.ETH_CALL, params);
382
+ }
383
+ };
384
+ var TalkenSolanaModule = class {
385
+ constructor(client) {
386
+ this.client = client;
387
+ }
388
+ async getLatestBlockhash(network) {
389
+ const qs = new URLSearchParams({ network }).toString();
390
+ return this.client.get(
391
+ `${TALKEN_API_ENDPOINTS.SOLANA_LATEST_BLOCKHASH}?${qs}`,
392
+ {
393
+ skipAuth: true
394
+ }
395
+ );
396
+ }
397
+ async getTransactionStatus(params) {
398
+ const qs = new URLSearchParams({
399
+ network: params.network,
400
+ signature: params.signature
401
+ }).toString();
402
+ return this.client.get(`${TALKEN_API_ENDPOINTS.SOLANA_TX_STATUS}?${qs}`, {
403
+ skipAuth: true
404
+ });
405
+ }
406
+ /**
407
+ * Transfer SOL or SPL token (integrated sign + broadcast)
408
+ * POST /wallet/tx/sol
409
+ * - kind='native': { toAddress, amountLamports, pin }
410
+ * - kind='spl': { toAddress, mintAddress, amount, decimals, network, pin }
411
+ */
412
+ async transfer(params) {
413
+ return this.client.post(
414
+ TALKEN_API_ENDPOINTS.TX_SOL,
415
+ params
416
+ );
417
+ }
418
+ /**
419
+ * Sign Solana transaction or message (sign-only, no broadcast)
420
+ * POST /wallet/sign/sol
421
+ *
422
+ * Two modes:
423
+ * - PIN mode: { message, pin } — server resolves signing material
424
+ * - Share mode: { message, keyId, encryptedShare } — no PIN required (for signMessage)
425
+ *
426
+ * @returns Ed25519 signature (hex) and optionally public key
427
+ */
428
+ async sign(params) {
429
+ return this.client.post(
430
+ TALKEN_API_ENDPOINTS.SIGN_SVM,
431
+ params
432
+ );
433
+ }
434
+ };
435
+ var TalkenBitcoinModule = class {
436
+ constructor(client) {
437
+ this.client = client;
438
+ }
439
+ /**
440
+ * BTC fee rate lookup
441
+ * GET /wallet/btc?mode=fee&network=&blocks=
442
+ */
443
+ async getFeeRate(network, blocks) {
444
+ const qs = new URLSearchParams({
445
+ mode: "fee",
446
+ network,
447
+ ...typeof blocks === "number" ? { blocks: String(blocks) } : {}
448
+ }).toString();
449
+ return this.client.get(
450
+ `${TALKEN_API_ENDPOINTS.WALLET_BTC}?${qs}`
451
+ );
452
+ }
453
+ /**
454
+ * BTC tx status by hash
455
+ * GET /wallet/btc?mode=txHash&network=&txHash=
456
+ */
457
+ async getTxByHash(txHash, network) {
458
+ const qs = new URLSearchParams({
459
+ mode: "txHash",
460
+ network,
461
+ txHash
462
+ }).toString();
463
+ return this.client.get(
464
+ `${TALKEN_API_ENDPOINTS.WALLET_BTC}?${qs}`
465
+ );
466
+ }
467
+ /**
468
+ * BTC UTXO list
469
+ * GET /wallet/btc?mode=utxos&network=&address=
470
+ */
471
+ async getUtxos(network, address) {
472
+ const qs = new URLSearchParams({
473
+ mode: "utxos",
474
+ network,
475
+ ...address ? { address } : {}
476
+ }).toString();
477
+ return this.client.get(
478
+ `${TALKEN_API_ENDPOINTS.WALLET_BTC}?${qs}`
479
+ );
480
+ }
481
+ /**
482
+ * BTC address validation
483
+ * GET /wallet/btc?mode=validateAddress&network=&address=
484
+ */
485
+ async validateAddress(address, network) {
486
+ const qs = new URLSearchParams({
487
+ mode: "validateAddress",
488
+ network,
489
+ address
490
+ }).toString();
491
+ return this.client.get(
492
+ `${TALKEN_API_ENDPOINTS.WALLET_BTC}?${qs}`
493
+ );
494
+ }
495
+ /**
496
+ * Send BTC (integrated sign + broadcast, action=transfer)
497
+ * POST /wallet/tx/btc
498
+ */
499
+ async sendTransaction(params) {
500
+ const payload = { action: "transfer", ...params };
501
+ return this.client.post(
502
+ TALKEN_API_ENDPOINTS.TX_BTC,
503
+ withCachedEvmCreds(payload)
504
+ );
505
+ }
506
+ /**
507
+ * Execute BTC action endpoint (transfer|generate|signHash|finalize)
508
+ * POST /wallet/tx/btc
509
+ */
510
+ async execute(params) {
511
+ return this.client.post(
512
+ TALKEN_API_ENDPOINTS.TX_BTC,
513
+ withCachedEvmCreds(params)
514
+ );
515
+ }
516
+ /**
517
+ * Sign raw hash via secp256k1 MPC (universal: EVM/BTC/TVM)
518
+ * POST /wallet/sign/hash { hash, pin, network? }
519
+ */
520
+ async signHash(params) {
521
+ return this.client.post(
522
+ TALKEN_API_ENDPOINTS.SIGN_HASH,
523
+ withCachedEvmCreds(params)
524
+ );
525
+ }
526
+ /**
527
+ * Broadcast raw BTC transaction
528
+ * POST /wallet/tx/raw { network, signedSerializeTx }
529
+ */
530
+ async broadcastRawTransaction(params) {
531
+ return this.client.post(TALKEN_API_ENDPOINTS.TX_RAW, {
532
+ network: params.network,
533
+ signedSerializeTx: params.rawTransaction
534
+ });
535
+ }
536
+ };
537
+ var TalkenTronModule = class {
538
+ constructor(client) {
539
+ this.client = client;
540
+ }
541
+ /**
542
+ * Execute TVM(TRON) transfer/call (integrated sign + broadcast)
543
+ * POST /wallet/tx/tvm
544
+ * - kind=native: TRX transfer (amountSun)
545
+ * - kind=trc20: TRC-20 transfer (contractAddress + amount)
546
+ * - kind=contract: arbitrary contract call (contractAddress + data)
547
+ */
548
+ async transferTrx(params) {
549
+ return this.client.post(
550
+ TALKEN_API_ENDPOINTS.TX_TVM,
551
+ params
552
+ );
553
+ }
554
+ /**
555
+ * Get TRON account info (balance, resource, permissions)
556
+ * GET /wallet/tron?mode=account&network=&address=
557
+ */
558
+ async getAccount(network, address) {
559
+ return this.client.get(
560
+ `${TALKEN_API_ENDPOINTS.TRON_UTIL}?mode=account&network=${encodeURIComponent(network)}&address=${encodeURIComponent(address)}`
561
+ );
562
+ }
563
+ /**
564
+ * Get TRC20 token balance
565
+ * GET /wallet/tron?mode=trc20Balance&network=&address=&contractAddress=
566
+ */
567
+ async getTrc20Balance(network, address, contractAddress) {
568
+ return this.client.get(
569
+ `${TALKEN_API_ENDPOINTS.TRON_UTIL}?mode=trc20Balance&network=${encodeURIComponent(network)}&address=${encodeURIComponent(address)}&contractAddress=${encodeURIComponent(contractAddress)}`
570
+ );
571
+ }
572
+ };
573
+ var TalkenWalletscanModule = class {
574
+ constructor(client) {
575
+ this.client = client;
576
+ }
577
+ /**
578
+ * Get all wallet tokens with balances
579
+ * GET /wallet/scan?type=tokens&chainKeys=...&walletAddress=...
580
+ */
581
+ async getTokens(params) {
582
+ const qs = new URLSearchParams({
583
+ type: "tokens",
584
+ chainKeys: params.chainKeys,
585
+ walletAddress: params.walletAddress
586
+ }).toString();
587
+ return this.client.get(
588
+ `${TALKEN_API_ENDPOINTS.WALLET_SCAN}?${qs}`
589
+ );
590
+ }
591
+ /**
592
+ * Get token metadata by contract address
593
+ * GET /wallet/scan?type=tokens&chainKeys=...&contractAddress=...
594
+ */
595
+ async getToken(params) {
596
+ const qs = new URLSearchParams({
597
+ type: "tokens",
598
+ chainKeys: params.chainKeys,
599
+ contractAddress: params.contractAddress
600
+ }).toString();
601
+ return this.client.get(
602
+ `${TALKEN_API_ENDPOINTS.WALLET_SCAN}?${qs}`
603
+ );
604
+ }
605
+ /**
606
+ * Get native coin balances
607
+ * GET /wallet/scan?type=natives&chainKeys=...&walletAddress=...
608
+ */
609
+ async getNativeTokens(params) {
610
+ const qs = new URLSearchParams({
611
+ type: "natives",
612
+ chainKeys: params.chainKeys,
613
+ walletAddress: params.walletAddress
614
+ }).toString();
615
+ return this.client.get(
616
+ `${TALKEN_API_ENDPOINTS.WALLET_SCAN}?${qs}`
617
+ );
618
+ }
619
+ /**
620
+ * Get NFT metadata
621
+ * GET /wallet/scan?type=nfts&chainKeys=...&walletAddress=...
622
+ */
623
+ async getNfts(params) {
624
+ const qs = new URLSearchParams({
625
+ type: "nfts",
626
+ chainKeys: params.chainKeys,
627
+ walletAddress: params.walletAddress
628
+ }).toString();
629
+ return this.client.get(
630
+ `${TALKEN_API_ENDPOINTS.WALLET_SCAN}?${qs}`
631
+ );
632
+ }
633
+ };
634
+ var TalkenApiClient = class {
635
+ constructor(config) {
636
+ this.accessToken = null;
637
+ this.refreshToken_ = null;
638
+ this.expiresAt = null;
639
+ this.isRefreshing = false;
640
+ this.refreshPromise = null;
641
+ this.config = {
642
+ baseUrl: config?.baseUrl || DEFAULT_TALKEN_API_CONFIG.baseUrl || "",
643
+ timeout: config?.timeout || DEFAULT_TALKEN_API_CONFIG.timeout,
644
+ environment: config?.environment || "development",
645
+ defaultChainId: config?.defaultChainId,
646
+ debug: config?.debug || false
647
+ };
648
+ this.auth = new TalkenAuthModule(this);
649
+ this.wallet = new TalkenWalletModule(this);
650
+ this.evm = new TalkenEvmModule(this);
651
+ this.solana = new TalkenSolanaModule(this);
652
+ this.bitcoin = new TalkenBitcoinModule(this);
653
+ this.tron = new TalkenTronModule(this);
654
+ this.walletscan = new TalkenWalletscanModule(this);
655
+ this.restoreSession();
656
+ }
657
+ // ── Session Management ──────────────────────────────────────────────
658
+ restoreSession() {
659
+ this.accessToken = loadToken("access_token");
660
+ this.refreshToken_ = loadToken("refresh_token");
661
+ const expiresAtStr = loadToken("expires_at");
662
+ this.expiresAt = expiresAtStr ? Number(expiresAtStr) : null;
663
+ }
664
+ /** @internal Called by auth module after successful login/register */
665
+ handleAuthResponse(response) {
666
+ this.accessToken = response.accessToken;
667
+ this.refreshToken_ = response.refreshToken;
668
+ this.expiresAt = Date.now() + response.expiresIn * 1e3;
669
+ saveToken("access_token", response.accessToken);
670
+ saveToken("refresh_token", response.refreshToken);
671
+ saveToken("expires_at", String(this.expiresAt));
672
+ }
673
+ /** @internal Clear all session data */
674
+ clearSession() {
675
+ this.accessToken = null;
676
+ this.refreshToken_ = null;
677
+ this.expiresAt = null;
678
+ removeToken("access_token");
679
+ removeToken("refresh_token");
680
+ removeToken("expires_at");
681
+ }
682
+ /**
683
+ * Check if user is authenticated (has valid access token)
684
+ */
685
+ isAuthenticated() {
686
+ return !!this.accessToken && !this.isTokenExpired();
687
+ }
688
+ /**
689
+ * Check if current access token is expired
690
+ */
691
+ isTokenExpired() {
692
+ if (!this.expiresAt)
693
+ return true;
694
+ return Date.now() >= this.expiresAt - TOKEN_EXPIRY.REFRESH_BUFFER;
695
+ }
696
+ /**
697
+ * Get current access token
698
+ */
699
+ getAccessToken() {
700
+ return this.accessToken;
701
+ }
702
+ /**
703
+ * Get current refresh token
704
+ * @internal Used by auth module for token refresh
705
+ */
706
+ getRefreshToken() {
707
+ return this.refreshToken_;
708
+ }
709
+ /**
710
+ * Get client configuration
711
+ */
712
+ getConfig() {
713
+ return { ...this.config };
714
+ }
715
+ /**
716
+ * Set tokens externally (useful for SSR or state management integration)
717
+ */
718
+ setTokens(accessToken, refreshToken, expiresAt) {
719
+ this.accessToken = accessToken;
720
+ this.refreshToken_ = refreshToken;
721
+ this.expiresAt = expiresAt;
722
+ saveToken("access_token", accessToken);
723
+ saveToken("refresh_token", refreshToken);
724
+ saveToken("expires_at", String(expiresAt));
725
+ }
726
+ // ── HTTP Methods (Internal) ─────────────────────────────────────────
727
+ /** @internal Make a GET request */
728
+ async get(endpoint, options) {
729
+ return this.request(endpoint, { method: "GET", ...options });
730
+ }
731
+ /** @internal Make a POST request */
732
+ async post(endpoint, body, options) {
733
+ return this.request(endpoint, { method: "POST", body, ...options });
734
+ }
735
+ /**
736
+ * Core HTTP request handler with automatic token refresh
737
+ * @internal
738
+ */
739
+ async request(endpoint, options) {
740
+ const { method, body, skipAuth = false } = options;
741
+ if (!skipAuth && this.isTokenExpired() && this.refreshToken_) {
742
+ await this.ensureTokenRefreshed();
743
+ }
744
+ const url = `${this.config.baseUrl}${endpoint}`;
745
+ const headers = {
746
+ "Content-Type": "application/json",
747
+ Accept: "application/json"
748
+ };
749
+ if (!skipAuth && this.accessToken) {
750
+ headers.Authorization = `Bearer ${this.accessToken}`;
751
+ } else if (!skipAuth) {
752
+ const ak = loadToken("access_key_raw");
753
+ const tgAk = !ak && typeof window !== "undefined" ? (() => {
754
+ try {
755
+ return localStorage.getItem("talken_ak");
756
+ } catch {
757
+ return null;
758
+ }
759
+ })() : null;
760
+ const effectiveAk = ak || tgAk;
761
+ if (effectiveAk) {
762
+ headers["X-Access-Key"] = effectiveAk;
763
+ }
764
+ }
765
+ if (this.config.debug) {
766
+ console.log(`[TalkenApiClient] ${method} ${url}`);
767
+ }
768
+ try {
769
+ const controller = new AbortController();
770
+ const timeoutId = setTimeout(
771
+ () => controller.abort(),
772
+ this.config.timeout || 3e4
773
+ );
774
+ const response = await fetch(url, {
775
+ method,
776
+ headers,
777
+ body: body ? JSON.stringify(body) : void 0,
778
+ signal: controller.signal,
779
+ credentials: "include"
780
+ });
781
+ clearTimeout(timeoutId);
782
+ const text = await response.text();
783
+ let data;
784
+ if (text) {
785
+ try {
786
+ data = JSON.parse(text);
787
+ } catch {
788
+ throw new TalkenApiError(
789
+ "PARSE_ERROR",
790
+ "Invalid JSON response from server",
791
+ response.status,
792
+ { text: text.substring(0, 200) }
793
+ );
794
+ }
795
+ } else {
796
+ data = { success: true };
797
+ }
798
+ if (response.status === 401 && !skipAuth && this.refreshToken_) {
799
+ try {
800
+ await this.forceTokenRefresh();
801
+ return this.request(endpoint, { ...options, skipAuth: true });
802
+ } catch {
803
+ this.clearSession();
804
+ throw new TalkenApiError(
805
+ "TOKEN_EXPIRED",
806
+ "Session expired. Please login again.",
807
+ 401
808
+ );
809
+ }
810
+ }
811
+ if (!response.ok) {
812
+ const errCode = data.error?.code || "UNKNOWN_ERROR";
813
+ const errMsg = data.error?.message || `Request failed with status ${response.status}`;
814
+ if (this.config.debug) {
815
+ console.error(`[TalkenApiClient] Error: ${errCode} - ${errMsg}`);
816
+ }
817
+ throw new TalkenApiError(
818
+ errCode,
819
+ errMsg,
820
+ response.status,
821
+ data.error?.details || data.details
822
+ );
823
+ }
824
+ return data.data ?? data;
825
+ } catch (error) {
826
+ if (error instanceof TalkenApiError) {
827
+ throw error;
828
+ }
829
+ if (error instanceof DOMException && error.name === "AbortError") {
830
+ throw new TalkenApiError("TIMEOUT", "Request timed out", 408);
831
+ }
832
+ const errorMessage = error instanceof Error ? error.message : "Network request failed";
833
+ throw new TalkenApiError("NETWORK_ERROR", errorMessage, 0);
834
+ }
835
+ }
836
+ /**
837
+ * Ensure token is refreshed (handles concurrent requests)
838
+ */
839
+ async ensureTokenRefreshed() {
840
+ if (this.isRefreshing && this.refreshPromise) {
841
+ await this.refreshPromise;
842
+ return;
843
+ }
844
+ await this.forceTokenRefresh();
845
+ }
846
+ /**
847
+ * Force a token refresh
848
+ */
849
+ async forceTokenRefresh() {
850
+ if (this.isRefreshing && this.refreshPromise) {
851
+ await this.refreshPromise;
852
+ return;
853
+ }
854
+ this.isRefreshing = true;
855
+ this.refreshPromise = (async () => {
856
+ try {
857
+ await this.auth.refresh();
858
+ } finally {
859
+ this.isRefreshing = false;
860
+ this.refreshPromise = null;
861
+ }
862
+ })();
863
+ await this.refreshPromise;
864
+ }
865
+ };
866
+ function createTalkenApiClient(config) {
867
+ return new TalkenApiClient(config);
868
+ }
869
+
870
+ export {
871
+ TalkenApiClient,
872
+ createTalkenApiClient
873
+ };