Add method to get statuses and change pushing args

This commit is contained in:
Ilya 2022-01-12 23:32:06 +03:00
parent 42cec0a8f2
commit 4b0d21e494
3 changed files with 48 additions and 10 deletions

View file

@ -1,6 +1,6 @@
{ {
"name": "uptimekuma-api", "name": "uptimekuma-api",
"version": "1.0.0", "version": "1.0.1",
"description": "", "description": "",
"main": "src/index.js", "main": "src/index.js",
"types": "src/index.d.ts", "types": "src/index.d.ts",

21
src/index.d.ts vendored
View file

@ -1,9 +1,26 @@
export default class UptimeKumaApi { export default class UptimeKumaApi {
constructor(); constructor(baseURL: string);
startPushing(code: string, interval?: number);
startPushing(url:string, interval?:number);
cancelPushing(); cancelPushing();
on(event: "pushSuccessful", handle: (url: string) => void); on(event: "pushSuccessful", handle: (url: string) => void);
on(event: "pushFailed", handle: (url: string, err: Error) => void); on(event: "pushFailed", handle: (url: string, err: Error) => void);
status(): Promise<[{
id: number,
name: string,
weight: number, monitors: [{
id: number,
name: string,
uptime: number,
heartbeats: [{
status: number,
time: string,
msg: string,
ping: number
}]
}]
}]>;
} }

View file

@ -4,9 +4,11 @@ const EventEmitter = require("events");
module.exports = class UptimeKumaApi extends EventEmitter { module.exports = class UptimeKumaApi extends EventEmitter {
_pushTimer; _pushTimer;
_baseURL;
constructor() { constructor(baseURL = "") {
super(); super();
this._baseURL = baseURL.endsWith("/") ? baseURL : baseURL + "/";
} }
push(url) { push(url) {
@ -17,17 +19,36 @@ module.exports = class UptimeKumaApi extends EventEmitter {
}); });
} }
startPushing(url, interval=60) { startPushing(code, interval = 60) {
if (this._pushTimer) if (this._pushTimer)
this._pushTimer.cancel(); this._pushTimer.cancel();
this._pushTimer = setInterval(() => { this._pushTimer = setInterval(() => {
this.push(url); this.push(this.baseURL+"api/push/"+code);
}, interval * 1000); }, interval * 1000);
this.push(url); this.push(this.baseURL+"api/push/"+code);
} }
cancelPushing() { cancelPushing() {
if (this._pushTimer) if (this._pushTimer)
this._pushTimer.cancel(); this._pushTimer.cancel();
} }
async status() {
let resp = await client.get(this._baseURL + "api/status-page/monitor-list");
let heartBeats = (await client.get(this._baseURL + "api/status-page/heartbeat")).data;
let result = [];
for (let srcCategory of resp.data) {
let targetCategory = {id: srcCategory.id, name: srcCategory.name, weight: srcCategory.weight, monitors: []};
for (let srcMonitor of srcCategory.monitorList) {
targetCategory.monitors.push({
id: srcMonitor.id,
name: srcMonitor.name,
uptime: heartBeats.uptimeList[srcMonitor.id + "_24"],
heartbeats: heartBeats.heartbeatList[srcMonitor.id]
});
}
result.push(targetCategory);
}
return result;
}
} }