Skip to content

Commit 4952763

Browse files
authored
Merge pull request #901 from NilFoundation/fix-fetch-call
fix: fix calling fetch in niljs http transport
2 parents 0563822 + e734e72 commit 4952763

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

niljs/src/transport/HttpTransport.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ import { requestHeadersWithDefaults } from "../utils/rpc.js";
33
import type { IHttpTransportConfig } from "./types/IHttpTransportConfig.js";
44
import type { ITransport } from "./types/ITransport.js";
55

6+
const getIsomorphicFetch = () => {
7+
if (typeof window !== "undefined") {
8+
return window.fetch.bind(window);
9+
}
10+
11+
if (typeof globalThis !== "undefined") {
12+
return globalThis.fetch;
13+
}
14+
15+
throw new Error("No fetch implementation found");
16+
};
17+
618
/**
719
* HttpTransport represents the HTTP transport for connecting to the network.
820
*
@@ -35,7 +47,7 @@ class HttpTransport implements ITransport {
3547
this.endpoint = endpoint;
3648
this.timeout = timeout;
3749
this.headers = requestHeadersWithDefaults(headers);
38-
this.fetcher = fetcher || globalThis.fetch;
50+
this.fetcher = fetcher || getIsomorphicFetch();
3951
}
4052

4153
/**

0 commit comments

Comments
 (0)