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.
yarn add @segment/jsonrpc2
Sets up a new JSON-RPC client for the given addr
. (example: http://localhost:3000/rpc
)
Type: number
Default: 10000
Request timeout (in milliseconds).
Type: function
Optional logger for capturing request metrics.
Calls the given method
with the given params
. (if not an array, it will be converted)
Type: string
Method to call on the server.
Type: object
Params to pass to the method.
Type: number
Override the default client timeout.
Type: boolean
Default: false
Enable when you don't need the answer back from the server (ie: it will set id: null
).
Type: boolean
Default: true
Enable when you want the request params to be converted to an array.
Adds a custom middleware to the stack, allowing to customize input and even result, intercept calls or abort them completely.
Type: function
Middleware is a function that accepts these arguments:
context
object which contains all data related to the current callmethod
method to callparams
params (input) of the calloptions
options passed tocall()
result
response from the server (equals tonull
before server answers)
next
call 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"
MIT © 2017 Segment Inc. <friends@segment.com>