json-rpc-protocol
JSON-RPC 2 protocol messages parsing and formatting
Install
Installation of the npm package:
> npm install --save json-rpc-protocol
Usage
Errors
var protocol = require('json-rpc-protocol')
var JsonRpcError = protocol.JsonRpcError
var InvalidJson = protocol.InvalidJson
var InvalidRequest = protocol.InvalidRequest
var MethodNotFound = protocol.MethodNotFound
var InvalidParameters = protocol.InvalidParameters
import {
JsonRpcError,
InvalidJson,
InvalidRequest,
MethodNotFound,
InvalidParameters
} from 'json-rpc-protocol'
This is the base error for all JSON-RPC errors:
throw new JsonRpcError(message, code)
The JSON-RPC 2 specification defined also the following specialized
errors:
throw new InvalidJson()
throw new InvalidRequest()
throw new MethodNotFound(methodName)
throw new InvalidParameters(data)
Custom errors can of course be created, they just have to inherit
JsonRpcError
:
function MyError () {
JsonRpcError.call(this, 'my error', 1)
}
MyError.prototype = Object.create(JsonRpcError.prototype, {
constructor: {
value: MyError
}
})
class MyError extends JsonRpcError {
constructor () {
super('my error', 1)
}
}
Parsing
var parse = require('json-rpc-protocol').parse
import {parse} from 'json-rpc-protocol'
The parse()
function parses, normalizes and validates JSON-RPC 1 or
JSON-RPC 2 messages.
These message can be either JS objects or JSON strings (they will be
parsed automatically).
This function may throws:
InvalidJson
: if the string cannot be parsed as a JSON;InvalidRequest
: if the message is not a valid JSON-RPC message.
parse('{"jsonrpc":"2.0", "method": "foo", "params": ["bar"]}')
parse('{"jsonrpc":"2.0", "id": 0, "method": "add", "params": [1, 2]}')
parse('{"jsonrpc":"2.0", "id": 0, "result": 3}')
A parsed message has a non enumerable property type
set to easily
differentiate between types of JSON-RPC messages.
Response/Error
The parse.result
helper parses and returns the result of a response message or throws the error of an error message:
try {
const result = await parse.result(message)
} catch (error) {
}
Formatting
var format = require('json-rpc-protocol').format
import {format} from 'json-rpc-protocol'
The format.*()
functions can be used to create valid JSON-RPC
messages (as JavaScript strings).
Notification
format.notification('foo', ['bars'])
The last argument, the parameters of the notification, is optional and
defaults to undefined
.
Request
The last argument, the parameters of the request, is optional and
defaults to undefined
.
format.request(0, 'add', [1, 2])
Response
A successful response:
format.response(0, 3)
A failed response:
var MethodNotFound = require('json-rpc-protocol').MethodNotFound
format.error(0, new MethodNotFound('add'))
Note: the error to format must implement a toJsonRpcError
function which returns an object or it
will be automatically replaced by an unknown error for security
reasons.
toJsonRpcError
example:
toJsonRpcError () {
return {
code: 42,
message: 'Hacking too much time!',
data: [ 'Hackerman' ]
}
}
Development
# Install dependencies
> npm install
# Run the tests
> npm test
# Continuously compile
> npm run dev
# Continuously run the tests
> npm run dev-test
# Build for production (automatically called by npm install)
> npm run build
Related
Contributions
Contributions are very welcomed, either on the documentation or on
the code.
You may:
- report any issue
you've encountered;
- fork and create a pull request.
License
ISC © Julien Fontanet