superso-js-sdk 1.0.1

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,15 @@
1
+ export class SupersoClient {
2
+ constructor(config) {
3
+ this.projectId =
4
+ config.projectId;
5
+ this.apiKey =
6
+ config.apiKey;
7
+ this.baseUrl =
8
+ config.baseUrl ||
9
+ "http://localhost:8080";
10
+ }
11
+ }
12
+ // initialize sdk
13
+ export function createClient(config) {
14
+ return new SupersoClient(config);
15
+ }
@@ -0,0 +1,53 @@
1
+ import { SupersoClient } from "../core/client";
2
+ export interface QueryOptions {
3
+ limit?: number;
4
+ offset?: number;
5
+ sort?: string;
6
+ order?: "asc" | "desc";
7
+ filters?: Record<string, any>;
8
+ }
9
+ export interface AdvancedQuery {
10
+ where?: Record<string, any>;
11
+ orderBy?: {
12
+ field: string;
13
+ dir: "asc" | "desc";
14
+ }[];
15
+ limit?: number;
16
+ offset?: number;
17
+ search?: string;
18
+ cursor?: string;
19
+ after?: string;
20
+ }
21
+ export declare function createDocument(client: SupersoClient, collection: string, data: any, auth?: boolean): Promise<any>;
22
+ export declare function getDocuments(client: SupersoClient, collection: string, query?: QueryOptions, auth?: boolean): Promise<any>;
23
+ export declare function getDocument(client: SupersoClient, collection: string, documentId: string, auth?: boolean): Promise<any>;
24
+ export declare function updateDocument(client: SupersoClient, collection: string, documentId: string, data: any, auth?: boolean): Promise<any>;
25
+ export declare function replaceDocument(client: SupersoClient, collection: string, documentId: string, data: any, auth?: boolean): Promise<any>;
26
+ export declare function deleteDocument(client: SupersoClient, collection: string, documentId: string, auth?: boolean): Promise<any>;
27
+ export declare function restoreDocument(client: SupersoClient, collection: string, documentId: string, auth?: boolean): Promise<any>;
28
+ export declare function purgeDocument(client: SupersoClient, collection: string, documentId: string, auth?: boolean): Promise<any>;
29
+ export declare function upsertDocument(client: SupersoClient, collection: string, documentId: string, data: any, auth?: boolean): Promise<any>;
30
+ export declare function existsDocument(client: SupersoClient, collection: string, documentId: string, auth?: boolean): Promise<boolean>;
31
+ export declare function countDocuments(client: SupersoClient, collection: string, query?: QueryOptions, auth?: boolean): Promise<any>;
32
+ export declare function queryDocuments(client: SupersoClient, collection: string, query: AdvancedQuery, auth?: boolean): Promise<any>;
33
+ export declare function searchDocuments(client: SupersoClient, collection: string, search: string, auth?: boolean): Promise<any>;
34
+ export declare function paginateDocuments(client: SupersoClient, collection: string, query: AdvancedQuery, auth?: boolean): Promise<any>;
35
+ export declare function infiniteScroll(client: SupersoClient, collection: string, cursor: string, limit?: number, auth?: boolean): Promise<any>;
36
+ export declare function aggregateDocuments(client: SupersoClient, collection: string, payload: any, auth?: boolean): Promise<any>;
37
+ export declare function distinctValues(client: SupersoClient, collection: string, field: string, auth?: boolean): Promise<any>;
38
+ export declare function getCollectionStats(client: SupersoClient, collection: string, auth?: boolean): Promise<any>;
39
+ export declare function getDeletedDocuments(client: SupersoClient, collection: string, auth?: boolean): Promise<any>;
40
+ export declare function getFieldVisibility(client: SupersoClient, collection: string, auth?: boolean): Promise<any>;
41
+ export declare function setFieldVisibility(client: SupersoClient, collection: string, rules: any, auth?: boolean): Promise<any>;
42
+ export declare function bulkCreate(client: SupersoClient, collection: string, documents: any[], auth?: boolean): Promise<any>;
43
+ export declare function bulkUpdate(client: SupersoClient, collection: string, updates: any[], auth?: boolean): Promise<any>;
44
+ export declare function bulkDelete(client: SupersoClient, collection: string, ids: string[], auth?: boolean): Promise<any>;
45
+ export declare function transaction(client: SupersoClient, operations: any[], auth?: boolean): Promise<any>;
46
+ export declare class WriteBatch {
47
+ private operations;
48
+ create(collection: string, data: any): this;
49
+ update(collection: string, documentId: string, data: any): this;
50
+ delete(collection: string, documentId: string): this;
51
+ commit(client: SupersoClient): Promise<any>;
52
+ }
53
+ export declare function batch(): WriteBatch;
@@ -0,0 +1,285 @@
1
+ // ================================
2
+ // TOKEN
3
+ // ================================
4
+ const ACCESS_TOKEN_KEY = "superso_access_token";
5
+ // ================================
6
+ // REQUEST
7
+ // ================================
8
+ async function request(client, endpoint, method = "GET", body = null, auth = false) {
9
+ const headers = {
10
+ "Content-Type": "application/json",
11
+ "X-Superso-Project-Key": client.apiKey,
12
+ };
13
+ // auth token
14
+ const token = localStorage.getItem(ACCESS_TOKEN_KEY);
15
+ if (auth && token) {
16
+ headers["Authorization"] =
17
+ `Bearer ${token}`;
18
+ }
19
+ const response = await fetch(`${client.baseUrl}/api/project/${client.projectId}${endpoint}`, {
20
+ method,
21
+ headers,
22
+ body: body
23
+ ? JSON.stringify(body)
24
+ : undefined,
25
+ });
26
+ let data = {};
27
+ try {
28
+ data =
29
+ await response.json();
30
+ }
31
+ catch (_a) {
32
+ data = {};
33
+ }
34
+ if (!response.ok) {
35
+ throw new Error((data === null || data === void 0 ? void 0 : data.message) ||
36
+ "Database request failed");
37
+ }
38
+ return data;
39
+ }
40
+ // ================================
41
+ // QUERY BUILDER
42
+ // ================================
43
+ function buildQuery(query) {
44
+ if (!query)
45
+ return "";
46
+ const params = new URLSearchParams();
47
+ if (query.limit !==
48
+ undefined) {
49
+ params.append("limit", String(query.limit));
50
+ }
51
+ if (query.offset !==
52
+ undefined) {
53
+ params.append("offset", String(query.offset));
54
+ }
55
+ if (query.sort) {
56
+ params.append("sort", query.sort);
57
+ }
58
+ if (query.order) {
59
+ params.append("order", query.order);
60
+ }
61
+ // filters
62
+ if (query.filters) {
63
+ Object.entries(query.filters).forEach(([key, value]) => {
64
+ params.append(key, String(value));
65
+ });
66
+ }
67
+ const qs = params.toString();
68
+ return qs
69
+ ? `?${qs}`
70
+ : "";
71
+ }
72
+ // ================================
73
+ // CREATE
74
+ // ================================
75
+ export async function createDocument(client, collection, data, auth = false) {
76
+ return await request(client, `/db/${collection}`, "POST", data, auth);
77
+ }
78
+ // ================================
79
+ // LIST
80
+ // ================================
81
+ export async function getDocuments(client, collection, query, auth = false) {
82
+ return await request(client, `/db/${collection}${buildQuery(query)}`, "GET", null, auth);
83
+ }
84
+ // ================================
85
+ // GET SINGLE
86
+ // ================================
87
+ export async function getDocument(client, collection, documentId, auth = false) {
88
+ return await request(client, `/db/${collection}/${documentId}`, "GET", null, auth);
89
+ }
90
+ // ================================
91
+ // UPDATE
92
+ // ================================
93
+ export async function updateDocument(client, collection, documentId, data, auth = false) {
94
+ return await request(client, `/db/${collection}/${documentId}`, "PATCH", data, auth);
95
+ }
96
+ // ================================
97
+ // REPLACE
98
+ // ================================
99
+ export async function replaceDocument(client, collection, documentId, data, auth = false) {
100
+ return await request(client, `/db/${collection}/${documentId}`, "PUT", data, auth);
101
+ }
102
+ // ================================
103
+ // DELETE
104
+ // ================================
105
+ export async function deleteDocument(client, collection, documentId, auth = false) {
106
+ return await request(client, `/db/${collection}/${documentId}`, "DELETE", null, auth);
107
+ }
108
+ // ================================
109
+ // RESTORE
110
+ // ================================
111
+ export async function restoreDocument(client, collection, documentId, auth = false) {
112
+ return await request(client, `/db/${collection}/${documentId}/restore`, "POST", null, auth);
113
+ }
114
+ // ================================
115
+ // PURGE
116
+ // ================================
117
+ export async function purgeDocument(client, collection, documentId, auth = false) {
118
+ return await request(client, `/db/${collection}/${documentId}/purge`, "DELETE", null, auth);
119
+ }
120
+ // ================================
121
+ // UPSERT
122
+ // ================================
123
+ export async function upsertDocument(client, collection, documentId, data, auth = false) {
124
+ return await request(client, `/db/${collection}/upsert/${documentId}`, "PUT", data, auth);
125
+ }
126
+ // ================================
127
+ // EXISTS
128
+ // ================================
129
+ export async function existsDocument(client, collection, documentId, auth = false) {
130
+ const headers = {
131
+ "X-Superso-Project-Key": client.apiKey,
132
+ };
133
+ const token = localStorage.getItem(ACCESS_TOKEN_KEY);
134
+ if (auth && token) {
135
+ headers["Authorization"] =
136
+ `Bearer ${token}`;
137
+ }
138
+ const response = await fetch(`${client.baseUrl}/api/project/${client.projectId}/db/${collection}/${documentId}/exists`, {
139
+ method: "HEAD",
140
+ headers,
141
+ });
142
+ return response.ok;
143
+ }
144
+ // ================================
145
+ // COUNT
146
+ // ================================
147
+ export async function countDocuments(client, collection, query, auth = false) {
148
+ return await request(client, `/db/${collection}/count${buildQuery(query)}`, "GET", null, auth);
149
+ }
150
+ // ================================
151
+ // ADVANCED QUERY
152
+ // ================================
153
+ export async function queryDocuments(client, collection, query, auth = false) {
154
+ return await request(client, `/db/${collection}/query`, "POST", query, auth);
155
+ }
156
+ // ================================
157
+ // SEARCH
158
+ // ================================
159
+ export async function searchDocuments(client, collection, search, auth = false) {
160
+ return await queryDocuments(client, collection, {
161
+ search,
162
+ }, auth);
163
+ }
164
+ // ================================
165
+ // CURSOR PAGINATION
166
+ // ================================
167
+ export async function paginateDocuments(client, collection, query, auth = false) {
168
+ return await queryDocuments(client, collection, query, auth);
169
+ }
170
+ // ================================
171
+ // INFINITE SCROLL
172
+ // ================================
173
+ export async function infiniteScroll(client, collection, cursor, limit = 20, auth = false) {
174
+ return await queryDocuments(client, collection, {
175
+ cursor,
176
+ limit,
177
+ }, auth);
178
+ }
179
+ // ================================
180
+ // AGGREGATION
181
+ // ================================
182
+ export async function aggregateDocuments(client, collection, payload, auth = false) {
183
+ return await request(client, `/db/${collection}/aggregation`, "POST", payload, auth);
184
+ }
185
+ // ================================
186
+ // DISTINCT
187
+ // ================================
188
+ export async function distinctValues(client, collection, field, auth = false) {
189
+ return await request(client, `/db/${collection}/distinct?field=${field}`, "GET", null, auth);
190
+ }
191
+ // ================================
192
+ // COLLECTION STATS
193
+ // ================================
194
+ export async function getCollectionStats(client, collection, auth = false) {
195
+ return await request(client, `/db/${collection}/stats`, "GET", null, auth);
196
+ }
197
+ // ================================
198
+ // DELETED DOCUMENTS
199
+ // ================================
200
+ export async function getDeletedDocuments(client, collection, auth = false) {
201
+ return await request(client, `/db/${collection}/deleted`, "GET", null, auth);
202
+ }
203
+ // ================================
204
+ // FIELD VISIBILITY
205
+ // ================================
206
+ export async function getFieldVisibility(client, collection, auth = false) {
207
+ return await request(client, `/db/${collection}/field-visibility`, "GET", null, auth);
208
+ }
209
+ export async function setFieldVisibility(client, collection, rules, auth = false) {
210
+ return await request(client, `/db/${collection}/field-visibility`, "PUT", rules, auth);
211
+ }
212
+ // ================================
213
+ // BULK CREATE
214
+ // ================================
215
+ export async function bulkCreate(client, collection, documents, auth = false) {
216
+ return await request(client, `/db/${collection}/bulk-insert`, "POST", {
217
+ documents,
218
+ }, auth);
219
+ }
220
+ // ================================
221
+ // BULK UPDATE
222
+ // ================================
223
+ export async function bulkUpdate(client, collection, updates, auth = false) {
224
+ return await request(client, `/db/${collection}/bulk-update`, "PUT", {
225
+ updates,
226
+ }, auth);
227
+ }
228
+ // ================================
229
+ // BULK DELETE
230
+ // ================================
231
+ export async function bulkDelete(client, collection, ids, auth = false) {
232
+ return await request(client, `/db/${collection}/bulk-delete`, "DELETE", {
233
+ ids,
234
+ }, auth);
235
+ }
236
+ // ================================
237
+ // TRANSACTIONS
238
+ // ================================
239
+ export async function transaction(client, operations, auth = true) {
240
+ return await request(client, `/transactions`, "POST", {
241
+ operations,
242
+ }, auth);
243
+ }
244
+ // ================================
245
+ // WRITE BATCH
246
+ // ================================
247
+ export class WriteBatch {
248
+ constructor() {
249
+ this.operations = [];
250
+ }
251
+ create(collection, data) {
252
+ this.operations.push({
253
+ type: "create",
254
+ collection,
255
+ data,
256
+ });
257
+ return this;
258
+ }
259
+ update(collection, documentId, data) {
260
+ this.operations.push({
261
+ type: "update",
262
+ collection,
263
+ documentId,
264
+ data,
265
+ });
266
+ return this;
267
+ }
268
+ delete(collection, documentId) {
269
+ this.operations.push({
270
+ type: "delete",
271
+ collection,
272
+ documentId,
273
+ });
274
+ return this;
275
+ }
276
+ async commit(client) {
277
+ return await transaction(client, this.operations, true);
278
+ }
279
+ }
280
+ // ================================
281
+ // CREATE BATCH
282
+ // ================================
283
+ export function batch() {
284
+ return new WriteBatch();
285
+ }
@@ -0,0 +1,6 @@
1
+ export * from "./core/client";
2
+ export * from "./auth/auth";
3
+ export * from "./database/database";
4
+ export * from "./realtime/realtime";
5
+ export * from "./notifications/notifications";
6
+ export * from "./storage/storage";
package/dist/index.js ADDED
@@ -0,0 +1,6 @@
1
+ export * from "./core/client.js";
2
+ export * from "./auth/auth.js";
3
+ export * from "./database/database.js";
4
+ export * from "./realtime/realtime.js";
5
+ export * from "./notifications/notifications.js";
6
+ export * from "./storage/storage.js";
@@ -0,0 +1,79 @@
1
+ import { SupersoClient } from "../core/client";
2
+ export type NotificationType = "in_app" | "email" | "push" | "sms";
3
+ export interface SendNotificationData {
4
+ type?: NotificationType;
5
+ subject?: string;
6
+ body?: string;
7
+ recipient?: string;
8
+ auth_user_id?: string;
9
+ user_id?: string;
10
+ user_ids?: string[];
11
+ broadcast?: boolean;
12
+ data?: any;
13
+ template?: string;
14
+ template_vars?: any;
15
+ scheduled_at?: string;
16
+ image_url?: string;
17
+ collection?: string;
18
+ realtime_channel?: string;
19
+ }
20
+ export declare function sendNotification(client: SupersoClient, payload: SendNotificationData): Promise<any>;
21
+ export declare function sendEmail(client: SupersoClient, recipient: string, subject: string, body: string, scheduled_at?: string): Promise<any>;
22
+ export declare function sendSMS(client: SupersoClient, recipient: string, body: string, scheduled_at?: string): Promise<any>;
23
+ export declare function sendPush(client: SupersoClient, user_id: string, subject: string, body: string, options?: {
24
+ image_url?: string;
25
+ data?: any;
26
+ scheduled_at?: string;
27
+ }): Promise<any>;
28
+ export declare function sendInApp(client: SupersoClient, payload: {
29
+ subject: string;
30
+ body: string;
31
+ user_id?: string;
32
+ user_ids?: string[];
33
+ broadcast?: boolean;
34
+ data?: any;
35
+ image_url?: string;
36
+ collection: string;
37
+ realtime_channel: string;
38
+ }): Promise<any>;
39
+ export declare function broadcastNotification(client: SupersoClient, subject: string, body: string, collection: string, realtime_channel: string): Promise<any>;
40
+ export declare function sendTemplateNotification(client: SupersoClient, payload: {
41
+ type: NotificationType;
42
+ template: string;
43
+ recipient?: string;
44
+ user_id?: string;
45
+ user_ids?: string[];
46
+ broadcast?: boolean;
47
+ template_vars?: any;
48
+ collection?: string;
49
+ realtime_channel?: string;
50
+ }): Promise<any>;
51
+ export declare function getNotificationLogs(client: SupersoClient): Promise<any>;
52
+ export declare function getInAppNotifications(client: SupersoClient, collection: string): Promise<any>;
53
+ export declare function getUnreadCount(client: SupersoClient, collection: string): Promise<any>;
54
+ export declare function markNotificationRead(client: SupersoClient, notificationId: string, collection: string, realtime_channel: string): Promise<any>;
55
+ export declare function markAllNotificationsRead(client: SupersoClient, collection: string, realtime_channel: string): Promise<any>;
56
+ export declare function deleteNotification(client: SupersoClient, notificationId: string, collection: string, realtime_channel: string): Promise<any>;
57
+ export interface NotificationPreferences {
58
+ email_enabled?: boolean;
59
+ push_enabled?: boolean;
60
+ sms_enabled?: boolean;
61
+ in_app_enabled?: boolean;
62
+ marketing_enabled?: boolean;
63
+ security_enabled?: boolean;
64
+ }
65
+ export declare function getNotificationPreferences(client: SupersoClient): Promise<any>;
66
+ export declare function updateNotificationPreferences(client: SupersoClient, payload: NotificationPreferences): Promise<any>;
67
+ export interface RegisterDeviceData {
68
+ platform: "ios" | "android" | "web";
69
+ token: string;
70
+ app_version?: string;
71
+ device_name?: string;
72
+ }
73
+ export declare function registerDevice(client: SupersoClient, payload: RegisterDeviceData): Promise<any>;
74
+ export declare function getDevices(client: SupersoClient): Promise<any>;
75
+ export declare function deleteDevice(client: SupersoClient, tokenId: string): Promise<any>;
76
+ export declare function onNotification(realtime: any, callback: Function): void;
77
+ export declare function onNotificationDelivered(realtime: any, callback: Function): void;
78
+ export declare function onNotificationFailed(realtime: any, callback: Function): void;
79
+ export declare function connectNotificationChannel(client: SupersoClient, channel: string, jwt?: string): WebSocket;
@@ -0,0 +1,202 @@
1
+ // =======================================
2
+ // STORAGE
3
+ // =======================================
4
+ const ACCESS_TOKEN_KEY = "superso_access_token";
5
+ // =======================================
6
+ // REQUEST
7
+ // =======================================
8
+ async function request(client, endpoint, method = "GET", body, auth = false) {
9
+ const headers = {
10
+ "Content-Type": "application/json",
11
+ "X-Superso-Project-Key": client.apiKey,
12
+ };
13
+ const token = localStorage.getItem(ACCESS_TOKEN_KEY);
14
+ if (auth && token) {
15
+ headers["Authorization"] =
16
+ `Bearer ${token}`;
17
+ }
18
+ const response = await fetch(`${client.baseUrl}/api/project/${client.projectId}${endpoint}`, {
19
+ method,
20
+ headers,
21
+ body: body
22
+ ? JSON.stringify(body)
23
+ : undefined,
24
+ });
25
+ let data = {};
26
+ try {
27
+ data =
28
+ await response.json();
29
+ }
30
+ catch (_a) {
31
+ data = {};
32
+ }
33
+ if (!response.ok) {
34
+ throw new Error((data === null || data === void 0 ? void 0 : data.message) ||
35
+ "Notification request failed");
36
+ }
37
+ return data;
38
+ }
39
+ // =======================================
40
+ // SEND NOTIFICATION
41
+ // =======================================
42
+ export async function sendNotification(client, payload) {
43
+ return await request(client, "/notifications/send", "POST", payload);
44
+ }
45
+ // =======================================
46
+ // SEND EMAIL
47
+ // =======================================
48
+ export async function sendEmail(client, recipient, subject, body, scheduled_at) {
49
+ return await sendNotification(client, {
50
+ type: "email",
51
+ recipient,
52
+ subject,
53
+ body,
54
+ scheduled_at,
55
+ });
56
+ }
57
+ // =======================================
58
+ // SEND SMS
59
+ // =======================================
60
+ export async function sendSMS(client, recipient, body, scheduled_at) {
61
+ return await sendNotification(client, {
62
+ type: "sms",
63
+ recipient,
64
+ body,
65
+ scheduled_at,
66
+ });
67
+ }
68
+ // =======================================
69
+ // SEND PUSH
70
+ // =======================================
71
+ export async function sendPush(client, user_id, subject, body, options) {
72
+ return await sendNotification(client, {
73
+ type: "push",
74
+ user_id,
75
+ subject,
76
+ body,
77
+ image_url: options === null || options === void 0 ? void 0 : options.image_url,
78
+ data: options === null || options === void 0 ? void 0 : options.data,
79
+ scheduled_at: options === null || options === void 0 ? void 0 : options.scheduled_at,
80
+ });
81
+ }
82
+ // =======================================
83
+ // SEND IN APP
84
+ // =======================================
85
+ export async function sendInApp(client, payload) {
86
+ return await sendNotification(client, Object.assign({ type: "in_app" }, payload));
87
+ }
88
+ // =======================================
89
+ // BROADCAST
90
+ // =======================================
91
+ export async function broadcastNotification(client, subject, body, collection, realtime_channel) {
92
+ return await sendNotification(client, {
93
+ type: "in_app",
94
+ subject,
95
+ body,
96
+ broadcast: true,
97
+ collection,
98
+ realtime_channel,
99
+ });
100
+ }
101
+ // =======================================
102
+ // TEMPLATE NOTIFICATION
103
+ // =======================================
104
+ export async function sendTemplateNotification(client, payload) {
105
+ return await sendNotification(client, payload);
106
+ }
107
+ // =======================================
108
+ // LOGS
109
+ // =======================================
110
+ export async function getNotificationLogs(client) {
111
+ return await request(client, "/notifications/logs", "GET");
112
+ }
113
+ // =======================================
114
+ // IN APP NOTIFICATIONS
115
+ // =======================================
116
+ export async function getInAppNotifications(client, collection) {
117
+ return await request(client, `/notifications/in-app?collection=${collection}`, "GET");
118
+ }
119
+ // =======================================
120
+ // UNREAD COUNT
121
+ // =======================================
122
+ export async function getUnreadCount(client, collection) {
123
+ return await request(client, `/notifications/in-app/unread-count?collection=${collection}`, "GET");
124
+ }
125
+ // =======================================
126
+ // MARK AS READ
127
+ // =======================================
128
+ export async function markNotificationRead(client, notificationId, collection, realtime_channel) {
129
+ return await request(client, `/notifications/in-app/${notificationId}/read?collection=${collection}&realtime_channel=${realtime_channel}`, "POST");
130
+ }
131
+ // =======================================
132
+ // MARK ALL READ
133
+ // =======================================
134
+ export async function markAllNotificationsRead(client, collection, realtime_channel) {
135
+ return await request(client, `/notifications/in-app/read-all?collection=${collection}&realtime_channel=${realtime_channel}`, "POST");
136
+ }
137
+ // =======================================
138
+ // DELETE NOTIFICATION
139
+ // =======================================
140
+ export async function deleteNotification(client, notificationId, collection, realtime_channel) {
141
+ return await request(client, `/notifications/in-app/${notificationId}?collection=${collection}&realtime_channel=${realtime_channel}`, "DELETE");
142
+ }
143
+ // =======================================
144
+ // GET PREFERENCES
145
+ // =======================================
146
+ export async function getNotificationPreferences(client) {
147
+ return await request(client, "/notifications/preferences", "GET", null, true);
148
+ }
149
+ // =======================================
150
+ // UPDATE PREFERENCES
151
+ // =======================================
152
+ export async function updateNotificationPreferences(client, payload) {
153
+ return await request(client, "/notifications/preferences", "PUT", payload, true);
154
+ }
155
+ // =======================================
156
+ // REGISTER DEVICE
157
+ // =======================================
158
+ export async function registerDevice(client, payload) {
159
+ return await request(client, "/notifications/devices", "POST", payload, true);
160
+ }
161
+ // =======================================
162
+ // GET DEVICES
163
+ // =======================================
164
+ export async function getDevices(client) {
165
+ return await request(client, "/notifications/devices", "GET", null, true);
166
+ }
167
+ // =======================================
168
+ // DELETE DEVICE
169
+ // =======================================
170
+ export async function deleteDevice(client, tokenId) {
171
+ return await request(client, `/notifications/devices/${tokenId}`, "DELETE", null, true);
172
+ }
173
+ // =======================================
174
+ // REALTIME EVENTS
175
+ // =======================================
176
+ export function onNotification(realtime, callback) {
177
+ realtime.on("notification.received", callback);
178
+ realtime.on("notification", callback);
179
+ }
180
+ // =======================================
181
+ // DELIVERED
182
+ // =======================================
183
+ export function onNotificationDelivered(realtime, callback) {
184
+ realtime.on("notification.delivered", callback);
185
+ }
186
+ // =======================================
187
+ // FAILED
188
+ // =======================================
189
+ export function onNotificationFailed(realtime, callback) {
190
+ realtime.on("notification.failed", callback);
191
+ }
192
+ // =======================================
193
+ // WEBSOCKET HELPER
194
+ // =======================================
195
+ export function connectNotificationChannel(client, channel, jwt) {
196
+ const protocol = client.baseUrl.startsWith("https")
197
+ ? "wss"
198
+ : "ws";
199
+ const base = client.baseUrl.replace(/^https?/, protocol);
200
+ const url = `${base}/ws/project/${client.projectId}/channel/${channel}?key=${client.apiKey}${jwt ? `&token=${jwt}` : ""}`;
201
+ return new WebSocket(url);
202
+ }