Start implementing methods of dashboard api

This commit is contained in:
Ilya 2024-08-22 23:31:24 +03:00
parent e5f98194c5
commit b420b34694
4 changed files with 106 additions and 12 deletions

View file

@ -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());
```

View file

@ -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
View file

@ -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>;
}

View file

@ -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 {
@ -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);
});
});
}
}