-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
78 lines (64 loc) · 2.07 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
74
75
76
77
78
const https = require('./services/https.js');
const BASE_URL = 'https://api.nekosia.cat';
const API_URL = `${BASE_URL}/api/v1`;
class NekosiaAPI {
buildQueryParams(options = {}) {
return Object.entries(options)
.filter(([, value]) => {
if (typeof value === 'string' && value.includes(',')) {
throw new Error('A single tag in the string cannot contain commas. Please use an array instead.');
}
return value != null && value !== '' && (!Array.isArray(value) || value.length > 0);
})
.map(([key, value]) => `${encodeURIComponent(key)}=${value}`)
.join('&');
}
async makeHttpRequest(endpoint) {
try {
return https.get(endpoint);
} catch (err) {
console.error(`HTTP request failed for endpoint ${endpoint}: ${err.message}`);
throw err;
}
}
async fetchImages(category, options = {}) {
if (!category) {
throw new Error('The image category is required. For example, use fetchImages(\'catgirl\').');
}
if (options.session && !['id', 'ip'].includes(options.session)) {
throw new Error('The `session` setting can contain only the following values `id` and `ip`, both as strings.');
}
if (!options.session && options.id) {
throw new Error('`id` is not required if the session is `null` or `undefined`');
}
const queryString = this.buildQueryParams({
session: null,
id: null,
count: 1,
additionalTags: [],
blacklistedTags: [],
...options
});
return this.makeHttpRequest(`${API_URL}/images/${category}?${queryString}`);
}
async fetchShadowImages(options = {}) {
if (!Array.isArray(options.additionalTags) || options.additionalTags.length === 0) {
throw new Error('`additionalTags` must be a non-empty array for the shadow category');
}
return this.fetchImages('shadow', options);
}
async fetchById(id) {
if (!id) throw new Error('`id` parameter is required');
return this.makeHttpRequest(`${API_URL}/getImageById/${id}`);
}
}
const NekosiaVersion = {
module: https.version,
api: async () => {
return await https.get(BASE_URL);
}
};
module.exports = {
NekosiaAPI: new NekosiaAPI(),
NekosiaVersion
};