import uuid = require ( 'uuid' ) ; import { RpcClient , JsonRpcAdapter , JsonRpcRequest , Client , isJsonRpcErrorResponse , } from '../../../types' ; /**
 * Client for making requests to a JSON-RPC server.
 */ export class JsonRpcClient < TransportRequest , TransportResponse > implements RpcClient { constructor ( private adapter , private client ) { } param method param params returns public async handle < T > ( method , params ? ) { const request = { jsonrpc : '2.0' , method , params , id : uuid . v4 ( ) , } ; const encodedRequest = this . adapter . encodeRequest ( request ) ; const encodedResponse = await this . client . request ( encodedRequest ) ; const response = this . adapter . decodeResponse ( encodedResponse ) ; if ( isJsonRpcErrorResponse ( response ) ) { throw new Error ( `${ JSON . stringify ( response . error ) }` ) ; } return response . result ; } }