@insforge/sdk 1.0.5-dev.3 → 1.0.5
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 +9 -1
- package/dist/index.d.ts +9 -1
- package/dist/index.js +34 -3
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +34 -3
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -163,6 +163,8 @@ var TokenManager = class {
|
|
|
163
163
|
this.user = null;
|
|
164
164
|
// Mode: 'memory' (new backend) or 'storage' (legacy backend, default)
|
|
165
165
|
this._mode = "storage";
|
|
166
|
+
// Callback for token changes (used by realtime to reconnect with new token)
|
|
167
|
+
this.onTokenChange = null;
|
|
166
168
|
if (storage) {
|
|
167
169
|
this.storage = storage;
|
|
168
170
|
} else if (typeof window !== "undefined" && window.localStorage) {
|
|
@@ -223,12 +225,16 @@ var TokenManager = class {
|
|
|
223
225
|
* Save session (memory always, localStorage only in storage mode)
|
|
224
226
|
*/
|
|
225
227
|
saveSession(session) {
|
|
228
|
+
const tokenChanged = session.accessToken !== this.accessToken;
|
|
226
229
|
this.accessToken = session.accessToken;
|
|
227
230
|
this.user = session.user;
|
|
228
231
|
if (this._mode === "storage") {
|
|
229
232
|
this.storage.setItem(TOKEN_KEY, session.accessToken);
|
|
230
233
|
this.storage.setItem(USER_KEY, JSON.stringify(session.user));
|
|
231
234
|
}
|
|
235
|
+
if (tokenChanged && this.onTokenChange) {
|
|
236
|
+
this.onTokenChange();
|
|
237
|
+
}
|
|
232
238
|
}
|
|
233
239
|
/**
|
|
234
240
|
* Get current session
|
|
@@ -252,10 +258,14 @@ var TokenManager = class {
|
|
|
252
258
|
* Set access token
|
|
253
259
|
*/
|
|
254
260
|
setAccessToken(token) {
|
|
261
|
+
const tokenChanged = token !== this.accessToken;
|
|
255
262
|
this.accessToken = token;
|
|
256
263
|
if (this._mode === "storage") {
|
|
257
264
|
this.storage.setItem(TOKEN_KEY, token);
|
|
258
265
|
}
|
|
266
|
+
if (tokenChanged && this.onTokenChange) {
|
|
267
|
+
this.onTokenChange();
|
|
268
|
+
}
|
|
259
269
|
}
|
|
260
270
|
/**
|
|
261
271
|
* Get user
|
|
@@ -276,10 +286,14 @@ var TokenManager = class {
|
|
|
276
286
|
* Clear session (both memory and localStorage)
|
|
277
287
|
*/
|
|
278
288
|
clearSession() {
|
|
289
|
+
const hadToken = this.accessToken !== null;
|
|
279
290
|
this.accessToken = null;
|
|
280
291
|
this.user = null;
|
|
281
292
|
this.storage.removeItem(TOKEN_KEY);
|
|
282
293
|
this.storage.removeItem(USER_KEY);
|
|
294
|
+
if (hadToken && this.onTokenChange) {
|
|
295
|
+
this.onTokenChange();
|
|
296
|
+
}
|
|
283
297
|
}
|
|
284
298
|
/**
|
|
285
299
|
* Check if there's a session in localStorage (for legacy detection)
|
|
@@ -1468,13 +1482,15 @@ var Functions = class {
|
|
|
1468
1482
|
import { io } from "socket.io-client";
|
|
1469
1483
|
var CONNECT_TIMEOUT = 1e4;
|
|
1470
1484
|
var Realtime = class {
|
|
1471
|
-
constructor(baseUrl, tokenManager) {
|
|
1485
|
+
constructor(baseUrl, tokenManager, anonKey) {
|
|
1472
1486
|
this.socket = null;
|
|
1473
1487
|
this.connectPromise = null;
|
|
1474
1488
|
this.subscribedChannels = /* @__PURE__ */ new Set();
|
|
1475
1489
|
this.eventListeners = /* @__PURE__ */ new Map();
|
|
1476
1490
|
this.baseUrl = baseUrl;
|
|
1477
1491
|
this.tokenManager = tokenManager;
|
|
1492
|
+
this.anonKey = anonKey;
|
|
1493
|
+
this.tokenManager.onTokenChange = () => this.onTokenChange();
|
|
1478
1494
|
}
|
|
1479
1495
|
notifyListeners(event, payload) {
|
|
1480
1496
|
const listeners = this.eventListeners.get(event);
|
|
@@ -1500,7 +1516,7 @@ var Realtime = class {
|
|
|
1500
1516
|
}
|
|
1501
1517
|
this.connectPromise = new Promise((resolve, reject) => {
|
|
1502
1518
|
const session = this.tokenManager.getSession();
|
|
1503
|
-
const token = session?.accessToken;
|
|
1519
|
+
const token = session?.accessToken ?? this.anonKey;
|
|
1504
1520
|
this.socket = io(this.baseUrl, {
|
|
1505
1521
|
transports: ["websocket"],
|
|
1506
1522
|
auth: token ? { token } : void 0
|
|
@@ -1566,6 +1582,21 @@ var Realtime = class {
|
|
|
1566
1582
|
}
|
|
1567
1583
|
this.subscribedChannels.clear();
|
|
1568
1584
|
}
|
|
1585
|
+
/**
|
|
1586
|
+
* Handle token changes (e.g., after auth refresh)
|
|
1587
|
+
* Updates socket auth so reconnects use the new token
|
|
1588
|
+
* If connected, triggers reconnect to apply new token immediately
|
|
1589
|
+
*/
|
|
1590
|
+
onTokenChange() {
|
|
1591
|
+
const token = this.tokenManager.getAccessToken() ?? this.anonKey;
|
|
1592
|
+
if (this.socket) {
|
|
1593
|
+
this.socket.auth = token ? { token } : {};
|
|
1594
|
+
}
|
|
1595
|
+
if (this.socket && (this.socket.connected || this.connectPromise)) {
|
|
1596
|
+
this.socket.disconnect();
|
|
1597
|
+
this.socket.connect();
|
|
1598
|
+
}
|
|
1599
|
+
}
|
|
1569
1600
|
/**
|
|
1570
1601
|
* Check if connected to the realtime server
|
|
1571
1602
|
*/
|
|
@@ -1742,7 +1773,7 @@ var InsForgeClient = class {
|
|
|
1742
1773
|
this.storage = new Storage(this.http);
|
|
1743
1774
|
this.ai = new AI(this.http);
|
|
1744
1775
|
this.functions = new Functions(this.http);
|
|
1745
|
-
this.realtime = new Realtime(this.http.baseUrl, this.tokenManager);
|
|
1776
|
+
this.realtime = new Realtime(this.http.baseUrl, this.tokenManager, config.anonKey);
|
|
1746
1777
|
this.emails = new Emails(this.http);
|
|
1747
1778
|
}
|
|
1748
1779
|
/**
|