@mailhooks/sdk 2.6.11 → 2.6.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +20 -4
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -312,10 +312,26 @@ var RealtimeResource = class {
|
|
|
312
312
|
);
|
|
313
313
|
}
|
|
314
314
|
const baseUrl = this.config.baseUrl ?? "https://mailhooks.dev/api";
|
|
315
|
-
const url = `${baseUrl}/v1/realtime/events?
|
|
315
|
+
const url = `${baseUrl}/v1/realtime/events?mode=${mode}`;
|
|
316
|
+
const apiKey = this.config.apiKey;
|
|
316
317
|
const connect = () => {
|
|
317
|
-
|
|
318
|
-
|
|
318
|
+
const urlWithToken = `${url}&token=${encodeURIComponent(apiKey)}`;
|
|
319
|
+
const eventSourceOptions = {
|
|
320
|
+
fetch: (input, init) => fetch(input, {
|
|
321
|
+
...init,
|
|
322
|
+
headers: {
|
|
323
|
+
...init?.headers || {},
|
|
324
|
+
"X-API-Key": apiKey
|
|
325
|
+
}
|
|
326
|
+
})
|
|
327
|
+
};
|
|
328
|
+
try {
|
|
329
|
+
this.eventSource = new EventSource(url, eventSourceOptions);
|
|
330
|
+
} catch {
|
|
331
|
+
this.eventSource = new EventSource(urlWithToken);
|
|
332
|
+
}
|
|
333
|
+
const es = this.eventSource;
|
|
334
|
+
es.onmessage = (event) => {
|
|
319
335
|
try {
|
|
320
336
|
const data = JSON.parse(event.data);
|
|
321
337
|
switch (data.type) {
|
|
@@ -337,7 +353,7 @@ var RealtimeResource = class {
|
|
|
337
353
|
onError?.(new Error(`Failed to parse event: ${message}`));
|
|
338
354
|
}
|
|
339
355
|
};
|
|
340
|
-
|
|
356
|
+
es.onerror = () => {
|
|
341
357
|
onError?.(new Error("SSE connection error"));
|
|
342
358
|
this.eventSource?.close();
|
|
343
359
|
this.eventSource = null;
|