Consul Client
A client for Hashicorp Consul written in TypeScript.
This library currently has clients to support the Consul KV API and the Consul Catalog API.
Install
$ npm install --save @creditkarma/consul-client
K/V Store
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.
import { KvStore, IKey, Observer } from '@creditkarma/consul-client'
const kvStore: KvStore = new KvStore([ 'http://localhost:8500' ])
IKey
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.
kvStore.set({ path: 'key', dc: 'dc1' }, 'test').then((success: boolean) => {
if (success) {
}
})
kvStore.get({ path: 'key', dc: 'dc1' }).then((val: string) => {
})
kvStore.watch({ path: 'key', dc: 'dc1' }).onValue((val: string) => {
})
kvStore.unwatch({ path: 'key', dc: 'dc1' })
kvStore.delete({ path: 'key', dc: 'dc1' }).then((success: boolean) => {
if (success) {
}
})
Observer
An Observer
is the object returned from a call to watch
.
It has five public methods:
const observer: Observer<string> = kvStore.watch({ path: 'key', dc: 'dc1' })
observer.onValue((val: string) => {
})
observer.onError((err: Error) => {
})
observer.current()
observer.previous()
observer.destroy()
const currentVal: string | null = observer.current()
const previousVal: string | null = observer.previous()
Request Options
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.
const kvStore: KvStore = new KvStore([ 'http://localhost:8500' ], { headers: { ... } })
kvStore.get({ path: 'key' }, { headers: { ... } })
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.
Available Options
Catalog
The Catalog API allows you to discover other assets registered with your Consul instance. This is useful for service discovery.
import { Catalog } from '@creditkarma/consul-client'
const catalog: Catalog = new Catalog([ 'http://localhost:8500' ])
API Overview
catalog.listNodes().then((res: Array<INodeDescription>) => {
})
catalog.listServices().then((res: IServiceMap) => {
})
catalog.listNodesForService('service-name').then((res: IServiceMap) => {
})
catalog.resolveAddress('service-name').then((res: string) => {
})
catalog.watchAddress('service-name').onValue((res: string) => {
})
Contributing
For more information about contributing new features and bug fixes, see our Contribution Guidelines.
External contributors must sign Contributor License Agreement (CLA)
License
This project is licensed under Apache License Version 2.0