|
1 | 1 |
|
2 | 2 | import { IEthereumRpcError } from 'eth-rpc-errors/@types' |
3 | 3 |
|
4 | | -/** A String specifying the version of the JSON-RPC protocol. MUST be exactly "2.0". */ |
| 4 | +/** |
| 5 | + * A String specifying the version of the JSON-RPC protocol. |
| 6 | + * MUST be exactly "2.0". |
| 7 | + */ |
5 | 8 | export type JsonRpcVersion = "2.0"; |
6 | 9 |
|
7 | 10 | /** Method names that begin with the word rpc followed by a period character |
8 | 11 | * (U+002E or ASCII 46) are reserved for rpc-internal methods and extensions |
9 | | - * and MUST NOT be used for anything else. */ |
| 12 | + * and MUST NOT be used for anything else. |
| 13 | + */ |
10 | 14 | export type JsonRpcReservedMethod = string; |
11 | 15 |
|
12 | | -/** An identifier established by the Client that MUST contain a String, Number, |
13 | | - * or NULL value if included. If it is not included it is assumed to be a |
14 | | - * notification. The value SHOULD normally not be Null and Numbers SHOULD |
15 | | - * NOT contain fractional parts [2] */ |
| 16 | +/** |
| 17 | + * An identifier established by the Client that MUST contain a String, Number, |
| 18 | + * or NULL value if included. If it is not included it is assumed to be a |
| 19 | + * notification. The value SHOULD normally not be Null and Numbers SHOULD |
| 20 | + * NOT contain fractional parts. |
| 21 | + */ |
16 | 22 | export type JsonRpcId = number | string | void; |
17 | 23 |
|
18 | | -interface JsonRpcError<T> extends IEthereumRpcError<T> {} |
| 24 | +export interface JsonRpcError<T> extends IEthereumRpcError<T> {} |
19 | 25 |
|
20 | | -interface JsonRpcRequest<T> { |
| 26 | +export interface JsonRpcRequest<T> { |
21 | 27 | jsonrpc: JsonRpcVersion; |
22 | 28 | method: string; |
23 | 29 | id: JsonRpcId; |
24 | 30 | params?: T; |
25 | 31 | } |
26 | 32 |
|
27 | | -interface JsonRpcNotification<T> extends JsonRpcResponse<T> { |
| 33 | +export interface JsonRpcNotification<T> { |
28 | 34 | jsonrpc: JsonRpcVersion; |
| 35 | + method: string, |
29 | 36 | params?: T; |
30 | 37 | } |
31 | 38 |
|
32 | | -interface JsonRpcResponse<T> { |
33 | | - result?: any; |
34 | | - error?: JsonRpcError<any>; |
35 | | - jsonrpc: JsonRpcVersion; |
36 | | - id: JsonRpcId; |
| 39 | +interface JsonRpcResponseBase { |
| 40 | + jsonrpc: JsonRpcVersion, |
| 41 | + id: JsonRpcId, |
37 | 42 | } |
38 | 43 |
|
39 | | -interface JsonRpcSuccess<T> extends JsonRpcResponse<T> { |
40 | | - result: any; |
| 44 | +export interface JsonRpcSuccess<T> extends JsonRpcResponseBase { |
| 45 | + result: T; |
41 | 46 | } |
42 | 47 |
|
43 | | -interface JsonRpcFailure<T> extends JsonRpcResponse<T> { |
44 | | - error: JsonRpcError<T>; |
| 48 | +export interface JsonRpcFailure<T> extends JsonRpcResponseBase { |
| 49 | + error: JsonRpcError<T>; |
45 | 50 | } |
46 | 51 |
|
47 | | -type JsonRpcEngineEndCallback = (error?: JsonRpcError<any>) => void; |
48 | | -type JsonRpcEngineNextCallback = (returnFlightCallback?: (done: () => void) => void) => void; |
| 52 | +export type JsonRpcResponse<T> = JsonRpcSuccess<T> | JsonRpcFailure<T> |
49 | 53 |
|
50 | | -interface JsonRpcMiddleware { |
| 54 | +export type JsonRpcEngineEndCallback = (error?: JsonRpcError<unknown>) => void; |
| 55 | +export type JsonRpcEngineNextCallback = ( |
| 56 | + returnFlightCallback?: (done: () => void) => void, |
| 57 | +) => void; |
| 58 | + |
| 59 | +export interface JsonRpcMiddleware { |
51 | 60 | ( |
52 | | - req: JsonRpcRequest<any>, |
53 | | - res: JsonRpcResponse<any>, |
| 61 | + req: JsonRpcRequest<unknown>, |
| 62 | + res: JsonRpcResponse<unknown>, |
54 | 63 | next: JsonRpcEngineNextCallback, |
55 | 64 | end: JsonRpcEngineEndCallback, |
56 | 65 | ) : void; |
57 | 66 | } |
58 | 67 |
|
59 | | -interface JsonRpcEngine { |
| 68 | +export interface JsonRpcEngine { |
60 | 69 | push: (middleware: JsonRpcMiddleware) => void; |
61 | | - handle: (req: JsonRpcRequest<any>, callback: (error: JsonRpcError<any>, res: JsonRpcResponse<any>) => void) => void; |
| 70 | + handle: ( |
| 71 | + req: JsonRpcRequest<unknown>, |
| 72 | + callback: ( |
| 73 | + error: JsonRpcError<unknown>, |
| 74 | + res: JsonRpcResponse<unknown>, |
| 75 | + ) => void, |
| 76 | + ) => void; |
62 | 77 | } |
63 | | - |
|
0 commit comments