Skip to content

Commit

Permalink
lint:fix
Browse files Browse the repository at this point in the history
  • Loading branch information
legobeat committed Sep 11, 2023
1 parent 9ad49a8 commit df6af39
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 31 deletions.
19 changes: 11 additions & 8 deletions src/create-infura-middleware.ts
Original file line number Diff line number Diff line change
@@ -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<string, string>;
}
};

const log = createModuleLogger(projectLogger, 'create-infura-middleware');
const RETRIABLE_ERRORS = [
Expand Down Expand Up @@ -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}"`);
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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<void> {
async function timeout(length: number): Promise<void> {
return new Promise((resolve) => {
setTimeout(resolve, length);
});
Expand Down
9 changes: 4 additions & 5 deletions src/create-provider.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
20 changes: 5 additions & 15 deletions src/fetch-config-from-req.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions src/fetch-config-from-req.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -44,6 +45,7 @@ export function fetchConfigFromReq({
req: ExtendedJsonRpcRequest<unknown>;
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',
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { JsonRpcRequest } from 'json-rpc-engine';

export type RequestHeaders = Record<string, string>;

export type ExtendedJsonRpcRequest<T> = JsonRpcRequest<T> & { origin?: string };
export type ExtendedJsonRpcRequest<Params> = JsonRpcRequest<Params> & { origin?: string };

/**
* These are networks:
Expand Down

0 comments on commit df6af39

Please sign in to comment.