A simple implementation of jsonrpc2
npm install @happapi/jsonrpc-core``js
const { JsonRpcHandler, JsonRpcError } = require('tian-jsonrpc');
let jsonRpc = new JsonRpcHandler();
//add a single method like this...
jsonRpc.setMethodHandler('sayHello', function ({ name }) {
if (!name) {
throw JsonRpcError.InvalidParams('parameter name is required');hello ${name}
}
return ;
});
//now you can handle jsonrpc request
jsonRpc.handle('{"jsonrpc":"2.0","method":"doSomething","id":"1"}');
//await to get result
(async () => {
let result2 = await jsonRpc.handle([
{ jsonrpc: '2.0', method: 'unexisted method', id: 1 },
{ jsonrpc: '2.0', method: 'sayHello', id: 2, params: { name: 'world' } }
]);
})();
``