JSON-RPC Wrapper
This library provides a basic json rpc wrapper for any object class that needs to be exposed to the network. It builds a proxy with method validations and argument processing arround any variable that has methods. The provided library is infrastructure agnostic so it can be used on any possible transport layer.
Installing
npm install --save json-rpc-wrapper
Testing
npm run test
Usage
Wrapping a simple object through json rpc protocol would be similar to this:
const RpcWrapper = const myService = params a - b a / b const rpcProxy = myService const req = '{"jsonrpc":"2.0","method":"add","params":[1,2,3],"id":"1"}' // data received through some protocolconst res = await rpcProxy // {jsonrpc:'2.0',id:'1',result:6}const json = JSON // or res.toString() -> '{"jsonrpc":"2.0","id":"1","result":6}'
The wrapper also supports functions with callbacks, we just have to specify which methods expect callback functions:
const service = { fs }const rpcProxy = service const req = '{"jsonrpc":"2.0","method":"storeContent","params":["some content"],"id":56}'const res = await rpcProxy // {jsonrpc:'2.0',id:56,result:true}
In additional wrapper also works with promises and async/await functions without an issue:
const service = store: async { await await return true }const rpcProxy = service const req = '{"jsonrpc":"2.0","method":"storeContent","params":{"user":1,"products":[{"id":1,"name":"p1"},{"id":2,"name":"p2"}]},"id":"xgsdoigh-dsgh-sdgjlsgj"}'const res = await rpcProxy // {jsonrpc:'2.0',id:'xgsdoigh-dsgh-sdgjlsgj',result:true}
Wrapper works also with any type of class instance without a problem. A special type of class that can be used is RpcServiceBase
abstract class that provides also method parameter validation during method invocation. Here's a quick example of usage of this class:
{ super thismethods thisproducts = products } { return thisproducts } { return thisproducts } { thisproducts = thisproducts } async { } const myService = id: 1 name: 'Product 1' id: 2 name: 'Product 2' const rpcProxy = myService const req = JSON const rpcRes = await rpcProxy// [// { "jsonrpc": "2.0", "id": "1", "result": null },// { "jsonrpc": "2.0", "id": "2", "result": null },// { "jsonrpc": "2.0", "id": "3", "result": { "id": 1, "name": "Product 1" } },// { "jsonrpc": "2.0", "id": 4, "error": { "code": -32602, "message": "Invalid params" } }// ]
Simple TCP JSONRPC 2.0 instance
const net = const RpcWrapper = const service = Date paramsconst rpcProxy = service const server = net server server
Simple HTTP JSONRPC 2.0 instance
const http = const RpcWrapper = const service = Date paramsconst rpcProxy = service const server = http server
Typescript usage
The library can also be used directly into typescript without needing to install types package separately, all declaration files can be found in @types folder. Usage example:
; server.listen8080,
Additional detailed examples can be found in examples folder.
Also full documentation related classes can be found in docs/DEFINITIONS.md
Authors
- vigan-abd (vigan.abd@gmail.com)