@calimero-network/mero-js 19.17.0 → 19.18.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.
@@ -30,10 +30,12 @@ export declare class SseClient {
30
30
  private subscribedGroupIds;
31
31
  private closed;
32
32
  private listeners;
33
+ private fetchImpl;
33
34
  constructor(opts: {
34
35
  baseUrl: string;
35
36
  getAuthToken: () => Promise<string>;
36
37
  reconnectDelayMs?: number;
38
+ fetch?: typeof fetch;
37
39
  });
38
40
  on(event: 'connect', handler: SseConnectHandler): void;
39
41
  on(event: 'event', handler: SseEventHandler): void;
@@ -44,6 +44,9 @@ export class SseClient {
44
44
  this.baseUrl = opts.baseUrl.replace(/\/+$/, '');
45
45
  this.getAuthToken = opts.getAuthToken;
46
46
  this.reconnectDelayMs = opts.reconnectDelayMs ?? 3000;
47
+ // globalThis.fetch must be called as a method on globalThis, not through a
48
+ // variable, or browsers throw "Illegal invocation".
49
+ this.fetchImpl = (url, init) => opts.fetch ? opts.fetch(url, init) : globalThis.fetch(url, init);
47
50
  }
48
51
  on(event, handler) {
49
52
  const key = event;
@@ -122,7 +125,7 @@ export class SseClient {
122
125
  try {
123
126
  const token = await this.getAuthToken();
124
127
  const url = `${this.baseUrl}/sse`;
125
- const response = await fetch(url, {
128
+ const response = await this.fetchImpl(url, {
126
129
  headers: {
127
130
  'Authorization': `Bearer ${token}`,
128
131
  'Accept': 'text/event-stream',
@@ -300,7 +303,7 @@ export class SseClient {
300
303
  params.contextIds = ids.contextIds;
301
304
  if (ids.groupIds && ids.groupIds.length > 0)
302
305
  params.groupIds = ids.groupIds;
303
- const response = await fetch(`${this.baseUrl}/sse/subscription`, {
306
+ const response = await this.fetchImpl(`${this.baseUrl}/sse/subscription`, {
304
307
  method: 'POST',
305
308
  headers: {
306
309
  'Authorization': `Bearer ${token}`,
@@ -2,6 +2,7 @@ import { Transport, HttpClient } from './http-types.js';
2
2
  export declare function createHttpClient(transport: Transport): HttpClient;
3
3
  export declare function createBrowserHttpClient(options: {
4
4
  baseUrl: string;
5
+ fetch?: typeof fetch;
5
6
  getAuthToken?: () => Promise<string | undefined>;
6
7
  getProof?: Transport['getProof'];
7
8
  onTokenRefresh?: (newToken: string) => Promise<void>;
@@ -6,9 +6,9 @@ export function createHttpClient(transport) {
6
6
  // Factory function for browser environments
7
7
  export function createBrowserHttpClient(options) {
8
8
  const transport = {
9
- // Wrap fetch in arrow function to prevent "Illegal invocation" error
10
- // This preserves the correct 'this' context when fetch is called
11
- fetch: (url, init) => globalThis.fetch(url, init),
9
+ // globalThis.fetch must be called as a method on globalThis, not through a
10
+ // variable, or browsers throw "Illegal invocation".
11
+ fetch: (url, init) => options.fetch ? options.fetch(url, init) : globalThis.fetch(url, init),
12
12
  baseUrl: options.baseUrl,
13
13
  getAuthToken: options.getAuthToken,
14
14
  getProof: options.getProof,