diff --git a/src/create-infura-middleware.ts b/src/create-infura-middleware.ts index 1115f98..dd1edb8 100644 --- a/src/create-infura-middleware.ts +++ b/src/create-infura-middleware.ts @@ -1,23 +1,25 @@ -import type { PendingJsonRpcResponse } from 'json-rpc-engine'; -import { createAsyncMiddleware } from 'json-rpc-engine'; import type { EthereumRpcError } from 'eth-rpc-errors'; import { ethErrors } from 'eth-rpc-errors'; +import { createAsyncMiddleware } from 'json-rpc-engine'; +import type { PendingJsonRpcResponse } from 'json-rpc-engine'; +// eslint-disable-next-line @typescript-eslint/no-shadow import fetch from 'node-fetch'; + +import { fetchConfigFromReq } from './fetch-config-from-req'; +import { projectLogger, createModuleLogger } from './logging-utils'; import type { ExtendedJsonRpcRequest, InfuraJsonRpcSupportedNetwork, RequestHeaders, } from './types'; -import { fetchConfigFromReq } from './fetch-config-from-req'; -import { projectLogger, createModuleLogger } from './logging-utils'; -export interface CreateInfuraMiddlewareOptions { +export type CreateInfuraMiddlewareOptions = { network?: InfuraJsonRpcSupportedNetwork; maxAttempts?: number; source?: string; projectId: string; headers?: Record; -} +}; const log = createModuleLogger(projectLogger, 'create-infura-middleware'); const RETRIABLE_ERRORS = [ @@ -58,6 +60,7 @@ export function createInfuraMiddleware({ } if (!headers || typeof headers !== 'object') { + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions throw new Error(`Invalid value for 'headers': "${headers}"`); } @@ -104,7 +107,7 @@ export function createInfuraMiddleware({ res, err, ); - const errMsg = `InfuraProvider - cannot complete request. All retries exhausted.\nOriginal Error:\n${err.toString()}\n\n`; + const errMsg = `InfuraProvider - cannot complete request. All retries exhausted.\nOriginal Error:\n${err.toString() as string}\n\n`; const retriesExhaustedErr = new Error(errMsg); throw retriesExhaustedErr; } @@ -240,7 +243,7 @@ function isRetriableError(err: any): boolean { * @param length - The number of milliseconds to wait. * @returns A promise that resolves after the given time has elapsed. */ -function timeout(length: number): Promise { +async function timeout(length: number): Promise { return new Promise((resolve) => { setTimeout(resolve, length); }); diff --git a/src/create-provider.ts b/src/create-provider.ts index a28e9e0..9c9d2c7 100644 --- a/src/create-provider.ts +++ b/src/create-provider.ts @@ -1,10 +1,9 @@ -import { JsonRpcEngine } from 'json-rpc-engine'; import { providerFromEngine } from '@metamask/eth-json-rpc-provider'; import type { SafeEventEmitterProvider } from '@metamask/eth-json-rpc-provider'; -import { - createInfuraMiddleware, - CreateInfuraMiddlewareOptions, -} from './create-infura-middleware'; +import { JsonRpcEngine } from 'json-rpc-engine'; + +import type { CreateInfuraMiddlewareOptions } from './create-infura-middleware'; +import { createInfuraMiddleware } from './create-infura-middleware'; /** * Creates a provider (as defined in diff --git a/src/fetch-config-from-req.test.ts b/src/fetch-config-from-req.test.ts index 6802b1d..4dccbf5 100644 --- a/src/fetch-config-from-req.test.ts +++ b/src/fetch-config-from-req.test.ts @@ -17,9 +17,7 @@ describe('fetchConfigFromReq', () => { body: JSON.parse(fetchParams.body), }; - expect(fetchUrl).toStrictEqual( - 'https://mainnet.infura.io/v3/abcdef1234567890', - ); + expect(fetchUrl).toBe('https://mainnet.infura.io/v3/abcdef1234567890'); expect(decodedFetchParams).toStrictEqual({ method: 'POST', @@ -53,9 +51,7 @@ describe('fetchConfigFromReq', () => { body: JSON.parse(fetchParams.body), }; - expect(fetchUrl).toStrictEqual( - 'https://mainnet.infura.io/v3/abcdef1234567890', - ); + expect(fetchUrl).toBe('https://mainnet.infura.io/v3/abcdef1234567890'); expect(decodedFetchParams).toStrictEqual({ method: 'POST', @@ -91,9 +87,7 @@ describe('fetchConfigFromReq', () => { body: JSON.parse(fetchParams.body), }; - expect(fetchUrl).toStrictEqual( - 'https://mainnet.infura.io/v3/abcdef1234567890', - ); + expect(fetchUrl).toBe('https://mainnet.infura.io/v3/abcdef1234567890'); expect(decodedFetchParams).toStrictEqual({ method: 'POST', @@ -130,9 +124,7 @@ describe('fetchConfigFromReq', () => { body: JSON.parse(fetchParams.body), }; - expect(fetchUrl).toStrictEqual( - 'https://mainnet.infura.io/v3/abcdef1234567890', - ); + expect(fetchUrl).toBe('https://mainnet.infura.io/v3/abcdef1234567890'); expect(decodedFetchParams).toStrictEqual({ method: 'POST', @@ -167,9 +159,7 @@ describe('fetchConfigFromReq', () => { body: JSON.parse(fetchParams.body), }); - expect(fetchUrl).toStrictEqual( - 'https://mainnet.infura.io/v3/abcdef1234567890', - ); + expect(fetchUrl).toBe('https://mainnet.infura.io/v3/abcdef1234567890'); expect(decodedFetchParams.body).toStrictEqual({ id: 1, diff --git a/src/fetch-config-from-req.ts b/src/fetch-config-from-req.ts index e0a31fa..22615d8 100644 --- a/src/fetch-config-from-req.ts +++ b/src/fetch-config-from-req.ts @@ -1,18 +1,19 @@ import type { JsonRpcRequest } from 'json-rpc-engine'; + import type { ExtendedJsonRpcRequest, RequestHeaders, InfuraJsonRpcSupportedNetwork, } from './types'; -interface FetchConfig { +type FetchConfig = { fetchUrl: string; fetchParams: { method: string; headers: RequestHeaders; body: string; }; -} +}; /** * Determines the arguments to feed into `fetch` in order to make a request to @@ -44,6 +45,7 @@ export function fetchConfigFromReq({ req: ExtendedJsonRpcRequest; source?: string; }): FetchConfig { + // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing const requestOrigin = req.origin || 'internal'; const headers = Object.assign({}, extraHeaders, { Accept: 'application/json', diff --git a/src/types.ts b/src/types.ts index 1b4748e..b6e3aaa 100644 --- a/src/types.ts +++ b/src/types.ts @@ -2,7 +2,7 @@ import type { JsonRpcRequest } from 'json-rpc-engine'; export type RequestHeaders = Record; -export type ExtendedJsonRpcRequest = JsonRpcRequest & { origin?: string }; +export type ExtendedJsonRpcRequest = JsonRpcRequest & { origin?: string }; /** * These are networks: