Skip to content

Commit c24d3c0

Browse files
committed
Simplify and upgrade expected RpcService type
`createFetchMiddleware` needs to declare that it takes an `RpcService`, but it cannot get a type from `@metamask/network-controller` or else this would create a circular dependency. Currently this package contains a copy of `AbstractRpcService` from `@metamask/network-controller` 22.2.0, but there are a couple of problems: 1) this type is not compatible with the latest version of the package, and 2) we want to avoid further incompatibilities in the future. This PR changes the signature for `createFetchMiddleware` so that it requires a simplified version of `RpcService` — one that only contains a `request` method. It also upgrades the dev dependency on `@metamask/network-controller` to 24.1.0.
1 parent badb634 commit c24d3c0

File tree

7 files changed

+84
-171
lines changed

7 files changed

+84
-171
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- **BREAKING:** `createFetchMiddleware` no longer takes `fetch`, `btoa`, `rpcUrl`, and `originHttpHeaderKey` ([#402](https://github.com/MetaMask/eth-json-rpc-middleware/pull/402))
1313
- The existing signature (`rpcService` and `options`) is now the only way to use this function; please use that instead.
14+
- **BREAKING:** `createFetchMiddleware` now takes an RPC service object that must be compatible with `@metamask/network-controller` 24.x and `@metamask/utils` 11.x ([#403](https://github.com/MetaMask/eth-json-rpc-middleware/pull/403))
15+
- On the other hand, the only required property for this object now is `request`; all other properties will be ignored.
1416
- Bump `@metamask/utils` to `^11.7.0` ([#404](https://github.com/MetaMask/eth-json-rpc-middleware/pull/404))
1517

1618
### Removed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@
4646
"@jest/globals": "^27.5.1",
4747
"@lavamoat/allow-scripts": "^3.0.4",
4848
"@metamask/auto-changelog": "^3.1.0",
49+
"@metamask/error-reporting-service": "^2.0.0",
4950
"@metamask/eslint-config": "^12.1.0",
5051
"@metamask/eslint-config-jest": "^12.1.0",
5152
"@metamask/eslint-config-nodejs": "^12.1.0",
5253
"@metamask/eslint-config-typescript": "^12.1.0",
53-
"@metamask/network-controller": "22.2.0",
54+
"@metamask/network-controller": "^24.1.0",
5455
"@types/btoa": "^1.2.3",
5556
"@types/jest": "^27.4.1",
5657
"@types/node": "^18.16",

src/fetch.test.ts

Lines changed: 2 additions & 23 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 { createFetchMiddleware } from './fetch';
5-
import type { AbstractRpcService } from './types';
5+
import type { AbstractRpcServiceRequestMethod } from './types';
66

77
describe('createFetchMiddleware', () => {
88
it('calls the RPC service with the correct request headers and body when no `originHttpHeaderKey` option given', async () => {
@@ -236,7 +236,7 @@ describe('createFetchMiddleware', () => {
236236
*
237237
* @returns The fake failover service.
238238
*/
239-
function buildRpcService(): AbstractRpcService {
239+
function buildRpcService(): { request: AbstractRpcServiceRequestMethod } {
240240
return {
241241
async request<Params extends JsonRpcParams, Result extends Json>(
242242
jsonRpcRequest: JsonRpcRequest<Params>,
@@ -248,26 +248,5 @@ function buildRpcService(): AbstractRpcService {
248248
result: 'ok' as Result,
249249
};
250250
},
251-
onRetry() {
252-
return {
253-
dispose() {
254-
// do nothing
255-
},
256-
};
257-
},
258-
onBreak() {
259-
return {
260-
dispose() {
261-
// do nothing
262-
},
263-
};
264-
},
265-
onDegraded() {
266-
return {
267-
dispose() {
268-
// do nothing
269-
},
270-
};
271-
},
272251
};
273252
}

src/fetch.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { createAsyncMiddleware } from '@metamask/json-rpc-engine';
33
import { rpcErrors } from '@metamask/rpc-errors';
44
import type { Json, JsonRpcParams, JsonRpcRequest } from '@metamask/utils';
55

6-
import type { AbstractRpcService } from './types';
6+
import type { AbstractRpcServiceRequestMethod } from './types';
77

88
/**
99
* Like a JSON-RPC request, but includes an optional `origin` property.
@@ -20,6 +20,7 @@ type JsonRpcRequestWithOrigin<Params extends JsonRpcParams> =
2020
*
2121
* @param args - The arguments to this function.
2222
* @param args.rpcService - The RPC service to use.
23+
* @param args.rpcService.request - The `request` method of the RPC service.
2324
* @param args.options - Options.
2425
* @param args.options.originHttpHeaderKey - If provided, the origin field for
2526
* each JSON-RPC request will be attached to each outgoing fetch request under
@@ -30,7 +31,7 @@ export function createFetchMiddleware({
3031
rpcService,
3132
options = {},
3233
}: {
33-
rpcService: AbstractRpcService;
34+
rpcService: { request: AbstractRpcServiceRequestMethod };
3435
options?: {
3536
originHttpHeaderKey?: string;
3637
};

src/types.test-d.ts

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

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

66
// Confirm that the AbstractRpcService in this repo is compatible with the same
77
// one in `@metamask/network-controller` (from where it was copied)
8-
declare const rpcService: LocalAbstractRpcService;
9-
expectAssignable<NetworkControllerAbstractRpcService>(rpcService);
8+
declare const rpcServiceMethod: AbstractRpcServiceRequestMethod;
9+
expectAssignable<NetworkControllerAbstractRpcService['request']>(
10+
rpcServiceMethod,
11+
);

src/types.ts

Lines changed: 15 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -30,61 +30,19 @@ export type BlockCache = Record<string, Block>;
3030
export type Cache = Record<number, BlockCache>;
3131

3232
/**
33-
* The interface for a service class responsible for making a request to an RPC
34-
* endpoint.
33+
* A copy of the type for the `request` method from `AbstractRpcService` in
34+
*`@metamask/network-controller`.
35+
*
36+
* We cannot get this directly from `@metamask/network-controller` because
37+
* relying on this package would create a circular dependency.
38+
*
39+
* This type should be accurate as of `@metamask/network-controller` 24.x and
40+
* `@metamask/utils` 11.x.
3541
*/
36-
export type AbstractRpcService = {
37-
/**
38-
* Listens for when the RPC service retries the request.
39-
*
40-
* @param listener - The callback to be called when the retry occurs.
41-
* @returns A disposable.
42-
*/
43-
onRetry: (
44-
listener: (
45-
data: ({ error: Error } | { value: unknown }) & {
46-
delay: number;
47-
attempt: number;
48-
endpointUrl: string;
49-
},
50-
) => void,
51-
) => {
52-
dispose(): void;
53-
};
54-
55-
/**
56-
* Listens for when the RPC service retries the request too many times in a
57-
* row.
58-
*
59-
* @param listener - The callback to be called when the circuit is broken.
60-
* @returns A disposable.
61-
*/
62-
onBreak: (
63-
listener: (
64-
data: ({ error: Error } | { value: unknown } | { isolated: true }) & {
65-
endpointUrl: string;
66-
},
67-
) => void,
68-
) => {
69-
dispose(): void;
70-
};
71-
72-
/**
73-
* Listens for when the policy underlying this RPC service detects a slow
74-
* request.
75-
*
76-
* @param listener - The callback to be called when the request is slow.
77-
* @returns A disposable.
78-
*/
79-
onDegraded: (listener: (data: { endpointUrl: string }) => void) => {
80-
dispose(): void;
81-
};
82-
83-
/**
84-
* Makes a request to the RPC endpoint.
85-
*/
86-
request<Params extends JsonRpcParams, Result extends Json>(
87-
jsonRpcRequest: JsonRpcRequest<Params>,
88-
fetchOptions?: RequestInit,
89-
): Promise<JsonRpcResponse<Result | null>>;
90-
};
42+
export type AbstractRpcServiceRequestMethod = <
43+
Params extends JsonRpcParams,
44+
Result extends Json,
45+
>(
46+
jsonRpcRequest: JsonRpcRequest<Params>,
47+
fetchOptions?: RequestInit,
48+
) => Promise<JsonRpcResponse<Result | null>>;

0 commit comments

Comments
 (0)