@rebasepro/client 0.0.1-canary.09e5ec5

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,2279 @@
1
+ (function(global, factory) {
2
+ typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("@rebasepro/types"), require("@rebasepro/utils")) : typeof define === "function" && define.amd ? define(["exports", "@rebasepro/types", "@rebasepro/utils"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global["Rebase Client"] = {}, global.types, global.utils));
3
+ })(this, function(exports2, types, utils) {
4
+ "use strict";
5
+ function rebaseReviver(_key, value) {
6
+ if (value && typeof value === "object" && "__type" in value) {
7
+ const record = value;
8
+ switch (record.__type) {
9
+ case "date":
10
+ case "Date": {
11
+ if (typeof record.value !== "string") {
12
+ return value;
13
+ }
14
+ const date = new Date(record.value);
15
+ return isNaN(date.getTime()) ? null : date;
16
+ }
17
+ case "reference":
18
+ case "EntityReference":
19
+ return new types.EntityReference({
20
+ id: String(record.id),
21
+ path: record.path,
22
+ driver: record.driver,
23
+ databaseId: record.databaseId
24
+ });
25
+ case "relation":
26
+ case "EntityRelation":
27
+ return new types.EntityRelation(
28
+ record.id,
29
+ record.path,
30
+ record.data
31
+ );
32
+ case "GeoPoint":
33
+ return new types.GeoPoint(record.latitude, record.longitude);
34
+ case "Vector":
35
+ return new types.Vector(record.value);
36
+ default:
37
+ return value;
38
+ }
39
+ }
40
+ return value;
41
+ }
42
+ class RebaseApiError extends Error {
43
+ status;
44
+ code;
45
+ details;
46
+ constructor(status, message, code, details) {
47
+ super(message);
48
+ this.name = "RebaseApiError";
49
+ this.status = status;
50
+ this.code = code;
51
+ this.details = details;
52
+ }
53
+ }
54
+ const OP_MAP = {
55
+ "==": "eq",
56
+ "!=": "neq",
57
+ ">": "gt",
58
+ ">=": "gte",
59
+ "<": "lt",
60
+ "<=": "lte",
61
+ "not-in": "nin",
62
+ "array-contains": "cs",
63
+ "array-contains-any": "csa"
64
+ };
65
+ function normalizeWhereValue(value) {
66
+ if (value === null) return "eq.null";
67
+ if (typeof value === "boolean") return `eq.${value}`;
68
+ if (typeof value === "number") return String(value);
69
+ if (Array.isArray(value) && value.length === 2) {
70
+ const [rawOp, val] = value;
71
+ const op = OP_MAP[rawOp] ?? rawOp;
72
+ if (val === null) return `${op}.null`;
73
+ if (Array.isArray(val)) return `${op}.(${val.join(",")})`;
74
+ return `${op}.${val}`;
75
+ }
76
+ return String(value);
77
+ }
78
+ function buildQueryString(params) {
79
+ if (!params) return "";
80
+ const parts = [];
81
+ if (params.limit != null) parts.push(`limit=${params.limit}`);
82
+ if (params.offset != null) parts.push(`offset=${params.offset}`);
83
+ if (params.page != null) parts.push(`page=${params.page}`);
84
+ if (params.orderBy) {
85
+ parts.push(`orderBy=${encodeURIComponent(params.orderBy)}`);
86
+ }
87
+ if (params.searchString) {
88
+ parts.push(`searchString=${encodeURIComponent(params.searchString)}`);
89
+ }
90
+ if (params.include && params.include.length > 0) {
91
+ parts.push(`include=${encodeURIComponent(params.include.join(","))}`);
92
+ }
93
+ if (params.where) {
94
+ for (const [field, value] of Object.entries(params.where)) {
95
+ const normalized = normalizeWhereValue(value);
96
+ parts.push(`${encodeURIComponent(field)}=${encodeURIComponent(normalized)}`);
97
+ }
98
+ }
99
+ return parts.length > 0 ? "?" + parts.join("&") : "";
100
+ }
101
+ function createTransport(config) {
102
+ const fetchFn = config.fetch || globalThis.fetch;
103
+ const apiPath = config.apiPath || "/api";
104
+ let token = config.token;
105
+ let tokenGetter;
106
+ let onUnauthorizedHandler = config.onUnauthorized;
107
+ function getHeaders(activeToken, init) {
108
+ return {
109
+ "Content-Type": "application/json",
110
+ ...activeToken ? { Authorization: `Bearer ${activeToken}` } : {},
111
+ ...init?.headers || {}
112
+ };
113
+ }
114
+ async function request(path, init) {
115
+ const base = config.baseUrl ? config.baseUrl.replace(/\/$/, "") : "";
116
+ const url = base + apiPath + path;
117
+ let activeToken = token;
118
+ if (tokenGetter) {
119
+ try {
120
+ const fetched = await tokenGetter();
121
+ if (fetched !== null && fetched !== void 0) {
122
+ activeToken = fetched;
123
+ }
124
+ } catch (e) {
125
+ }
126
+ }
127
+ const headers = getHeaders(activeToken, init);
128
+ if (init?.body instanceof FormData) {
129
+ delete headers["Content-Type"];
130
+ }
131
+ const res = await fetchFn(url, {
132
+ ...init,
133
+ headers
134
+ });
135
+ if (res.status === 204) return void 0;
136
+ const text = await res.text().catch(() => "");
137
+ let body = {};
138
+ if (text) {
139
+ try {
140
+ body = JSON.parse(text, rebaseReviver);
141
+ } catch (e) {
142
+ }
143
+ }
144
+ if (res.status === 401 && onUnauthorizedHandler) {
145
+ const retried = await onUnauthorizedHandler();
146
+ if (retried) {
147
+ let retryToken = token;
148
+ if (tokenGetter) {
149
+ try {
150
+ const fetched = await tokenGetter();
151
+ if (fetched !== null && fetched !== void 0) {
152
+ retryToken = fetched;
153
+ }
154
+ } catch (e) {
155
+ }
156
+ }
157
+ const retryHeaders = getHeaders(retryToken, init);
158
+ const retryRes = await fetchFn(url, {
159
+ ...init,
160
+ headers: retryHeaders
161
+ });
162
+ if (retryRes.status === 204) return void 0;
163
+ const retryText = await retryRes.text().catch(() => "");
164
+ let retryBody = {};
165
+ if (retryText) {
166
+ try {
167
+ retryBody = JSON.parse(retryText, rebaseReviver);
168
+ } catch (e) {
169
+ }
170
+ }
171
+ if (!retryRes.ok) {
172
+ let fallbackMessage = retryRes.statusText;
173
+ if (retryRes.status === 404 && !fallbackMessage) {
174
+ const method = init?.method || "GET";
175
+ fallbackMessage = `Endpoint not found (${method} ${path}). This usually means the collection is not registered on the backend, or the frontend API URL configuration (e.g. VITE_API_URL) is missing or pointing to the wrong host.`;
176
+ }
177
+ throw new RebaseApiError(
178
+ retryRes.status,
179
+ retryBody?.error?.message || retryBody?.message || fallbackMessage || `Request failed with status ${retryRes.status}`,
180
+ retryBody?.error?.code || retryBody?.code,
181
+ retryBody?.error?.details || retryBody?.details
182
+ );
183
+ }
184
+ return retryBody;
185
+ }
186
+ }
187
+ if (!res.ok) {
188
+ let fallbackMessage = res.statusText;
189
+ if (res.status === 404 && !fallbackMessage) {
190
+ const method = init?.method || "GET";
191
+ fallbackMessage = `Endpoint not found (${method} ${path}). This usually means the collection is not registered on the backend, or the frontend API URL configuration (e.g. VITE_API_URL) is missing or pointing to the wrong host.`;
192
+ }
193
+ throw new RebaseApiError(
194
+ res.status,
195
+ body?.error?.message || body?.message || fallbackMessage || `Request failed with status ${res.status}`,
196
+ body?.error?.code || body?.code,
197
+ body?.error?.details || body?.details
198
+ );
199
+ }
200
+ return body;
201
+ }
202
+ return {
203
+ request,
204
+ setToken(newToken) {
205
+ token = newToken || void 0;
206
+ },
207
+ setAuthTokenGetter(getter) {
208
+ tokenGetter = getter;
209
+ },
210
+ setOnUnauthorized(handler) {
211
+ onUnauthorizedHandler = handler;
212
+ },
213
+ get baseUrl() {
214
+ return config.baseUrl ? config.baseUrl.replace(/\/$/, "") : "";
215
+ },
216
+ get apiPath() {
217
+ return apiPath;
218
+ },
219
+ get fetchFn() {
220
+ return fetchFn;
221
+ },
222
+ getHeaders: (init) => getHeaders(token, init),
223
+ resolveToken: async () => {
224
+ if (tokenGetter) {
225
+ try {
226
+ const fetched = await tokenGetter();
227
+ if (fetched !== null && fetched !== void 0) {
228
+ return fetched;
229
+ }
230
+ } catch (e) {
231
+ }
232
+ }
233
+ return token || null;
234
+ }
235
+ };
236
+ }
237
+ function createMemoryStorage() {
238
+ const store = {};
239
+ return {
240
+ getItem(key) {
241
+ return store[key] ?? null;
242
+ },
243
+ setItem(key, value) {
244
+ store[key] = value;
245
+ },
246
+ removeItem(key) {
247
+ delete store[key];
248
+ }
249
+ };
250
+ }
251
+ function detectStorage() {
252
+ try {
253
+ if (typeof localStorage !== "undefined") {
254
+ localStorage.setItem("__rebase_test__", "1");
255
+ localStorage.removeItem("__rebase_test__");
256
+ return localStorage;
257
+ }
258
+ } catch (e) {
259
+ }
260
+ return createMemoryStorage();
261
+ }
262
+ function createAuth(transport, options) {
263
+ const opts = options || {};
264
+ const storage = opts.storage || detectStorage();
265
+ const authPath = opts.authPath || "/auth";
266
+ const autoRefresh = opts.autoRefresh !== false;
267
+ const persistSession = opts.persistSession !== false;
268
+ const STORAGE_KEY = "rebase_auth";
269
+ const REFRESH_BUFFER_MS = 12e4;
270
+ let currentSession = null;
271
+ const listeners = /* @__PURE__ */ new Set();
272
+ let refreshTimeout = null;
273
+ function authUrl(endpoint) {
274
+ return transport.baseUrl + transport.apiPath + authPath + endpoint;
275
+ }
276
+ function getFetch() {
277
+ return transport.fetchFn || globalThis.fetch;
278
+ }
279
+ function throwApiError(status, body, statusText) {
280
+ throw new RebaseApiError(
281
+ status,
282
+ body?.error?.message || body?.message || statusText,
283
+ body?.error?.code || body?.code,
284
+ body?.error?.details || body?.details
285
+ );
286
+ }
287
+ function emit(event, session) {
288
+ for (const fn of listeners) {
289
+ try {
290
+ fn(event, session);
291
+ } catch (e) {
292
+ }
293
+ }
294
+ }
295
+ function saveSession(session) {
296
+ if (!persistSession) return;
297
+ try {
298
+ storage.setItem(STORAGE_KEY, JSON.stringify(session));
299
+ } catch (e) {
300
+ }
301
+ }
302
+ function clearStoredSession() {
303
+ try {
304
+ storage.removeItem(STORAGE_KEY);
305
+ } catch (e) {
306
+ }
307
+ }
308
+ function loadStoredSession() {
309
+ try {
310
+ const raw = storage.getItem(STORAGE_KEY);
311
+ if (raw) return JSON.parse(raw);
312
+ } catch (e) {
313
+ }
314
+ return null;
315
+ }
316
+ function scheduleRefresh(expiresAt) {
317
+ if (refreshTimeout) clearTimeout(refreshTimeout);
318
+ if (!autoRefresh) return;
319
+ const delay = expiresAt - REFRESH_BUFFER_MS - Date.now();
320
+ if (delay <= 0) {
321
+ refreshSession().catch(() => signOut());
322
+ return;
323
+ }
324
+ refreshTimeout = setTimeout(async () => {
325
+ try {
326
+ await refreshSession();
327
+ } catch (e) {
328
+ signOut();
329
+ }
330
+ }, delay);
331
+ }
332
+ function handleAuthResponse(data, event) {
333
+ const session = {
334
+ accessToken: data.tokens.accessToken,
335
+ refreshToken: data.tokens.refreshToken,
336
+ expiresAt: data.tokens.accessTokenExpiresAt,
337
+ user: data.user
338
+ };
339
+ currentSession = session;
340
+ saveSession(session);
341
+ transport.setToken(session.accessToken);
342
+ scheduleRefresh(session.expiresAt);
343
+ emit(event, session);
344
+ return session;
345
+ }
346
+ async function signInWithEmail(email, password) {
347
+ const fetchFn = getFetch();
348
+ const res = await fetchFn(authUrl("/login"), {
349
+ method: "POST",
350
+ headers: { "Content-Type": "application/json" },
351
+ body: JSON.stringify({
352
+ email,
353
+ password
354
+ })
355
+ });
356
+ const body = await res.json().catch(() => ({}));
357
+ if (!res.ok) throwApiError(res.status, body, res.statusText);
358
+ const session = handleAuthResponse(body, "SIGNED_IN");
359
+ return {
360
+ user: session.user,
361
+ accessToken: session.accessToken,
362
+ refreshToken: session.refreshToken
363
+ };
364
+ }
365
+ async function signUp(email, password, displayName) {
366
+ const fetchFn = getFetch();
367
+ const payload = {
368
+ email,
369
+ password
370
+ };
371
+ if (displayName !== void 0) payload.displayName = displayName;
372
+ const res = await fetchFn(authUrl("/register"), {
373
+ method: "POST",
374
+ headers: { "Content-Type": "application/json" },
375
+ body: JSON.stringify(payload)
376
+ });
377
+ const body = await res.json().catch(() => ({}));
378
+ if (!res.ok) throwApiError(res.status, body, res.statusText);
379
+ const session = handleAuthResponse(body, "SIGNED_IN");
380
+ return {
381
+ user: session.user,
382
+ accessToken: session.accessToken,
383
+ refreshToken: session.refreshToken
384
+ };
385
+ }
386
+ async function signInWithGoogle(idToken) {
387
+ const fetchFn = getFetch();
388
+ const res = await fetchFn(authUrl("/google"), {
389
+ method: "POST",
390
+ headers: { "Content-Type": "application/json" },
391
+ body: JSON.stringify({ idToken })
392
+ });
393
+ const body = await res.json().catch(() => ({}));
394
+ if (!res.ok) throwApiError(res.status, body, res.statusText);
395
+ const session = handleAuthResponse(body, "SIGNED_IN");
396
+ return {
397
+ user: session.user,
398
+ accessToken: session.accessToken,
399
+ refreshToken: session.refreshToken
400
+ };
401
+ }
402
+ async function signInWithLinkedin(code, redirectUri) {
403
+ const fetchFn = getFetch();
404
+ const res = await fetchFn(authUrl("/linkedin"), {
405
+ method: "POST",
406
+ headers: { "Content-Type": "application/json" },
407
+ body: JSON.stringify({
408
+ code,
409
+ redirectUri
410
+ })
411
+ });
412
+ const body = await res.json().catch(() => ({}));
413
+ if (!res.ok) throwApiError(res.status, body, res.statusText);
414
+ const session = handleAuthResponse(body, "SIGNED_IN");
415
+ return {
416
+ user: session.user,
417
+ accessToken: session.accessToken,
418
+ refreshToken: session.refreshToken
419
+ };
420
+ }
421
+ async function signInWithOAuth(providerId, payload) {
422
+ const fetchFn = getFetch();
423
+ const res = await fetchFn(authUrl(`/${providerId}`), {
424
+ method: "POST",
425
+ headers: { "Content-Type": "application/json" },
426
+ body: JSON.stringify(payload)
427
+ });
428
+ const body = await res.json().catch(() => ({}));
429
+ if (!res.ok) throwApiError(res.status, body, res.statusText);
430
+ const session = handleAuthResponse(body, "SIGNED_IN");
431
+ return {
432
+ user: session.user,
433
+ accessToken: session.accessToken,
434
+ refreshToken: session.refreshToken
435
+ };
436
+ }
437
+ async function signInWithGitHub(code, redirectUri) {
438
+ return signInWithOAuth("github", {
439
+ code,
440
+ redirectUri
441
+ });
442
+ }
443
+ async function signInWithMicrosoft(code, redirectUri) {
444
+ return signInWithOAuth("microsoft", {
445
+ code,
446
+ redirectUri
447
+ });
448
+ }
449
+ async function signInWithApple(code, redirectUri, user) {
450
+ return signInWithOAuth("apple", {
451
+ code,
452
+ redirectUri,
453
+ user
454
+ });
455
+ }
456
+ async function signInWithFacebook(code, redirectUri) {
457
+ return signInWithOAuth("facebook", {
458
+ code,
459
+ redirectUri
460
+ });
461
+ }
462
+ async function signInWithTwitter(code, redirectUri, codeVerifier) {
463
+ return signInWithOAuth("twitter", {
464
+ code,
465
+ redirectUri,
466
+ codeVerifier
467
+ });
468
+ }
469
+ async function signInWithDiscord(code, redirectUri) {
470
+ return signInWithOAuth("discord", {
471
+ code,
472
+ redirectUri
473
+ });
474
+ }
475
+ async function signInWithGitLab(code, redirectUri) {
476
+ return signInWithOAuth("gitlab", {
477
+ code,
478
+ redirectUri
479
+ });
480
+ }
481
+ async function signInWithBitbucket(code, redirectUri) {
482
+ return signInWithOAuth("bitbucket", {
483
+ code,
484
+ redirectUri
485
+ });
486
+ }
487
+ async function signInWithSlack(code, redirectUri) {
488
+ return signInWithOAuth("slack", {
489
+ code,
490
+ redirectUri
491
+ });
492
+ }
493
+ async function signInWithSpotify(code, redirectUri) {
494
+ return signInWithOAuth("spotify", {
495
+ code,
496
+ redirectUri
497
+ });
498
+ }
499
+ async function signOut() {
500
+ const fetchFn = getFetch();
501
+ try {
502
+ if (currentSession?.refreshToken) {
503
+ await fetchFn(authUrl("/logout"), {
504
+ method: "POST",
505
+ headers: { "Content-Type": "application/json" },
506
+ body: JSON.stringify({ refreshToken: currentSession.refreshToken })
507
+ });
508
+ }
509
+ } catch (e) {
510
+ }
511
+ currentSession = null;
512
+ clearStoredSession();
513
+ if (refreshTimeout) {
514
+ clearTimeout(refreshTimeout);
515
+ refreshTimeout = null;
516
+ }
517
+ transport.setToken(null);
518
+ emit("SIGNED_OUT", null);
519
+ }
520
+ async function refreshSession() {
521
+ if (!currentSession?.refreshToken) {
522
+ throw new Error("No active session to refresh");
523
+ }
524
+ const fetchFn = getFetch();
525
+ const res = await fetchFn(authUrl("/refresh"), {
526
+ method: "POST",
527
+ headers: { "Content-Type": "application/json" },
528
+ body: JSON.stringify({ refreshToken: currentSession.refreshToken })
529
+ });
530
+ const body = await res.json().catch(() => ({}));
531
+ if (!res.ok) throwApiError(res.status, body, res.statusText);
532
+ const session = {
533
+ accessToken: body.tokens.accessToken,
534
+ refreshToken: body.tokens.refreshToken,
535
+ expiresAt: body.tokens.accessTokenExpiresAt,
536
+ user: currentSession.user
537
+ };
538
+ currentSession = session;
539
+ saveSession(session);
540
+ transport.setToken(session.accessToken);
541
+ scheduleRefresh(session.expiresAt);
542
+ emit("TOKEN_REFRESHED", session);
543
+ return session;
544
+ }
545
+ async function getUser() {
546
+ const data = await transport.request(authPath + "/me", { method: "GET" });
547
+ return data.user;
548
+ }
549
+ async function updateUser(updates) {
550
+ const data = await transport.request(authPath + "/me", {
551
+ method: "PATCH",
552
+ body: JSON.stringify(updates)
553
+ });
554
+ if (currentSession) {
555
+ currentSession = {
556
+ ...currentSession,
557
+ user: data.user
558
+ };
559
+ saveSession(currentSession);
560
+ emit("USER_UPDATED", currentSession);
561
+ }
562
+ return data.user;
563
+ }
564
+ async function resetPasswordForEmail(email) {
565
+ const fetchFn = getFetch();
566
+ const res = await fetchFn(authUrl("/forgot-password"), {
567
+ method: "POST",
568
+ headers: { "Content-Type": "application/json" },
569
+ body: JSON.stringify({ email })
570
+ });
571
+ const body = await res.json().catch(() => ({}));
572
+ if (!res.ok) throwApiError(res.status, body, res.statusText);
573
+ return body;
574
+ }
575
+ async function resetPassword(token, password) {
576
+ const fetchFn = getFetch();
577
+ const res = await fetchFn(authUrl("/reset-password"), {
578
+ method: "POST",
579
+ headers: { "Content-Type": "application/json" },
580
+ body: JSON.stringify({
581
+ token,
582
+ password
583
+ })
584
+ });
585
+ const body = await res.json().catch(() => ({}));
586
+ if (!res.ok) throwApiError(res.status, body, res.statusText);
587
+ return body;
588
+ }
589
+ async function changePassword(oldPassword, newPassword) {
590
+ return transport.request(authPath + "/change-password", {
591
+ method: "POST",
592
+ body: JSON.stringify({
593
+ oldPassword,
594
+ newPassword
595
+ })
596
+ });
597
+ }
598
+ async function sendVerificationEmail() {
599
+ return transport.request(authPath + "/send-verification", {
600
+ method: "POST"
601
+ });
602
+ }
603
+ async function verifyEmail(token) {
604
+ const fetchFn = getFetch();
605
+ const res = await fetchFn(authUrl("/verify-email?token=" + encodeURIComponent(token)), {
606
+ method: "GET",
607
+ headers: { "Content-Type": "application/json" }
608
+ });
609
+ const body = await res.json().catch(() => ({}));
610
+ if (!res.ok) throwApiError(res.status, body, res.statusText);
611
+ return body;
612
+ }
613
+ async function getSessions() {
614
+ const data = await transport.request(authPath + "/sessions", { method: "GET" });
615
+ return data.sessions;
616
+ }
617
+ async function revokeSession(sessionId) {
618
+ return transport.request(authPath + "/sessions/" + encodeURIComponent(sessionId), {
619
+ method: "DELETE"
620
+ });
621
+ }
622
+ async function revokeAllSessions() {
623
+ const result = await transport.request(authPath + "/sessions", {
624
+ method: "DELETE"
625
+ });
626
+ currentSession = null;
627
+ clearStoredSession();
628
+ if (refreshTimeout) {
629
+ clearTimeout(refreshTimeout);
630
+ refreshTimeout = null;
631
+ }
632
+ transport.setToken(null);
633
+ emit("SIGNED_OUT", null);
634
+ return result;
635
+ }
636
+ async function getAuthConfig() {
637
+ const fetchFn = getFetch();
638
+ const res = await fetchFn(authUrl("/config"), {
639
+ method: "GET",
640
+ headers: { "Content-Type": "application/json" }
641
+ });
642
+ const body = await res.json().catch(() => ({}));
643
+ if (!res.ok) throwApiError(res.status, body, res.statusText);
644
+ return body;
645
+ }
646
+ function getSession() {
647
+ return currentSession;
648
+ }
649
+ function onAuthStateChange(callback) {
650
+ listeners.add(callback);
651
+ return () => listeners.delete(callback);
652
+ }
653
+ if (persistSession) {
654
+ const stored = loadStoredSession();
655
+ if (stored && stored.accessToken && stored.refreshToken) {
656
+ if (stored.expiresAt > Date.now()) {
657
+ currentSession = stored;
658
+ transport.setToken(stored.accessToken);
659
+ scheduleRefresh(stored.expiresAt);
660
+ } else if (stored.refreshToken) {
661
+ currentSession = stored;
662
+ refreshSession().catch(() => {
663
+ currentSession = null;
664
+ clearStoredSession();
665
+ transport.setToken(null);
666
+ });
667
+ }
668
+ }
669
+ }
670
+ return {
671
+ signInWithEmail,
672
+ signUp,
673
+ signInWithGoogle,
674
+ signInWithLinkedin,
675
+ signInWithOAuth,
676
+ signInWithGitHub,
677
+ signInWithMicrosoft,
678
+ signInWithApple,
679
+ signInWithFacebook,
680
+ signInWithTwitter,
681
+ signInWithDiscord,
682
+ signInWithGitLab,
683
+ signInWithBitbucket,
684
+ signInWithSlack,
685
+ signInWithSpotify,
686
+ signOut,
687
+ refreshSession,
688
+ getUser,
689
+ updateUser,
690
+ resetPasswordForEmail,
691
+ resetPassword,
692
+ changePassword,
693
+ sendVerificationEmail,
694
+ verifyEmail,
695
+ getSessions,
696
+ revokeSession,
697
+ revokeAllSessions,
698
+ getAuthConfig,
699
+ getSession,
700
+ onAuthStateChange
701
+ };
702
+ }
703
+ function createAdmin(transport, options) {
704
+ const opts = options || {};
705
+ const adminPath = opts.adminPath || "/admin";
706
+ async function listUsers() {
707
+ return transport.request(adminPath + "/users", { method: "GET" });
708
+ }
709
+ async function listUsersPaginated(options2) {
710
+ const params = new URLSearchParams();
711
+ if (options2?.limit !== void 0) params.set("limit", String(options2.limit));
712
+ if (options2?.offset !== void 0) params.set("offset", String(options2.offset));
713
+ if (options2?.search) params.set("search", options2.search);
714
+ if (options2?.orderBy) params.set("orderBy", options2.orderBy);
715
+ if (options2?.orderDir) params.set("orderDir", options2.orderDir);
716
+ const qs = params.toString();
717
+ return transport.request(
718
+ adminPath + "/users" + (qs ? "?" + qs : ""),
719
+ { method: "GET" }
720
+ );
721
+ }
722
+ async function getUser(userId) {
723
+ return transport.request(adminPath + "/users/" + encodeURIComponent(userId), { method: "GET" });
724
+ }
725
+ async function createUser(data) {
726
+ return transport.request(adminPath + "/users", {
727
+ method: "POST",
728
+ body: JSON.stringify(data)
729
+ });
730
+ }
731
+ async function updateUser(userId, data) {
732
+ return transport.request(adminPath + "/users/" + encodeURIComponent(userId), {
733
+ method: "PUT",
734
+ body: JSON.stringify(data)
735
+ });
736
+ }
737
+ async function deleteUser(userId) {
738
+ return transport.request(adminPath + "/users/" + encodeURIComponent(userId), {
739
+ method: "DELETE"
740
+ });
741
+ }
742
+ async function listRoles() {
743
+ return transport.request(adminPath + "/roles", { method: "GET" });
744
+ }
745
+ async function getRole(roleId) {
746
+ return transport.request(adminPath + "/roles/" + encodeURIComponent(roleId), { method: "GET" });
747
+ }
748
+ async function createRole(data) {
749
+ return transport.request(adminPath + "/roles", {
750
+ method: "POST",
751
+ body: JSON.stringify(data)
752
+ });
753
+ }
754
+ async function updateRole(roleId, data) {
755
+ return transport.request(adminPath + "/roles/" + encodeURIComponent(roleId), {
756
+ method: "PUT",
757
+ body: JSON.stringify(data)
758
+ });
759
+ }
760
+ async function deleteRole(roleId) {
761
+ return transport.request(adminPath + "/roles/" + encodeURIComponent(roleId), {
762
+ method: "DELETE"
763
+ });
764
+ }
765
+ async function bootstrap() {
766
+ return transport.request(adminPath + "/bootstrap", {
767
+ method: "POST"
768
+ });
769
+ }
770
+ return {
771
+ listUsers,
772
+ listUsersPaginated,
773
+ getUser,
774
+ createUser,
775
+ updateUser,
776
+ deleteUser,
777
+ listRoles,
778
+ getRole,
779
+ createRole,
780
+ updateRole,
781
+ deleteRole,
782
+ bootstrap
783
+ };
784
+ }
785
+ function createCron(transport, options) {
786
+ const cronPath = options?.cronPath || "/cron";
787
+ async function listJobs() {
788
+ return transport.request(cronPath, { method: "GET" });
789
+ }
790
+ async function getJob(jobId) {
791
+ return transport.request(
792
+ cronPath + "/" + encodeURIComponent(jobId),
793
+ { method: "GET" }
794
+ );
795
+ }
796
+ async function triggerJob(jobId) {
797
+ return transport.request(
798
+ cronPath + "/" + encodeURIComponent(jobId) + "/trigger",
799
+ { method: "POST" }
800
+ );
801
+ }
802
+ async function getJobLogs(jobId, options2) {
803
+ const params = new URLSearchParams();
804
+ if (options2?.limit !== void 0) params.set("limit", String(options2.limit));
805
+ const qs = params.toString();
806
+ return transport.request(
807
+ cronPath + "/" + encodeURIComponent(jobId) + "/logs" + (qs ? "?" + qs : ""),
808
+ { method: "GET" }
809
+ );
810
+ }
811
+ async function toggleJob(jobId, enabled) {
812
+ return transport.request(
813
+ cronPath + "/" + encodeURIComponent(jobId),
814
+ {
815
+ method: "PUT",
816
+ body: JSON.stringify({ enabled })
817
+ }
818
+ );
819
+ }
820
+ return {
821
+ listJobs,
822
+ getJob,
823
+ triggerJob,
824
+ getJobLogs,
825
+ toggleJob
826
+ };
827
+ }
828
+ function mapOperator(op) {
829
+ switch (op) {
830
+ case "==":
831
+ return "eq";
832
+ case "!=":
833
+ return "neq";
834
+ case ">":
835
+ return "gt";
836
+ case ">=":
837
+ return "gte";
838
+ case "<":
839
+ return "lt";
840
+ case "<=":
841
+ return "lte";
842
+ case "array-contains":
843
+ return "cs";
844
+ case "array-contains-any":
845
+ return "csa";
846
+ case "not-in":
847
+ return "nin";
848
+ default:
849
+ return op;
850
+ }
851
+ }
852
+ class QueryBuilder {
853
+ constructor(collection) {
854
+ this.collection = collection;
855
+ }
856
+ params = { where: {} };
857
+ /**
858
+ * Add a filter condition to your query.
859
+ * @example
860
+ * client.collection('users').where('age', '>=', 18).find()
861
+ */
862
+ where(column, operator, value) {
863
+ if (!this.params.where) {
864
+ this.params.where = {};
865
+ }
866
+ const mappedOp = mapOperator(operator);
867
+ let formattedValue = value;
868
+ if (Array.isArray(value) && ["in", "nin", "cs", "csa"].includes(mappedOp)) {
869
+ formattedValue = `(${value.join(",")})`;
870
+ } else if (value === null) {
871
+ formattedValue = "null";
872
+ }
873
+ this.params.where[column] = mappedOp === "eq" ? String(formattedValue) : `${mappedOp}.${formattedValue}`;
874
+ return this;
875
+ }
876
+ /**
877
+ * Order the results by a specific column.
878
+ * @example
879
+ * client.collection('users').orderBy('createdAt', 'desc').find()
880
+ */
881
+ orderBy(column, ascending = "asc") {
882
+ this.params.orderBy = `${column}:${ascending}`;
883
+ return this;
884
+ }
885
+ /**
886
+ * Limit the number of results returned.
887
+ */
888
+ limit(count) {
889
+ this.params.limit = count;
890
+ return this;
891
+ }
892
+ /**
893
+ * Skip the first N results.
894
+ */
895
+ offset(count) {
896
+ this.params.offset = count;
897
+ return this;
898
+ }
899
+ /**
900
+ * Set a free-text search string if supported by the backend.
901
+ */
902
+ search(searchString) {
903
+ this.params.searchString = searchString;
904
+ return this;
905
+ }
906
+ /**
907
+ * Include related entities in the response.
908
+ * Relations will be populated with full entity data instead of just IDs.
909
+ *
910
+ * @param relations - Relation names to include, or "*" for all.
911
+ * @example
912
+ * // Include specific relations
913
+ * client.data.posts.include("tags", "author").find()
914
+ *
915
+ * // Include all relations
916
+ * client.data.posts.include("*").find()
917
+ */
918
+ include(...relations) {
919
+ this.params.include = relations;
920
+ return this;
921
+ }
922
+ /**
923
+ * Execute the find query and return the results.
924
+ */
925
+ async find() {
926
+ return this.collection.find(this.params);
927
+ }
928
+ /**
929
+ * Listen to realtime updates matching this query.
930
+ */
931
+ listen(onUpdate, onError) {
932
+ if (!this.collection.listen) {
933
+ throw new Error("Listen is only available when RebaseClient is configured with a websocketUrl.");
934
+ }
935
+ return this.collection.listen(this.params, onUpdate, onError);
936
+ }
937
+ }
938
+ function parseWhereFilter(where) {
939
+ if (!where) return void 0;
940
+ const filters = {};
941
+ for (const [key, rawValue] of Object.entries(where)) {
942
+ if (rawValue === null) {
943
+ filters[key] = ["==", null];
944
+ continue;
945
+ }
946
+ if (typeof rawValue === "boolean") {
947
+ filters[key] = ["==", rawValue];
948
+ continue;
949
+ }
950
+ if (typeof rawValue === "number") {
951
+ filters[key] = ["==", rawValue];
952
+ continue;
953
+ }
954
+ if (Array.isArray(rawValue) && rawValue.length === 2) {
955
+ const [rawOp, val] = rawValue;
956
+ const OP_TO_FILTER = {
957
+ "eq": "==",
958
+ "neq": "!=",
959
+ "gt": ">",
960
+ "gte": ">=",
961
+ "lt": "<",
962
+ "lte": "<=",
963
+ "==": "==",
964
+ "!=": "!=",
965
+ ">": ">",
966
+ ">=": ">=",
967
+ "<": "<",
968
+ "<=": "<=",
969
+ "in": "in",
970
+ "nin": "not-in",
971
+ "not-in": "not-in",
972
+ "cs": "array-contains",
973
+ "csa": "array-contains-any",
974
+ "array-contains": "array-contains",
975
+ "array-contains-any": "array-contains-any"
976
+ };
977
+ filters[key] = [OP_TO_FILTER[rawOp] ?? "==", val];
978
+ continue;
979
+ }
980
+ const value = String(rawValue);
981
+ const dotIndex = value.indexOf(".");
982
+ if (dotIndex > 0) {
983
+ const opStr = value.substring(0, dotIndex);
984
+ const valStr = value.substring(dotIndex + 1);
985
+ let op = "==";
986
+ let val = valStr;
987
+ switch (opStr) {
988
+ case "eq":
989
+ op = "==";
990
+ break;
991
+ case "neq":
992
+ op = "!=";
993
+ break;
994
+ case "gt":
995
+ op = ">";
996
+ break;
997
+ case "gte":
998
+ op = ">=";
999
+ break;
1000
+ case "lt":
1001
+ op = "<";
1002
+ break;
1003
+ case "lte":
1004
+ op = "<=";
1005
+ break;
1006
+ case "in":
1007
+ op = "in";
1008
+ val = valStr.startsWith("(") && valStr.endsWith(")") ? valStr.slice(1, -1).split(",").map((v) => v.trim()) : valStr.split(",");
1009
+ break;
1010
+ case "nin":
1011
+ op = "not-in";
1012
+ val = valStr.startsWith("(") && valStr.endsWith(")") ? valStr.slice(1, -1).split(",").map((v) => v.trim()) : valStr.split(",");
1013
+ break;
1014
+ case "cs":
1015
+ op = "array-contains";
1016
+ break;
1017
+ case "csa":
1018
+ op = "array-contains-any";
1019
+ val = valStr.startsWith("(") && valStr.endsWith(")") ? valStr.slice(1, -1).split(",").map((v) => v.trim()) : valStr.split(",");
1020
+ break;
1021
+ default:
1022
+ op = "==";
1023
+ val = value;
1024
+ }
1025
+ if (val === "true") val = true;
1026
+ else if (val === "false") val = false;
1027
+ else if (val === "null") val = null;
1028
+ else if (typeof val === "string" && /^[0-9]+(\.[0-9]+)?$/.test(val) && key !== "id" && !key.endsWith("_id")) val = Number(val);
1029
+ filters[key] = [op, val];
1030
+ } else {
1031
+ filters[key] = ["==", value];
1032
+ }
1033
+ }
1034
+ return filters;
1035
+ }
1036
+ function rowToEntity(row, slug) {
1037
+ return {
1038
+ id: row.id,
1039
+ path: slug,
1040
+ values: row
1041
+ };
1042
+ }
1043
+ function createCollectionClient(transport, slug, ws) {
1044
+ const basePath = `/data/${slug}`;
1045
+ const client = {
1046
+ async find(params) {
1047
+ const qs = buildQueryString(params);
1048
+ const raw = await transport.request(basePath + qs, { method: "GET" });
1049
+ return {
1050
+ data: (raw.data || []).map((row) => rowToEntity(row, slug)),
1051
+ meta: raw.meta
1052
+ };
1053
+ },
1054
+ async findById(id) {
1055
+ const raw = await transport.request(`${basePath}/${encodeURIComponent(String(id))}`, { method: "GET" });
1056
+ if (!raw) return void 0;
1057
+ return rowToEntity(raw, slug);
1058
+ },
1059
+ async create(data, id) {
1060
+ const body = { ...data };
1061
+ if (id !== void 0) {
1062
+ body.id = id;
1063
+ }
1064
+ const raw = await transport.request(basePath, {
1065
+ method: "POST",
1066
+ body: JSON.stringify(body)
1067
+ });
1068
+ return rowToEntity(raw, slug);
1069
+ },
1070
+ async update(id, data) {
1071
+ const raw = await transport.request(`${basePath}/${encodeURIComponent(String(id))}`, {
1072
+ method: "PUT",
1073
+ body: JSON.stringify(data)
1074
+ });
1075
+ return rowToEntity(raw, slug);
1076
+ },
1077
+ async delete(id) {
1078
+ return transport.request(`${basePath}/${encodeURIComponent(String(id))}`, {
1079
+ method: "DELETE"
1080
+ });
1081
+ },
1082
+ // Fluent builder instantiation
1083
+ where(column, operator, value) {
1084
+ return new QueryBuilder(client).where(column, operator, value);
1085
+ },
1086
+ orderBy(column, ascending) {
1087
+ return new QueryBuilder(client).orderBy(column, ascending);
1088
+ },
1089
+ limit(count) {
1090
+ return new QueryBuilder(client).limit(count);
1091
+ },
1092
+ offset(count) {
1093
+ return new QueryBuilder(client).offset(count);
1094
+ },
1095
+ search(searchString) {
1096
+ return new QueryBuilder(client).search(searchString);
1097
+ },
1098
+ include(...relations) {
1099
+ return new QueryBuilder(client).include(...relations);
1100
+ }
1101
+ };
1102
+ if (ws) {
1103
+ client.listen = (params, onUpdate, onError) => {
1104
+ return ws.listenCollection(
1105
+ {
1106
+ path: slug,
1107
+ filter: parseWhereFilter(params?.where),
1108
+ limit: params?.limit,
1109
+ startAfter: params?.offset ? String(params.offset) : void 0,
1110
+ orderBy: params?.orderBy?.split(":")[0],
1111
+ order: params?.orderBy?.split(":")[1],
1112
+ searchString: params?.searchString
1113
+ },
1114
+ (entities) => {
1115
+ const requestedLimit = params?.limit || 20;
1116
+ onUpdate({
1117
+ data: entities,
1118
+ meta: {
1119
+ total: entities.length,
1120
+ limit: requestedLimit,
1121
+ offset: params?.offset || 0,
1122
+ hasMore: entities.length >= requestedLimit
1123
+ }
1124
+ });
1125
+ },
1126
+ onError
1127
+ );
1128
+ };
1129
+ client.listenById = (id, onUpdate, onError) => {
1130
+ return ws.listenEntity(
1131
+ {
1132
+ path: slug,
1133
+ entityId: String(id)
1134
+ },
1135
+ (entity) => {
1136
+ if (entity) {
1137
+ onUpdate(entity);
1138
+ } else {
1139
+ onUpdate(void 0);
1140
+ }
1141
+ },
1142
+ onError
1143
+ );
1144
+ };
1145
+ }
1146
+ return client;
1147
+ }
1148
+ function rehydrateEntity(entity) {
1149
+ return entity;
1150
+ }
1151
+ function extractMessageError(message) {
1152
+ const payload = message.payload;
1153
+ const errPayload = payload?.error;
1154
+ const errorMessage = typeof errPayload === "object" ? errPayload.message : payload?.message || (typeof errPayload === "string" ? errPayload : void 0) || message.error || "Unknown error";
1155
+ const errorCode = typeof errPayload === "object" ? errPayload.code : payload?.code;
1156
+ return {
1157
+ errorMessage,
1158
+ errorCode
1159
+ };
1160
+ }
1161
+ class ApiError extends Error {
1162
+ code;
1163
+ error;
1164
+ constructor(message, error, code) {
1165
+ super(message);
1166
+ this.name = "ApiError";
1167
+ this.code = code;
1168
+ this.error = error;
1169
+ }
1170
+ }
1171
+ class RebaseWebSocketClient {
1172
+ websocketUrl;
1173
+ ws = null;
1174
+ getAuthToken;
1175
+ subscriptions = /* @__PURE__ */ new Map();
1176
+ listeners = /* @__PURE__ */ new Map();
1177
+ on(event, cb) {
1178
+ if (!this.listeners.has(event)) {
1179
+ this.listeners.set(event, /* @__PURE__ */ new Set());
1180
+ }
1181
+ this.listeners.get(event).add(cb);
1182
+ return () => this.listeners.get(event).delete(cb);
1183
+ }
1184
+ emit(event, ...args) {
1185
+ if (this.listeners.has(event)) {
1186
+ this.listeners.get(event).forEach((cb) => cb(...args));
1187
+ }
1188
+ }
1189
+ // New: Subscription deduplication management with optimizations
1190
+ collectionSubscriptions = /* @__PURE__ */ new Map();
1191
+ entitySubscriptions = /* @__PURE__ */ new Map();
1192
+ // Maps to quickly find subscription by backend subscription ID
1193
+ backendToCollectionKey = /* @__PURE__ */ new Map();
1194
+ backendToEntityKey = /* @__PURE__ */ new Map();
1195
+ pendingRequests = /* @__PURE__ */ new Map();
1196
+ reconnectAttempts = 0;
1197
+ maxReconnectAttempts = 5;
1198
+ isConnected = false;
1199
+ messageQueue = [];
1200
+ reconnectTimeout = null;
1201
+ isAuthenticated = false;
1202
+ authPromise = null;
1203
+ WebSocketConstructor;
1204
+ constructor(config) {
1205
+ this.websocketUrl = config.websocketUrl;
1206
+ this.getAuthToken = config.getAuthToken;
1207
+ this.WebSocketConstructor = config.WebSocket || (typeof WebSocket !== "undefined" ? WebSocket : void 0);
1208
+ if (!this.WebSocketConstructor) {
1209
+ console.warn("WebSocket is not defined in this environment. Realtime subscriptions will not work unless you provide a WebSocket implementation in the config.");
1210
+ } else {
1211
+ this.initWebSocket();
1212
+ }
1213
+ }
1214
+ /**
1215
+ * Authenticate the WebSocket connection
1216
+ */
1217
+ async authenticate(token) {
1218
+ return new Promise((resolve, reject) => {
1219
+ const requestId = `auth_${Date.now()}`;
1220
+ const timeout = setTimeout(() => {
1221
+ this.pendingRequests.delete(requestId);
1222
+ this.authPromise = null;
1223
+ reject(new Error("Authentication timeout"));
1224
+ }, 3e4);
1225
+ this.pendingRequests.set(requestId, {
1226
+ resolve: () => {
1227
+ clearTimeout(timeout);
1228
+ this.isAuthenticated = true;
1229
+ resolve();
1230
+ },
1231
+ reject: (error) => {
1232
+ clearTimeout(timeout);
1233
+ reject(error);
1234
+ }
1235
+ });
1236
+ const message = {
1237
+ type: "AUTHENTICATE",
1238
+ requestId,
1239
+ payload: { token }
1240
+ };
1241
+ if (!this.isConnected || !this.ws) {
1242
+ this.messageQueue.unshift(message);
1243
+ } else {
1244
+ this.ws.send(JSON.stringify(message));
1245
+ }
1246
+ });
1247
+ }
1248
+ /**
1249
+ * Set the auth token getter function
1250
+ */
1251
+ setAuthTokenGetter(getAuthToken) {
1252
+ this.getAuthToken = getAuthToken;
1253
+ if (this.isConnected && !this.isAuthenticated && !this.authPromise) {
1254
+ console.log("WebSocket auto-authenticating after token getter set");
1255
+ this.getAuthToken().then((token) => {
1256
+ if (!this.ws) return;
1257
+ if (token) {
1258
+ this.authenticate(token).catch((e) => {
1259
+ if (this.ws) console.warn("WebSocket auto-auth failed:", e);
1260
+ });
1261
+ }
1262
+ }).catch((e) => {
1263
+ if (this.ws) console.warn("WebSocket auto-auth failed:", e);
1264
+ });
1265
+ }
1266
+ }
1267
+ disconnect() {
1268
+ this.isAuthenticated = false;
1269
+ this.authPromise = null;
1270
+ if (this.reconnectTimeout) {
1271
+ clearTimeout(this.reconnectTimeout);
1272
+ this.reconnectTimeout = null;
1273
+ }
1274
+ if (this.ws) {
1275
+ this.ws.onclose = null;
1276
+ this.ws.onerror = null;
1277
+ this.ws.onopen = null;
1278
+ this.ws.onmessage = null;
1279
+ this.ws.close();
1280
+ this.ws = null;
1281
+ }
1282
+ }
1283
+ // Initialize WebSocket connection
1284
+ initWebSocket() {
1285
+ if (!this.WebSocketConstructor) return;
1286
+ if (this.ws?.readyState === this.WebSocketConstructor.OPEN) return;
1287
+ try {
1288
+ this.ws = new this.WebSocketConstructor(this.websocketUrl);
1289
+ this.ws.onopen = async () => {
1290
+ console.log("Connected to PostgreSQL backend");
1291
+ const wasReconnect = this.reconnectAttempts > 0;
1292
+ this.isConnected = true;
1293
+ this.reconnectAttempts = 0;
1294
+ if (this.getAuthToken && !this.isAuthenticated) {
1295
+ try {
1296
+ const token = await this.getAuthToken();
1297
+ if (token) {
1298
+ await this.authenticate(token);
1299
+ console.log("WebSocket auto-authenticated");
1300
+ }
1301
+ } catch (error) {
1302
+ console.warn("WebSocket auto-auth failed, requests may fail:", error);
1303
+ }
1304
+ }
1305
+ this.emit(wasReconnect ? "reconnect" : "connect");
1306
+ this.processMessageQueue();
1307
+ if (wasReconnect) {
1308
+ this.resubscribeAll();
1309
+ }
1310
+ };
1311
+ this.ws.onmessage = (event) => {
1312
+ try {
1313
+ const message = JSON.parse(event.data, rebaseReviver);
1314
+ this.handleWebSocketMessage(message);
1315
+ } catch (error) {
1316
+ console.error("Error parsing WebSocket message:", error);
1317
+ }
1318
+ };
1319
+ this.ws.onclose = () => {
1320
+ console.log("Disconnected from PostgreSQL backend");
1321
+ this.isConnected = false;
1322
+ this.isAuthenticated = false;
1323
+ this.authPromise = null;
1324
+ this.emit("disconnect");
1325
+ for (const [reqId, request] of this.pendingRequests.entries()) {
1326
+ if (reqId.startsWith("auth_")) {
1327
+ request.reject(new Error("Connection closed during authentication"));
1328
+ } else if (request.message) {
1329
+ request.message._queuedResolve = request.resolve;
1330
+ request.message._queuedReject = request.reject;
1331
+ this.messageQueue.push(request.message);
1332
+ } else {
1333
+ request.reject(new ApiError("Connection closed", "Connection closed"));
1334
+ }
1335
+ this.pendingRequests.delete(reqId);
1336
+ }
1337
+ this.attemptReconnect();
1338
+ };
1339
+ this.ws.onerror = (error) => {
1340
+ console.error("WebSocket error:", error);
1341
+ this.isConnected = false;
1342
+ this.emit("error", error);
1343
+ };
1344
+ } catch (error) {
1345
+ console.error("Failed to initialize WebSocket:", error);
1346
+ this.attemptReconnect();
1347
+ }
1348
+ }
1349
+ processMessageQueue() {
1350
+ while (this.messageQueue.length > 0 && this.isConnected) {
1351
+ const message = this.messageQueue.shift();
1352
+ if (message) this.sendMessage(message);
1353
+ }
1354
+ }
1355
+ attemptReconnect() {
1356
+ if (this.reconnectAttempts >= this.maxReconnectAttempts) {
1357
+ console.error("Max reconnection attempts reached");
1358
+ return;
1359
+ }
1360
+ this.reconnectAttempts++;
1361
+ const delay = Math.min(1e3 * Math.pow(2, this.reconnectAttempts), 3e4);
1362
+ console.log(`Attempting to reconnect in ${delay}ms (attempt ${this.reconnectAttempts})`);
1363
+ if (this.reconnectTimeout) {
1364
+ clearTimeout(this.reconnectTimeout);
1365
+ }
1366
+ this.reconnectTimeout = setTimeout(() => {
1367
+ this.reconnectTimeout = null;
1368
+ this.initWebSocket();
1369
+ }, delay);
1370
+ }
1371
+ handleWebSocketMessage(message) {
1372
+ const {
1373
+ type,
1374
+ requestId,
1375
+ subscriptionId
1376
+ } = message;
1377
+ if (requestId && this.pendingRequests.has(requestId)) {
1378
+ const {
1379
+ resolve,
1380
+ reject
1381
+ } = this.pendingRequests.get(requestId);
1382
+ this.pendingRequests.delete(requestId);
1383
+ if (type === "ERROR" || type === "AUTH_ERROR" || message.error) {
1384
+ const { errorMessage, errorCode } = extractMessageError(message);
1385
+ reject(new ApiError(errorMessage, errorMessage, errorCode));
1386
+ } else {
1387
+ resolve(message.payload || message);
1388
+ }
1389
+ return;
1390
+ }
1391
+ if (subscriptionId && type === "collection_update") {
1392
+ const subscriptionKey = this.backendToCollectionKey.get(subscriptionId);
1393
+ if (subscriptionKey) {
1394
+ const collectionSub = this.collectionSubscriptions.get(subscriptionKey);
1395
+ if (collectionSub) {
1396
+ const incomingEntities = (message.entities || []).map((e) => rehydrateEntity(e));
1397
+ const entities = this.mergeEntities(collectionSub.latestData, incomingEntities);
1398
+ collectionSub.latestData = entities;
1399
+ collectionSub.lastUpdated = Date.now();
1400
+ collectionSub.isInitialDataReceived = true;
1401
+ collectionSub.callbacks.forEach((callback) => {
1402
+ try {
1403
+ callback.onUpdate(entities);
1404
+ } catch (error) {
1405
+ console.error("Error in collection subscription callback:", error);
1406
+ if (callback.onError) {
1407
+ callback.onError(error instanceof Error ? error : new Error(String(error)));
1408
+ }
1409
+ }
1410
+ });
1411
+ return;
1412
+ }
1413
+ }
1414
+ }
1415
+ if (subscriptionId && type === "collection_entity_patch") {
1416
+ const subscriptionKey = this.backendToCollectionKey.get(subscriptionId);
1417
+ if (subscriptionKey) {
1418
+ const collectionSub = this.collectionSubscriptions.get(subscriptionKey);
1419
+ if (collectionSub && collectionSub.isInitialDataReceived && collectionSub.latestData) {
1420
+ const patchEntity = message.entity ? rehydrateEntity(message.entity) : message.entity;
1421
+ const patchEntityId = message.entityId;
1422
+ let updated;
1423
+ if (patchEntity === null || patchEntity === void 0) {
1424
+ updated = collectionSub.latestData.filter((e) => String(e.id) !== String(patchEntityId));
1425
+ } else {
1426
+ const idx = collectionSub.latestData.findIndex((e) => String(e.id) === String(patchEntity.id));
1427
+ if (idx >= 0) {
1428
+ updated = [...collectionSub.latestData];
1429
+ updated[idx] = patchEntity;
1430
+ } else {
1431
+ updated = [patchEntity, ...collectionSub.latestData];
1432
+ }
1433
+ }
1434
+ collectionSub.latestData = updated;
1435
+ collectionSub.lastUpdated = Date.now();
1436
+ collectionSub.callbacks.forEach((callback) => {
1437
+ try {
1438
+ callback.onUpdate(updated);
1439
+ } catch (error) {
1440
+ console.error("Error in collection patch callback:", error);
1441
+ if (callback.onError) {
1442
+ callback.onError(error instanceof Error ? error : new Error(String(error)));
1443
+ }
1444
+ }
1445
+ });
1446
+ return;
1447
+ }
1448
+ }
1449
+ }
1450
+ if (subscriptionId && type === "entity_update") {
1451
+ const subscriptionKey = this.backendToEntityKey.get(subscriptionId);
1452
+ if (subscriptionKey) {
1453
+ const entitySub = this.entitySubscriptions.get(subscriptionKey);
1454
+ if (entitySub) {
1455
+ const entity = message.entity ? rehydrateEntity(message.entity) : null;
1456
+ entitySub.latestData = entity;
1457
+ entitySub.lastUpdated = Date.now();
1458
+ entitySub.isInitialDataReceived = true;
1459
+ entitySub.callbacks.forEach((callback) => {
1460
+ try {
1461
+ callback.onUpdate(entity);
1462
+ } catch (error) {
1463
+ console.error("Error in entity subscription callback:", error);
1464
+ if (callback.onError) {
1465
+ callback.onError(error instanceof Error ? error : new Error(String(error)));
1466
+ }
1467
+ }
1468
+ });
1469
+ return;
1470
+ }
1471
+ }
1472
+ }
1473
+ if (subscriptionId && (type === "ERROR" || message.error)) {
1474
+ const collectionKey = this.backendToCollectionKey.get(subscriptionId);
1475
+ if (collectionKey) {
1476
+ const collectionSub = this.collectionSubscriptions.get(collectionKey);
1477
+ if (collectionSub) {
1478
+ const { errorMessage, errorCode } = extractMessageError(message);
1479
+ const error = new ApiError(errorMessage, errorMessage, errorCode);
1480
+ collectionSub.callbacks.forEach((callback) => {
1481
+ if (callback.onError) {
1482
+ callback.onError(error);
1483
+ }
1484
+ });
1485
+ return;
1486
+ }
1487
+ }
1488
+ const entityKey = this.backendToEntityKey.get(subscriptionId);
1489
+ if (entityKey) {
1490
+ const entitySub = this.entitySubscriptions.get(entityKey);
1491
+ if (entitySub) {
1492
+ const { errorMessage, errorCode } = extractMessageError(message);
1493
+ const error = new ApiError(errorMessage, errorMessage, errorCode);
1494
+ entitySub.callbacks.forEach((callback) => {
1495
+ if (callback.onError) {
1496
+ callback.onError(error);
1497
+ }
1498
+ });
1499
+ return;
1500
+ }
1501
+ }
1502
+ }
1503
+ if (subscriptionId && this.subscriptions.has(subscriptionId)) {
1504
+ const callback = this.subscriptions.get(subscriptionId);
1505
+ if (!callback) {
1506
+ throw new Error(`Subscription callback not found for subscriptionId: ${subscriptionId}`);
1507
+ }
1508
+ if (message.type === "ERROR" || message.error) {
1509
+ if (callback.onError) {
1510
+ const { errorMessage, errorCode } = extractMessageError(message);
1511
+ callback.onError(new ApiError(errorMessage, errorMessage, errorCode));
1512
+ }
1513
+ } else {
1514
+ callback.onUpdate(message);
1515
+ }
1516
+ }
1517
+ }
1518
+ async ensureAuthenticated(retryCount = 3) {
1519
+ if (this.isAuthenticated || !this.getAuthToken) return;
1520
+ if (this.authPromise) {
1521
+ await this.authPromise;
1522
+ return;
1523
+ }
1524
+ let lastError = null;
1525
+ for (let attempt = 0; attempt < retryCount; attempt++) {
1526
+ try {
1527
+ const token = await this.getAuthToken();
1528
+ if (!token) throw new Error("user not logged in");
1529
+ this.authPromise = this.authenticate(token);
1530
+ await this.authPromise;
1531
+ this.authPromise = null;
1532
+ console.log("WebSocket authenticated on demand");
1533
+ return;
1534
+ } catch (error) {
1535
+ this.authPromise = null;
1536
+ lastError = error;
1537
+ const errMsg = error instanceof Error ? error.message : String(error);
1538
+ if (errMsg.includes("not logged in") || errMsg.includes("Session expired")) {
1539
+ console.warn("WebSocket auth failed: user not logged in");
1540
+ throw error;
1541
+ }
1542
+ if (errMsg.includes("still loading")) {
1543
+ if (attempt < retryCount - 1) {
1544
+ const delay = Math.min(500 * (attempt + 1), 2e3);
1545
+ await new Promise((resolve) => setTimeout(resolve, delay));
1546
+ continue;
1547
+ }
1548
+ }
1549
+ if (attempt < retryCount - 1) {
1550
+ const delay = Math.min(1e3 * (attempt + 1), 3e3);
1551
+ console.log(`WebSocket auth attempt ${attempt + 1} failed, retrying in ${delay}ms...`);
1552
+ await new Promise((resolve) => setTimeout(resolve, delay));
1553
+ }
1554
+ }
1555
+ }
1556
+ console.warn("WebSocket on-demand auth failed after retries:", lastError);
1557
+ throw lastError;
1558
+ }
1559
+ /**
1560
+ * Force re-authentication (call after token refresh)
1561
+ */
1562
+ async reauthenticate() {
1563
+ if (!this.getAuthToken) return;
1564
+ this.isAuthenticated = false;
1565
+ try {
1566
+ const token = await this.getAuthToken();
1567
+ await this.authenticate(token);
1568
+ console.log("WebSocket reauthenticated successfully");
1569
+ } catch (error) {
1570
+ console.error("WebSocket reauthentication failed:", error);
1571
+ throw error;
1572
+ }
1573
+ }
1574
+ sendMessage(message) {
1575
+ const queuedMsg = message;
1576
+ if (queuedMsg._queuedResolve && queuedMsg._queuedReject) {
1577
+ return this.doSendMessage(message, queuedMsg._queuedResolve, queuedMsg._queuedReject);
1578
+ }
1579
+ if (!this.isConnected || !this.ws) {
1580
+ return new Promise((resolve, reject) => {
1581
+ const queueable = message;
1582
+ queueable._queuedResolve = resolve;
1583
+ queueable._queuedReject = reject;
1584
+ this.messageQueue.push(message);
1585
+ });
1586
+ }
1587
+ return new Promise((resolve, reject) => {
1588
+ this.doSendMessage(message, resolve, reject);
1589
+ });
1590
+ }
1591
+ async doSendMessage(message, resolve, reject) {
1592
+ if (message.type !== "AUTHENTICATE" && this.getAuthToken && !this.isAuthenticated) {
1593
+ try {
1594
+ await this.ensureAuthenticated();
1595
+ } catch (error) {
1596
+ const errorMessage = error instanceof Error ? error.message : "Authentication required";
1597
+ reject(new ApiError(errorMessage, errorMessage));
1598
+ return;
1599
+ }
1600
+ }
1601
+ const requestId = message.requestId || `req_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;
1602
+ message.requestId = requestId;
1603
+ if (!this.pendingRequests.has(requestId)) {
1604
+ this.pendingRequests.set(requestId, {
1605
+ resolve,
1606
+ reject,
1607
+ message
1608
+ });
1609
+ }
1610
+ try {
1611
+ this.ws.send(JSON.stringify(message));
1612
+ } catch (error) {
1613
+ this.pendingRequests.delete(requestId);
1614
+ reject(new ApiError("Failed to send message", error instanceof Error ? error.message : "Unknown error"));
1615
+ }
1616
+ }
1617
+ // Data source methods
1618
+ async fetchCollection(props) {
1619
+ const response = await this.sendMessage({
1620
+ type: "FETCH_COLLECTION",
1621
+ payload: props
1622
+ });
1623
+ return (response.entities || []).map((e) => rehydrateEntity(e));
1624
+ }
1625
+ async fetchEntity(props) {
1626
+ const response = await this.sendMessage({
1627
+ type: "FETCH_ENTITY",
1628
+ payload: props
1629
+ });
1630
+ return response.entity ? rehydrateEntity(response.entity) : void 0;
1631
+ }
1632
+ async saveEntity(props) {
1633
+ const response = await this.sendMessage({
1634
+ type: "SAVE_ENTITY",
1635
+ payload: props
1636
+ });
1637
+ return rehydrateEntity(response.entity);
1638
+ }
1639
+ async deleteEntity(props) {
1640
+ await this.sendMessage({
1641
+ type: "DELETE_ENTITY",
1642
+ payload: props
1643
+ });
1644
+ }
1645
+ async executeSql(sql, options) {
1646
+ const response = await this.sendMessage({
1647
+ type: "EXECUTE_SQL",
1648
+ payload: {
1649
+ sql,
1650
+ options
1651
+ }
1652
+ });
1653
+ return response.result || [];
1654
+ }
1655
+ async fetchAvailableDatabases() {
1656
+ const response = await this.sendMessage({
1657
+ type: "FETCH_DATABASES",
1658
+ payload: {}
1659
+ });
1660
+ return response.databases || [];
1661
+ }
1662
+ async fetchAvailableRoles() {
1663
+ const response = await this.sendMessage({
1664
+ type: "FETCH_ROLES"
1665
+ });
1666
+ return response.roles || [];
1667
+ }
1668
+ async fetchCurrentDatabase() {
1669
+ const response = await this.sendMessage({
1670
+ type: "FETCH_CURRENT_DATABASE"
1671
+ });
1672
+ return response.database;
1673
+ }
1674
+ async checkUniqueField(path, name, value, entityId, collection) {
1675
+ const response = await this.sendMessage({
1676
+ type: "CHECK_UNIQUE_FIELD",
1677
+ payload: {
1678
+ path,
1679
+ name,
1680
+ value,
1681
+ entityId,
1682
+ collection
1683
+ }
1684
+ });
1685
+ return response.isUnique;
1686
+ }
1687
+ async countEntities(props) {
1688
+ const response = await this.sendMessage({
1689
+ type: "COUNT_ENTITIES",
1690
+ payload: props
1691
+ });
1692
+ return response.count;
1693
+ }
1694
+ async fetchUnmappedTables(mappedPaths) {
1695
+ const response = await this.sendMessage({
1696
+ type: "FETCH_UNMAPPED_TABLES",
1697
+ payload: { mappedPaths }
1698
+ });
1699
+ return response.tables || [];
1700
+ }
1701
+ async fetchTableMetadata(tableName) {
1702
+ const response = await this.sendMessage({
1703
+ type: "FETCH_TABLE_METADATA",
1704
+ payload: { tableName }
1705
+ });
1706
+ return response.metadata || {
1707
+ columns: [],
1708
+ foreignKeys: [],
1709
+ junctions: [],
1710
+ policies: []
1711
+ };
1712
+ }
1713
+ async createBranch(name, options) {
1714
+ const response = await this.sendMessage({
1715
+ type: "CREATE_BRANCH",
1716
+ payload: {
1717
+ name,
1718
+ options
1719
+ }
1720
+ });
1721
+ return response.branch;
1722
+ }
1723
+ async deleteBranch(name) {
1724
+ await this.sendMessage({
1725
+ type: "DELETE_BRANCH",
1726
+ payload: { name }
1727
+ });
1728
+ }
1729
+ async listBranches() {
1730
+ const response = await this.sendMessage({
1731
+ type: "LIST_BRANCHES",
1732
+ payload: {}
1733
+ });
1734
+ return response.branches || [];
1735
+ }
1736
+ /**
1737
+ * Recursively compare two values for structural equality.
1738
+ * Handles primitives, null, undefined, Date, RegExp, arrays, and plain objects.
1739
+ */
1740
+ deepEqual(a, b) {
1741
+ if (a === b) return true;
1742
+ if (a === null || b === null || a === void 0 || b === void 0) return false;
1743
+ if (typeof a !== typeof b) return false;
1744
+ if (typeof a !== "object") return false;
1745
+ if (a instanceof Date && b instanceof Date) {
1746
+ return a.getTime() === b.getTime();
1747
+ }
1748
+ if (a instanceof Date || b instanceof Date) return false;
1749
+ if (a instanceof RegExp && b instanceof RegExp) {
1750
+ return a.source === b.source && a.flags === b.flags;
1751
+ }
1752
+ if (a instanceof RegExp || b instanceof RegExp) return false;
1753
+ const aIsArray = Array.isArray(a);
1754
+ const bIsArray = Array.isArray(b);
1755
+ if (aIsArray !== bIsArray) return false;
1756
+ if (aIsArray && bIsArray) {
1757
+ if (a.length !== b.length) return false;
1758
+ for (let i = 0; i < a.length; i++) {
1759
+ if (!this.deepEqual(a[i], b[i])) return false;
1760
+ }
1761
+ return true;
1762
+ }
1763
+ const aObj = a;
1764
+ const bObj = b;
1765
+ const aKeys = Object.keys(aObj);
1766
+ const bKeys = Object.keys(bObj);
1767
+ if (aKeys.length !== bKeys.length) return false;
1768
+ for (const key of aKeys) {
1769
+ if (!Object.prototype.hasOwnProperty.call(bObj, key)) return false;
1770
+ if (!this.deepEqual(aObj[key], bObj[key])) return false;
1771
+ }
1772
+ return true;
1773
+ }
1774
+ normalizeForComparison(val) {
1775
+ if (!val) return val;
1776
+ if (Array.isArray(val)) {
1777
+ return val.map((item) => this.normalizeForComparison(item));
1778
+ }
1779
+ if (typeof val === "object") {
1780
+ if (val instanceof Date) return val;
1781
+ if (val instanceof RegExp) return val;
1782
+ const obj = val;
1783
+ if (obj.__type === "relation") {
1784
+ const { data, ...rest } = obj;
1785
+ return rest;
1786
+ }
1787
+ const result = {};
1788
+ for (const [k, v] of Object.entries(obj)) {
1789
+ result[k] = this.normalizeForComparison(v);
1790
+ }
1791
+ return result;
1792
+ }
1793
+ return val;
1794
+ }
1795
+ /**
1796
+ * Merge incoming entities with cached data, preserving cached references
1797
+ * for entities whose values haven't changed. This avoids unnecessary
1798
+ * React re-renders when the server refetches all entities but most
1799
+ * haven't actually changed.
1800
+ */
1801
+ mergeEntities(cached, incoming) {
1802
+ if (!cached || cached.length === 0) return incoming;
1803
+ const cachedById = /* @__PURE__ */ new Map();
1804
+ for (const entity of cached) {
1805
+ cachedById.set(entity.id, entity);
1806
+ }
1807
+ return incoming.map((incomingEntity) => {
1808
+ const cachedEntity = cachedById.get(incomingEntity.id);
1809
+ if (!cachedEntity) return incomingEntity;
1810
+ if (cachedEntity.path === incomingEntity.path) {
1811
+ const normCached = this.normalizeForComparison(cachedEntity.values);
1812
+ const normIncoming = this.normalizeForComparison(incomingEntity.values);
1813
+ if (this.deepEqual(normCached, normIncoming)) {
1814
+ return cachedEntity;
1815
+ } else {
1816
+ const mismatches = {};
1817
+ const allKeys = /* @__PURE__ */ new Set([...Object.keys(normCached), ...Object.keys(normIncoming)]);
1818
+ for (const key of allKeys) {
1819
+ if (!this.deepEqual(normCached[key], normIncoming[key])) {
1820
+ mismatches[key] = {
1821
+ cached: normCached[key],
1822
+ incoming: normIncoming[key]
1823
+ };
1824
+ }
1825
+ }
1826
+ console.log(`[RebaseWS] Row ${incomingEntity.id} refetch mismatch:
1827
+ `, JSON.stringify(mismatches, null, 2));
1828
+ }
1829
+ }
1830
+ return incomingEntity;
1831
+ });
1832
+ }
1833
+ // Subscription methods
1834
+ listenCollection(props, onUpdate, onError) {
1835
+ const subscriptionKey = this.createCollectionSubscriptionKey(props);
1836
+ const callbackId = `callback_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;
1837
+ const existingSubscription = this.collectionSubscriptions.get(subscriptionKey);
1838
+ if (existingSubscription) {
1839
+ const callbackMap2 = existingSubscription.callbacks;
1840
+ callbackMap2.set(callbackId, {
1841
+ onUpdate,
1842
+ onError
1843
+ });
1844
+ if (existingSubscription.latestData !== void 0 && existingSubscription.isInitialDataReceived) {
1845
+ try {
1846
+ onUpdate(existingSubscription.latestData);
1847
+ } catch (error) {
1848
+ console.error("Error in collection subscription callback:", error);
1849
+ if (onError) {
1850
+ onError(error instanceof Error ? error : new Error(String(error)));
1851
+ }
1852
+ }
1853
+ }
1854
+ return () => {
1855
+ callbackMap2.delete(callbackId);
1856
+ if (callbackMap2.size === 0) {
1857
+ this.collectionSubscriptions.delete(subscriptionKey);
1858
+ this.backendToCollectionKey.delete(existingSubscription.backendSubscriptionId);
1859
+ if (this.isConnected && this.ws) {
1860
+ this.sendMessage({
1861
+ type: "unsubscribe",
1862
+ payload: { subscriptionId: existingSubscription.backendSubscriptionId }
1863
+ }).catch(console.error);
1864
+ }
1865
+ }
1866
+ };
1867
+ }
1868
+ const backendSubscriptionId = `collection_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;
1869
+ const callbackMap = /* @__PURE__ */ new Map();
1870
+ callbackMap.set(callbackId, {
1871
+ onUpdate,
1872
+ onError
1873
+ });
1874
+ this.collectionSubscriptions.set(subscriptionKey, {
1875
+ backendSubscriptionId,
1876
+ callbacks: callbackMap,
1877
+ props
1878
+ });
1879
+ this.backendToCollectionKey.set(backendSubscriptionId, subscriptionKey);
1880
+ this.sendMessage({
1881
+ type: "subscribe_collection",
1882
+ payload: {
1883
+ ...props,
1884
+ subscriptionId: backendSubscriptionId
1885
+ }
1886
+ }).catch((error) => {
1887
+ if (onError) onError(error);
1888
+ });
1889
+ return () => {
1890
+ const subscription = this.collectionSubscriptions.get(subscriptionKey);
1891
+ if (subscription) {
1892
+ const callbacks = subscription.callbacks;
1893
+ callbacks.delete(callbackId);
1894
+ if (callbacks.size === 0) {
1895
+ this.collectionSubscriptions.delete(subscriptionKey);
1896
+ this.backendToCollectionKey.delete(subscription.backendSubscriptionId);
1897
+ if (this.isConnected && this.ws) {
1898
+ this.sendMessage({
1899
+ type: "unsubscribe",
1900
+ payload: { subscriptionId: subscription.backendSubscriptionId }
1901
+ }).catch(console.error);
1902
+ }
1903
+ }
1904
+ }
1905
+ };
1906
+ }
1907
+ listenEntity(props, onUpdate, onError) {
1908
+ const subscriptionKey = this.createEntitySubscriptionKey(props);
1909
+ const callbackId = `callback_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;
1910
+ const existingSubscription = this.entitySubscriptions.get(subscriptionKey);
1911
+ if (existingSubscription) {
1912
+ const callbackMap2 = existingSubscription.callbacks;
1913
+ callbackMap2.set(callbackId, {
1914
+ onUpdate,
1915
+ onError
1916
+ });
1917
+ if (existingSubscription.latestData !== void 0 && existingSubscription.isInitialDataReceived) {
1918
+ try {
1919
+ onUpdate(existingSubscription.latestData);
1920
+ } catch (error) {
1921
+ console.error("Error in entity subscription callback:", error);
1922
+ if (onError) {
1923
+ onError(error instanceof Error ? error : new Error(String(error)));
1924
+ }
1925
+ }
1926
+ }
1927
+ return () => {
1928
+ callbackMap2.delete(callbackId);
1929
+ if (callbackMap2.size === 0) {
1930
+ this.entitySubscriptions.delete(subscriptionKey);
1931
+ this.backendToEntityKey.delete(existingSubscription.backendSubscriptionId);
1932
+ if (this.isConnected && this.ws) {
1933
+ this.sendMessage({
1934
+ type: "unsubscribe",
1935
+ payload: { subscriptionId: existingSubscription.backendSubscriptionId }
1936
+ }).catch(console.error);
1937
+ }
1938
+ }
1939
+ };
1940
+ }
1941
+ const backendSubscriptionId = `entity_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;
1942
+ const callbackMap = /* @__PURE__ */ new Map();
1943
+ callbackMap.set(callbackId, {
1944
+ onUpdate,
1945
+ onError
1946
+ });
1947
+ this.entitySubscriptions.set(subscriptionKey, {
1948
+ backendSubscriptionId,
1949
+ callbacks: callbackMap,
1950
+ props
1951
+ });
1952
+ this.backendToEntityKey.set(backendSubscriptionId, subscriptionKey);
1953
+ this.sendMessage({
1954
+ type: "subscribe_entity",
1955
+ payload: {
1956
+ ...props,
1957
+ subscriptionId: backendSubscriptionId
1958
+ }
1959
+ }).catch((error) => {
1960
+ if (onError) onError(error);
1961
+ });
1962
+ return () => {
1963
+ const subscription = this.entitySubscriptions.get(subscriptionKey);
1964
+ if (subscription) {
1965
+ const callbacks = subscription.callbacks;
1966
+ callbacks.delete(callbackId);
1967
+ if (callbacks.size === 0) {
1968
+ this.entitySubscriptions.delete(subscriptionKey);
1969
+ this.backendToEntityKey.delete(subscription.backendSubscriptionId);
1970
+ if (this.isConnected && this.ws) {
1971
+ this.sendMessage({
1972
+ type: "unsubscribe",
1973
+ payload: { subscriptionId: subscription.backendSubscriptionId }
1974
+ }).catch(console.error);
1975
+ }
1976
+ }
1977
+ }
1978
+ };
1979
+ }
1980
+ /**
1981
+ * Re-send all active subscriptions to the backend after a reconnect.
1982
+ * The server wipes subscription state when a client disconnects, so
1983
+ * we need to re-register everything to resume receiving updates.
1984
+ */
1985
+ resubscribeAll() {
1986
+ console.log(`[WS] Re-subscribing: ${this.collectionSubscriptions.size} collection(s), ${this.entitySubscriptions.size} entity(ies)`);
1987
+ for (const [key, sub] of this.collectionSubscriptions.entries()) {
1988
+ const oldBackendId = sub.backendSubscriptionId;
1989
+ const newBackendId = `collection_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;
1990
+ sub.backendSubscriptionId = newBackendId;
1991
+ this.backendToCollectionKey.delete(oldBackendId);
1992
+ this.backendToCollectionKey.set(newBackendId, key);
1993
+ this.sendMessage({
1994
+ type: "subscribe_collection",
1995
+ payload: {
1996
+ ...sub.props,
1997
+ subscriptionId: newBackendId
1998
+ }
1999
+ }).catch((error) => {
2000
+ console.error("[WS] Failed to re-subscribe collection:", key, error);
2001
+ });
2002
+ }
2003
+ for (const [key, sub] of this.entitySubscriptions.entries()) {
2004
+ const oldBackendId = sub.backendSubscriptionId;
2005
+ const newBackendId = `entity_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;
2006
+ sub.backendSubscriptionId = newBackendId;
2007
+ this.backendToEntityKey.delete(oldBackendId);
2008
+ this.backendToEntityKey.set(newBackendId, key);
2009
+ this.sendMessage({
2010
+ type: "subscribe_entity",
2011
+ payload: {
2012
+ ...sub.props,
2013
+ subscriptionId: newBackendId
2014
+ }
2015
+ }).catch((error) => {
2016
+ console.error("[WS] Failed to re-subscribe entity:", key, error);
2017
+ });
2018
+ }
2019
+ }
2020
+ createCollectionSubscriptionKey(props) {
2021
+ const key = {
2022
+ path: props.path,
2023
+ filter: props.filter,
2024
+ limit: props.limit,
2025
+ startAfter: props.startAfter,
2026
+ orderBy: props.orderBy,
2027
+ order: props.order,
2028
+ searchString: props.searchString,
2029
+ collection: props.collection?.name
2030
+ };
2031
+ return JSON.stringify(key, (_, value) => {
2032
+ if (value && typeof value === "object" && !Array.isArray(value)) {
2033
+ return Object.keys(value).sort().reduce((sorted, k) => {
2034
+ sorted[k] = value[k];
2035
+ return sorted;
2036
+ }, {});
2037
+ }
2038
+ return value;
2039
+ });
2040
+ }
2041
+ createEntitySubscriptionKey(props) {
2042
+ return `${props.path}|${props.entityId}`;
2043
+ }
2044
+ }
2045
+ function createStorage(transport) {
2046
+ const urlsCache = /* @__PURE__ */ new Map();
2047
+ async function putObject({
2048
+ file,
2049
+ key,
2050
+ metadata,
2051
+ bucket
2052
+ }) {
2053
+ const formData = new FormData();
2054
+ formData.append("file", file);
2055
+ if (key) formData.append("key", key);
2056
+ if (bucket) formData.append("bucket", bucket);
2057
+ if (metadata) {
2058
+ for (const [key2, value] of Object.entries(metadata)) {
2059
+ if (value !== void 0 && value !== null) {
2060
+ formData.append(
2061
+ `metadata_${key2}`,
2062
+ typeof value === "string" ? value : JSON.stringify(value)
2063
+ );
2064
+ }
2065
+ }
2066
+ }
2067
+ const result = await transport.request("/storage/upload", {
2068
+ method: "POST",
2069
+ body: formData,
2070
+ headers: {
2071
+ // transport.request merges headers, so to prevent it setting application/json we can delete it
2072
+ // in transport if body is FormData, or we can explicitly set it to an empty string.
2073
+ // Let's rely on standard behaviour for now and adjust transport if it fails.
2074
+ }
2075
+ });
2076
+ return result.data;
2077
+ }
2078
+ async function getSignedUrl(keyOrUrl, bucket) {
2079
+ const cacheKey = bucket ? `${bucket}/${keyOrUrl}` : keyOrUrl;
2080
+ const cached = urlsCache.get(cacheKey);
2081
+ if (cached) return cached;
2082
+ let filePath = keyOrUrl;
2083
+ if (filePath && (filePath.startsWith("local://") || filePath.startsWith("s3://"))) {
2084
+ filePath = filePath.substring(filePath.indexOf("://") + 3);
2085
+ }
2086
+ if (bucket && filePath && !filePath.startsWith(bucket)) {
2087
+ filePath = `${bucket}/${filePath}`;
2088
+ }
2089
+ if (!filePath || filePath.trim() === "" || filePath === "/") {
2090
+ return {
2091
+ url: null,
2092
+ fileNotFound: true
2093
+ };
2094
+ }
2095
+ try {
2096
+ const result = await transport.request(`/storage/metadata/${filePath}`);
2097
+ const activeToken = await transport.resolveToken();
2098
+ const tokenQuery = activeToken ? `?token=${activeToken}` : "";
2099
+ const downloadConfig = {
2100
+ url: `${transport.baseUrl}${transport.apiPath}/storage/file/${filePath}${tokenQuery}`,
2101
+ metadata: result.data
2102
+ };
2103
+ urlsCache.set(cacheKey, downloadConfig);
2104
+ return downloadConfig;
2105
+ } catch (e) {
2106
+ if (e instanceof Error && "status" in e && e.status === 404) {
2107
+ return {
2108
+ url: null,
2109
+ fileNotFound: true
2110
+ };
2111
+ }
2112
+ throw e;
2113
+ }
2114
+ }
2115
+ async function getObject(key, bucket) {
2116
+ let filePath = key;
2117
+ if (filePath && (filePath.startsWith("local://") || filePath.startsWith("s3://"))) {
2118
+ filePath = filePath.substring(filePath.indexOf("://") + 3);
2119
+ }
2120
+ if (bucket && filePath && !filePath.startsWith(bucket)) {
2121
+ filePath = `${bucket}/${filePath}`;
2122
+ }
2123
+ if (!filePath || filePath.trim() === "" || filePath === "/") {
2124
+ return null;
2125
+ }
2126
+ const url = `${transport.baseUrl}${transport.apiPath}/storage/file/${filePath}`;
2127
+ const response = await transport.fetchFn(url, {
2128
+ headers: transport.getHeaders ? transport.getHeaders() : {}
2129
+ });
2130
+ if (response.status === 404) return null;
2131
+ if (!response.ok) throw new Error("Failed to get file");
2132
+ const blob = await response.blob();
2133
+ const fileName = filePath.split("/").pop() || "file";
2134
+ return new File([blob], fileName, { type: blob.type });
2135
+ }
2136
+ async function deleteObject(key, bucket) {
2137
+ let filePath = key;
2138
+ if (filePath && (filePath.startsWith("local://") || filePath.startsWith("s3://"))) {
2139
+ filePath = filePath.substring(filePath.indexOf("://") + 3);
2140
+ }
2141
+ if (bucket && filePath && !filePath.startsWith(bucket)) {
2142
+ filePath = `${bucket}/${filePath}`;
2143
+ }
2144
+ if (!filePath || filePath.trim() === "" || filePath === "/") {
2145
+ return;
2146
+ }
2147
+ try {
2148
+ await transport.request(`/storage/file/${filePath}`, { method: "DELETE" });
2149
+ } catch (e) {
2150
+ if (!(e instanceof Error && "status" in e && e.status === 404)) throw e;
2151
+ }
2152
+ urlsCache.delete(bucket ? `${bucket}/${key}` : key);
2153
+ }
2154
+ async function listObjects(prefix, options) {
2155
+ const params = new URLSearchParams();
2156
+ if (prefix) params.set("prefix", prefix);
2157
+ if (options?.bucket) params.set("bucket", options.bucket);
2158
+ if (options?.maxResults) params.set("maxResults", String(options.maxResults));
2159
+ if (options?.pageToken) params.set("pageToken", options.pageToken);
2160
+ const result = await transport.request(`/storage/list?${params.toString()}`);
2161
+ return result.data;
2162
+ }
2163
+ return {
2164
+ putObject,
2165
+ getSignedUrl,
2166
+ getObject,
2167
+ deleteObject,
2168
+ listObjects
2169
+ };
2170
+ }
2171
+ function deriveWebSocketUrl(baseUrl) {
2172
+ if (!baseUrl) {
2173
+ if (typeof window !== "undefined") {
2174
+ const protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
2175
+ return `${protocol}//${window.location.host}`;
2176
+ }
2177
+ return "";
2178
+ }
2179
+ return baseUrl.replace(/^https:\/\//, "wss://").replace(/^http:\/\//, "ws://").replace(/\/$/, "");
2180
+ }
2181
+ function createRebaseClient(options) {
2182
+ const transport = createTransport(options);
2183
+ const auth = createAuth(transport, options.auth);
2184
+ const admin = createAdmin(transport, options.admin);
2185
+ const cron = createCron(transport, options.cron);
2186
+ const storage = createStorage(transport);
2187
+ const resolvedWsUrl = options.websocketUrl ?? deriveWebSocketUrl(options.baseUrl);
2188
+ let ws;
2189
+ if (resolvedWsUrl) {
2190
+ ws = new RebaseWebSocketClient({
2191
+ websocketUrl: resolvedWsUrl,
2192
+ getAuthToken: async () => {
2193
+ const session = await auth.getSession();
2194
+ return session?.accessToken || options.token || "";
2195
+ }
2196
+ });
2197
+ auth.onAuthStateChange((event, session) => {
2198
+ if (!ws) return;
2199
+ if (event === "SIGNED_OUT") {
2200
+ ws.disconnect();
2201
+ } else if (event === "SIGNED_IN" || event === "TOKEN_REFRESHED") {
2202
+ if (session?.accessToken) {
2203
+ ws.authenticate(session.accessToken).catch(console.warn);
2204
+ }
2205
+ }
2206
+ });
2207
+ }
2208
+ if (!options.onUnauthorized) {
2209
+ transport.setOnUnauthorized(async () => {
2210
+ try {
2211
+ await auth.refreshSession();
2212
+ return true;
2213
+ } catch (e) {
2214
+ return false;
2215
+ }
2216
+ });
2217
+ }
2218
+ const collectionClients = /* @__PURE__ */ new Map();
2219
+ function collection(slug) {
2220
+ if (!collectionClients.has(slug)) {
2221
+ collectionClients.set(slug, createCollectionClient(transport, slug, ws));
2222
+ }
2223
+ return collectionClients.get(slug);
2224
+ }
2225
+ const dataTarget = { collection };
2226
+ const dataProxy = new Proxy(dataTarget, {
2227
+ get(_target, prop) {
2228
+ if (prop === "collection") {
2229
+ return collection;
2230
+ }
2231
+ if (typeof prop === "symbol") return void 0;
2232
+ if (typeof prop === "string" && prop !== "then" && prop !== "toJSON" && prop !== "$$typeof") {
2233
+ const slug = utils.toSnakeCase(prop);
2234
+ return collection(slug);
2235
+ }
2236
+ return void 0;
2237
+ }
2238
+ });
2239
+ const target = {
2240
+ auth,
2241
+ admin,
2242
+ cron,
2243
+ storage,
2244
+ ws,
2245
+ setToken: transport.setToken,
2246
+ setAuthTokenGetter: transport.setAuthTokenGetter,
2247
+ setOnUnauthorized: transport.setOnUnauthorized,
2248
+ resolveToken: transport.resolveToken,
2249
+ baseUrl: transport.baseUrl,
2250
+ collection,
2251
+ call: async (endpoint, payload) => {
2252
+ const prefix = endpoint.startsWith("/") ? "" : "/";
2253
+ const res = await transport.request(`${prefix}${endpoint}`, {
2254
+ method: "POST",
2255
+ body: payload ? JSON.stringify(payload) : void 0
2256
+ });
2257
+ return res.data ?? res;
2258
+ },
2259
+ data: dataProxy,
2260
+ email: void 0
2261
+ };
2262
+ return target;
2263
+ }
2264
+ exports2.ApiError = ApiError;
2265
+ exports2.RebaseApiError = RebaseApiError;
2266
+ exports2.RebaseWebSocketClient = RebaseWebSocketClient;
2267
+ exports2.buildQueryString = buildQueryString;
2268
+ exports2.createAdmin = createAdmin;
2269
+ exports2.createAuth = createAuth;
2270
+ exports2.createCollectionClient = createCollectionClient;
2271
+ exports2.createCron = createCron;
2272
+ exports2.createMemoryStorage = createMemoryStorage;
2273
+ exports2.createRebaseClient = createRebaseClient;
2274
+ exports2.createStorage = createStorage;
2275
+ exports2.createTransport = createTransport;
2276
+ exports2.rebaseReviver = rebaseReviver;
2277
+ Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
2278
+ });
2279
+ //# sourceMappingURL=index.umd.js.map