@fjedi/zabbix-client
Version:
Zabbix Javascript API Client
47 lines (46 loc) • 1.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ZabbixClient = void 0;
const ZabbixAPI_1 = require("./ZabbixAPI");
const ZabbixSocket_1 = require("./ZabbixSocket");
const ZabbixResponseException_1 = require("./ZabbixResponseException");
class ZabbixClient {
url;
constructor(url) {
this.url = url;
}
async login(username, password, relogin = false) {
const socket = new ZabbixSocket_1.ZabbixSocket(this.url);
const api = new ZabbixAPI_1.ZabbixAPI(socket);
const token = await api.login(username, password);
socket.setToken(token);
if (relogin)
this.ajustLogin(api, username, password);
return Promise.resolve(api);
}
ajustLogin(api, username, password) {
const http = api.getHttpSocket();
const interceptor = http.interceptors.response.use(async (response) => {
if (response.data && response.data.error) {
const error = response.data.error;
if (error.data.toLowerCase().includes('re-login')) {
try {
const client = new ZabbixClient(this.url);
const newApi = await client.login(username, password);
api.getSocket().setToken(newApi.getSocket().getToken());
const data = JSON.parse(response.config.data);
data.auth = api.getSocket().getToken();
response.config.data = JSON.stringify(data);
return http.request(response.config);
}
catch (error) {
throw new ZabbixResponseException_1.ZabbixResponseException(error.message, response.config);
}
}
}
return response;
}, error => error);
api.setReloginInterceptor(interceptor);
}
}
exports.ZabbixClient = ZabbixClient;