fix: correct the types of the fetch infra in web3.js

This commit is contained in:
steveluscher 2022-05-26 13:17:14 -07:00 committed by Steven Luscher
parent 994ae755a9
commit 9f1b876c74
1 changed files with 12 additions and 12 deletions

View File

@ -955,7 +955,7 @@ function createRpcClient(
url: string, url: string,
useHttps: boolean, useHttps: boolean,
httpHeaders?: HttpHeaders, httpHeaders?: HttpHeaders,
customFetch?: typeof crossFetch, customFetch?: FetchFn,
fetchMiddleware?: FetchMiddleware, fetchMiddleware?: FetchMiddleware,
disableRetryOnRateLimit?: boolean, disableRetryOnRateLimit?: boolean,
): RpcClient { ): RpcClient {
@ -965,17 +965,15 @@ function createRpcClient(
agentManager = new AgentManager(useHttps); agentManager = new AgentManager(useHttps);
} }
let fetchWithMiddleware: let fetchWithMiddleware: FetchFn | undefined;
| ((url: string, options: any) => Promise<Response>)
| undefined;
if (fetchMiddleware) { if (fetchMiddleware) {
fetchWithMiddleware = async (url: string, options: any) => { fetchWithMiddleware = async (info, init) => {
const modifiedFetchArgs = await new Promise<[string, any]>( const modifiedFetchArgs = await new Promise<Parameters<FetchFn>>(
(resolve, reject) => { (resolve, reject) => {
try { try {
fetchMiddleware(url, options, (modifiedUrl, modifiedOptions) => fetchMiddleware(info, init, (modifiedInfo, modifiedInit) =>
resolve([modifiedUrl, modifiedOptions]), resolve([modifiedInfo, modifiedInit]),
); );
} catch (error) { } catch (error) {
reject(error); reject(error);
@ -2162,13 +2160,15 @@ export type ConfirmedSignatureInfo = {
*/ */
export type HttpHeaders = {[header: string]: string}; export type HttpHeaders = {[header: string]: string};
export type FetchFn = typeof crossFetch;
/** /**
* A callback used to augment the outgoing HTTP request * A callback used to augment the outgoing HTTP request
*/ */
export type FetchMiddleware = ( export type FetchMiddleware = (
url: string, info: Parameters<FetchFn>[0],
options: any, init: Parameters<FetchFn>[1],
fetch: (modifiedUrl: string, modifiedOptions: any) => void, fetch: (...a: Parameters<FetchFn>) => void,
) => void; ) => void;
/** /**
@ -2182,7 +2182,7 @@ export type ConnectionConfig = {
/** Optional HTTP headers object */ /** Optional HTTP headers object */
httpHeaders?: HttpHeaders; httpHeaders?: HttpHeaders;
/** Optional custom fetch function */ /** Optional custom fetch function */
fetch?: typeof crossFetch; fetch?: FetchFn;
/** Optional fetch middleware callback */ /** Optional fetch middleware callback */
fetchMiddleware?: FetchMiddleware; fetchMiddleware?: FetchMiddleware;
/** Optional Disable retrying calls when server responds with HTTP 429 (Too Many Requests) */ /** Optional Disable retrying calls when server responds with HTTP 429 (Too Many Requests) */