This repository has been archived by the owner on Nov 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 62
BREAKING: Use @metamask/utils #105
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,19 @@ | ||
import SafeEventEmitter from '@metamask/safe-event-emitter'; | ||
import { | ||
hasProperty, | ||
JsonRpcError, | ||
JsonRpcRequest, | ||
JsonRpcResponse, | ||
} from '@metamask/utils'; | ||
import { errorCodes, EthereumRpcError, serializeError } from 'eth-rpc-errors'; | ||
|
||
type Maybe<T> = Partial<T> | null | undefined; | ||
|
||
export type Json = | ||
| boolean | ||
| number | ||
| string | ||
| null | ||
| { [property: string]: Json } | ||
| Json[]; | ||
|
||
/** | ||
* A String specifying the version of the JSON-RPC protocol. | ||
* MUST be exactly "2.0". | ||
*/ | ||
export type JsonRpcVersion = '2.0'; | ||
|
||
/** | ||
* An identifier established by the Client that MUST contain a String, Number, | ||
* or NULL value if included. If it is not included it is assumed to be a | ||
* notification. The value SHOULD normally not be Null and Numbers SHOULD | ||
* NOT contain fractional parts. | ||
*/ | ||
export type JsonRpcId = number | string | null; | ||
|
||
export interface JsonRpcError { | ||
code: number; | ||
message: string; | ||
data?: unknown; | ||
stack?: string; | ||
} | ||
|
||
export interface JsonRpcRequest<T> { | ||
jsonrpc: JsonRpcVersion; | ||
method: string; | ||
id: JsonRpcId; | ||
params?: T; | ||
} | ||
|
||
export interface JsonRpcNotification<T> { | ||
jsonrpc: JsonRpcVersion; | ||
method: string; | ||
params?: T; | ||
} | ||
|
||
interface JsonRpcResponseBase { | ||
jsonrpc: JsonRpcVersion; | ||
id: JsonRpcId; | ||
} | ||
|
||
export interface JsonRpcSuccess<T> extends JsonRpcResponseBase { | ||
result: Maybe<T>; | ||
} | ||
|
||
export interface JsonRpcFailure extends JsonRpcResponseBase { | ||
error: JsonRpcError; | ||
} | ||
|
||
export type JsonRpcResponse<T> = JsonRpcSuccess<T> | JsonRpcFailure; | ||
|
||
export interface PendingJsonRpcResponse<T> extends JsonRpcResponseBase { | ||
result?: T; | ||
error?: Error | JsonRpcError; | ||
} | ||
export type PendingJsonRpcResponse<Result> = Omit< | ||
JsonRpcResponse<Result>, | ||
'error' | 'result' | ||
> & { | ||
result?: Result; | ||
error?: JsonRpcError; | ||
}; | ||
|
||
export type JsonRpcEngineCallbackError = Error | JsonRpcError | null; | ||
|
||
|
@@ -79,9 +29,9 @@ export type JsonRpcEngineEndCallback = ( | |
error?: JsonRpcEngineCallbackError, | ||
) => void; | ||
|
||
export type JsonRpcMiddleware<T, U> = ( | ||
req: JsonRpcRequest<T>, | ||
res: PendingJsonRpcResponse<U>, | ||
export type JsonRpcMiddleware<Params, Result> = ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like these type parameter names a lot better than |
||
req: JsonRpcRequest<Params>, | ||
res: PendingJsonRpcResponse<Result>, | ||
next: JsonRpcEngineNextCallback, | ||
end: JsonRpcEngineEndCallback, | ||
) => void; | ||
|
@@ -103,7 +53,7 @@ export class JsonRpcEngine extends SafeEventEmitter { | |
* | ||
* @param middleware - The middleware function to add. | ||
*/ | ||
push<T, U>(middleware: JsonRpcMiddleware<T, U>): void { | ||
push<Params, Result>(middleware: JsonRpcMiddleware<Params, Result>): void { | ||
this._middleware.push(middleware as JsonRpcMiddleware<unknown, unknown>); | ||
} | ||
|
||
|
@@ -113,9 +63,9 @@ export class JsonRpcEngine extends SafeEventEmitter { | |
* @param request - The request to handle. | ||
* @param callback - An error-first callback that will receive the response. | ||
*/ | ||
handle<T, U>( | ||
request: JsonRpcRequest<T>, | ||
callback: (error: unknown, response: JsonRpcResponse<U>) => void, | ||
handle<Params, Result>( | ||
request: JsonRpcRequest<Params>, | ||
callback: (error: unknown, response: JsonRpcResponse<Result>) => void, | ||
): void; | ||
|
||
/** | ||
|
@@ -125,9 +75,9 @@ export class JsonRpcEngine extends SafeEventEmitter { | |
* @param callback - An error-first callback that will receive the array of | ||
* responses. | ||
*/ | ||
handle<T, U>( | ||
requests: JsonRpcRequest<T>[], | ||
callback: (error: unknown, responses: JsonRpcResponse<U>[]) => void, | ||
handle<Params, Result>( | ||
requests: JsonRpcRequest<Params>[], | ||
callback: (error: unknown, responses: JsonRpcResponse<Result>[]) => void, | ||
): void; | ||
|
||
/** | ||
|
@@ -136,15 +86,19 @@ export class JsonRpcEngine extends SafeEventEmitter { | |
* @param request - The JSON-RPC request to handle. | ||
* @returns The JSON-RPC response. | ||
*/ | ||
handle<T, U>(request: JsonRpcRequest<T>): Promise<JsonRpcResponse<U>>; | ||
handle<Params, Result>( | ||
request: JsonRpcRequest<Params>, | ||
): Promise<JsonRpcResponse<Result>>; | ||
|
||
/** | ||
* Handle an array of JSON-RPC requests, and return an array of responses. | ||
* | ||
* @param request - The JSON-RPC requests to handle. | ||
* @returns An array of JSON-RPC responses. | ||
*/ | ||
handle<T, U>(requests: JsonRpcRequest<T>[]): Promise<JsonRpcResponse<U>[]>; | ||
handle<Params, Result>( | ||
requests: JsonRpcRequest<Params>[], | ||
): Promise<JsonRpcResponse<Result>[]>; | ||
|
||
handle(req: unknown, callback?: any) { | ||
if (callback && typeof callback !== 'function') { | ||
|
@@ -487,7 +441,7 @@ export class JsonRpcEngine extends SafeEventEmitter { | |
res: PendingJsonRpcResponse<unknown>, | ||
isComplete: boolean, | ||
): void { | ||
if (!('result' in res) && !('error' in res)) { | ||
if (!hasProperty(res, 'result') && !hasProperty(res, 'error')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The replaced |
||
throw new EthereumRpcError( | ||
errorCodes.rpc.internal, | ||
`JsonRpcEngine: Response has no error or result for request:\n${jsonify( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not suggesting any changes, but I wonder if this is type is right, considering that it seems that such a response can theoretically have both
result
anderror
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type is meant to be used inside middleware functions and internally in
JsonRpcEngine
during request processing. It permits both, either, or neither property being present. Or did you mean something else?