2.2.0 • Published 5 years ago

jsonrpc-lite v2.2.0

Weekly downloads
18,398
License
MIT
Repository
github
Last release
5 years ago

JSON-RPC lite

Parse and Serialize JSON-RPC2 messages in node.js or browser.

Inspired by https://github.com/soggie/jsonrpc-serializer

NPM version Build Status Downloads

A implementation of JSON-RPC 2.0 specifications

Install

npm install jsonrpc-lite

API

const jsonrpc = require('jsonrpc-lite')

jsonrpc.request(id, method, params)

Creates a JSON-RPC 2.0 request object, return JsonRpc object.

  • id: {String|Integer}
  • method: {String}
  • params: {Object|Array}, optional
const requestObj = jsonrpc.request('123', 'update', {list: [1, 2, 3]})
// {
//   jsonrpc: '2.0',
//   id: '123',
//   method: 'update',
//   params: {list: [1, 2, 3]}
// }

jsonrpc.notification(method, params)

Creates a JSON-RPC 2.0 notification object, return JsonRpc object.

  • method: {String}
  • params: {Object|Array}, optional
const notificationObj = jsonrpc.notification('update', {list: [1, 2, 3]})
// {
//   jsonrpc: '2.0',
//   method: 'update',
//   params: {list: [1, 2, 3]}
// }

jsonrpc.success(id, result)

Creates a JSON-RPC 2.0 success response object, return JsonRpc object.

  • id: {String|Integer}
  • result: {Mixed}
const successObj = jsonrpc.success('123', 'OK')
// {
//   jsonrpc: '2.0',
//   id: '123',
//   result: 'OK',
// }

jsonrpc.error(id, error)

Creates a JSON-RPC 2.0 error response object, return JsonRpc object.

  • id: {String|Integer}
  • error: {JsonRpcError}
const errorObj = jsonrpc.error('123', new jsonrpc.JsonRpcError('some error', 99))
// {
//   jsonrpc: '2.0',
//   id: '123',
//   error: {code: 99, 'message': 'some error'},
// }

jsonrpc.parse(message)

Takes a JSON-RPC 2.0 payload (string) and tries to parse it into a JSON. If successful, determine what object is it (response, notification, success, error, or invalid), and return it's type and properly formatted object.

  • message: {String}

return an array, or an object of this format:

single parsed request:

{
  type: 'request',
  payload: {
    jsonrpc: '2.0',
    id: 123,
    method: 'update',
    params: {}
  }
}

batch parsed result:

[{
  type: 'request',
  payload: {
    jsonrpc: '2.0',
    id: '123',
    method: 'update',
    params: [1, 2, 3]
  }
}, {
  type: 'notification',
  payload: {
    jsonrpc: '2.0',
    method: 'update',
    params: {_id: 'xxx'}
  }
}, {
  type: 'success',
  payload: {
    jsonrpc: '2.0',
    id: '123',
    result: 'OK'
  }
}, {
  type: 'error',
  payload: {
    jsonrpc: '2.0',
    id: '123',
    error: [jsonrpc.JsonRpcError object]
  }
}, {
  type: 'invalid',
  payload: [jsonrpc.JsonRpcError object]
}]

jsonrpc.parseObject(message)

Takes a JSON-RPC 2.0 payload (Object) and tries to parse it into a JSON. If successful, determine what object is it (response, notification, success, error, or invalid), and return it's type and properly formatted object.

  • message: {Object}

return an JsonRpcParsed object with type and payload.

Class: jsonrpc.JsonRpc()

Class: jsonrpc.JsonRpcError(message, code, data)

Create a JsonRpcError instance.

  • message: {String}
  • code: {Integer}
  • data: {Mixed} optional
const error = new jsonrpc.JsonRpcError('some error', 999)

Class Method: jsonrpc.JsonRpcError.invalidRequest(data)

Class Method: jsonrpc.JsonRpcError.methodNotFound(data)

Class Method: jsonrpc.JsonRpcError.invalidParams(data)

Class Method: jsonrpc.JsonRpcError.internalError(data)

Class Method: jsonrpc.JsonRpcError.parseError(data)

@deadcanaries/kadence@orcproject/orcvite-wallet@fh0/clijsonrpcc@ciondigital/cionstyx-client@infinitebrahmanuniverse/nolb-jsonr@lcnc/nodejsorpcenconvo.sdk.node@everything-registry/sub-chunk-1988talkpilews-keepaliveteambition-sdk-socketvitejs-notthomizvitejs-notthomiz-walletvitejs-notthomiz-wsvitra@connectlive/connectlive-web-sdk@vitelabs/vite.jsjson-rpc-constructorjs_registry_clientkadkad-spartacuskadence-kimjsonrpc2-expressjsonrpcv2@contentacms/contentajstoa-net@atek-cloud/node-rpc@atek-cloud/atek@adshares/ads-client@akiroz/mqtt-json-rpc@blockstack/ws-rpc-client@chrishdx/platformio-node-helperszhixin-node-helpers@hbgj/common@hbgj/native-api@hbgj/quickapi@hbgj/tools@mocobaas/server-sdk@kadenceproject/kadence@jolocom/sdk-rpc-client@jsonrpc2/rpc@jsrpc/rpc@react-native-windows/automation-channel@qiwi/json-rpc-common@qiwi/json-rpc-open-rpc@stacks/blockchain-api-client@statechannels/xstate-wallet@opengsn/gsn@openovate/eventrpc@pointnetwork/kadence@pnetwork-association/ptokens-utils@p.network/ptokens-node@rocket.chat/apps-enginemqtt-json-rpcmoleculer-wsp3-json-rpcodootsrpc-apitestsatoexchange-rpc-api-clientnode-rnw-rpcnexus-clientnestjs-json-rpcplatformio-node-helpersplatformio-node-helper-codeserverpioarduino-node-helpersptokens-nodeqlc.jsservice_jssnapper-consumersnapper-coresi-node-helpersgitple-botide_qsysgsnnelohafas-client-rpc@verady/veranet@vite/vitejs@vite/vitejs-wallet@yjd-test/quickapi@tacticalchihuahua/dusk@tacticalchihuahua/kadenceaide-node-helpersale-iot-hub-mqttcolab-coinjoin-apiboscaremitrpcexpressjs-json-rpcfullstack-gatsby-theme-bch-wallet-v1fullstack-gatsby-theme-bch-walletgatsby-theme-bch-walletgatsby-ipfs-web-wallet
2.2.0

5 years ago

2.1.1

5 years ago

2.1.0

6 years ago

2.0.7

6 years ago

2.0.6

6 years ago

2.0.5

6 years ago

2.0.4

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.3.1

7 years ago

1.3.0

8 years ago

1.2.4

8 years ago

1.2.3

9 years ago

1.2.2

9 years ago

1.2.1

9 years ago

1.2.0

9 years ago

1.1.2

9 years ago

1.1.1

9 years ago

1.1.0

10 years ago

1.0.0

10 years ago

0.1.0

10 years ago