Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@logzio-node-toolbox/consul

Package Overview
Dependencies
Maintainers
6
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@logzio-node-toolbox/consul

Consul easy use for json configs and service discovery

  • 0.0.31
  • Source
  • npm
  • Socket score

Version published
Maintainers
6
Created
Source

consul

easy to use warper for consul to register to service discovery and get, merge and watch keys

Usage

import { Consul } from '@logzio-node-toolbox/consul';

const consul = new Consul({ port: 18500, host: '127.0.0.1', baseUrl = 'some_base_url' });

initialized params:

  1. port - consul port to connect default 8500 (default 8500)
  2. host - consul host to connect default 8500 (default localhost)
  3. baseUrl - some default path to load configs from, will prefix the path to each get request (default '')
  4. validateOptions - object with defaults { fail: true, timeout: 5000, retries: 6, factor: 2, onRetry: null } can override each one of the defaults.
  5. defaultWatchOptions - object with defaults { backoffFactor: 100, backoffMax: 30000, maxAttempts: 10000 } can override each one of the defaults.
  6. defaultRegisterRetryOptions - object with defaults { factor: 2, retries: 6, onRetry: null } can override each one of the defaults.

methods

validateConnected - make use connected to consul service (will retry (connectMaxRetries) of times);

receive same params as validateOptions and will merge with th one passed to the initializer

  await consul.validateConnected(validateOptions)

get - will get a key from consul service, (if initlized with base path it will prefix it)

  const { key, value } = await consul.get('somepath/config.json')
  // if have base path will fetch 'some_base_url/somepath/config.json'

set - will set a value to consul, (if initlized with basebath it will prefix it)

   await consul.set({key: 'somepath/config.json', value: {some: "value"}})
  // if have base path will set to 'some_base_url/somepath/config.json'

keys - list of keys in path

   await consul.keys('somepath/config.json')

merge - deepmerge current values with new values

it will fetch current values will deep merge all keys and set it

   await consul.merge({key: 'somepath/config.json', value: {toOverride: "newValue" }})

watch - listen to key change and invoke handler

options - same watchOptions object as the initializer ( will merge them together)

   await consul.watch({
   key: 'somepath/config.json',
   onChange: ({key, value}) => {
       console.log(key)
       console.log(value) // new values
   },
   onError:(err) => {
    console.log(err)
   },
   options
   })

register - will register service to consul

options - same as registerRetryOptions object as the initializer ( will merge them together)

    interface RegisterData {
      meta?: AnyObject;
      checks?: AnyObject;
      address?: string;
      id?: string;
      name?: string;
      port?: number;
    }

   await consul.register({
   data,
   options
   })

registerInterval - will create interval to validate service always register to consul

options - same as registerRetryOptions object as the initializer ( will merge them together)

    interface RegisterData {
      meta?: AnyObject;
      checks?: AnyObject;
      address?: string;
      id?: string;
      name?: string;
      port?: number;
    }

   await consul.register({
   data,
   interval: 3000,
   onError:(err) => {
    console.log(err)
   },
   options
   })
close - deregister and close all watchers

options - same as registerRetryOptions object as the initializer ( will merge them together)

   await consul.close(options)

multiConsul

extend consul to work on multiple keys and merged config

import { MultiConsul } from '@logzio-node-toolbox/consul';
const consul = new MultiConsul({ port: 18500, paths:['config1', 'config2', 'config3'] });
/*
config1 in consul:
{
  "key1": "value1"
  key2": "value1"
  key3": "value1"
}

config2 in consul:
{
  key2": "value2"
}
 // no config 3
*/

const values = await consul.getAll() // {key1: value1, key2: value2, key3: value1};

watchAll(({ key, value, changedValue }) => {
  /* settings new consul file config3
    { key: value3 }
    will invoke watch with
    key: config3
    changedValue: { key: value3 }
    values: {key1: value1, key2: value2, key3: value3};
  */
})

FAQs

Package last updated on 31 Aug 2022

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc