npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jsonrpc2-tsclient

v2.1.1

Published

A library for developing JSON-RPC 2.0 clients.

Downloads

22

Readme

JSON RPC 2.0 Client

A collection of classes for creating JSON RPC 2.0 clients.

Install

npm i jsonrpc2-tsclient

Example

Example of the client decorator implementing RPC methods from reading method signatures.

import {rpcClient, RPCHTTPClient} from "jsonrpc2-tsclient";

const transport = new RPCHTTPClient("http://localhost:8000/api/v1");

interface Vector3 {
  x: number;
  y: number;
  z: number;
}

@rpcClient(transport, "math.")
class MathClient {
  // @ts-ignore
  async add(a: number, b: number): Promise<number> {}
  // @ts-ignore
  async get_distance(a: Vector3, b: Vector3): Promise<Vector3> {}
}

const client = new MathClient();
client.add(2, 2).then(res => console.log(res));
client.get_distance({x: 1, y: 1, z: 1}, {x: 1, y: 1, z: 1}).then(res => console.log(res));

Usage

The JSONRPCClient will handle forming requests and parsing responses. To call a JSON-RPC 2.0 method with an implementation of JSONRPCClient, call the call method, passing it the name of the method to call and the params.

If the response is JSON-RPC 2.0 result object, only the result will be returned, none of the wrapper.

If the response is JSON-RPC 2.0 error response, and exception will be thrown for the error.

A default HTTP and WebSocket client are provided.

import {RPCHTTPClient} from "jsonrpc2-tsclient";
import {RPCWebSocketClient} from "jsonrpc2-tsclient";

let httpClient = new RPCHTTPClient("http://localhost:8000/api/v1");
client.call("divide", [0, 0])
  .then(res => console.log(`JSON-RPC Result: ${res}`))
  .catch(err => console.log(`JSON-RPC Error: ${err}`));

let wsClient = new RPCWebSocketClient("ws://localhost:8000/api/v1");
client.call("divide", [0, 0])
  .then(res => console.log(`JSON-RPC Result: ${res}`))
  .catch(err => console.log(`JSON-RPC Error: ${err}`));
wsClient.close();

JSONRPCClient Abstract Class

JSON-RPC 2.0 is transport agnostic. This library provides an abstract class that can be extended to create clients for different transports.

Implementations

To make client for a transport, extend the JSONRPCClient class and implement the sendAndGetJSON which takes a request object and is expected to return a JSON-RPC 2.0 response as an object. JSONRPCClient has a call method that uses this internally.

Example HTTP implementation:

export class RPCHTTPClient extends JSONRPCClient {
  url: string;
  headers: object;

  constructor(url: string, headers: object | null = null) {
    super();
    this.url = url;
    this.headers = headers || {};
    this.headers["Content-Type"] = "application/json";
  }

  protected async sendAndGetJSON(request: RequestObject): Promise<any> {
    return (await axios.post(this.url, request, {headers: this.headers})).data;
  }
}