-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
73 lines (59 loc) · 3.01 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* T N A I A P I
* TNAI is an interactive API delegated by a token system for obtaining gif in a random way.
*
* **SUPPORT:** https://discord.gg/2BQMYyV
*/
const fetch = require('node-fetch');
const { URL, URLSearchParams } = require('url');
const endpoints = require('./endpoints.json');
module.exports = class TnaiAPI {
sfw = new Object();
hentai = new Object();
/**
* Main class with which you can access each function to obtain a gif from a certain endpoint.
* @param {string} token - You can get some token on (https://tnai.ml/).
*/
constructor(token) {
const baseURL = 'https://tnai.ml/api';
if (!token) throw new Error("[NO TOKEN INCLUDED] Didn't include an access token, get it on the website. (https://tnai.ml/).");
Object.keys(endpoints.sfw).forEach((endpoint) => {
this.sfw[endpoint] = async (queryParams) => {
let url = new URL(`${baseURL}/image?type=${endpoint}`);
if (!url || (url && !url.search)) throw new Error("[INVALID ENDPOINT SEARCH] The endpoint doesn't have a valid search parameter.");
if (queryParams) url.search = new URLSearchParams(queryParams);
let response = await this.getContent(url.toString(), token);
if (!response || (response && !response.url)) throw new Error("[INVALID RESPONSE] The client received an invalid response, please go to our support Discord for get help.");
return response.url;
};
}); // end of SFW endpoints
Object.keys(endpoints.hentai).forEach((endpoint) => {
this.hentai[endpoint] = async (queryParams) => {
let url = new URL(`${baseURL}/hentai?type=${endpoint}`);
if (!url || (url && !url.search)) throw new Error("[INVALID ENDPOINT SEARCH] The endpoint doesn't have a valid search parameter.");
if (queryParams) url.search = new URLSearchParams(queryParams);
let response = await this.getContent(url.toString(), token);
if (!response || (response && !response.url)) throw new Error("[INVALID RESPONSE] The client received an invalid response, please go to our support Discord for get help.");
return response.url;
};
}); // end of HENTAI enpoints
}
/**
* @name getContent
*
* With this function you can make requests to the API in a more personalized way.
* @param {string} url - API Endpoint.
* @param {string} token - Your secret token.
*/
async getContent(url, token) {
if(!(url || token) || (url && token && !url.startsWith("https://tnai.ml/api/"))) throw new Error ("[INVALID PARAMETERS] You dont includes URL or Token, or your URL doesn't match with any TNAI api endpoint.");
let res = await fetch(`${url}`, {
"headers": {
"Authorization": `${token}`
}
}).then((res) => res.json());
if (res.status !== 200) throw new Error(`[API] An error has ocurred, error: ${res.message}.`);
if (!res.url) throw new Error(`[API] An error has ocurred, error: The response doesn't contain an URL, please check our support Discord for get information about API issues.`);
return res || null;
}
}