diff --git a/web3.js/src/connection.ts b/web3.js/src/connection.ts index 3ec1ce6dc4578f..88f668e417e1d8 100644 --- a/web3.js/src/connection.ts +++ b/web3.js/src/connection.ts @@ -1,6 +1,5 @@ import bs58 from 'bs58'; import {Buffer} from 'buffer'; -import fetch from 'cross-fetch'; import type {Response} from 'cross-fetch'; import { type as pick, @@ -751,6 +750,7 @@ export type PerfSample = { function createRpcClient( url: string, useHttps: boolean, + _fetch: typeof fetch, httpHeaders?: HttpHeaders, fetchMiddleware?: FetchMiddleware, disableRetryOnRateLimit?: boolean, @@ -767,7 +767,7 @@ function createRpcClient( return new Promise((resolve, reject) => { fetchMiddleware(url, options, async (url: string, options: any) => { try { - resolve(await fetch(url, options)); + resolve(await _fetch(url, options)); } catch (error) { reject(error); } @@ -798,7 +798,7 @@ function createRpcClient( if (fetchWithMiddleware) { res = await fetchWithMiddleware(url, options); } else { - res = await fetch(url, options); + res = await _fetch(url, options); } if (res.status !== 429 /* Too many requests */) { @@ -2067,10 +2067,12 @@ export class Connection { * * @param endpoint URL to the fullnode JSON RPC endpoint * @param commitmentOrConfig optional default commitment level or optional ConnectionConfig configuration object + * @param fetch optional fetch compatible [API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) for making a request */ constructor( endpoint: string, commitmentOrConfig?: Commitment | ConnectionConfig, + _fetch?: typeof fetch, ) { let url = new URL(endpoint); const useHttps = url.protocol === 'https:'; @@ -2097,6 +2099,7 @@ export class Connection { this._rpcClient = createRpcClient( url.toString(), useHttps, + _fetch || require('cross-fetch'), httpHeaders, fetchMiddleware, disableRetryOnRateLimit, diff --git a/web3.js/test/connection.test.ts b/web3.js/test/connection.test.ts index ed52f4accc5ffe..2cff5b6a6769c5 100644 --- a/web3.js/test/connection.test.ts +++ b/web3.js/test/connection.test.ts @@ -3338,5 +3338,20 @@ describe('Connection', () => { const version = await connection.getVersion(); expect(version['solana-core']).to.be.ok; }); + + it('custom fetch option', async () => { + let count = 0; + const connection = new Connection( + 'https://api.mainnet-beta.solana.com', + {}, + (input, init) => { + count++; + return fetch(input, init); + }, + ); + const version = await connection.getVersion(); + expect(version['solana-core']).to.be.ok; + expect(count).to.eq(1); + }); } });