@fjedi/zabbix-client
Version:
Zabbix Javascript API Client
55 lines (54 loc) • 1.51 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ZabbixSocket = void 0;
const axios_1 = __importDefault(require("axios"));
class ZabbixSocket {
http;
token = null;
constructor(url) {
this.http = axios_1.default.create({
baseURL: url,
headers: {
Accept: 'application/json',
},
timeout: 40000,
retries: 0,
});
this.http.interceptors.response.use(null, error => {
if (error.code === 'ECONNABORTED' && error.config && error.config.retries < 3) {
error.config.retries++;
return this.http.request(error.config);
}
return Promise.reject(error);
});
}
getHttp() {
return this.http;
}
getToken() {
return this.token;
}
setToken(token) {
this.token = token;
return this.token;
}
async call(method, params, noAuth = false) {
const id = Math.random()
.toString()
.substr(3);
const data = {
jsonrpc: '2.0',
method,
params,
id,
auth: this.token,
};
if (noAuth)
data.auth = null;
return await this.http.post('', data);
}
}
exports.ZabbixSocket = ZabbixSocket;