A client for Hashicorp Consul written in TypeScript.
$ npm install --save @creditkarma/consul-client
The K/V store provides a simple JS API for getting values in and out of Consul. This API is primarily exposed through the KvStore class.
/*** Instantiate KvStore with location of Consul, default is localhost:8500.** The argument is actually an array of locations in order to support fail over.*/
The KvStore reads, writes and deletes values with Consul based on
IKey objects. These are objects with one required property and one optional property. The required property is
path. The path is the key name to look up. The optional property is
dc. The dc is the datacenter to read from. Per the Consul docs this will default to the datacenter of the agent being queried, specified by the host address.
// Set value for keykvStore.set, 'test'.then// Get current value of keykvStore.get.then// Watch a key for value changeskvStore.watch.onValue// Stop watching a valuekvStore.unwatch// Delete key from consulkvStore.delete.then
Observer is the object returned from a call to
It has five public methods:
observer.onValueobserver.onErrorobserver.current // Returns the current valueobserver.previous // Returns the previous valueobserver.destroy // Nulls out all internal state// Get the current value, returns null if no value// Get the previous value, returns null in no previous value
We use Request as our underlying HTTP client. As such you can pass options through to Request to customize the HTTP request for your environment. This can be done both when instantiating a new KvStore or when making a request.
The options given to the KvStore constructor are used on every request. Options given to a method are only used for that request. Options passed to a request method are deep merged with the instance options before performing the request.
The Catalog API allows you to discover other assets registered with your Consul instance. This is useful for service discovery.
/*** Instantiate Catalog with location of Consul, default is localhost:8500** Like KvStore the Catalog supports fail over by providing a list of addresses.*/
// List all nodes registered with Consulcatalog.listNodes.then// List all services registered with Consulcatalog.listServices.then// List all nodes registered with a given servicecatalog.listNodesForService'service-name'.then// Get the registered address for a given servicecatalog.resolveAddress'service-name'.then// Watch for runtime changes to a service addresscatalog.watchAddress'service-name'.onValue
For more information about contributing new features and bug fixes, see our Contribution Guidelines. External contributors must sign Contributor License Agreement (CLA)
This project is licensed under Apache License Version 2.0