@mailhooks/sdk 2.6.11 → 2.6.13

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.
Files changed (2) hide show
  1. package/dist/index.js +24 -6
  2. package/package.json +2 -1
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import axios from 'axios';
2
+ import { EventSource as EventSource$1 } from 'eventsource';
2
3
  import { createHmac, timingSafeEqual } from 'crypto';
3
4
  import { simpleParser } from 'mailparser';
4
5
 
@@ -241,8 +242,9 @@ var EmailsResource = class extends MailhooksClient {
241
242
  }
242
243
  }
243
244
  };
244
-
245
- // src/realtime.ts
245
+ if (typeof globalThis.EventSource === "undefined") {
246
+ globalThis.EventSource = EventSource$1;
247
+ }
246
248
  var RealtimeEventType = /* @__PURE__ */ ((RealtimeEventType2) => {
247
249
  RealtimeEventType2["EMAIL_RECEIVED"] = "email.received";
248
250
  RealtimeEventType2["EMAIL_UPDATED"] = "email.updated";
@@ -312,10 +314,26 @@ var RealtimeResource = class {
312
314
  );
313
315
  }
314
316
  const baseUrl = this.config.baseUrl ?? "https://mailhooks.dev/api";
315
- const url = `${baseUrl}/v1/realtime/events?token=${encodeURIComponent(this.config.apiKey)}&mode=${mode}`;
317
+ const url = `${baseUrl}/v1/realtime/events?mode=${mode}`;
318
+ const apiKey = this.config.apiKey;
316
319
  const connect = () => {
317
- this.eventSource = new EventSource(url);
318
- this.eventSource.onmessage = (event) => {
320
+ const urlWithToken = `${url}&token=${encodeURIComponent(apiKey)}`;
321
+ const eventSourceOptions = {
322
+ fetch: (input, init) => fetch(input, {
323
+ ...init,
324
+ headers: {
325
+ ...init?.headers || {},
326
+ "X-API-Key": apiKey
327
+ }
328
+ })
329
+ };
330
+ try {
331
+ this.eventSource = new EventSource(url, eventSourceOptions);
332
+ } catch {
333
+ this.eventSource = new EventSource(urlWithToken);
334
+ }
335
+ const es = this.eventSource;
336
+ es.onmessage = (event) => {
319
337
  try {
320
338
  const data = JSON.parse(event.data);
321
339
  switch (data.type) {
@@ -337,7 +355,7 @@ var RealtimeResource = class {
337
355
  onError?.(new Error(`Failed to parse event: ${message}`));
338
356
  }
339
357
  };
340
- this.eventSource.onerror = (event) => {
358
+ es.onerror = () => {
341
359
  onError?.(new Error("SSE connection error"));
342
360
  this.eventSource?.close();
343
361
  this.eventSource = null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mailhooks/sdk",
3
- "version": "2.6.11",
3
+ "version": "2.6.13",
4
4
  "description": "TypeScript SDK for Mailhooks API",
5
5
  "repository": {
6
6
  "type": "git",
@@ -26,6 +26,7 @@
26
26
  ],
27
27
  "dependencies": {
28
28
  "axios": "^1.6.0",
29
+ "eventsource": "^3.0.0",
29
30
  "mailparser": "^3.7.2"
30
31
  },
31
32
  "devDependencies": {