Repository navigation
Expand file tree
/
Copy pathhelpers.js
More file actions
76 lines (71 loc) · 1.97 KB
/
Copy pathhelpers.js
File metadata and controls
76 lines (71 loc) · 1.97 KB
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
71
72
73
74
75
76
const fetch = require("node-fetch");
const config = require("./config.js");
const formdata = require("form-data");
const moment = require("moment");
/**
* Send a request to the specified AnonDev API endpoint
* @param {String} endpoint The resource being requested
* @param {String} method The method of the request
* @param {JSON} bodyJSON The body of the request
* @returns {Promise<Object<string, any>>}
*/
async function request(
endpoint,
method = "GET",
bodyJSON = {},
bodyType = "formdata"
) {
const apiURL = config.apiURL ? config.apiURL : "https://api.anondev.ml";
const apiKey = config.apiKey;
if (bodyType == "formdata") {
var body = new formdata();
for await (const [name, value] of Object.entries(bodyJSON)) {
body.append(name, value);
}
} else if (bodyType == "urlencoded") {
const formBody = [];
for (const property in bodyJSON) {
const encodedKey = encodeURIComponent(property);
const encodedValue = encodeURIComponent(bodyJSON[property]);
formBody.push(`${encodedKey}=${encodedValue}`);
}
var body = formBody.join("&");
}
return await fetch(`${apiURL}${endpoint}`, {
method,
body: ["GET", "HEAD"].includes(method) ? null : body,
headers: {
Authorization: `${apiKey}`,
Accept: "application/json",
"User-Agent": `AnonDev_API_Bot_Request@v${
require("./package.json").version
}`,
},
});
}
function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function formatDate(date, divide) {
if (divide) {
let formatted = `<t:${moment(date / 1000).format("x")}:f>`;
if (formatted.includes("Invalid date")) return "Invalid date";
return formatted;
} else {
let formatted = `<t:${moment(date).format("x")}:f>`;
if (formatted.includes("Invalid date")) return "Invalid date";
return formatted;
}
}
function isObject(val) {
if (val === null) {
return false;
}
return typeof val === "function" || typeof val === "object";
}
module.exports = {
request,
capitalize,
formatDate,
isObject,
};