Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Improved flexibility of FetchMiddleware for Web3.js #22433

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 4 additions & 26 deletions web3.js/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -761,27 +761,6 @@ function createRpcClient(
agentManager = new AgentManager(useHttps);
}

let fetchWithMiddleware:
| ((url: string, options: any) => Promise<Response>)
| 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 = {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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<Response>;

/**
* Configuration for instantiating a Connection
Expand Down
12 changes: 6 additions & 6 deletions web3.js/test/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
},
});

Expand All @@ -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');
},
});
Expand All @@ -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;
Expand Down