UNPKG

@hapiness/consul

Version:
121 lines 4.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const consul = require("consul"); const utils_1 = require("../utils"); class ConsulClientManager { constructor(config) { this._config = this._buildConfig(config); } _buildConfig(config) { const _config = { scheme: config.scheme || 'http', host: config.host || '127.0.0.1', port: config.port || 8500, defaults: config.defaults, ca: config.ca, baseUrl: config.baseUrl }; if (_config.baseUrl) { delete _config.scheme; delete _config.host; delete _config.port; } return Object.keys(_config).reduce((finalConf, prop) => { if (!_config[prop]) { return finalConf; } return Object.assign({}, finalConf, { [prop]: _config[prop] }); }, {}); } _convertClientFunctionToObservable(client, functions) { const functionsSet = {}; [].concat(functions.root) .filter(_ => !!_) .forEach(func_name => { const _bindObservable = utils_1.bindObservable.bind({ client }); Object.defineProperty(functionsSet, func_name, { configurable: true, enumerable: true, writable: true, value(...args) { return _bindObservable(func_name, ...args); } }); }); const subClients = Object.keys(functions).filter(name => name !== 'root'); if (subClients.length > 0) { subClients.forEach(cliName => { const _bindObservable = utils_1.bindObservable.bind({ client: client[cliName] }); functionsSet[cliName] = {}; [].concat(functions[cliName]) .filter(_ => !!_) .forEach(func_name => { Object.defineProperty(functionsSet[cliName], func_name, { configurable: true, enumerable: true, writable: true, value(...args) { return _bindObservable(func_name, ...args); } }); }); }); } return functionsSet; } createClient() { this._consul = consul(Object.assign({}, this._config, { promisify: true })); this._client = { acl: this._convertClientFunctionToObservable(this._consul.acl, ConsulClientManager.KnownFunctions.acl), agent: this._convertClientFunctionToObservable(this._consul.agent, ConsulClientManager.KnownFunctions.agent), catalog: this._convertClientFunctionToObservable(this._consul.catalog, ConsulClientManager.KnownFunctions.catalog), event: this._convertClientFunctionToObservable(this._consul.event, ConsulClientManager.KnownFunctions.event), health: this._convertClientFunctionToObservable(this._consul.health, ConsulClientManager.KnownFunctions.health), kv: this._convertClientFunctionToObservable(this._consul.kv, ConsulClientManager.KnownFunctions.kv), lock: (args) => this._consul.lock(args), session: this._convertClientFunctionToObservable(this._consul.session, ConsulClientManager.KnownFunctions.session), status: this._convertClientFunctionToObservable(this._consul.status, ConsulClientManager.KnownFunctions.status), watch: (options) => this._consul.watch({ method: this._consul.kv.get, options }) }; } get client() { return this._client; } get config() { return this._config; } static get KnownFunctions() { return { acl: { root: ['create', 'update', 'destroy', 'get', 'clone', 'list'] }, agent: { root: ['members', 'self', 'maintenance', 'join', 'forceLeave'], check: ['list', 'register', 'deregister', 'pass', 'warn', 'fail'], service: ['list', 'register', 'deregister', 'maintenance'] }, catalog: { root: ['datacenters'], node: ['list', 'services'], service: ['list', 'nodes'] }, event: { root: ['fire', 'list'] }, health: { root: ['node', 'checks', 'service', 'state'] }, kv: { root: ['get', 'set', 'del', 'keys'] }, session: { root: ['create', 'destroy', 'get', 'node', 'list', 'renew'] }, status: { root: ['leader', 'peers'] } }; } } exports.ConsulClientManager = ConsulClientManager; //# sourceMappingURL=consul.manager.js.map