diff --git a/lib/salix.js b/lib/salix.js index c39e6b6..df3cacd 100644 --- a/lib/salix.js +++ b/lib/salix.js @@ -5,39 +5,52 @@ module.exports = class Salix { this._salixVersion = null; } async init(logger) { - this.conf = logger.conf; setInterval(() => this.getSalixVersion, this.conf.salix.renewInterval); await this.salixLogInfo(); -} + } -async getSalixVersion(){ - console.log("SALIX - CHECK VERSION"); - let salixCall = await fetch(`${this.conf.salix.url}/applications/version`, { - method: "GET", + async fetch(path) { + let salixCall = await fetch(`${this.conf.salix.url}/${path}`, { + method: "GET", }); - let salixVersion = JSON.parse(await salixCall.text()); - console.log("SALIX - VERSION CHECKED"); - if(this._salixVersion !== salixVersion) fetch(); -} - -async salixLogInfo() { - console.log("SALIX - LOGINFO REQUEST"); - let salixCall = await fetch(`${this.conf.salix.url}/schemas/logInfo`, { - method: "GET", - }); - let salixLogConfig = [JSON.parse(await salixCall.text())]; + const response = JSON.parse(await salixCall.text()); + if (salixCall.status !== 200) { + console.error(response.error); + throw new Error(response.error.message); + } this._salixVersion = salixCall.headers.get("Salix-version"); - this._logInfo = salixLogConfig - console.log("SALIX- LOGINFO REQUESTED"); + return response; } - get logInfo(){ - return this._logInfo + async getSalixVersion() { + log("CHECK VERSION"); + + const salixVersion = await this.fetch("applications/version"); + if (this._salixVersion !== salixVersion) fetch(); + + log("VERSION CHECKED"); } - get salixVersion(){ + async salixLogInfo() { + log("LOGINFO REQUEST"); + let salixLogConfig = await this.fetch("schemas/logInfo"); + + this._logInfo = [salixLogConfig]; + + log("LOGINFO REQUESTED"); + } + + log(param) { + console.log(`${Salix.name} - ${param}`); + } + + get logInfo() { + return this._logInfo; + } + + get salixVersion() { return this._salixVersion; } };