Skip to content

Commit f2031cd

Browse files
authored
chore: Simplify RPC service type (#127)
This applies the changes made in MetaMask/eth-json-rpc-middleware#407 to this package. This type simplification reduces the chances that we'll need to update this again in the future.
1 parent efbd6cb commit f2031cd

File tree

5 files changed

+26
-91
lines changed

5 files changed

+26
-91
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88
### Changed
9+
- Simplify `rpcService` parameter type of `createInfuraMiddleware` ([#127](https://github.com/MetaMask/eth-json-rpc-infura/pull/127))
910
- Bump `@metamask/eth-json-rpc-provider` from `^4.1.7` to `^5.0.0` ([#126](https://github.com/MetaMask/eth-json-rpc-infura/pull/126))
1011
- Bump `@metamask/json-rpc-engine` from `^10.0.2` to `^10.1.0` ([#126](https://github.com/MetaMask/eth-json-rpc-infura/pull/126))
1112

src/create-infura-middleware.test.ts

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { JsonRpcEngine } from '@metamask/json-rpc-engine';
22
import type { Json, JsonRpcParams, JsonRpcRequest } from '@metamask/utils';
33

44
import { createInfuraMiddleware } from '.';
5-
import type { AbstractRpcService } from './types';
5+
import type { AbstractRpcServiceLike } from './types';
66

77
describe('createInfuraMiddleware (given an RPC service)', () => {
88
it('calls the RPC service with the correct request headers and body when no `source` option given', async () => {
@@ -325,10 +325,8 @@ describe('createInfuraMiddleware (given an RPC endpoint)', () => {
325325
* Constructs a fake RPC service for use as a failover in tests.
326326
* @returns The fake failover service.
327327
*/
328-
function buildRpcService(): AbstractRpcService {
328+
function buildRpcService(): AbstractRpcServiceLike {
329329
return {
330-
endpointUrl: new URL('https://metamask.test'),
331-
332330
async request<Params extends JsonRpcParams, Result extends Json>(
333331
jsonRpcRequest: JsonRpcRequest<Params>,
334332
_fetchOptions?: RequestInit,
@@ -339,26 +337,5 @@ function buildRpcService(): AbstractRpcService {
339337
result: 'ok' as Result,
340338
};
341339
},
342-
onRetry() {
343-
return {
344-
dispose() {
345-
// do nothing
346-
},
347-
};
348-
},
349-
onBreak() {
350-
return {
351-
dispose() {
352-
// do nothing
353-
},
354-
};
355-
},
356-
onDegraded() {
357-
return {
358-
dispose() {
359-
// do nothing
360-
},
361-
};
362-
},
363340
};
364341
}

src/create-infura-middleware.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
import { fetchConfigFromReq } from './fetch-config-from-req';
1212
import { projectLogger, createModuleLogger } from './logging-utils';
1313
import type {
14-
AbstractRpcService,
14+
AbstractRpcServiceLike,
1515
ExtendedJsonRpcRequest,
1616
InfuraJsonRpcSupportedNetwork,
1717
RequestHeaders,
@@ -56,7 +56,7 @@ const RETRIABLE_ERRORS = [
5656
* @returns The fetch middleware.
5757
*/
5858
export function createInfuraMiddleware(args: {
59-
rpcService: AbstractRpcService;
59+
rpcService: AbstractRpcServiceLike;
6060
options?: {
6161
source?: string;
6262
headers?: Headers;
@@ -89,7 +89,7 @@ export function createInfuraMiddleware(
8989
export function createInfuraMiddleware(
9090
args:
9191
| {
92-
rpcService: AbstractRpcService;
92+
rpcService: AbstractRpcServiceLike;
9393
options?: {
9494
source?: string;
9595
headers?: Headers;
@@ -118,7 +118,7 @@ function createInfuraMiddlewareWithRpcService({
118118
rpcService,
119119
options = {},
120120
}: {
121-
rpcService: AbstractRpcService;
121+
rpcService: AbstractRpcServiceLike;
122122
options?: {
123123
source?: string;
124124
headers?: Headers;

src/types.test-d.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import type { AbstractRpcService as NetworkControllerAbstractRpcService } from '@metamask/network-controller';
1+
import type { AbstractRpcService } from '@metamask/network-controller';
22
import { expectAssignable } from 'tsd';
33

4-
import type { AbstractRpcService as LocalAbstractRpcService } from './types';
4+
import type { AbstractRpcServiceLike } from './types';
55

6-
// Confirm that the AbstractRpcService in this repo is compatible with the same
7-
// one in `@metamask/network-controller` (from where it was copied)
8-
declare const rpcService: LocalAbstractRpcService;
9-
expectAssignable<NetworkControllerAbstractRpcService>(rpcService);
6+
// Confirm that the `AbstractRpcServiceLike` type in this repo is compatible
7+
// with the type of the `request` method from `AbstractRpcService` in
8+
// `@metamask/network-controller` (from where it was copied).
9+
declare const networkControllerRpcService: AbstractRpcService;
10+
expectAssignable<AbstractRpcServiceLike>(networkControllerRpcService);

src/types.ts

Lines changed: 12 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -46,63 +46,19 @@ export type InfuraJsonRpcSupportedNetwork =
4646
| 'sei-testnet';
4747

4848
/**
49-
* The interface for a service class responsible for making a request to an RPC
50-
* endpoint.
49+
* A copy of the `AbstractRpcService` type in metamask/network-controller`, but
50+
* keeping only the `request` method.
51+
*
52+
* We cannot get `AbstractRpcService` directly from
53+
* `@metamask/network-controller` because relying on this package would create a
54+
* circular dependency.
55+
*
56+
* This type should be accurate as of `@metamask/network-controller` 24.x and
57+
* `@metamask/utils` 11.x.
5158
*/
52-
export type AbstractRpcService = {
53-
/**
54-
* The URL of the RPC endpoint.
55-
*/
56-
endpointUrl: URL;
57-
58-
/**
59-
* Listens for when the RPC service retries the request.
60-
* @param listener - The callback to be called when the retry occurs.
61-
* @returns A disposable.
62-
*/
63-
onRetry: (
64-
listener: (
65-
data: ({ error: Error } | { value: unknown }) & {
66-
delay: number;
67-
attempt: number;
68-
endpointUrl: string;
69-
},
70-
) => void,
71-
) => {
72-
dispose(): void;
73-
};
74-
75-
/**
76-
* Listens for when the RPC service retries the request too many times in a
77-
* row.
78-
* @param listener - The callback to be called when the circuit is broken.
79-
* @returns A disposable.
80-
*/
81-
onBreak: (
82-
listener: (
83-
data: ({ error: Error } | { value: unknown } | { isolated: true }) & {
84-
endpointUrl: string;
85-
},
86-
) => void,
87-
) => {
88-
dispose(): void;
89-
};
90-
91-
/**
92-
* Listens for when the policy underlying this RPC service detects a slow
93-
* request.
94-
* @param listener - The callback to be called when the request is slow.
95-
* @returns A disposable.
96-
*/
97-
onDegraded: (listener: (data: { endpointUrl: string }) => void) => {
98-
dispose(): void;
99-
};
100-
101-
/**
102-
* Makes a request to the RPC endpoint.
103-
*/
104-
request<Params extends JsonRpcParams, Result extends Json>(
59+
export type AbstractRpcServiceLike = {
60+
request: <Params extends JsonRpcParams, Result extends Json>(
10561
jsonRpcRequest: JsonRpcRequest<Params>,
10662
fetchOptions?: RequestInit,
107-
): Promise<JsonRpcResponse<Result | null>>;
63+
) => Promise<JsonRpcResponse<Result | null>>;
10864
};

0 commit comments

Comments
 (0)