Allow to start multiple push tasks

This commit is contained in:
Ilya 2023-08-20 22:47:52 +03:00
parent 47e1d5d9e0
commit c8778ffe70
3 changed files with 29 additions and 9 deletions

View file

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

14
src/index.d.ts vendored
View file

@ -1,4 +1,8 @@
export default class UptimeKumaApi { export default class UptimeKumaApi {
/**
* Creates a new instance of the UptimeKumaApi class
* @param baseURL
*/
constructor(baseURL: string); constructor(baseURL: string);
/** /**
@ -8,12 +12,20 @@ export default class UptimeKumaApi {
*/ */
startPushing(code: string, interval?: number); startPushing(code: string, interval?: number);
cancelPushing(); /**
* Stops pushing heartbeats to the server
* @param code The monitor code, if not specified, all monitors will be stopped
*/
cancelPushing(code?: string);
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);
on(event: "prePush", handle: (url: string, params: {status:"up"|"down",msg:string,ping?:number}) => void); on(event: "prePush", handle: (url: string, params: {status:"up"|"down",msg:string,ping?:number}) => void);
/**
* Gets the status of public monitors
* @param name The name of target status page
*/
status(name?:string): Promise<[{ status(name?:string): Promise<[{
id: number, id: number,
name: string, name: string,

View file

@ -3,7 +3,7 @@ const EventEmitter = require("events");
module.exports = class UptimeKumaApi extends EventEmitter { module.exports = class UptimeKumaApi extends EventEmitter {
_pushTimer; _pushTimers = {};
_baseURL; _baseURL;
constructor(baseURL = "") { constructor(baseURL = "") {
@ -22,17 +22,25 @@ module.exports = class UptimeKumaApi extends EventEmitter {
} }
startPushing(code, interval = 60) { startPushing(code, interval = 60) {
if (this._pushTimer) if (this._pushTimers[code])
this._pushTimer.cancel(); this._pushTimers[code].cancel();
this._pushTimer = setInterval(() => { this._pushTimers[code] = setInterval(() => {
this.push(this._baseURL+"api/push/"+code); this.push(this._baseURL+"api/push/"+code);
}, interval * 1000); }, interval * 1000);
this.push(this._baseURL+"api/push/"+code); this.push(this._baseURL+"api/push/"+code);
} }
cancelPushing() { cancelPushing(code = undefined) {
if (this._pushTimer) if(code) {
this._pushTimer.cancel(); if (this._pushTimers[code])
this._pushTimers[code].cancel();
} else {
for (let code in this._pushTimers) {
if (this._pushTimers.hasOwnProperty(code)) {
this._pushTimers[code].cancel();
}
}
}
} }
async status(name = "default") { async status(name = "default") {