diff --git a/web3.js/src/connection.ts b/web3.js/src/connection.ts index 0f9b788649a882..1aea062f23343a 100644 --- a/web3.js/src/connection.ts +++ b/web3.js/src/connection.ts @@ -761,27 +761,6 @@ function createRpcClient( agentManager = new AgentManager(useHttps); } - let fetchWithMiddleware: - | ((url: string, options: any) => Promise) - | undefined; - - if (fetchMiddleware) { - fetchWithMiddleware = async (url: string, options: any) => { - const modifiedFetchArgs = await new Promise<[string, any]>( - (resolve, reject) => { - try { - fetchMiddleware(url, options, (modifiedUrl, modifiedOptions) => - resolve([modifiedUrl, modifiedOptions]), - ); - } catch (error) { - reject(error); - } - }, - ); - return await fetch(...modifiedFetchArgs); - }; - } - const clientBrowser = new RpcClient(async (request, callback) => { const agent = agentManager ? agentManager.requestStart() : undefined; const options = { @@ -801,8 +780,8 @@ function createRpcClient( let res: Response; let waitTime = 500; for (;;) { - if (fetchWithMiddleware) { - res = await fetchWithMiddleware(url, options); + if (fetchMiddleware) { + res = await fetchMiddleware(url, options); } else { res = await fetch(url, options); } @@ -1988,9 +1967,8 @@ export type HttpHeaders = {[header: string]: string}; */ export type FetchMiddleware = ( url: string, - options: any, - fetch: (modifiedUrl: string, modifiedOptions: any) => void, -) => void; + options: any +) => Promise; /** * Configuration for instantiating a Connection diff --git a/web3.js/test/connection.test.ts b/web3.js/test/connection.test.ts index e34ccc0f953d26..754af9f234b5da 100644 --- a/web3.js/test/connection.test.ts +++ b/web3.js/test/connection.test.ts @@ -3,7 +3,7 @@ import {Buffer} from 'buffer'; import {Token, u64} from '@solana/spl-token'; import {expect, use} from 'chai'; import chaiAsPromised from 'chai-as-promised'; - +import fetch from 'cross-fetch'; import { Account, Authorized, @@ -112,11 +112,11 @@ describe('Connection', () => { it('should allow middleware to augment request', async () => { let connection = new Connection(url, { - fetchMiddleware: (url, options, fetch) => { + fetchMiddleware: async (url, options) => { options.headers = Object.assign(options.headers, { Authorization: 'Bearer 123', }); - fetch(url, options); + return await fetch(url, options); }, }); @@ -136,7 +136,7 @@ describe('Connection', () => { it('should attribute middleware fatals to the middleware', async () => { let connection = new Connection(url, { // eslint-disable-next-line @typescript-eslint/no-unused-vars - fetchMiddleware: (_url, _options, _fetch) => { + fetchMiddleware: (_url, _options) => { throw new Error('This middleware experienced a fatal error'); }, }); @@ -151,8 +151,8 @@ describe('Connection', () => { it('should not attribute fetch errors to the middleware', async () => { let connection = new Connection(url, { - fetchMiddleware: (url, _options, fetch) => { - fetch(url, 'An `Object` was expected here; this is a `TypeError`.'); + fetchMiddleware: async (url, _options) => { + return await fetch(url, 'An `Object` was expected here; this is a `TypeError`.' as RequestInit); }, }); const error = await expect(connection.getVersion()).to.be.rejected;