-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.js
70 lines (66 loc) · 2.09 KB
/
api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const { get } = require("http");
const { URL, URLSearchParams } = require("url");
const endpoints = require("./localized.json");
function getContent(url) {
return new Promise((resolve, reject) => {
get(url, (res) => {
const { statusCode } = res;
if (statusCode !== 200) {
res.resume();
reject(`Request failed. Status code: ${statusCode}`);
}
res.setEncoding("utf8");
let rawData = "";
res.on("data", (chunk) => {
rawData += chunk;
});
res.on("end", () => {
try {
const parsedData = JSON.parse(rawData);
resolve(parsedData);
} catch (e) {
reject(`Error: ${e.message}`);
}
});
}).on("error", (err) => {
reject(`Error: ${err.message}`);
});
});
}
class strygwyrClient {
constructor() {
let self = this;
self.changes = {};
self.stats = {};
self.talent = {};
let baseURL = "http://192.145.238.5/~pasirm5/v3/strygwyr";
Object.keys(endpoints.changes).forEach(async (endpoint) => {
self.changes[endpoint] = async function (queryParams = "") {
let url = new URL(`${baseURL}${endpoints.changes[endpoint]}`);
queryParams !== ""
? (url.search = new URLSearchParams(queryParams))
: "";
return await getContent(url.toString());
};
});
Object.keys(endpoints.stats).forEach(async (endpoint) => {
self.stats[endpoint] = async function (queryParams = "") {
let data = new URL(`${baseURL}${endpoints.stats[endpoint]}`);
queryParams !== ""
? (data.search = new URLSearchParams(queryParams))
: "";
return await getContent(data.toString());
};
});
Object.keys(endpoints.talent).forEach(async (endpoint) => {
self.talent[endpoint] = async function (queryParams = "") {
let data = new URL(`${baseURL}${endpoints.talent[endpoint]}`);
queryParams !== ""
? (data.search = new URLSearchParams(queryParams))
: "";
return await getContent(data.toString());
};
});
}
}
module.exports = strygwyrClient;