1 | const fetch = require('node-fetch');
|
2 | const {URL, URLSearchParams} = require('url');
|
3 | const endpoints = require('./endpoints.json');
|
4 | let version = require("./package.json").version;
|
5 | async function getContent(url) {
|
6 | try {
|
7 | const res = await fetch(url, {
|
8 | headers: {
|
9 | 'User-Agent': `AlexFlipnote.js@${version} by HarutoHiroki#4000`
|
10 | }
|
11 | });
|
12 | return res.headers.get("content-type") === "application/json" ? await res.json() : await res.buffer();
|
13 | }
|
14 | catch (e) {
|
15 | return `Error: ${e}`;
|
16 | }
|
17 | }
|
18 |
|
19 | class AlexClient {
|
20 | constructor() {
|
21 | this.image = {};
|
22 | this.others = {};
|
23 | let baseURL = 'https://api.alexflipnote.dev';
|
24 | Object.keys(endpoints.image).forEach(async (endpoint) => {
|
25 | this.image[endpoint] = async function (queryParams = '') {
|
26 | if(endpoint.includes("coffee")){
|
27 | baseURL = 'https://coffee.alexflipnote.dev'
|
28 | }
|
29 | let url = new URL(`${baseURL}${endpoints.image[endpoint]}`);
|
30 | queryParams !== '' ? url.search = new URLSearchParams(queryParams) : '';
|
31 | return await getContent(url.toString());
|
32 | };
|
33 | });
|
34 | Object.keys(endpoints.others).forEach(async (endpoint) => {
|
35 | this.others[endpoint] = async function (params = '') {
|
36 | let url = new URL(`${baseURL}${endpoints.others[endpoint]}`);
|
37 | if (endpoint.includes("color")) {
|
38 | if (/^[0-9A-F]{6}$/i.test(params.toUpperCase())) {
|
39 | url = url.toString() + params
|
40 | return await getContent(url);
|
41 | } else {
|
42 | return console.error("Not a valid hex value")
|
43 | }
|
44 | }else{
|
45 | params !== '' ? url.search = new URLSearchParams(params) : '';
|
46 | return await getContent(url.toString());
|
47 | }
|
48 | };
|
49 | });
|
50 | }
|
51 | }
|
52 |
|
53 | module.exports = AlexClient; |
\ | No newline at end of file |