Git Product home page Git Product logo

jsonrpc-ts's Introduction

JSONRPC Typescript

Build Status Greenkeeper badge Coverage Status npm version

Strongly πŸ’ͺ Typed JSON RPC 2.0 Client for Nodejs

Fully tested to comply with the official JSON-RPC 2.0 specification

Quick Overview

By Declaring events using a simple interface mapping methods names to their parameters to get Strongly Typed, Fast and Modern Rpc client for your service.

Install

npm i jsonrpc-ts

Usage

First you need to define your Methods, that could be achieved by creating an interface of methods name and there parameters.

// we have a service that can do math,
// and it has this methods
interface MathService {
  // a method called sum that accepts 2 args of type number
  sum: [number, number];
  // methods can have named paramerter too.
  sub: { left: number; right: number };
  // or if you need return type, you can have that too :)
  sumWithReturnType: ({ x, y }: { x: number; y: number }) => number;
}

then import RpcClient and start making requests

import { RpcClient } from 'jsonrpc-ts';

const rpcClient = new RpcClient<MathService>({ url: '...' });
// now you have a strongly typed methods.
// try to change [3, 2] to ['3', '2'] and the typescript compiler will catch you !
const response = await rpcClient.makeRequest({
  method: 'sum',
  params: [3, 2],
  id: 1,
  jsonrpc: '2.0',
});
// response.data.result === 5

// response2.data.result has type of number :)
const response2 = await rpcClient.makeRequest({
  method: 'sumWithReturnType',
  params: { x: 3, y: 2 },
  id: 2,
  jsonrpc: '2.0',
});
// response2.data.result === 5

jsonrpc-ts's People

Contributors

andersbc avatar dependabot[bot] avatar greenkeeper[bot] avatar shekohex avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

jsonrpc-ts's Issues

What are advantages of the package?

Hi, I'm newcomer to frontend and interested in a ligth jsonrpc library for TypeScript. I found this implementation:
https://github.com/jow-/luci-ng/blob/master/luci2-ui-core/src/src/app/shared/jsonrpc.interface.ts
https://github.com/jow-/luci-ng/blob/master/luci2-ui-core/src/src/app/shared/jsonrpc.ts

Also I see a lot of similar packages in NPM like this:
https://github.com/nojvek/noice-json-rpc

But it looks like your lib is newest. Can you give a hint why you decided to implement a new one and what was wrong with others?

An in-range update of @types/node is breaking the build 🚨

The devDependency @types/node was updated from 10.12.8 to 10.12.9.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

@types/node is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

access to data? property in rpcError

How do you access the optional data property of an RpcError?

RpcError is defined here:

export class RpcError<TError = any> extends Error {
  constructor(private readonly err: RpcResponseError<TError>) {
      super();
      super.message = err.message;
  }

  public getCode(): RpcErrorCode | number {
      return this.err.code;
  }
}

I can get the code and message, but the data? property - which is part of the RpcResponseError - is not exposed. Or am I missing something?

Typing return value duplication

When typing the return value like so:

    const rpcClient = new RpcClient<MyService>({ url });
    try {
        const {data: {result}} = await rpcClient.makeRequest<'myMethod', ArtistResponse>({ 
            method: 'myMethod', params:  { filter: true }, id: '1', jsonrpc: '2.0' 
        });
        console.log(result);
    } catch (e) {
        console.log(e)
    }

It would be nice if there wasn't the redundancy of 'myMethod' in the Typing of templateTRequest and the method function argument. Is there a point to keeping these separated. I'm not sure how to express this at the moment

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.