Start implementing methods of dashboard api
This commit is contained in:
parent
e5f98194c5
commit
b420b34694
28
README.md
28
README.md
|
@ -8,7 +8,7 @@ Supports UptimeKuma versions from 1.13.1 to 1.23.13
|
|||
npm install uptimekuma-api
|
||||
```
|
||||
|
||||
## Usage
|
||||
## Pushing usage
|
||||
|
||||
### Start pushing
|
||||
```js
|
||||
|
@ -19,9 +19,23 @@ kuma.startPushing("push code",60);
|
|||
|
||||
### Stop pushing
|
||||
```js
|
||||
kuma.cancelPushing("push code");
|
||||
```
|
||||
or to stop all pushes
|
||||
```js
|
||||
kuma.cancelPushing();
|
||||
```
|
||||
|
||||
### Push custom data
|
||||
```js
|
||||
kuma.on("prePush", (url, params) => {
|
||||
params.msg = "test";
|
||||
params.status = "down";
|
||||
});
|
||||
```
|
||||
|
||||
## Status pages usage
|
||||
|
||||
### Get statuses
|
||||
```js
|
||||
for (let x of (await kuma.status())) {
|
||||
|
@ -30,3 +44,15 @@ for (let x of (await kuma.status())) {
|
|||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Dashboard usage
|
||||
|
||||
### Login to dashboard
|
||||
```js
|
||||
await kuma.login("username", "password");
|
||||
```
|
||||
|
||||
### Get database size
|
||||
```js
|
||||
console.log(await kuma.getDatabaseSize());
|
||||
```
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "uptimekuma-api",
|
||||
"version": "1.0.6",
|
||||
"version": "1.1.0",
|
||||
"description": "",
|
||||
"main": "src/index.js",
|
||||
"types": "src/index.d.ts",
|
||||
|
@ -18,6 +18,7 @@
|
|||
"url": "https://github.com/RedGuys/uptimekuma-api"
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": "^0.21.1"
|
||||
"axios": "1.6.0",
|
||||
"socket.io-client": "^4.7.5"
|
||||
}
|
||||
}
|
||||
|
|
16
src/index.d.ts
vendored
16
src/index.d.ts
vendored
|
@ -10,13 +10,13 @@ export default class UptimeKumaApi {
|
|||
* @param code The monitor code
|
||||
* @param interval The interval in seconds
|
||||
*/
|
||||
startPushing(code: string, interval?: number);
|
||||
startPushing(code: string, interval?: number): void;
|
||||
|
||||
/**
|
||||
* Stops pushing heartbeats to the server
|
||||
* @param code The monitor code, if not specified, all monitors will be stopped
|
||||
*/
|
||||
cancelPushing(code?: string);
|
||||
cancelPushing(code?: string): void;
|
||||
|
||||
on(event: "pushSuccessful", handle: (url: string) => void);
|
||||
on(event: "pushFailed", handle: (url: string, err: Error) => void);
|
||||
|
@ -41,4 +41,16 @@ export default class UptimeKumaApi {
|
|||
}]
|
||||
}]
|
||||
}]>;
|
||||
|
||||
/**
|
||||
* Initiates websocket connection and logs in
|
||||
* @param login Login used to authenticate
|
||||
* @param password Password used to authenticate
|
||||
*/
|
||||
login(login: string, password: string): Promise<void>;
|
||||
|
||||
/**
|
||||
* Gets the size of the database
|
||||
*/
|
||||
getDatabaseSize(): Promise<number>;
|
||||
}
|
||||
|
|
69
src/index.js
69
src/index.js
|
@ -1,5 +1,6 @@
|
|||
const client = require("axios").default;
|
||||
const EventEmitter = require("events");
|
||||
const {io} = require("socket.io-client");
|
||||
|
||||
module.exports = class UptimeKumaApi extends EventEmitter {
|
||||
|
||||
|
@ -12,9 +13,9 @@ module.exports = class UptimeKumaApi extends EventEmitter {
|
|||
}
|
||||
|
||||
push(url) {
|
||||
let params = {status:"up",msg:"OK",ping:undefined};
|
||||
let params = {status: "up", msg: "OK", ping: undefined};
|
||||
this.emit("prePush", url, params);
|
||||
client.get(url,{params}).then(res => {
|
||||
client.get(url, {params}).then(res => {
|
||||
this.emit("pushSuccessful", url);
|
||||
}).catch(err => {
|
||||
this.emit("pushFailed", url, err);
|
||||
|
@ -25,13 +26,13 @@ module.exports = class UptimeKumaApi extends EventEmitter {
|
|||
if (this._pushTimers[code])
|
||||
this._pushTimers[code].cancel();
|
||||
this._pushTimers[code] = setInterval(() => {
|
||||
this.push(this._baseURL+"api/push/"+code);
|
||||
this.push(this._baseURL + "api/push/" + code);
|
||||
}, interval * 1000);
|
||||
this.push(this._baseURL+"api/push/"+code);
|
||||
this.push(this._baseURL + "api/push/" + code);
|
||||
}
|
||||
|
||||
cancelPushing(code = undefined) {
|
||||
if(code) {
|
||||
if (code) {
|
||||
if (this._pushTimers[code])
|
||||
this._pushTimers[code].cancel();
|
||||
} else {
|
||||
|
@ -44,8 +45,8 @@ module.exports = class UptimeKumaApi extends EventEmitter {
|
|||
}
|
||||
|
||||
async status(name = "default") {
|
||||
let resp = await client.get(this._baseURL + "api/status-page/"+name);
|
||||
let heartBeats = (await client.get(this._baseURL + "api/status-page/heartbeat/"+name)).data;
|
||||
let resp = await client.get(this._baseURL + "api/status-page/" + name);
|
||||
let heartBeats = (await client.get(this._baseURL + "api/status-page/heartbeat/" + name)).data;
|
||||
let result = [];
|
||||
for (let srcCategory of resp.data.publicGroupList) {
|
||||
let targetCategory = {id: srcCategory.id, name: srcCategory.name, weight: srcCategory.weight, monitors: []};
|
||||
|
@ -61,4 +62,58 @@ module.exports = class UptimeKumaApi extends EventEmitter {
|
|||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
async connect() {
|
||||
this._socket = io(this._baseURL, {
|
||||
transports: ["websocket"],
|
||||
upgrade: true
|
||||
});
|
||||
this._socket.onAny((event, ...args) => {
|
||||
this.processMessage(event, ...args);
|
||||
});
|
||||
return new Promise((resolve, reject) => {
|
||||
this._socket.on("connect", () => {
|
||||
this._socket.emit("login", {
|
||||
username: this._username,
|
||||
password: this._password,
|
||||
token: ""
|
||||
}, () => {
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
this._socket.on("connect_error", (err) => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async processMessage(event, ...args) {
|
||||
switch (event) {
|
||||
case "430": {
|
||||
this.emit("login", data[0]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async handleReconnect() {
|
||||
this.connect();
|
||||
}
|
||||
|
||||
async login(username, password) {
|
||||
this._username = username;
|
||||
this._password = password;
|
||||
await this.connect();
|
||||
}
|
||||
|
||||
getDatabaseSize() {
|
||||
return new Promise((resolve, reject) => {
|
||||
this._socket.emit("getDatabaseSize", (args) => {
|
||||
if (args.ok)
|
||||
resolve(args.size);
|
||||
else
|
||||
reject(args.error);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue