Json-rpc implementation


Keywords
json, rpc
License
ISC
Install
npm install @etk/jsonrpc@2.2.0

Documentation

JsonRpc

Build Status npm version Code Climate

Schema

{
	"version": string,
	"id": number,
	"resource": string,
	"method": string,
	"params": Object,
	"result": *,
	"error": {
		"code": string,
		"message": string
	}
}

Usage

New request

const JR = require("@etk/jsonrpc");
const jr = new JR;
const request = jr.Request();
request.setResource('someResource').setMethod('methodName').setParams({'param1' : 'paramValue'});
console.log(request.toString());
// {"version":"1.2.0","id":1,"resource" : "someResource","method":"methodName","params":{"param1":"paramValue"}}

equals to

const JR = require("@etk/jsonrpc");
const jr = new JR;
const request = jr.Request({
	resource : 'someResource',
	method : 'methodName',
	params : {param1 : 'paramValue'}
});
console.log(request.toString());
// {"version":"1.2.0","id":1,"resource" : "someResource","method":"methodName","params":{"param1":"paramValue"}}

New response

with result

const JR = require("@etk/jsonrpc");
const jr = new JR;
const response = jr.Response();
response.setId(1).setResult('someResult');
console.log(response.toString());
// {"version":"1.2.0","id":1,"result":"someResult"}

equals to

const JR = require("@etk/jsonrpc");
const jr = new JR;
const request = jr.Request({
	id : 1,
	result : 'someResult'
});
console.log(request.toString());
// {"version":"1.2.0","id":1,"result":"someResult"}

with error

const JR = require("@etk/jsonrpc");
const jr = new JR;
const response = jr.Response({
	id : 1,
	error : {
		code : "ERR_CODE",
		message : 'Error message'
	}
});
console.log(response.toString());
// {"version":"1.2.0","id":1,"error":{"code":"ERR_CODE","message":"Error message"}}

equals to

const JR = require("@etk/jsonrpc");
const jr = new JR;
const response = jr.Response();
response.setId(1).setError({
	code : "ERR_CODE",
	message : 'Error message'
});
console.log(response.toString());
// {"version":"1.2.0","id":1,"error":{"code":"ERR_CODE","message":"Error message"}}

equals to

const JR = require("@etk/jsonrpc");
const jr = new JR;
const response = jr.Response({
	id : 1,
	error : {
		code : "ERR_CODE",
		message : 'Error message'
	}
});
console.log(response.toString());
// {"version":"1.2.0","id":1,"error":{"code":"ERR_CODE","message":"Error message"}}

New notification

const JR = require("@etk/jsonrpc");
const jr = new JR;
const notification = jr.Notification();
notification.setResource('someResource').setMethod('methodName').setParams({'param1' : 'paramValue'});
console.log(notification.toString());
// {"version":"1.2.0","resource" : "someResource","method":"methodName","params":{"param1":"paramValue"}}

equals to

const JR = require("@etk/jsonrpc");
const jr = new JR;
const notification = jr.Notification({
	resource : 'someResource',
	method : 'methodName',
	params : {param1 : 'paramValue'}
});
console.log(notification.toString());
// {"version":"1.2.0","resource" : "someResource","method":"methodName","params":{"param1":"paramValue"}}

Parse message

const JR = require("@etk/jsonrpc");
const jr = new JR;
const notification = jr.parse('{"version":"1.2.0","resource" : "someResource","method":"methodName","params":{"param1":"paramValue"}}');
console.log(notification.toString());
// {"version":"1.2.0","resource" : "someResource","method":"methodName","params":{"param1":"paramValue"}}

Callbacks

const JR = require("@etk/jsonrpc");
const jr = new JR;
const request = jr.Request();
request.setMethod('someMethod');
request.setCallback((err, res) => {
	if(err) {
		console.log('Got error: ' + err.message);
		return;
	}
	console.log('Got response for message #' + request.getId());
	console.log(res);
});
console.log("Request", request);

const response = jr.Response();
response.setId(request.getId());
response.setResult({some : 'result'});
console.log("Response", response);

// Callback will fire automagicaly after response is parsed
jr.parse(response.toString());

output

Request JsonRpcRequest {
  message: 
   { version: '1.2.0',
     id: 1,
     resource: '__global__',
     params: {},
     method: 'someMethod' } }
     
Response JsonRpcResponse {
  message: { version: '1.2.0', id: 1, result: { some: 'result' } } }

Got response for message #1
JsonRpcResponse {
  message: { version: '1.2.0', id: 1, result: { some: 'result' } } }

JSONLess

JSONLess allows non-primitives values like Date or MongoDB ObjectID to be transfered over JSON See JSONLess github.io pages for mor info

Docs

See github.io pages