🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

brake-client

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

brake-client

An client with circuit.

0.1.3
latest
Source
npm
Version published
Weekly downloads
7
-72%
Maintainers
1
Weekly downloads
 
Created
Source

cloud-client

Usage

ResourceClient

import BrakeClient from 'brake-client';
import rp from 'request-promise';

const SERVICE_NAME = 'a-service';
const brake = new BrakeClient(SERVICE_NAME);

//set health check.
brake.healthCheck(() => {
  return rp({
      method: 'get',
      url: `/${SERVICE_NAME}/health`,
      headers: {
          'Content-Type': 'application/json'
      }
  });
});

brake.on('circuitOpen', () => {
    logger.warn(`The service: ${SERVICE_NAME}'s circuit is opened.`);
});

brake.on('circuitClosed', () => {
    logger.info(`The service: ${SERVICE_NAME}'s circuit is closed.`);
});

brake.fallback(err => {
    throw new Error('Cannot invoke downstream service. please try again soon.', err);
});

export function getResource(id) {
    const request = {
        method: 'get',
        url: `/${SERVICE_NAME}/v1/resources/:id`,
        params: {id: id},
        headers: {
            'Content-Type': 'application/json'
        }
    };
    return brake.circuit({send: rp}).send(request);
}

You can use brake client to invoke resource api. The example is

import resourceClient from './ResourceClient';

resourceClient.getResource(id).then(resource => {
    console.log(resource);
})

API

new BrakeClient(serviceName, options)

serviceName

The service name.

options

The options param is the same as brakes. We extend it, and support request handlers

  • options.handler.preHandle(request)
  • options.handler.postHandle(err, response)
  • options.handler.postCircuit(response);

brake.healthCheck(callback)

Set a callback, when the circuit is open, the callback will be used for checking the service's health status, if the status is ok, the circuit will close.

brake.on(eventName, callback)

See brakes for detail.

brake.isOpen()

Return the circuit's status.

brake.circuit(client, fallback, options) : {send(request)}

  • client: (required) an object implement send function.
  • fallback: (optional) fallback function.
  • options (optional) the same as brakes.

brake.fallback(callback)

  • fallback: (optional) the global fallback function.

Keywords

circuit

FAQs

Package last updated on 11 Jul 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts