json-rpc-utils
Version:
JSON-RPC 2.0 client and server utilities
34 lines (33 loc) • 1.59 kB
TypeScript
import { JSONRPCRequest, JSONRPCResponse, JSONRPCParams, JSONRPCID } from "./models";
export declare type SimpleJSONRPCMethod<ServerContext> = (params?: Partial<JSONRPCParams>, ServerContext?: ServerContext) => any;
export declare type JSONRPCMethod<ServerContext> = (request: JSONRPCRequest, ServerContext?: ServerContext) => PromiseLike<JSONRPCResponse | null>;
export declare type ErrorDataGetter = (error: any) => any;
export interface Logger {
info(message: string, ...others: any[]): void;
warn(message: string, ...others: any[]): void;
error(message: string, ...others: any[]): void;
}
export interface ServerOptions {
getErrorData?: (error: any) => any;
onError?: (error: any, id: JSONRPCID | undefined) => void;
logger?: Logger;
}
export declare class JSONRPCServer<ServerContext = void> {
private readonly options;
private nameToMethodDictionary;
logger: Logger | null;
constructor(options?: ServerOptions);
/**
* Adds a new method to be invoked by remote JSON-RPC clients.
*/
addMethod(name: string, method: SimpleJSONRPCMethod<ServerContext>): void;
/**
* Processes an incoming JSON-RPC request.
*
* Returns an object suitable for JSON serialization with a response if the request merits a response, and returns null if no response is required. Nulls like this happen for JSON-RPC notifications.
*/
process(request: JSONRPCRequest, ServerContext?: ServerContext): Promise<JSONRPCResponse | null>;
private toJSONRPCMethod;
private callMethod;
private handleErrorAndCreateResponse;
}