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 8, 2023
1 parent 2b0f99c commit 513691b
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 24 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 { 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 type { JsonRpcParams, PendingJsonRpcResponse } from '@metamask/utils';
import { createAsyncMiddleware } from '@metamask/json-rpc-engine';
import type { JsonRpcError } from '@metamask/rpc-errors';
import { rpcErrors } from '@metamask/rpc-errors';
import fetch from 'node-fetch';
import type {
ExtendedJsonRpcRequest,
Expand Down Expand Up @@ -31,7 +31,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.
Expand All @@ -43,7 +43,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 @@ -79,7 +79,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 @@ -143,8 +152,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 @@ -160,7 +169,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 @@ -176,7 +185,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 @@ -194,7 +205,7 @@ async function performFetch(
*
* @returns The error object.
*/
function createRatelimitError(): EthereumRpcError<undefined> {
function createRatelimitError(): JsonRpcError<undefined> {
const msg = `Request is being rate limited.`;
return createInternalError(msg);
}
Expand All @@ -204,7 +215,7 @@ function createRatelimitError(): EthereumRpcError<undefined> {
*
* @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,4 +1,4 @@
import { JsonRpcEngine } from 'json-rpc-engine';
import { JsonRpcEngine } from '@metamask/json-rpc-engine';
import { providerFromEngine } from '@metamask/eth-json-rpc-provider';
import type { SafeEventEmitterProvider } from '@metamask/eth-json-rpc-provider';
import {
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,
RequestHeaders,
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 {
const requestOrigin = req.origin || 'internal';
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<JsonRpcParams> {
return {
id: req.id,
jsonrpc: req.jsonrpc,
Expand Down
5 changes: 3 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +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<T> = JsonRpcRequest<T> & { origin?: string };
export type ExtendedJsonRpcRequest<Params extends JsonRpcParams> =
JsonRpcRequest<Params> & { origin?: string };

/**
* These are networks:
Expand Down

0 comments on commit 513691b

Please sign in to comment.