Skip to content

Commit

Permalink
import and type migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
legobeat committed Sep 20, 2023
1 parent 6bb6ade commit 0864310
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 26 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
},
"dependencies": {
"@metamask/eth-json-rpc-provider": "^2.1.0",
"@metamask/utils": "^8.1.0",
"@metamask/rpc-errors": "^6.0.0",
"@metamask/json-rpc-engine": "^7.1.1",
"@metamask/rpc-errors": "^6.0.0",
"@metamask/utils": "^8.1.0",
"node-fetch": "^2.6.7"
},
"devDependencies": {
Expand Down
41 changes: 26 additions & 15 deletions src/create-infura-middleware.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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';
import { createAsyncMiddleware } from '@metamask/json-rpc-engine';
import type { JsonRpcError } from '@metamask/rpc-errors';
import { rpcErrors } from '@metamask/rpc-errors';
import type { JsonRpcParams, PendingJsonRpcResponse } from '@metamask/utils';
// eslint-disable-next-line @typescript-eslint/no-shadow
import fetch from 'node-fetch';

Expand Down Expand Up @@ -33,7 +33,7 @@ const RETRIABLE_ERRORS = [
];

/**
* Builds [`json-rpc-engine`](https://github.com/MetaMask/json-rpc-engine)-compatible middleware designed
* Builds [`@metamask/json-rpc-engine`](https://github.com/MetaMask/@metamask/json-rpc-engine)-compatible middleware designed
* for interfacing with Infura's JSON-RPC endpoints.
* @param opts - The options.
* @param opts.network - A network that Infura supports; plugs into
Expand All @@ -44,7 +44,7 @@ const RETRIABLE_ERRORS = [
* by Infura for analytics purposes.
* @param opts.projectId - The Infura project id.
* @param opts.headers - Extra headers that will be used to make the request.
* @returns The `json-rpc-engine`-compatible middleware.
* @returns The `@metamask/json-rpc-engine`-compatible middleware.
*/
export function createInfuraMiddleware({
network = 'mainnet',
Expand Down Expand Up @@ -81,7 +81,16 @@ export function createInfuraMiddleware({
headers,
req,
);
await performFetch(network, projectId, headers, req, res, source);

await performFetch(
network,
projectId,
headers,
req,
// TODO: investigate type mismatch
res as any,
source,
);
// request was successful
break;
} catch (err: any) {
Expand Down Expand Up @@ -146,8 +155,8 @@ async function performFetch(
network: InfuraJsonRpcSupportedNetwork,
projectId: string,
extraHeaders: RequestHeaders,
req: ExtendedJsonRpcRequest<unknown>,
res: PendingJsonRpcResponse<unknown>,
req: ExtendedJsonRpcRequest<JsonRpcParams>,
res: PendingJsonRpcResponse<JsonRpcParams>,
source: string | undefined,
): Promise<void> {
const { fetchUrl, fetchParams } = fetchConfigFromReq({
Expand All @@ -163,7 +172,7 @@ async function performFetch(
if (!response.ok) {
switch (response.status) {
case 405:
throw ethErrors.rpc.methodNotFound();
throw rpcErrors.methodNotFound();

case 429:
throw createRatelimitError();
Expand All @@ -179,7 +188,9 @@ async function performFetch(

// special case for now
if (req.method === 'eth_getBlockByNumber' && rawData === 'Not Found') {
res.result = null;
// TODO Would this be more correct?
// delete res.result;
res.result = null as any as JsonRpcParams;
return;
}

Expand All @@ -196,7 +207,7 @@ async function performFetch(
* error.
* @returns The error object.
*/
function createRatelimitError(): EthereumRpcError<undefined> {
function createRatelimitError(): JsonRpcError<undefined> {
const msg = `Request is being rate limited.`;
return createInternalError(msg);
}
Expand All @@ -205,7 +216,7 @@ function createRatelimitError(): EthereumRpcError<undefined> {
* Builds a JSON-RPC 2.0 internal error object describing a timeout error.
* @returns The error object.
*/
function createTimeoutError(): EthereumRpcError<undefined> {
function createTimeoutError(): JsonRpcError<undefined> {
let msg = `Gateway timeout. The request took too long to process. `;
msg += `This can happen when querying logs over too wide a block range.`;
return createInternalError(msg);
Expand All @@ -216,8 +227,8 @@ function createTimeoutError(): EthereumRpcError<undefined> {
* @param msg - The message.
* @returns The error object.
*/
function createInternalError(msg: string): EthereumRpcError<undefined> {
return ethErrors.rpc.internal(msg);
function createInternalError(msg: string): JsonRpcError<undefined> {
return rpcErrors.internal(msg);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/create-provider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { providerFromEngine } from '@metamask/eth-json-rpc-provider';
import type { SafeEventEmitterProvider } from '@metamask/eth-json-rpc-provider';
import { JsonRpcEngine } from 'json-rpc-engine';
import { JsonRpcEngine } from '@metamask/json-rpc-engine';

import type { CreateInfuraMiddlewareOptions } from './create-infura-middleware';
import { createInfuraMiddleware } from './create-infura-middleware';
Expand Down
8 changes: 4 additions & 4 deletions src/fetch-config-from-req.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { JsonRpcRequest } from 'json-rpc-engine';
import type { JsonRpcParams, JsonRpcRequest } from '@metamask/utils';

import type {
ExtendedJsonRpcRequest,
Expand Down Expand Up @@ -41,7 +41,7 @@ export function fetchConfigFromReq({
network: InfuraJsonRpcSupportedNetwork;
projectId: string;
extraHeaders?: RequestHeaders;
req: ExtendedJsonRpcRequest<unknown>;
req: ExtendedJsonRpcRequest<JsonRpcParams>;
source?: string;
}): FetchConfig {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
Expand Down Expand Up @@ -72,8 +72,8 @@ export function fetchConfigFromReq({
* @returns An object that describes a JSON-RPC request.
*/
function normalizeReq(
req: ExtendedJsonRpcRequest<unknown>,
): JsonRpcRequest<unknown> {
req: ExtendedJsonRpcRequest<JsonRpcParams>,
): JsonRpcRequest {
return {
id: req.id,
jsonrpc: req.jsonrpc,
Expand Down
7 changes: 3 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import type { JsonRpcRequest } from 'json-rpc-engine';
import type { JsonRpcParams, JsonRpcRequest } from '@metamask/utils';

export type RequestHeaders = Record<string, string>;

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

/**
* These are networks:
Expand Down

0 comments on commit 0864310

Please sign in to comment.