-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
52 lines (50 loc) · 1.81 KB
/
index.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
const fetch = require('node-fetch');
const {URL, URLSearchParams} = require('url');
const endpoints = require('./endpoints.json');
let version = require("./package.json").version;
async function getContent(url) {
try {
const res = await fetch(url, {
headers: {
'User-Agent': `AlexFlipnote.js@${version} by HarutoHiroki#4000`
}
});
return res.headers.get("content-type") === "application/json" ? await res.json() : await res.buffer();
}
catch (e) {
return `Error: ${e}`;
}
}
class AlexClient {
constructor() {
let self = this;
let baseURL = 'https://api.alexflipnote.dev';
Object.keys(endpoints).forEach(async (endpoint) => {
self[endpoint] = async function (queryParams = '') {
if(endpoint.includes("coffee")){
baseURL = 'https://coffee.alexflipnote.dev';
}
let url = new URL(`${baseURL}${endpoints[endpoint]}`);
if (endpoint.includes("color")) {
url = url.toString() + queryParams;
} else if (endpoint.includes("nft")) {
if (queryParams.hex) {
url = `${url}/${queryParams.hex}${queryParams.season ? `/${queryParams.season}` : ''}`;
} else {
queryParams !== '' ? url.search = new URLSearchParams(queryParams) : '';
}
} else if (endpoint.includes("sillycat")) {
if (queryParams.hex) {
url = `${url}/${queryParams.hex}${queryParams.hex2 ? `/${queryParams.hex2}` : ''}`;
} else {
queryParams !== '' ? url.search = new URLSearchParams(queryParams) : '';
}
} else {
queryParams !== '' ? url.search = new URLSearchParams(queryParams) : '';
}
return await getContent(url.toString());
};
});
}
}
module.exports = AlexClient;