blue-frog-core
v0.3.0
Published
a small JSON-RPC 2.0 library
Downloads
2
Maintainers
Readme
blue-frog-core
a small library for JSON-RPC 2.0.
api
var request = require('blue-frog-core/request')
var response = require('blue-frog-core/response')
var JsonRpcError = require('blue-frog-core/error')
create a json-rpc 2.0 request
var requestObject = request(id, method[, params])
id
: string or number, case notification then null.method
: stringparams
: object or array. optional
var requestObject = request(1, "sum", [1,3,5])
// {
// jsonrpc: '2.0',
// method: 'sum',
// id: 1,
// params: [ 1, 3, 5 ]
//}
create a json-rpc 2.0 request.notificacion
var notificationObj = request.notification(method[, params])
// or
// var notificationObj = request(null, method[, params])
var requestObject = request.notification("update", [6])
// {
// jsonrpc: '2.0',
// method: 'update',
// id: null,
// params: [ 6 ]
// }
create a json-rpc 2.0 response (success)
var responseObject = response(id, result)
id
: string or number or nullresult
: mix
var responseObject = response(1, 15)
// {
// jsonrpc: '2.0',
// id: 1,
// result: 15
// }
create a json-rpc 2.0 response (error)
var responseErrorObject = response.error(id, error)
id
: string or number or nullerror
: object or errorObject
error member
code
: number -32768 to -32000message
: stringdata
: string or errorObject, optional.
use JsonRpcError
var responseErrorObject = response.error(1, new JsonRpcError(-32111, 'Server error', new Error("db error")))
// {
// jsonrpc: '2.0',
// id: 1,
// error: { [JsonRpcError: Server error]
// name: 'JsonRpcError',
// code: -32111,
// message: 'Server error',
// data: [Error: db error]
// }
// }
var json = JSON.stringify(responseErrorObject)
// {
// "jsonrpc": "2.0",
// "id": 1,
// "error": {
// "code": -32111,
// "message": "Server error",
// "data": "Error: db error"
// }
// }
use object
var responseErrorObject = response.error(1, {code: -32112, message: 'some error'})
// {
// jsonrpc: '2.0',
// id: 1,
// error: {
// code: -32112,
// message: 'some error'
// }
// }
var json = JSON.stringify(responseErrorObject)
// {
// "jsonrpc": "2.0",
// "id": 1,
// "error": {
// "code": -32112,
// "message": "some error"
// }
// }
create json-rpc 2.0 error object
var err = new JsonRpcError(code, message[, data])
create defined error object
var parseError = JsonRpcError.ParseError([data])
var invalidRequest = JsonRpcError.InvalidRequest([data])
var methodNotFound = JsonRpcError.MethodNotFound([data])
var invalidParams = JsonRpcError.InvalidParams([data])
var internalError = JsonRpcError.InternalError([data])
extensions
create a json-rpc 2.0 request/response/error extend object
var requestObject = request.extend(request(id, method[, params]), opt)
var responseObject = response.extend(response(id, result), opt)
var errorObject = response.error.extend(response.error(id, error), opt)
var requestObject = request.extend(request(3, 'addMeta'), {charset: 'utf8'})
// {
// jsonrpc: '2.0',
// method: 'addMeta',
// id: 3,
// charset: 'utf8'
// }
see also
license
MIT