@rebasepro/client 0.0.1-canary.4d4fb3e

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