Skip to content

Commit

Permalink
Add missing types, fix remaining faulty ones (#67)
Browse files Browse the repository at this point in the history
* Add missing types, fix faulty ones
  • Loading branch information
rekmarks authored Nov 8, 2020
1 parent f4d6930 commit c6d49a7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
49 changes: 45 additions & 4 deletions merged-packages/json-rpc-engine/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@

import { IEthereumRpcError } from 'eth-rpc-errors/@types'

/**
* A String specifying the version of the JSON-RPC protocol.
* MUST be exactly "2.0".
*/
export type JsonRpcVersion = "2.0";
export type JsonRpcVersion = '2.0';

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

export type JsonRpcEngineEndCallback = (error?: JsonRpcError<unknown>) => void;

type ReturnHandlerCallback = (done: (error?: Error) => void) => void

export type JsonRpcEngineNextCallback = (
returnFlightCallback?: (done: () => void) => void,
returnHandlerCallback?: ReturnHandlerCallback,
) => void;

export type AsyncJsonRpcEngineNextCallback = (
returnHandlerCallback?: ReturnHandlerCallback,
) => Promise<void>;

export interface JsonRpcMiddleware {
(
req: JsonRpcRequest<unknown>,
res: JsonRpcResponse<unknown>,
next: JsonRpcEngineNextCallback,
end: JsonRpcEngineEndCallback,
) : void;
): void;
}

export interface AsyncJsonrpcMiddleware {
(
req: JsonRpcRequest<unknown>,
res: JsonRpcResponse<unknown>,
next: AsyncJsonRpcEngineNextCallback,
): Promise<void>;
}

export interface JsonRpcEngine {
Expand All @@ -75,3 +89,30 @@ export interface JsonRpcEngine {
) => void,
) => void;
}

export interface asMiddleware {
(engine: JsonRpcEngine): JsonRpcMiddleware;
}

export interface createAsyncMiddleware {
(asyncMiddleware: AsyncJsonrpcMiddleware): JsonRpcMiddleware;
}

type Serializable = boolean | number | string | Record<string, unknown> | unknown[] | null | undefined;
type ScaffoldMiddlewareHandler<T> = T extends Function ? JsonRpcMiddleware : Serializable;

export interface createScaffoldMiddleware<T> {
(handlers: {[methodName: string]: ScaffoldMiddlewareHandler<T>}): JsonRpcMiddleware;
}

export interface createIdRemapMiddleware {
(): JsonRpcMiddleware;
}

export interface mergeMiddleware {
(middlewares: JsonRpcMiddleware[]): JsonRpcMiddleware;
}

export interface getUniqueId {
(): number;
}
4 changes: 2 additions & 2 deletions merged-packages/json-rpc-engine/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const {
ERROR_CODES,
} = require('eth-rpc-errors')

module.exports = class RpcEngine extends SafeEventEmitter {
module.exports = class JsonRpcEngine extends SafeEventEmitter {
constructor () {
super()
this._middleware = []
Expand Down Expand Up @@ -133,7 +133,7 @@ module.exports = class RpcEngine extends SafeEventEmitter {

// go down stack of middleware, call and collect optional returnHandlers
for (const middleware of this._middleware) {
isComplete = await RpcEngine._runMiddleware(
isComplete = await JsonRpcEngine._runMiddleware(
req, res, middleware, returnHandlers,
)
if (isComplete) {
Expand Down

0 comments on commit c6d49a7

Please sign in to comment.