implement jsonrpc 2.0
https://www.jsonrpc.org/specification
we can
- from a json to request object
- build response json
- a mechanism to execute from json (request object) to response json (response object)
- all implement json rpc 2.0
server side
import { JsonRpcUtil } from "@kkito/jsonrpc";
const disp = JsonRpcUtil.getDispatcher();
disp.add("add", (a, b) => a + b);
const result = JsonRpcUtil.handle({
id: 1,
method: "add",
version: "2.0",
params: [3, 4],
});
// { jsonrpc: '2.0', id: 1, result: 7 }
console.log(result);
// deal with async
const asyncResult = JsonRpcUtil.asyncHandle({
id: 1,
method: "asyncAdd",
version: "2.0",
params: [3, 4],
});
// { jsonrpc: '2.0', id: 1, result: 7 }
console.log(asyncResult);
client side
import axios from "axios";
const proxy = RpcClient.build({
request: async (jsonReq) => {
return axios.post("/the_endpoint", jsonReq);
},
});
const result = await proxy.add(3, 4);