Skip to content

Commit a5c71a9

Browse files
authored
v0.0.1
1 parent 9dfe896 commit a5c71a9

3 files changed

Lines changed: 78 additions & 0 deletions

File tree

index.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const axios = require('axios');
2+
3+
module.exports = class FastPanel {
4+
constructor(url) {
5+
this.url = url;
6+
}
7+
8+
auth(username, password) {
9+
return new Promise(async (resolve, reject) => {
10+
const res = await axios.post(`${this.url}/login`, { username, password });
11+
12+
if(!res.data.token) {
13+
console.log('[FastPanel API by MayerDev] Username or password incorrect');
14+
reject();
15+
}
16+
17+
console.log('[FastPanel API by MayerDev] Got token:');
18+
console.log(res.data.token);
19+
20+
this.token = res.data.token;
21+
resolve();
22+
});
23+
}
24+
25+
_get(route) {
26+
return new Promise(async (resolve, reject) => {
27+
const res = await axios.get(`${this.url}/${route}`, {
28+
headers: {
29+
Authorization: `Bearer ${this.token}`
30+
}
31+
});
32+
33+
resolve(res.data);
34+
});
35+
}
36+
37+
_post(route, data) {
38+
return new Promise(async (resolve, reject) => {
39+
const res = await axios.get(`${this.url}/${route}`, {
40+
headers: {
41+
Authorization: `Bearer ${this.token}`
42+
}
43+
});
44+
45+
resolve(res.data);
46+
});
47+
}
48+
49+
me() {
50+
return new Promise(async (resolve, reject) => {
51+
const res = await this._get('api/me');
52+
if(res) resolve(res);
53+
54+
reject(res)
55+
});
56+
}
57+
}

package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "fastpanel-api",
3+
"version": "1.0.0",
4+
"description": "FastPanel API by MayerDev",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "Artem Lomakin <mayer.dev@yahoo.com>",
10+
"license": "MIT",
11+
"dependencies": {
12+
"axios": "^0.21.1"
13+
}
14+
}

test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const api = require('./index');
2+
3+
const Api = new api('https://cp.mayerdev.com');
4+
5+
Api.auth('fastuser', 'password').then(async () => {
6+
Api.me();
7+
});

0 commit comments

Comments
 (0)