Skip to content

Latest commit

 

History

History
110 lines (64 loc) · 2.47 KB

readme.md

File metadata and controls

110 lines (64 loc) · 2.47 KB

@segment/jsonrpc2.js Circle CI

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
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
Default: false

Enable when you don't need the answer back from the server (ie: it will set id: null).

forceArray

Type: boolean
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:

  • context object which contains all data related to the current call
    • method method to call
    • params params (input) of the call
    • options options passed to call()
    • result response from the server (equals to null 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"

License

MIT © 2017 Segment Inc. <friends@segment.com>