feat: support overriding fetch function in Connection (#24367)

Co-authored-by: elliott-home-pc <elliott.wagener@mude.com.au>
This commit is contained in:
Elliott W
2022-04-16 16:49:31 +10:00
committed by GitHub
parent 8274959c50
commit 6e03e0e987

View File

@ -1,6 +1,6 @@
import bs58 from 'bs58'; import bs58 from 'bs58';
import {Buffer} from 'buffer'; import {Buffer} from 'buffer';
import fetch from 'cross-fetch'; import crossFetch from 'cross-fetch';
import { import {
type as pick, type as pick,
number, number,
@ -814,9 +814,11 @@ function createRpcClient(
url: string, url: string,
useHttps: boolean, useHttps: boolean,
httpHeaders?: HttpHeaders, httpHeaders?: HttpHeaders,
customFetch?: typeof crossFetch,
fetchMiddleware?: FetchMiddleware, fetchMiddleware?: FetchMiddleware,
disableRetryOnRateLimit?: boolean, disableRetryOnRateLimit?: boolean,
): RpcClient { ): RpcClient {
const fetch = customFetch ? customFetch : crossFetch;
let agentManager: AgentManager | undefined; let agentManager: AgentManager | undefined;
if (!process.env.BROWSER) { if (!process.env.BROWSER) {
agentManager = new AgentManager(useHttps); agentManager = new AgentManager(useHttps);
@ -2108,6 +2110,8 @@ export type ConnectionConfig = {
wsEndpoint?: string; wsEndpoint?: string;
/** Optional HTTP headers object */ /** Optional HTTP headers object */
httpHeaders?: HttpHeaders; httpHeaders?: HttpHeaders;
/** Optional custom fetch function */
fetch?: typeof crossFetch;
/** 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) */
@ -2200,6 +2204,7 @@ export class Connection {
let wsEndpoint; let wsEndpoint;
let httpHeaders; let httpHeaders;
let fetch;
let fetchMiddleware; let fetchMiddleware;
let disableRetryOnRateLimit; let disableRetryOnRateLimit;
if (commitmentOrConfig && typeof commitmentOrConfig === 'string') { if (commitmentOrConfig && typeof commitmentOrConfig === 'string') {
@ -2210,6 +2215,7 @@ export class Connection {
commitmentOrConfig.confirmTransactionInitialTimeout; commitmentOrConfig.confirmTransactionInitialTimeout;
wsEndpoint = commitmentOrConfig.wsEndpoint; wsEndpoint = commitmentOrConfig.wsEndpoint;
httpHeaders = commitmentOrConfig.httpHeaders; httpHeaders = commitmentOrConfig.httpHeaders;
fetch = commitmentOrConfig.fetch;
fetchMiddleware = commitmentOrConfig.fetchMiddleware; fetchMiddleware = commitmentOrConfig.fetchMiddleware;
disableRetryOnRateLimit = commitmentOrConfig.disableRetryOnRateLimit; disableRetryOnRateLimit = commitmentOrConfig.disableRetryOnRateLimit;
} }
@ -2221,6 +2227,7 @@ export class Connection {
url.toString(), url.toString(),
useHttps, useHttps,
httpHeaders, httpHeaders,
fetch,
fetchMiddleware, fetchMiddleware,
disableRetryOnRateLimit, disableRetryOnRateLimit,
); );