opendb_test_rpc
Version:
general purpose library for OpenDB blockchain
128 lines (127 loc) • 4.37 kB
TypeScript
import WebSocket from 'isomorphic-ws';
export type RpcEventFunction = (e: Event) => void;
export type RpcMessageEventFunction = (e: MessageEvent) => void;
export type RpcCloseEventFunction = (e: CloseEvent) => void;
export type RpcNotificationEvent = (data: IRpcNotification) => void;
export type RpcRequestEvent = (data: IRpcRequest) => void;
export type RpcSuccessResponseEvent = (data: IRpcSuccessResponse) => void;
export type RpcErrorResponseEvent = (data: IRpcErrorResponse) => void;
export declare enum RpcVersions {
RPC_VERSION = "2.0"
}
export type RpcId = string | number;
export interface IRpcData {
method: string;
params?: unknown;
}
export interface IRpcNotification extends IRpcData {
jsonrpc: RpcVersions.RPC_VERSION;
}
export interface IRpcRequest extends IRpcNotification {
id: RpcId;
}
export interface IRpcResponse {
id: RpcId;
jsonrpc: RpcVersions.RPC_VERSION;
}
export interface IRpcSuccessResponse extends IRpcResponse {
result: unknown;
}
export interface IRpcError {
code: number;
message: string;
data?: unknown;
}
export interface IRpcErrorResponse extends IRpcResponse {
error: IRpcError;
}
export interface IRpcWebSocketConfig {
responseTimeout: number;
}
export type RpcUnidentifiedMessage = IRpcRequest | IRpcNotification | IRpcSuccessResponse | IRpcErrorResponse;
export declare class RpcWebSocketClient {
ws: WebSocket;
private idAwaiter;
private onOpenHandlers;
private onAnyMessageHandlers;
private onNotification;
private onRequest;
private onSuccessResponse;
private onErrorResponse;
private onErrorHandlers;
private onCloseHandlers;
private config;
/**
* Does not start WebSocket connection!
* You need to call connect() method first.
* @memberof RpcWebSocketClient
*/
constructor();
/**
* Starts WebSocket connection. Returns Promise when connection is established.
* @param {string} url
* @param {(string | string[])} [protocols]
* @memberof RpcWebSocketClient
*/
connect(url: string, protocols?: string | string[]): Promise<void>;
onOpen(fn: RpcEventFunction): void;
/**
* Native onMessage event. DO NOT USE THIS unless you really have to or for debugging purposes.
* Proper RPC events are onRequest, onNotification, onSuccessResponse and onErrorResponse (or just awaiting response).
* @param {RpcMessageEventFunction} fn
* @memberof RpcWebSocketClient
*/
onAnyMessage(fn: RpcMessageEventFunction): void;
onError(fn: RpcEventFunction): void;
onClose(fn: RpcCloseEventFunction): void;
/**
* Appends onmessage listener on native websocket with RPC handlers.
* If onmessage function was already there, it will call it on beggining.
* Useful if you want to use RPC WebSocket Client on already established WebSocket along with function changeSocket().
* @memberof RpcWebSocketClient
*/
listenMessages(): void;
/**
* Creates and sends RPC request. Resolves when appropirate response is returned from server or after config.responseTimeout.
* @param {string} method
* @param {*} [params]
* @returns
* @memberof RpcWebSocketClient
*/
call(method: string, params?: unknown): Promise<unknown>;
/**
* Creates and sends RPC Notification.
* @param {string} method
* @param {*} [params]
* @memberof RpcWebSocketClient
*/
/**
* You can provide custom id generation function to replace default uuid/v1.
* @param {() => string} idFn
* @memberof RpcWebSocketClient
*/
customId(idFn: () => string): void;
/**
* Allows modifying configuration.
* @param {RpcWebSocketConfig} options
* @memberof RpcWebSocketClient
*/
configure(options: unknown): void;
/**
* Allows you to change used native WebSocket client to another one.
* If you have already-connected WebSocket, use this with listenMessages().
* @param {WebSocket} ws
* @memberof RpcWebSocketClient
*/
changeSocket(ws: WebSocket): void;
private listen;
private buildRequest;
private buildRequestBase;
private idFn;
private isNotification;
private isRequest;
private isSuccessResponse;
private isErrorResponse;
private isRpcError;
}
export default RpcWebSocketClient;