Skip to content
This repository was archived by the owner on Nov 9, 2023. It is now read-only.

Commit 1e6f647

Browse files
authored
Add missing types, fix remaining faulty ones (#67)
* Add missing types, fix faulty ones
1 parent f938de6 commit 1e6f647

2 files changed

Lines changed: 47 additions & 6 deletions

File tree

src/index.d.ts

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
21
import { IEthereumRpcError } from 'eth-rpc-errors/@types'
32

43
/**
54
* A String specifying the version of the JSON-RPC protocol.
65
* MUST be exactly "2.0".
76
*/
8-
export type JsonRpcVersion = "2.0";
7+
export type JsonRpcVersion = '2.0';
98

109
/** Method names that begin with the word rpc followed by a period character
1110
* (U+002E or ASCII 46) are reserved for rpc-internal methods and extensions
@@ -52,17 +51,32 @@ export interface JsonRpcFailure<T> extends JsonRpcResponseBase {
5251
export type JsonRpcResponse<T> = JsonRpcSuccess<T> | JsonRpcFailure<T>
5352

5453
export type JsonRpcEngineEndCallback = (error?: JsonRpcError<unknown>) => void;
54+
55+
type ReturnHandlerCallback = (done: (error?: Error) => void) => void
56+
5557
export type JsonRpcEngineNextCallback = (
56-
returnFlightCallback?: (done: () => void) => void,
58+
returnHandlerCallback?: ReturnHandlerCallback,
5759
) => void;
5860

61+
export type AsyncJsonRpcEngineNextCallback = (
62+
returnHandlerCallback?: ReturnHandlerCallback,
63+
) => Promise<void>;
64+
5965
export interface JsonRpcMiddleware {
6066
(
6167
req: JsonRpcRequest<unknown>,
6268
res: JsonRpcResponse<unknown>,
6369
next: JsonRpcEngineNextCallback,
6470
end: JsonRpcEngineEndCallback,
65-
) : void;
71+
): void;
72+
}
73+
74+
export interface AsyncJsonrpcMiddleware {
75+
(
76+
req: JsonRpcRequest<unknown>,
77+
res: JsonRpcResponse<unknown>,
78+
next: AsyncJsonRpcEngineNextCallback,
79+
): Promise<void>;
6680
}
6781

6882
export interface JsonRpcEngine {
@@ -75,3 +89,30 @@ export interface JsonRpcEngine {
7589
) => void,
7690
) => void;
7791
}
92+
93+
export interface asMiddleware {
94+
(engine: JsonRpcEngine): JsonRpcMiddleware;
95+
}
96+
97+
export interface createAsyncMiddleware {
98+
(asyncMiddleware: AsyncJsonrpcMiddleware): JsonRpcMiddleware;
99+
}
100+
101+
type Serializable = boolean | number | string | Record<string, unknown> | unknown[] | null | undefined;
102+
type ScaffoldMiddlewareHandler<T> = T extends Function ? JsonRpcMiddleware : Serializable;
103+
104+
export interface createScaffoldMiddleware<T> {
105+
(handlers: {[methodName: string]: ScaffoldMiddlewareHandler<T>}): JsonRpcMiddleware;
106+
}
107+
108+
export interface createIdRemapMiddleware {
109+
(): JsonRpcMiddleware;
110+
}
111+
112+
export interface mergeMiddleware {
113+
(middlewares: JsonRpcMiddleware[]): JsonRpcMiddleware;
114+
}
115+
116+
export interface getUniqueId {
117+
(): number;
118+
}

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const {
77
ERROR_CODES,
88
} = require('eth-rpc-errors')
99

10-
module.exports = class RpcEngine extends SafeEventEmitter {
10+
module.exports = class JsonRpcEngine extends SafeEventEmitter {
1111
constructor () {
1212
super()
1313
this._middleware = []
@@ -133,7 +133,7 @@ module.exports = class RpcEngine extends SafeEventEmitter {
133133

134134
// go down stack of middleware, call and collect optional returnHandlers
135135
for (const middleware of this._middleware) {
136-
isComplete = await RpcEngine._runMiddleware(
136+
isComplete = await JsonRpcEngine._runMiddleware(
137137
req, res, middleware, returnHandlers,
138138
)
139139
if (isComplete) {

0 commit comments

Comments
 (0)