@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.
Files changed (2) hide show
  1. package/dist/index.js +20 -4
  2. 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?token=${encodeURIComponent(this.config.apiKey)}&mode=${mode}`;
315
+ const url = `${baseUrl}/v1/realtime/events?mode=${mode}`;
316
+ const apiKey = this.config.apiKey;
316
317
  const connect = () => {
317
- this.eventSource = new EventSource(url);
318
- this.eventSource.onmessage = (event) => {
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
- this.eventSource.onerror = (event) => {
356
+ es.onerror = () => {
341
357
  onError?.(new Error("SSE connection error"));
342
358
  this.eventSource?.close();
343
359
  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.12",
4
4
  "description": "TypeScript SDK for Mailhooks API",
5
5
  "repository": {
6
6
  "type": "git",