blue-frog-core

0.3.0 • Public • Published

blue-frog-core

a small library for JSON-RPC 2.0.

JSON-RPC 2.0 specifications

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 : string
  • params : 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 null
  • result : 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 null
  • error : object or errorObject

error member

  • code : number -32768 to -32000
  • message : string
  • data : 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

blue-frog-stream

license

MIT

Package Sidebar

Install

npm i blue-frog-core

Weekly Downloads

1

Version

0.3.0

License

MIT

Last publish

Collaborators

  • ishiduca