magicbell-js 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commonjs/project-client/services/broadcasts/models/broadcast-collection.d.ts +265 -265
- package/dist/commonjs/project-client/services/broadcasts/models/broadcast.d.ts +168 -168
- package/dist/commonjs/project-client/services/broadcasts/models/email.d.ts +3 -3
- package/dist/commonjs/project-client/services/broadcasts/models/in-app.d.ts +3 -3
- package/dist/commonjs/project-client/services/broadcasts/models/mobile-push.d.ts +3 -3
- package/dist/commonjs/project-client/services/broadcasts/models/overrides-channels.d.ts +27 -27
- package/dist/commonjs/project-client/services/broadcasts/models/overrides.d.ts +39 -39
- package/dist/commonjs/project-client/services/broadcasts/models/sms.d.ts +3 -3
- package/dist/commonjs/project-client/services/channels/models/apns-token-collection.d.ts +14 -14
- package/dist/commonjs/project-client/services/channels/models/apns-token.d.ts +8 -8
- package/dist/commonjs/project-client/services/channels/models/discard-result.d.ts +8 -8
- package/dist/commonjs/project-client/services/channels/models/expo-token-collection.d.ts +14 -14
- package/dist/commonjs/project-client/services/channels/models/expo-token.d.ts +8 -8
- package/dist/commonjs/project-client/services/channels/models/fcm-token-collection.d.ts +14 -14
- package/dist/commonjs/project-client/services/channels/models/fcm-token.d.ts +8 -8
- package/dist/commonjs/project-client/services/channels/models/inbox-token-response-collection.d.ts +28 -28
- package/dist/commonjs/project-client/services/channels/models/inbox-token-response.d.ts +16 -16
- package/dist/commonjs/project-client/services/channels/models/slack-token-collection.d.ts +14 -14
- package/dist/commonjs/project-client/services/channels/models/slack-token.d.ts +8 -8
- package/dist/commonjs/project-client/services/channels/models/teams-token-collection.d.ts +14 -14
- package/dist/commonjs/project-client/services/channels/models/teams-token.d.ts +8 -8
- package/dist/commonjs/project-client/services/channels/models/web-push-token-collection.d.ts +14 -14
- package/dist/commonjs/project-client/services/channels/models/web-push-token.d.ts +8 -8
- package/dist/commonjs/project-client/services/common/user.d.ts +6 -6
- package/dist/commonjs/project-client/services/integrations/models/inbox-config-collection.d.ts +182 -182
- package/dist/commonjs/project-client/services/integrations/models/inbox-config-payload.d.ts +98 -98
- package/dist/commonjs/project-client/services/integrations/models/inbox-config.d.ts +140 -140
- package/dist/commonjs/project-client/services/integrations/models/theme.d.ts +56 -56
- package/dist/commonjs/project-client/services/users/models/user-collection.d.ts +10 -10
- package/dist/commonjs/socket.d.ts +11 -0
- package/dist/commonjs/socket.d.ts.map +1 -0
- package/dist/commonjs/socket.js +196 -0
- package/dist/commonjs/socket.js.map +1 -0
- package/dist/commonjs/user-client/services/channels/models/apns-token-collection.d.ts +14 -14
- package/dist/commonjs/user-client/services/channels/models/apns-token.d.ts +8 -8
- package/dist/commonjs/user-client/services/channels/models/discard-result.d.ts +8 -8
- package/dist/commonjs/user-client/services/channels/models/expo-token-collection.d.ts +14 -14
- package/dist/commonjs/user-client/services/channels/models/expo-token.d.ts +8 -8
- package/dist/commonjs/user-client/services/channels/models/fcm-token-collection.d.ts +14 -14
- package/dist/commonjs/user-client/services/channels/models/fcm-token.d.ts +8 -8
- package/dist/commonjs/user-client/services/channels/models/inbox-token-response-collection.d.ts +28 -28
- package/dist/commonjs/user-client/services/channels/models/inbox-token-response.d.ts +16 -16
- package/dist/commonjs/user-client/services/channels/models/slack-token-collection.d.ts +14 -14
- package/dist/commonjs/user-client/services/channels/models/slack-token.d.ts +8 -8
- package/dist/commonjs/user-client/services/channels/models/teams-token-collection.d.ts +14 -14
- package/dist/commonjs/user-client/services/channels/models/teams-token.d.ts +8 -8
- package/dist/commonjs/user-client/services/channels/models/web-push-token-collection.d.ts +14 -14
- package/dist/commonjs/user-client/services/channels/models/web-push-token.d.ts +8 -8
- package/dist/commonjs/user-client/services/integrations/models/authed-user.d.ts +8 -8
- package/dist/commonjs/user-client/services/integrations/models/inbox-config-payload.d.ts +98 -98
- package/dist/commonjs/user-client/services/integrations/models/slack-installation.d.ts +57 -57
- package/dist/commonjs/user-client/services/integrations/models/slack-start-install.d.ts +8 -8
- package/dist/commonjs/user-client/services/integrations/models/theme.d.ts +56 -56
- package/dist/commonjs/user-client/services/notifications/models/notification-collection.d.ts +56 -56
- package/dist/commonjs/user-client/services/notifications/models/notification.d.ts +32 -32
- package/dist/esm/project-client/services/broadcasts/models/broadcast-collection.d.ts +265 -265
- package/dist/esm/project-client/services/broadcasts/models/broadcast.d.ts +168 -168
- package/dist/esm/project-client/services/broadcasts/models/email.d.ts +3 -3
- package/dist/esm/project-client/services/broadcasts/models/in-app.d.ts +3 -3
- package/dist/esm/project-client/services/broadcasts/models/mobile-push.d.ts +3 -3
- package/dist/esm/project-client/services/broadcasts/models/overrides-channels.d.ts +27 -27
- package/dist/esm/project-client/services/broadcasts/models/overrides.d.ts +39 -39
- package/dist/esm/project-client/services/broadcasts/models/sms.d.ts +3 -3
- package/dist/esm/project-client/services/channels/models/apns-token-collection.d.ts +14 -14
- package/dist/esm/project-client/services/channels/models/apns-token.d.ts +8 -8
- package/dist/esm/project-client/services/channels/models/discard-result.d.ts +8 -8
- package/dist/esm/project-client/services/channels/models/expo-token-collection.d.ts +14 -14
- package/dist/esm/project-client/services/channels/models/expo-token.d.ts +8 -8
- package/dist/esm/project-client/services/channels/models/fcm-token-collection.d.ts +14 -14
- package/dist/esm/project-client/services/channels/models/fcm-token.d.ts +8 -8
- package/dist/esm/project-client/services/channels/models/inbox-token-response-collection.d.ts +28 -28
- package/dist/esm/project-client/services/channels/models/inbox-token-response.d.ts +16 -16
- package/dist/esm/project-client/services/channels/models/slack-token-collection.d.ts +14 -14
- package/dist/esm/project-client/services/channels/models/slack-token.d.ts +8 -8
- package/dist/esm/project-client/services/channels/models/teams-token-collection.d.ts +14 -14
- package/dist/esm/project-client/services/channels/models/teams-token.d.ts +8 -8
- package/dist/esm/project-client/services/channels/models/web-push-token-collection.d.ts +14 -14
- package/dist/esm/project-client/services/channels/models/web-push-token.d.ts +8 -8
- package/dist/esm/project-client/services/common/user.d.ts +6 -6
- package/dist/esm/project-client/services/integrations/models/inbox-config-collection.d.ts +182 -182
- package/dist/esm/project-client/services/integrations/models/inbox-config-payload.d.ts +98 -98
- package/dist/esm/project-client/services/integrations/models/inbox-config.d.ts +140 -140
- package/dist/esm/project-client/services/integrations/models/theme.d.ts +56 -56
- package/dist/esm/project-client/services/users/models/user-collection.d.ts +10 -10
- package/dist/esm/socket.d.ts +11 -0
- package/dist/esm/socket.d.ts.map +1 -0
- package/dist/esm/socket.js +192 -0
- package/dist/esm/socket.js.map +1 -0
- package/dist/esm/user-client/services/channels/models/apns-token-collection.d.ts +14 -14
- package/dist/esm/user-client/services/channels/models/apns-token.d.ts +8 -8
- package/dist/esm/user-client/services/channels/models/discard-result.d.ts +8 -8
- package/dist/esm/user-client/services/channels/models/expo-token-collection.d.ts +14 -14
- package/dist/esm/user-client/services/channels/models/expo-token.d.ts +8 -8
- package/dist/esm/user-client/services/channels/models/fcm-token-collection.d.ts +14 -14
- package/dist/esm/user-client/services/channels/models/fcm-token.d.ts +8 -8
- package/dist/esm/user-client/services/channels/models/inbox-token-response-collection.d.ts +28 -28
- package/dist/esm/user-client/services/channels/models/inbox-token-response.d.ts +16 -16
- package/dist/esm/user-client/services/channels/models/slack-token-collection.d.ts +14 -14
- package/dist/esm/user-client/services/channels/models/slack-token.d.ts +8 -8
- package/dist/esm/user-client/services/channels/models/teams-token-collection.d.ts +14 -14
- package/dist/esm/user-client/services/channels/models/teams-token.d.ts +8 -8
- package/dist/esm/user-client/services/channels/models/web-push-token-collection.d.ts +14 -14
- package/dist/esm/user-client/services/channels/models/web-push-token.d.ts +8 -8
- package/dist/esm/user-client/services/integrations/models/authed-user.d.ts +8 -8
- package/dist/esm/user-client/services/integrations/models/inbox-config-payload.d.ts +98 -98
- package/dist/esm/user-client/services/integrations/models/slack-installation.d.ts +57 -57
- package/dist/esm/user-client/services/integrations/models/slack-start-install.d.ts +8 -8
- package/dist/esm/user-client/services/integrations/models/theme.d.ts +56 -56
- package/dist/esm/user-client/services/notifications/models/notification-collection.d.ts +56 -56
- package/dist/esm/user-client/services/notifications/models/notification.d.ts +32 -32
- package/package.json +14 -3
- package/socket/package.json +4 -0
- package/src/socket.ts +228 -0
package/src/socket.ts
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import invariant from 'tiny-invariant';
|
|
2
|
+
|
|
3
|
+
import { type Notification, Client } from './user-client.js';
|
|
4
|
+
|
|
5
|
+
export class Socket {
|
|
6
|
+
#client: Client;
|
|
7
|
+
#socketUrl = 'wss://ws.magicbell.com';
|
|
8
|
+
#inboxToken: string | undefined;
|
|
9
|
+
#origin: string | undefined;
|
|
10
|
+
#websocket: WebSocket | undefined;
|
|
11
|
+
#isConnected = false;
|
|
12
|
+
#reconnectAttempts = 0;
|
|
13
|
+
#maxReconnectAttempts = 5;
|
|
14
|
+
#reconnectInterval = 1000;
|
|
15
|
+
#notificationHandler: ((notification: Notification) => void) | undefined;
|
|
16
|
+
|
|
17
|
+
constructor(options: { token: string } | Client) {
|
|
18
|
+
if (options instanceof Client) {
|
|
19
|
+
this.#client = options;
|
|
20
|
+
} else {
|
|
21
|
+
this.#client = new Client({ token: options.token });
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async listen(onNotification: (notification: Notification) => void) {
|
|
26
|
+
this.#notificationHandler = onNotification;
|
|
27
|
+
|
|
28
|
+
if (this.#websocket && this.#isConnected) {
|
|
29
|
+
console.warn('Already connected to WebSocket');
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
try {
|
|
34
|
+
const url = await this.#getUrl();
|
|
35
|
+
this.#websocket = new WebSocket(url);
|
|
36
|
+
|
|
37
|
+
this.#websocket.onopen = () => {
|
|
38
|
+
this.#isConnected = true;
|
|
39
|
+
this.#reconnectAttempts = 0;
|
|
40
|
+
this.#reconnectInterval = 1000;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
this.#websocket.onmessage = (event) => {
|
|
44
|
+
if (event.origin !== this.#origin) return;
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
const data = JSON.parse(event.data);
|
|
48
|
+
this.#handleMessage(data);
|
|
49
|
+
} catch (error) {
|
|
50
|
+
console.error('Failed to parse WebSocket message:', error);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
this.#websocket.onclose = (event) => {
|
|
55
|
+
console.warn('WebSocket disconnected:', event.code, event.reason);
|
|
56
|
+
this.#isConnected = false;
|
|
57
|
+
this.#handleReconnect();
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
this.#websocket.onerror = (error) => {
|
|
61
|
+
console.error('WebSocket error:', error);
|
|
62
|
+
this.#isConnected = false;
|
|
63
|
+
};
|
|
64
|
+
} catch (error) {
|
|
65
|
+
console.error('Failed to connect to WebSocket:', error);
|
|
66
|
+
this.#handleReconnect();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
disconnect() {
|
|
71
|
+
if (this.#websocket) {
|
|
72
|
+
this.#websocket.close();
|
|
73
|
+
this.#websocket = undefined;
|
|
74
|
+
}
|
|
75
|
+
this.#isConnected = false;
|
|
76
|
+
this.#reconnectAttempts = 0;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
isListening() {
|
|
80
|
+
return this.#isConnected;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
#handleMessage(data: any) {
|
|
84
|
+
if (!this.#isNewNotificationMessage(data)) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
this.#handleNewNotification(data.data.id);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
#isNewNotificationMessage(data: any): data is { name: 'notifications/new'; data: { id: string } } {
|
|
92
|
+
return (
|
|
93
|
+
typeof data === 'object' &&
|
|
94
|
+
data !== null &&
|
|
95
|
+
data.name === 'notifications/new' &&
|
|
96
|
+
typeof data.data === 'object' &&
|
|
97
|
+
data.data !== null &&
|
|
98
|
+
typeof data.data.id === 'string'
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async #handleNewNotification(notificationId: string) {
|
|
103
|
+
if (!this.#notificationHandler) {
|
|
104
|
+
console.warn('No notification handler provided');
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
try {
|
|
109
|
+
const { data: notification, metadata: res } = await this.#client.notifications.fetchNotification(notificationId);
|
|
110
|
+
|
|
111
|
+
if (!isOK(res)) {
|
|
112
|
+
console.error(`Failed to fetch notification ${notificationId}: ${res.status} ${res.statusText}`);
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (notification) {
|
|
117
|
+
this.#notificationHandler(notification);
|
|
118
|
+
}
|
|
119
|
+
} catch (error) {
|
|
120
|
+
console.error(`Error fetching notification ${notificationId}:`, error);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
#handleReconnect() {
|
|
125
|
+
if (this.#reconnectAttempts >= this.#maxReconnectAttempts) {
|
|
126
|
+
console.error('Max reconnection attempts reached');
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (!this.#notificationHandler) {
|
|
131
|
+
console.warn('No notification handler, skipping reconnect');
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
setTimeout(() => {
|
|
136
|
+
this.#reconnectAttempts++;
|
|
137
|
+
this.#reconnectInterval = Math.min(this.#reconnectInterval * 2, 30000); // Max 30 seconds
|
|
138
|
+
this.listen(this.#notificationHandler!);
|
|
139
|
+
}, this.#reconnectInterval);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async #getUrl() {
|
|
143
|
+
const jwtToken = this.#client.config.token;
|
|
144
|
+
invariant(jwtToken, 'Failed to get token from client');
|
|
145
|
+
const apiKey = getApiKeyFromToken(jwtToken);
|
|
146
|
+
invariant(apiKey, 'Failed to get API key from token');
|
|
147
|
+
|
|
148
|
+
const token = await this.#getToken();
|
|
149
|
+
const url = new URL(this.#socketUrl);
|
|
150
|
+
url.searchParams.set('api_key', apiKey);
|
|
151
|
+
url.searchParams.set('token', token);
|
|
152
|
+
|
|
153
|
+
this.#origin = url.origin;
|
|
154
|
+
return url.toString();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async #getToken() {
|
|
158
|
+
if (this.#inboxToken) return this.#inboxToken;
|
|
159
|
+
|
|
160
|
+
const { data, metadata: res } = await this.#client.channels.saveInboxToken({
|
|
161
|
+
token: getSessionId(),
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
invariant(isOK(res), `Failed to save Inbox token: ${res.status} ${res.statusText}`);
|
|
165
|
+
invariant(data?.token, 'Unexpected server response, missing token');
|
|
166
|
+
|
|
167
|
+
this.#inboxToken = data.token;
|
|
168
|
+
return this.#inboxToken;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function isOK(response: { status: number }) {
|
|
173
|
+
return response.status >= 200 && response.status < 300;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function getSessionId() {
|
|
177
|
+
if (typeof sessionStorage === 'undefined') {
|
|
178
|
+
return generateID(64);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// sessionStorage gets cleared when the page session ends. A page
|
|
182
|
+
// session lasts for as long as the browser is open and survives
|
|
183
|
+
// over page reloads and restores. Opening a page in a new tab or
|
|
184
|
+
// window will cause a new session to be initiated. This gives us
|
|
185
|
+
// a stable ID per tab, and different ID's across tabs.
|
|
186
|
+
const stored = sessionStorage.getItem('magicbell--realtime-token');
|
|
187
|
+
if (stored) return stored;
|
|
188
|
+
|
|
189
|
+
const id = generateID(64);
|
|
190
|
+
sessionStorage.setItem('magicbell--realtime-token', id);
|
|
191
|
+
return id;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function generateID(length = 17) {
|
|
195
|
+
let id = '';
|
|
196
|
+
|
|
197
|
+
while (id.length < length) {
|
|
198
|
+
id += getRandomValues();
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return id.substring(0, length);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function getApiKeyFromToken(token: string) {
|
|
205
|
+
const data = getTokenPayload(token);
|
|
206
|
+
if (!data) return null;
|
|
207
|
+
|
|
208
|
+
if (data.api_key) {
|
|
209
|
+
return data.api_key;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return null;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function getTokenPayload(token: string) {
|
|
216
|
+
try {
|
|
217
|
+
const [_, payload] = token.split('.');
|
|
218
|
+
const data = JSON.parse(atob(payload));
|
|
219
|
+
return data || null;
|
|
220
|
+
} catch {
|
|
221
|
+
return null;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const getRandomValues =
|
|
226
|
+
typeof crypto !== 'undefined' && crypto.getRandomValues
|
|
227
|
+
? () => crypto.getRandomValues(new Uint32Array(1))[0].toString(36)
|
|
228
|
+
: () => Math.random().toString(36).substring(2, 15);
|