UNPKG

ethstats-cli

Version:
86 lines (67 loc) 3.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _requestPromise = _interopRequireDefault(require("request-promise")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } var Configurator = /*#__PURE__*/ function () { function Configurator(diContainer) { _classCallCheck(this, Configurator); this.config = diContainer.config; this.log = diContainer.logger; this.lodash = diContainer.lodash; this.cli = diContainer.cli; this.requestPromise = _requestPromise.default; this.url = this.config.configurator.url; var configStoreConfigurator = diContainer.config.configStore.get('configurator'); if (configStoreConfigurator) { this.url = configStoreConfigurator.url; } if (this.cli.flags.configuratorUrl) { this.url = this.cli.flags.configuratorUrl; this.config.configStore.set('configurator', { url: this.url }); } } _createClass(Configurator, [{ key: "get", value: function get(params) { var _this = this; var requestOptions = { method: 'GET', uri: "".concat(this.url, "/configs/").concat(params.configName), json: true }; if (!this.lodash.isEmpty(params.configParams)) { var configParamsValue = []; Object.keys(params.configParams).forEach(function (param) { configParamsValue.push("configParams[".concat(param, "]=").concat(params.configParams[param])); }); requestOptions.uri += "?".concat(configParamsValue.join('&')); } this.log.debug("Request config from server: ".concat(JSON.stringify(requestOptions))); return this.requestPromise(requestOptions).then(function (requestResult) { var result = null; if (requestResult.body.success) { result = requestResult.body.data[0]; } else { _this.log.error("Configurator => ".concat(requestResult.body.errors[0]), false, true); } return result; }).catch(function (error) { var errorMessage = _this.lodash.isObject(error.error) ? error.error.body === undefined ? error.error : error.error.body.errors[0] : error.message; var exit = params.configName === 'serverUrl'; _this.log.error("Configurator => ".concat(errorMessage), false, exit); }); } }]); return Configurator; }(); exports.default = Configurator;