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
|
npm install uptimekuma-api
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Pushing usage
|
||||||
|
|
||||||
### Start pushing
|
### Start pushing
|
||||||
```js
|
```js
|
||||||
|
@ -19,9 +19,23 @@ kuma.startPushing("push code",60);
|
||||||
|
|
||||||
### Stop pushing
|
### Stop pushing
|
||||||
```js
|
```js
|
||||||
|
kuma.cancelPushing("push code");
|
||||||
|
```
|
||||||
|
or to stop all pushes
|
||||||
|
```js
|
||||||
kuma.cancelPushing();
|
kuma.cancelPushing();
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Push custom data
|
||||||
|
```js
|
||||||
|
kuma.on("prePush", (url, params) => {
|
||||||
|
params.msg = "test";
|
||||||
|
params.status = "down";
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
## Status pages usage
|
||||||
|
|
||||||
### Get statuses
|
### Get statuses
|
||||||
```js
|
```js
|
||||||
for (let x of (await kuma.status())) {
|
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",
|
"name": "uptimekuma-api",
|
||||||
"version": "1.0.6",
|
"version": "1.1.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "src/index.js",
|
"main": "src/index.js",
|
||||||
"types": "src/index.d.ts",
|
"types": "src/index.d.ts",
|
||||||
|
@ -18,6 +18,7 @@
|
||||||
"url": "https://github.com/RedGuys/uptimekuma-api"
|
"url": "https://github.com/RedGuys/uptimekuma-api"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"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 code The monitor code
|
||||||
* @param interval The interval in seconds
|
* @param interval The interval in seconds
|
||||||
*/
|
*/
|
||||||
startPushing(code: string, interval?: number);
|
startPushing(code: string, interval?: number): void;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stops pushing heartbeats to the server
|
* Stops pushing heartbeats to the server
|
||||||
* @param code The monitor code, if not specified, all monitors will be stopped
|
* @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: "pushSuccessful", handle: (url: string) => void);
|
||||||
on(event: "pushFailed", handle: (url: string, err: Error) => 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>;
|
||||||
}
|
}
|
||||||
|
|
55
src/index.js
55
src/index.js
|
@ -1,5 +1,6 @@
|
||||||
const client = require("axios").default;
|
const client = require("axios").default;
|
||||||
const EventEmitter = require("events");
|
const EventEmitter = require("events");
|
||||||
|
const {io} = require("socket.io-client");
|
||||||
|
|
||||||
module.exports = class UptimeKumaApi extends EventEmitter {
|
module.exports = class UptimeKumaApi extends EventEmitter {
|
||||||
|
|
||||||
|
@ -61,4 +62,58 @@ module.exports = class UptimeKumaApi extends EventEmitter {
|
||||||
}
|
}
|
||||||
return result;
|
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