Module

x/json_rpc_ts/mod.ts>JSONRPCClient

A strictly typed json-rpc(2.0) implementation, zero dependency, minimal abstraction, with simple api
Latest
class JSONRPCClient
import { JSONRPCClient } from "https://dotland.deno.dev/x/json_rpc_ts@v0.2.0/mod.ts";

Provide a external requestForResponse function to the constructor, it should accept a string (json encoded from one or more json rpc request) and your customized function should send this string to a json rpc server for any response represented as string

The constructor optionally accept a customized id generator, otherwise it use a self added number

To customize the request or response, you can extend JSONRPCClient.

To customize request, overwrite createRequest and createNotification methods.

To customize response, overwrite request notify and batch methods.

Constructors

new
JSONRPCClient(requestForResponse: (input: string) => string | Promise<string>, idGenerator?: IDGenerator)

Properties

protected
idGenerator: IDGenerator

MUST be an infinite iterator

protected
requestForResponse: (input: string) => string | Promise<string>

The external function to send the json string to any rpc server, and fetch for response as string

Methods

You should use the createRequest() or createNotification() method to create the requests array. Response order is always matched by id.

Throws JSONRPCClientParseError if server response cannot be parsed, note that it does not throws for any JSONRPCErrorResponse, in this case it will be a single object: { status: 'rejected', reason: {...} }

Usually it returns be like (same as the Promise.allSettled() method):

[
   { status: 'fulfilled', value: '...' },
   {
       status: 'rejected',
       reason: {
           code: -32601,
           message: 'Method not found',
       },
   },
]
createNotification<T extends keyof Methods>(method: T extends string ? T : never, params: Parameters<Methods[T]>[0]): JSONRPCNotification
createRequest<T extends keyof Methods>(method: T extends string ? T : never, params: Parameters<Methods[T]>[0]): JSONRPCRequest
notify<T extends keyof Methods>(method: T extends string ? T : never, params: Parameters<Methods[T]>[0]): Promise<void>

Send JSONRPCNotification to server, no returns, only throws if your provided processor function throws

request<T extends keyof Methods>(method: T extends string ? T : never, params: Parameters<Methods[T]>[0]): Promise<ReturnType<Methods[T]>>

Send JSONRPCRequest to server, returns JSONRPCValue or throw JSONRPCErrorInterface (or JSONRPCClientParseError)