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