Giter VIP home page Giter VIP logo

jsonrpc2.js's Introduction

@segment/jsonrpc2.js Circle CI

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. <[email protected]>

jsonrpc2.js's People

Contributors

dominicbarnes avatar travisjeffery avatar stephenmathieson avatar rowno avatar fouad avatar calthomson avatar ndhoule avatar abraithwaite avatar flaque avatar lancejpollard avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.