Jsonrpc2.js
JSON-RPC over HTTP client
Install / Use
/learn @segmentio/Jsonrpc2.jsREADME
@segment/jsonrpc2.js 
[!NOTE] Segment has paused maintenance on this project, but may return it to an active status in the future. Issues and pull requests from external contributors are not being considered, although internal contributions may appear from time to time. The project remains available under its open source license for anyone to use.
A JSON-RPCv2 client.
Install
yarn add @segment/jsonrpc2
API
new Client(address, [options])
Sets up a new JSON-RPC client for the given addr. (example: http://localhost:3000/rpc)
options
timeout
Type: number<br>
Default: 10000
Request timeout (in milliseconds).
logger
Type: function
Optional logger for capturing request metrics.
Instance
call(method, [params], [options])
Calls the given method with the given params. (if not an array, it will be converted)
method
Type: string
Method to call on the server.
params
Type: object
Params to pass to the method.
options
timeout
Type: number
Override the default client timeout.
async
Type: boolean<br>
Default: false
Enable when you don't need the answer back from the server (ie: it will set id: null).
forceArray
Type: boolean<br>
Default: true
Enable when you want the request params to be converted to an array.
use(middleware)
Adds a custom middleware to the stack, allowing to customize input and even result, intercept calls or abort them completely.
middleware
Type: function
Middleware is a function that accepts these arguments:
contextobject which contains all data related to the current callmethodmethod to callparamsparams (input) of the calloptionsoptions passed tocall()resultresponse from the server (equals tonullbefore server answers)
nextcall next middleware in the stack, returns a promise
Here's an example middleware to measure call time:
client.use(async (context, next) => {
const startTime = new Date()
await next()
const duration = new Date() - startTime
console.log(`${context.method} spent ${duration}ms`)
})
await client.call('echo', 'Hello World')
//=> "echo spent 2ms"
License
MIT © 2017 Segment Inc. <friends@segment.com>
