@20syldev/api 3.5.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.prettierrc +7 -0
- package/README.md +32 -33
- package/dist/app.js +36 -0
- package/dist/app.js.map +1 -0
- package/dist/config/env.js +73 -0
- package/dist/config/env.js.map +1 -0
- package/dist/config/versions.js +72 -0
- package/dist/config/versions.js.map +1 -0
- package/dist/constants.js +42 -0
- package/dist/constants.js.map +1 -0
- package/dist/middleware/cors.js +15 -0
- package/dist/middleware/cors.js.map +1 -0
- package/dist/middleware/error.js +6 -0
- package/dist/middleware/error.js.map +1 -0
- package/dist/middleware/json.js +8 -0
- package/dist/middleware/json.js.map +1 -0
- package/dist/middleware/logger.js +26 -0
- package/dist/middleware/logger.js.map +1 -0
- package/dist/middleware/ratelimit.js +58 -0
- package/dist/middleware/ratelimit.js.map +1 -0
- package/dist/middleware/version.js +30 -0
- package/dist/middleware/version.js.map +1 -0
- package/dist/modules/v3/algorithms.js +221 -0
- package/dist/modules/v3/algorithms.js.map +1 -0
- package/{modules → dist/modules}/v3/captcha.js +4 -13
- package/dist/modules/v3/captcha.js.map +1 -0
- package/dist/modules/v3/chat.js +103 -0
- package/dist/modules/v3/chat.js.map +1 -0
- package/{modules → dist/modules}/v3/color.js +12 -13
- package/dist/modules/v3/color.js.map +1 -0
- package/dist/modules/v3/convert.js +41 -0
- package/dist/modules/v3/convert.js.map +1 -0
- package/dist/modules/v3/domain.js +83 -0
- package/dist/modules/v3/domain.js.map +1 -0
- package/dist/modules/v3/hash.js +16 -0
- package/dist/modules/v3/hash.js.map +1 -0
- package/dist/modules/v3/hyperplanning.js +50 -0
- package/dist/modules/v3/hyperplanning.js.map +1 -0
- package/dist/modules/v3/levenshtein.js +38 -0
- package/dist/modules/v3/levenshtein.js.map +1 -0
- package/dist/modules/v3/personal.js +129 -0
- package/dist/modules/v3/personal.js.map +1 -0
- package/dist/modules/v3/qrcode.js +21 -0
- package/dist/modules/v3/qrcode.js.map +1 -0
- package/dist/modules/v3/tic_tac_toe.js +221 -0
- package/dist/modules/v3/tic_tac_toe.js.map +1 -0
- package/dist/modules/v3/time.js +88 -0
- package/dist/modules/v3/time.js.map +1 -0
- package/{modules → dist/modules}/v3/token.js +10 -11
- package/dist/modules/v3/token.js.map +1 -0
- package/dist/modules/v3/username.js +92 -0
- package/dist/modules/v3/username.js.map +1 -0
- package/dist/modules/v3/utils.js +70 -0
- package/dist/modules/v3/utils.js.map +1 -0
- package/dist/modules/v3.js +16 -0
- package/dist/modules/v3.js.map +1 -0
- package/dist/modules/v4/algorithms.js +200 -0
- package/dist/modules/v4/algorithms.js.map +1 -0
- package/dist/modules/v4/captcha.js +35 -0
- package/dist/modules/v4/captcha.js.map +1 -0
- package/dist/modules/v4/chat.js +92 -0
- package/dist/modules/v4/chat.js.map +1 -0
- package/dist/modules/v4/color.js +43 -0
- package/dist/modules/v4/color.js.map +1 -0
- package/dist/modules/v4/convert.js +33 -0
- package/dist/modules/v4/convert.js.map +1 -0
- package/dist/modules/v4/dice.js +30 -0
- package/dist/modules/v4/dice.js.map +1 -0
- package/dist/modules/v4/domain.js +78 -0
- package/dist/modules/v4/domain.js.map +1 -0
- package/dist/modules/v4/encode.js +165 -0
- package/dist/modules/v4/encode.js.map +1 -0
- package/dist/modules/v4/geo.js +41 -0
- package/dist/modules/v4/geo.js.map +1 -0
- package/dist/modules/v4/hash.js +9 -0
- package/dist/modules/v4/hash.js.map +1 -0
- package/dist/modules/v4/hyperplanning.js +42 -0
- package/dist/modules/v4/hyperplanning.js.map +1 -0
- package/dist/modules/v4/levenshtein.js +29 -0
- package/dist/modules/v4/levenshtein.js.map +1 -0
- package/dist/modules/v4/palette.js +89 -0
- package/dist/modules/v4/palette.js.map +1 -0
- package/dist/modules/v4/personal.js +125 -0
- package/dist/modules/v4/personal.js.map +1 -0
- package/dist/modules/v4/placeholder.js +96 -0
- package/dist/modules/v4/placeholder.js.map +1 -0
- package/dist/modules/v4/qrcode.js +12 -0
- package/dist/modules/v4/qrcode.js.map +1 -0
- package/dist/modules/v4/statistics.js +41 -0
- package/dist/modules/v4/statistics.js.map +1 -0
- package/dist/modules/v4/text.js +293 -0
- package/dist/modules/v4/text.js.map +1 -0
- package/dist/modules/v4/tic_tac_toe.js +206 -0
- package/dist/modules/v4/tic_tac_toe.js.map +1 -0
- package/dist/modules/v4/time.js +73 -0
- package/dist/modules/v4/time.js.map +1 -0
- package/dist/modules/v4/token.js +36 -0
- package/dist/modules/v4/token.js.map +1 -0
- package/dist/modules/v4/username.js +87 -0
- package/dist/modules/v4/username.js.map +1 -0
- package/dist/modules/v4/utils.js +26 -0
- package/dist/modules/v4/utils.js.map +1 -0
- package/dist/modules/v4/validate.js +54 -0
- package/dist/modules/v4/validate.js.map +1 -0
- package/dist/modules/v4.js +18 -0
- package/dist/modules/v4.js.map +1 -0
- package/dist/routes/delete.js +65 -0
- package/dist/routes/delete.js.map +1 -0
- package/dist/routes/get.js +441 -0
- package/dist/routes/get.js.map +1 -0
- package/dist/routes/index.js +50 -0
- package/dist/routes/index.js.map +1 -0
- package/dist/routes/patch.js +41 -0
- package/dist/routes/patch.js.map +1 -0
- package/dist/routes/post.js +185 -0
- package/dist/routes/post.js.map +1 -0
- package/dist/storage/index.js +13 -0
- package/dist/storage/index.js.map +1 -0
- package/dist/types/storage.js +2 -0
- package/dist/types/storage.js.map +1 -0
- package/dist/utils/response.js +10 -0
- package/dist/utils/response.js.map +1 -0
- package/dist/utils/version.js +3 -0
- package/dist/utils/version.js.map +1 -0
- package/eslint.config.js +20 -0
- package/package.json +30 -15
- package/src/app.ts +44 -0
- package/src/config/env.ts +81 -0
- package/src/config/versions.ts +80 -0
- package/src/constants.ts +28 -0
- package/src/middleware/cors.ts +17 -0
- package/src/middleware/error.ts +7 -0
- package/src/middleware/json.ts +9 -0
- package/src/middleware/logger.ts +31 -0
- package/src/middleware/ratelimit.ts +66 -0
- package/src/middleware/version.ts +39 -0
- package/{modules → src/modules}/v3/algorithms.js +1 -1
- package/src/modules/v3/captcha.js +60 -0
- package/{modules → src/modules}/v3/chat.js +6 -4
- package/src/modules/v3/color.js +60 -0
- package/{modules → src/modules}/v3/convert.js +5 -5
- package/{modules → src/modules}/v3/domain.js +51 -4
- package/{modules → src/modules}/v3/hash.js +1 -1
- package/{modules → src/modules}/v3/hyperplanning.js +11 -10
- package/{modules → src/modules}/v3/levenshtein.js +3 -3
- package/{modules → src/modules}/v3/personal.js +20 -8
- package/{modules → src/modules}/v3/qrcode.js +1 -1
- package/{modules → src/modules}/v3/tic_tac_toe.js +24 -18
- package/{modules → src/modules}/v3/time.js +22 -11
- package/src/modules/v3/token.js +54 -0
- package/src/modules/v3/username.js +94 -0
- package/{modules → src/modules}/v3/utils.js +2 -2
- package/{modules → src/modules}/v3.js +1 -1
- package/src/modules/v4/algorithms.ts +155 -0
- package/src/modules/v4/captcha.ts +49 -0
- package/src/modules/v4/chat.ts +99 -0
- package/src/modules/v4/color.ts +64 -0
- package/src/modules/v4/convert.ts +38 -0
- package/src/modules/v4/domain.ts +81 -0
- package/src/modules/v4/hash.ts +9 -0
- package/src/modules/v4/hyperplanning.ts +58 -0
- package/src/modules/v4/levenshtein.ts +38 -0
- package/src/modules/v4/personal.ts +147 -0
- package/src/modules/v4/qrcode.ts +11 -0
- package/src/modules/v4/tic_tac_toe.ts +223 -0
- package/src/modules/v4/time.ts +92 -0
- package/src/modules/v4/token.ts +43 -0
- package/src/modules/v4/username.ts +89 -0
- package/src/modules/v4/utils.ts +38 -0
- package/src/modules/v4.ts +15 -0
- package/src/routes/get.ts +461 -0
- package/src/routes/index.ts +58 -0
- package/src/routes/post.ts +200 -0
- package/src/storage/index.ts +16 -0
- package/src/types/express.d.ts +17 -0
- package/src/types/storage.ts +48 -0
- package/src/utils/response.ts +11 -0
- package/tsconfig.json +17 -0
- package/app.js +0 -865
- package/modules/v3/username.js +0 -32
package/app.js
DELETED
|
@@ -1,865 +0,0 @@
|
|
|
1
|
-
// Import modules
|
|
2
|
-
import * as apiv3 from './modules/v3.js';
|
|
3
|
-
import { envList } from './modules/v3/utils.js';
|
|
4
|
-
|
|
5
|
-
// Import dependencies
|
|
6
|
-
import cors from 'cors';
|
|
7
|
-
import dotenv from 'dotenv';
|
|
8
|
-
import express from 'express';
|
|
9
|
-
import fetch from 'node-fetch';
|
|
10
|
-
import { urlencoded, json } from 'express';
|
|
11
|
-
import { dirname, join } from 'path';
|
|
12
|
-
import { fileURLToPath } from 'url';
|
|
13
|
-
|
|
14
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
15
|
-
const __dirname = dirname(__filename);
|
|
16
|
-
const app = express();
|
|
17
|
-
|
|
18
|
-
// Define allowed versions & endpoints for each version
|
|
19
|
-
const v1 = {
|
|
20
|
-
get: [
|
|
21
|
-
{ name: 'algorithms', path: '/algorithms?method={algorithm}&value={value}(&value2={value2})' },
|
|
22
|
-
{ name: 'captcha', path: '/captcha?text={text}' },
|
|
23
|
-
{ name: 'color', path: '/color' },
|
|
24
|
-
{ name: 'convert', path: '/convert?value={value}&from={unit}&to={unit}' },
|
|
25
|
-
{ name: 'domain', path: '/domain' },
|
|
26
|
-
{ name: 'infos', path: '/infos' },
|
|
27
|
-
{ name: 'personal', path: '/personal' },
|
|
28
|
-
{ name: 'qrcode', path: '/qrcode?url={URL}' },
|
|
29
|
-
{ name: 'username', path: '/username' },
|
|
30
|
-
{ name: 'website', path: '/website' }
|
|
31
|
-
],
|
|
32
|
-
post: [
|
|
33
|
-
{ name: 'token', path: '/token' }
|
|
34
|
-
]
|
|
35
|
-
};
|
|
36
|
-
const v2 = {
|
|
37
|
-
get: [
|
|
38
|
-
...v1.get,
|
|
39
|
-
{ name: 'chat', path: '/chat' }
|
|
40
|
-
],
|
|
41
|
-
post: [
|
|
42
|
-
...v1.post,
|
|
43
|
-
{
|
|
44
|
-
name: 'chat',
|
|
45
|
-
children: {
|
|
46
|
-
chat: '/chat',
|
|
47
|
-
private: '/chat/private'
|
|
48
|
-
}
|
|
49
|
-
},
|
|
50
|
-
{ name: 'hash', path: '/hash' },
|
|
51
|
-
{
|
|
52
|
-
name: 'tic_tac_toe',
|
|
53
|
-
children: {
|
|
54
|
-
tic_tac_toe: '/tic-tac-toe',
|
|
55
|
-
fetch: '/tic-tac-toe/fetch',
|
|
56
|
-
list: '/tic-tac-toe/list'
|
|
57
|
-
}
|
|
58
|
-
},
|
|
59
|
-
{ name: 'token', path: '/token' }
|
|
60
|
-
]
|
|
61
|
-
};
|
|
62
|
-
const v3 = {
|
|
63
|
-
get: [
|
|
64
|
-
...v2.get,
|
|
65
|
-
{ name: 'levenshtein', path: '/levenshtein?str1={string}&str2={string}' },
|
|
66
|
-
{ name: 'time', path: '/time(?type={type}&start={timestamp}&end={timestamp}&format={format}&timezone={timezone})' },
|
|
67
|
-
],
|
|
68
|
-
post: [
|
|
69
|
-
...v2.post,
|
|
70
|
-
{ name: 'hyperplanning', path: '/hyperplanning' }
|
|
71
|
-
]
|
|
72
|
-
};
|
|
73
|
-
const versions = {
|
|
74
|
-
v1: {
|
|
75
|
-
endpoints: v1,
|
|
76
|
-
modules: apiv3
|
|
77
|
-
},
|
|
78
|
-
v2: {
|
|
79
|
-
endpoints: v2,
|
|
80
|
-
modules: apiv3
|
|
81
|
-
},
|
|
82
|
-
v3: {
|
|
83
|
-
endpoints: v3,
|
|
84
|
-
modules: apiv3
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
// API storage
|
|
89
|
-
const logs = [], ipLimits = {};
|
|
90
|
-
|
|
91
|
-
// Chat storage
|
|
92
|
-
const chatStorage = {
|
|
93
|
-
messages: [],
|
|
94
|
-
privateChats: {},
|
|
95
|
-
sessions: {},
|
|
96
|
-
rateLimits: {}
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
// Tic-Tac-Toe storage
|
|
100
|
-
const ticTacToeStorage = {
|
|
101
|
-
games: {},
|
|
102
|
-
sessions: {},
|
|
103
|
-
rateLimits: {}
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
// Define documentation URL
|
|
107
|
-
const documentation = 'https://docs.sylvain.sh';
|
|
108
|
-
|
|
109
|
-
// Define global variables
|
|
110
|
-
let activity = [], lastFetch = 0, requests = 0, requestLimit, resetTime = Date.now() + 3600000;
|
|
111
|
-
|
|
112
|
-
// ----------- ----------- MIDDLEWARES SETUP ----------- ----------- //
|
|
113
|
-
|
|
114
|
-
dotenv.config();
|
|
115
|
-
|
|
116
|
-
// CORS & Express setup
|
|
117
|
-
app.set('trust proxy', 1);
|
|
118
|
-
app.use(cors({ methods: ['GET', 'POST'] }));
|
|
119
|
-
app.use(urlencoded({ extended: true }));
|
|
120
|
-
app.use(json());
|
|
121
|
-
|
|
122
|
-
// Set favicon for API
|
|
123
|
-
app.use('/favicon.ico', express.static(join(__dirname, 'src', 'favicon.ico')));
|
|
124
|
-
|
|
125
|
-
// Display robots.txt
|
|
126
|
-
app.use('/robots.txt', express.static(join(__dirname, 'robots.txt')));
|
|
127
|
-
|
|
128
|
-
// Return formatted JSON
|
|
129
|
-
app.use((req, res, next) => {
|
|
130
|
-
res.setHeader('Content-Type', 'application/json');
|
|
131
|
-
res.jsonResponse = (data) => {
|
|
132
|
-
res.send(JSON.stringify(data, null, 2));
|
|
133
|
-
};
|
|
134
|
-
next();
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
// Too many requests
|
|
138
|
-
app.use((req, res, next) => {
|
|
139
|
-
const ip = req.headers['cf-connecting-ip'] || req.socket.remoteAddress;
|
|
140
|
-
const token = req.headers.authorization?.split(' ')[1] || '';
|
|
141
|
-
|
|
142
|
-
const now = Date.now();
|
|
143
|
-
const minute = Math.floor(now / 60000) % 60;
|
|
144
|
-
const hour = Math.floor(now / 3600000) % 24;
|
|
145
|
-
|
|
146
|
-
const business = process.env.BUSINESS_TOKEN_LIST?.split(' ') || [];
|
|
147
|
-
const pro = process.env.PRO_TOKEN_LIST?.split(' ') || [];
|
|
148
|
-
const advanced = process.env.ADVANCED_TOKEN_LIST?.split(' ') || [];
|
|
149
|
-
|
|
150
|
-
if (token && ![...business, ...pro, ...advanced].includes(token) || token === 'undefined') {
|
|
151
|
-
return res.status(401).jsonResponse({
|
|
152
|
-
message: 'Unauthorized',
|
|
153
|
-
error: 'Invalid token.',
|
|
154
|
-
status: '401'
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
if (business.includes(token) && !business.includes('undefined')) requestLimit = process.env.BUSINESS_LIMIT;
|
|
159
|
-
else if (pro.includes(token) && !pro.includes('undefined')) requestLimit = process.env.PRO_LIMIT;
|
|
160
|
-
else if (advanced.includes(token) && !advanced.includes('undefined')) requestLimit = process.env.ADVANCED_LIMIT;
|
|
161
|
-
else requestLimit = process.env.DEFAULT_LIMIT;
|
|
162
|
-
|
|
163
|
-
if (now > resetTime) requests = 0, resetTime = now + 3600000;
|
|
164
|
-
if (++requests > Math.max(process.env.GLOBAL_LIMIT, requestLimit)) {
|
|
165
|
-
return res.status(429).jsonResponse({ message: 'Too Many Requests' });
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
if (['__proto__', 'constructor', 'prototype'].includes(ip)) {
|
|
169
|
-
return res.status(400).jsonResponse({ message: 'Invalid IP address' });
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
if (!ipLimits[ip]) ipLimits[ip] = {};
|
|
173
|
-
if (!ipLimits[ip][hour]) ipLimits[ip][hour] = {};
|
|
174
|
-
ipLimits[ip][hour][minute] = (ipLimits[ip][hour][minute] || 0) + 1;
|
|
175
|
-
|
|
176
|
-
if (ipLimits[ip][hour][minute] > requestLimit) {
|
|
177
|
-
return res.status(429).jsonResponse({
|
|
178
|
-
message: 'Too Many Requests (IP limited), authenticate to increase the limit',
|
|
179
|
-
error: `You have exceeded the limit of ${requestLimit} requests per hour.`,
|
|
180
|
-
reset: `Reset in ${((resetTime - now) / 60000).toFixed(0)} minutes.`,
|
|
181
|
-
status: '429'
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
Object.keys(ipLimits[ip]).forEach(h => { if (h != hour) delete ipLimits[ip][h]; });
|
|
186
|
-
|
|
187
|
-
next();
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
// Save and send logs
|
|
191
|
-
app.use((req, res, next) => {
|
|
192
|
-
if (req.method === 'HEAD') return next();
|
|
193
|
-
if (req.originalUrl === '/logs') return next();
|
|
194
|
-
|
|
195
|
-
const ip = req.headers['cf-connecting-ip'] || req.socket.remoteAddress;
|
|
196
|
-
|
|
197
|
-
const startTime = Date.now();
|
|
198
|
-
const timestamp = new Date().toISOString();
|
|
199
|
-
const method = req.method;
|
|
200
|
-
const url = req.originalUrl;
|
|
201
|
-
const platform = req.headers['sec-ch-ua-platform']?.replace(/"/g, '');
|
|
202
|
-
|
|
203
|
-
res.on('finish', () => {
|
|
204
|
-
const status = res.statusCode === 304 ? 200 : res.statusCode;
|
|
205
|
-
const duration = `${Date.now() - startTime}ms`;
|
|
206
|
-
|
|
207
|
-
logs.push({ timestamp, method, url, status, duration, platform });
|
|
208
|
-
console.log(`[${new Date().toISOString()}] ${method} ${url} ${res.statusCode} - ${duration} - ${ip}`);
|
|
209
|
-
if (logs.length > 1000) logs.shift();
|
|
210
|
-
});
|
|
211
|
-
next();
|
|
212
|
-
});
|
|
213
|
-
|
|
214
|
-
// Internal Server Error
|
|
215
|
-
app.use((err, req, res, next) => {
|
|
216
|
-
console.error(err.stack);
|
|
217
|
-
res.status(500).jsonResponse({
|
|
218
|
-
message: 'Internal Server Error',
|
|
219
|
-
error: err.message,
|
|
220
|
-
documentation,
|
|
221
|
-
status: '500'
|
|
222
|
-
});
|
|
223
|
-
});
|
|
224
|
-
|
|
225
|
-
// Check if version exists
|
|
226
|
-
app.use('/:version', (req, res, next) => {
|
|
227
|
-
const { version } = req.params;
|
|
228
|
-
const latest = Object.keys(versions).pop();
|
|
229
|
-
const endpoint = req.originalUrl.split('/').slice(2).join('/');
|
|
230
|
-
|
|
231
|
-
req.version = version;
|
|
232
|
-
req.latest = latest;
|
|
233
|
-
|
|
234
|
-
if (['latest', 'fr', 'en'].includes(version)) {
|
|
235
|
-
return res.redirect(endpoint ? `/${latest}/${endpoint}` : `/${latest}`);
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
if (version === 'logs') return res.jsonResponse(logs);
|
|
239
|
-
|
|
240
|
-
if (!versions[version] && version !== 'logs') {
|
|
241
|
-
return res.status(404).jsonResponse({
|
|
242
|
-
message: 'Not Found',
|
|
243
|
-
error: `Invalid API version (${version}).`,
|
|
244
|
-
documentation: `${documentation}/${latest}`,
|
|
245
|
-
status: '404'
|
|
246
|
-
});
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
req.module = versions[version].modules;
|
|
250
|
-
|
|
251
|
-
if (!req.module) {
|
|
252
|
-
return res.status(404).jsonResponse({
|
|
253
|
-
message: 'Not Found',
|
|
254
|
-
error: `Module not found for version ${version}.`,
|
|
255
|
-
documentation: `${documentation}/${latest}`,
|
|
256
|
-
status: '404'
|
|
257
|
-
});
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
next();
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
// Check if endpoint exists
|
|
264
|
-
app.use('/:version/:endpoint', (req, res, next) => {
|
|
265
|
-
const { version, endpoint } = req.params;
|
|
266
|
-
|
|
267
|
-
req.version = version;
|
|
268
|
-
req.endpoint = endpoint;
|
|
269
|
-
|
|
270
|
-
if (!req.endpoint) {
|
|
271
|
-
return res.status(404).jsonResponse({
|
|
272
|
-
message: 'Not Found',
|
|
273
|
-
error: `Endpoint '${endpoint}' does not exist in ${req.version}.`,
|
|
274
|
-
documentation: `${documentation}/${versions[version.length - 1]}`,
|
|
275
|
-
status: '404'
|
|
276
|
-
});
|
|
277
|
-
}
|
|
278
|
-
next();
|
|
279
|
-
});
|
|
280
|
-
|
|
281
|
-
// ----------- ----------- MAIN ENDPOINTS ----------- ----------- //
|
|
282
|
-
|
|
283
|
-
// Main route
|
|
284
|
-
app.get('/', (req, res) => {
|
|
285
|
-
const base = `${req.protocol}://${req.get('host')}`;
|
|
286
|
-
const links = Object.keys(versions).reduce((link, version) => {
|
|
287
|
-
link[version] = `${base}/${version}`;
|
|
288
|
-
return link;
|
|
289
|
-
}, {});
|
|
290
|
-
|
|
291
|
-
res.jsonResponse({
|
|
292
|
-
documentation,
|
|
293
|
-
latest: `${base}/latest`,
|
|
294
|
-
logs: `${base}/logs`,
|
|
295
|
-
versions: links
|
|
296
|
-
});
|
|
297
|
-
});
|
|
298
|
-
|
|
299
|
-
// Display version information
|
|
300
|
-
app.get('/:version', (req, res) => {
|
|
301
|
-
const { version } = req.params;
|
|
302
|
-
|
|
303
|
-
if (!versions[version]) {
|
|
304
|
-
return res.status(404).jsonResponse({
|
|
305
|
-
message: 'Not Found',
|
|
306
|
-
error: `Invalid API version (${version}).`,
|
|
307
|
-
documentation: `${documentation}/${Object.keys(versions).pop()}`,
|
|
308
|
-
status: '404'
|
|
309
|
-
});
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
const endpoints = Object.keys(versions[version].endpoints).reduce((acc, method) => {
|
|
313
|
-
acc[method] = versions[version].endpoints[method]
|
|
314
|
-
.filter(({ name }) => name !== 'website')
|
|
315
|
-
.sort((a, b) => a.name.localeCompare(b.name))
|
|
316
|
-
.reduce((group, endpoint) => {
|
|
317
|
-
if (endpoint.children) {
|
|
318
|
-
group[endpoint.name] = Object.keys(endpoint.children)
|
|
319
|
-
.sort((a, b) => a.localeCompare(b))
|
|
320
|
-
.reduce((childGroup, childName) => {
|
|
321
|
-
childGroup[childName] = `/${version}${endpoint.children[childName]}`;
|
|
322
|
-
return childGroup;
|
|
323
|
-
}, {});
|
|
324
|
-
} else {
|
|
325
|
-
group[endpoint.name] = `/${version}${endpoint.path}`;
|
|
326
|
-
}
|
|
327
|
-
return group;
|
|
328
|
-
}, {});
|
|
329
|
-
return acc;
|
|
330
|
-
}, {});
|
|
331
|
-
|
|
332
|
-
res.jsonResponse({
|
|
333
|
-
version,
|
|
334
|
-
documentation: `${documentation}/${version}`,
|
|
335
|
-
endpoints
|
|
336
|
-
});
|
|
337
|
-
});
|
|
338
|
-
|
|
339
|
-
// ----------- ----------- GET ENDPOINTS ----------- ----------- //
|
|
340
|
-
|
|
341
|
-
// Algorithms
|
|
342
|
-
app.get('/:version/algorithms', (req, res) => {
|
|
343
|
-
const { method, value, value2 } = req.query;
|
|
344
|
-
const { version } = req.params;
|
|
345
|
-
|
|
346
|
-
if (!req.module.algorithms || !req.module.algorithms[method]) {
|
|
347
|
-
return res.jsonResponse({
|
|
348
|
-
error: 'Please provide a valid algorithm (?method={algorithm})',
|
|
349
|
-
documentation: `${documentation}/${version}/en/algorithms`
|
|
350
|
-
});
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
try {
|
|
354
|
-
const answer = req.module.algorithms[method](value, value2);
|
|
355
|
-
res.jsonResponse({ answer });
|
|
356
|
-
} catch (err) {
|
|
357
|
-
res.jsonResponse({
|
|
358
|
-
error: err.message,
|
|
359
|
-
documentation: `${documentation}/${version}/en/algorithms`
|
|
360
|
-
});
|
|
361
|
-
}
|
|
362
|
-
});
|
|
363
|
-
|
|
364
|
-
// Generate captcha
|
|
365
|
-
app.get('/:version/captcha', (req, res) => {
|
|
366
|
-
const text = req.query.text;
|
|
367
|
-
|
|
368
|
-
if (!text) return res.jsonResponse({ error: 'Please provide a valid argument (?text={text})' });
|
|
369
|
-
|
|
370
|
-
try {
|
|
371
|
-
const result = req.module.captcha(text);
|
|
372
|
-
res.type('png').send(result);
|
|
373
|
-
} catch (err) {
|
|
374
|
-
res.jsonResponse({
|
|
375
|
-
error: err.message,
|
|
376
|
-
documentation: `${documentation}/${req.version}/en/captcha`
|
|
377
|
-
});
|
|
378
|
-
}
|
|
379
|
-
});
|
|
380
|
-
|
|
381
|
-
// Display stored data
|
|
382
|
-
app.get('/:version/chat', (req, res) => {
|
|
383
|
-
try {
|
|
384
|
-
const messages = req.module.chat('fetch', {
|
|
385
|
-
username: 'system',
|
|
386
|
-
storage: chatStorage
|
|
387
|
-
});
|
|
388
|
-
res.jsonResponse(messages);
|
|
389
|
-
} catch (err) {
|
|
390
|
-
res.jsonResponse({ error: err.message });
|
|
391
|
-
}
|
|
392
|
-
});
|
|
393
|
-
|
|
394
|
-
// GET private chat error
|
|
395
|
-
app.get('/:version/chat/private', (req, res) => {
|
|
396
|
-
res.jsonResponse({ error: 'This endpoint only supports POST requests.' });
|
|
397
|
-
});
|
|
398
|
-
|
|
399
|
-
// Generate color
|
|
400
|
-
app.get('/:version/color', (req, res) => {
|
|
401
|
-
try {
|
|
402
|
-
const result = req.module.color();
|
|
403
|
-
res.jsonResponse(result);
|
|
404
|
-
} catch (err) {
|
|
405
|
-
res.jsonResponse({
|
|
406
|
-
error: err.message,
|
|
407
|
-
documentation: `${documentation}/${req.version}/en/color`
|
|
408
|
-
});
|
|
409
|
-
}
|
|
410
|
-
});
|
|
411
|
-
|
|
412
|
-
// Convert units
|
|
413
|
-
app.get('/:version/convert', (req, res) => {
|
|
414
|
-
const { value, from, to } = req.query;
|
|
415
|
-
|
|
416
|
-
if (!value || isNaN(value)) return res.jsonResponse({ error: 'Please provide a valid value (?value={value})' });
|
|
417
|
-
if (!from) return res.jsonResponse({ error: 'Please provide a valid source unit (&from={unit})' });
|
|
418
|
-
if (!to) return res.jsonResponse({ error: 'Please provide a valid target unit (&to={unit})' });
|
|
419
|
-
|
|
420
|
-
try {
|
|
421
|
-
const result = req.module.convert(value, from, to);
|
|
422
|
-
res.jsonResponse(result);
|
|
423
|
-
} catch (err) {
|
|
424
|
-
res.jsonResponse({
|
|
425
|
-
error: err.message,
|
|
426
|
-
documentation: `${documentation}/${req.version}/en/convert`
|
|
427
|
-
});
|
|
428
|
-
}
|
|
429
|
-
});
|
|
430
|
-
|
|
431
|
-
// Generate domain informations
|
|
432
|
-
app.get('/:version/domain', (req, res) => {
|
|
433
|
-
try {
|
|
434
|
-
const result = req.module.domain();
|
|
435
|
-
res.jsonResponse(result);
|
|
436
|
-
} catch (err) {
|
|
437
|
-
res.jsonResponse({
|
|
438
|
-
error: err.message,
|
|
439
|
-
documentation: `${documentation}/${req.version}/en/domain`
|
|
440
|
-
});
|
|
441
|
-
}
|
|
442
|
-
});
|
|
443
|
-
|
|
444
|
-
// GET planning error
|
|
445
|
-
app.get('/:version/hyperplanning', (req, res) => {
|
|
446
|
-
res.jsonResponse({ error: 'This endpoint only supports POST requests.' });
|
|
447
|
-
});
|
|
448
|
-
|
|
449
|
-
// GET hash error
|
|
450
|
-
app.get('/:version/hash', (req, res) => {
|
|
451
|
-
res.jsonResponse({ error: 'This endpoint only supports POST requests.' });
|
|
452
|
-
});
|
|
453
|
-
|
|
454
|
-
// Display API informations
|
|
455
|
-
app.get('/:version/infos', (req, res) => {
|
|
456
|
-
const endpoints = Object.values(versions[req.version].endpoints).flat();
|
|
457
|
-
|
|
458
|
-
const paths = endpoints.flatMap(e => e.children
|
|
459
|
-
? Object.values(e.children)
|
|
460
|
-
: (e.path ? [e.path] : []));
|
|
461
|
-
|
|
462
|
-
res.jsonResponse({
|
|
463
|
-
endpoints: new Set(paths).size,
|
|
464
|
-
last_version: Object.keys(versions).pop(),
|
|
465
|
-
documentation,
|
|
466
|
-
github: 'https://github.com/20syldev/api',
|
|
467
|
-
creation: 'November 25th 2024',
|
|
468
|
-
});
|
|
469
|
-
});
|
|
470
|
-
|
|
471
|
-
// Calculate Levenshtein distance
|
|
472
|
-
app.get('/:version/levenshtein', (req, res) => {
|
|
473
|
-
const { str1, str2 } = req.query;
|
|
474
|
-
|
|
475
|
-
if (!str1 || typeof str1 !== 'string') return res.jsonResponse({ error: 'Please provide a first string (?str1={string})' });
|
|
476
|
-
if (!str2 || typeof str2 !== 'string') return res.jsonResponse({ error: 'Please provide a second string (&str2={string})' });
|
|
477
|
-
|
|
478
|
-
try {
|
|
479
|
-
const result = req.module.levenshtein(str1, str2);
|
|
480
|
-
res.jsonResponse(result);
|
|
481
|
-
} catch (err) {
|
|
482
|
-
res.jsonResponse({
|
|
483
|
-
error: err.message,
|
|
484
|
-
documentation: `${documentation}/${req.version}/en/levenshtein`
|
|
485
|
-
});
|
|
486
|
-
}
|
|
487
|
-
});
|
|
488
|
-
|
|
489
|
-
// Generate personal data
|
|
490
|
-
app.get('/:version/personal', (req, res) => {
|
|
491
|
-
try {
|
|
492
|
-
const result = req.module.personal();
|
|
493
|
-
res.jsonResponse(result);
|
|
494
|
-
} catch (err) {
|
|
495
|
-
res.jsonResponse({
|
|
496
|
-
error: err.message,
|
|
497
|
-
documentation: `${documentation}/${req.version}/en/personal`
|
|
498
|
-
});
|
|
499
|
-
}
|
|
500
|
-
});
|
|
501
|
-
|
|
502
|
-
// Generate QR Code
|
|
503
|
-
app.get('/:version/qrcode', async (req, res) => {
|
|
504
|
-
const { url } = req.query;
|
|
505
|
-
|
|
506
|
-
if (!url) return res.jsonResponse({ error: 'Please provide a valid url (?url={URL})' });
|
|
507
|
-
|
|
508
|
-
try {
|
|
509
|
-
const result = await req.module.qrcode(url);
|
|
510
|
-
res.jsonResponse(result);
|
|
511
|
-
} catch (err) {
|
|
512
|
-
res.jsonResponse({
|
|
513
|
-
error: err.message,
|
|
514
|
-
documentation: `${documentation}/${req.version}/en/qrcode`
|
|
515
|
-
});
|
|
516
|
-
}
|
|
517
|
-
});
|
|
518
|
-
|
|
519
|
-
// GET tic-tac-toe game error
|
|
520
|
-
app.get('/:version/tic-tac-toe', (req, res) => {
|
|
521
|
-
res.jsonResponse({ error: 'This endpoint only supports POST requests.' });
|
|
522
|
-
});
|
|
523
|
-
|
|
524
|
-
// GET tic-tac-toe fetch error
|
|
525
|
-
app.get('/:version/tic-tac-toe/fetch', (req, res) => {
|
|
526
|
-
res.jsonResponse({ error: 'This endpoint only supports POST requests.' });
|
|
527
|
-
});
|
|
528
|
-
|
|
529
|
-
// GET tic-tac-toe list error
|
|
530
|
-
app.get('/:version/tic-tac-toe/list', (req, res) => {
|
|
531
|
-
res.jsonResponse({ error: 'This endpoint only supports POST requests.' });
|
|
532
|
-
});
|
|
533
|
-
|
|
534
|
-
// Display or generate time informations
|
|
535
|
-
app.get('/:version/time', (req, res) => {
|
|
536
|
-
const { type = 'live', start, end, format, timezone } = req.query;
|
|
537
|
-
|
|
538
|
-
const validFormats = ['iso', 'utc', 'timestamp', 'locale', 'date', 'time', 'year', 'month', 'day', 'hour', 'minute', 'second', 'ms', 'dayOfWeek', 'dayOfYear', 'weekNumber', 'timezone', 'timezoneOffset'];
|
|
539
|
-
const validTimezones = ['UTC', 'America/New_York', 'Europe/Paris', 'Asia/Tokyo', 'Australia/Sydney'];
|
|
540
|
-
|
|
541
|
-
if (type !== 'live' && type !== 'random') return res.jsonResponse({ error: 'Please provide a valid type (?type={type})' });
|
|
542
|
-
if (start && !Date.parse(start)) return res.jsonResponse({ error: 'Please provide a valid start date (?start={YYYY-MM-DD})' });
|
|
543
|
-
if (end && !Date.parse(end)) return res.jsonResponse({ error: 'Please provide a valid end date (?end={YYYY-MM-DD})' });
|
|
544
|
-
if (format && !validFormats.includes(format)) return res.jsonResponse({ error: 'Please provide a valid format (?format={format})' });
|
|
545
|
-
if (timezone && !validTimezones.includes(timezone)) return res.jsonResponse({ error: 'Please provide a valid timezone (?timezone={timezone})' });
|
|
546
|
-
|
|
547
|
-
try {
|
|
548
|
-
const time = req.module.time(type, start, end, format, timezone);
|
|
549
|
-
res.jsonResponse(time);
|
|
550
|
-
} catch (err) {
|
|
551
|
-
res.jsonResponse({
|
|
552
|
-
error: err.message,
|
|
553
|
-
documentation: `${documentation}/${req.version}/en/time`
|
|
554
|
-
});
|
|
555
|
-
}
|
|
556
|
-
});
|
|
557
|
-
|
|
558
|
-
// GET token error
|
|
559
|
-
app.get('/:version/token', (req, res) => {
|
|
560
|
-
res.jsonResponse({ error: 'This endpoint only supports POST requests.' });
|
|
561
|
-
});
|
|
562
|
-
|
|
563
|
-
// Generate username
|
|
564
|
-
app.get('/:version/username', (req, res) => {
|
|
565
|
-
try {
|
|
566
|
-
const result = req.module.username();
|
|
567
|
-
res.jsonResponse(result);
|
|
568
|
-
} catch (err) {
|
|
569
|
-
res.jsonResponse({
|
|
570
|
-
error: err.message,
|
|
571
|
-
documentation: `${documentation}/${req.version}/en/username`
|
|
572
|
-
});
|
|
573
|
-
}
|
|
574
|
-
});
|
|
575
|
-
|
|
576
|
-
// Display informations for owner's website
|
|
577
|
-
app.get('/:version/website', async (req, res) => {
|
|
578
|
-
const currentTime = Date.now();
|
|
579
|
-
|
|
580
|
-
if (currentTime - lastFetch >= 10 * 60 * 1000) {
|
|
581
|
-
try {
|
|
582
|
-
const username = '20syldev';
|
|
583
|
-
const token = process.env.GITHUB_TOKEN;
|
|
584
|
-
const lastYear = new Date(new Date().setFullYear(new Date().getFullYear() - 1)).toISOString().split('T')[0];
|
|
585
|
-
|
|
586
|
-
const query = `
|
|
587
|
-
{
|
|
588
|
-
user(login: "${username}") {
|
|
589
|
-
contributionsCollection(from: "${lastYear}T00:00:00Z") {
|
|
590
|
-
contributionCalendar {
|
|
591
|
-
totalContributions
|
|
592
|
-
weeks {
|
|
593
|
-
firstDay
|
|
594
|
-
contributionDays {
|
|
595
|
-
date
|
|
596
|
-
contributionCount
|
|
597
|
-
}
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
}`;
|
|
603
|
-
|
|
604
|
-
const apiResponse = await fetch('https://api.github.com/graphql', {
|
|
605
|
-
method: 'POST',
|
|
606
|
-
headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' },
|
|
607
|
-
body: JSON.stringify({ query })
|
|
608
|
-
});
|
|
609
|
-
|
|
610
|
-
if (!apiResponse.ok) throw new Error('Error fetching data.');
|
|
611
|
-
|
|
612
|
-
const data = await apiResponse.json();
|
|
613
|
-
const user = data?.data?.user;
|
|
614
|
-
|
|
615
|
-
const weeks = user?.contributionsCollection?.contributionCalendar?.weeks || [];
|
|
616
|
-
activity = weeks.map(w => ({
|
|
617
|
-
week: w.firstDay,
|
|
618
|
-
total: w.contributionDays.reduce((sum, d) => sum + d.contributionCount, 0),
|
|
619
|
-
days: w.contributionDays.map(d => ({ date: d.date, count: d.contributionCount }))
|
|
620
|
-
}));
|
|
621
|
-
|
|
622
|
-
lastFetch = currentTime;
|
|
623
|
-
} catch { activity = []; }
|
|
624
|
-
}
|
|
625
|
-
|
|
626
|
-
const response = {
|
|
627
|
-
versions: {
|
|
628
|
-
2048: process.env.G_2048,
|
|
629
|
-
api: process.env.API,
|
|
630
|
-
cdn: process.env.CDN,
|
|
631
|
-
coop_api: process.env.COOP_API,
|
|
632
|
-
coop_status: process.env.COOP_STATUS,
|
|
633
|
-
chat: process.env.CHAT,
|
|
634
|
-
digit: process.env.DIGIT,
|
|
635
|
-
doc_coopbot: process.env.DOC_COOPBOT,
|
|
636
|
-
docs: process.env.DOCS,
|
|
637
|
-
donut: process.env.DONUT,
|
|
638
|
-
drawio_plugin: process.env.DRAWIO_PLUGIN,
|
|
639
|
-
flowers: process.env.FLOWERS,
|
|
640
|
-
gemsync: process.env.GEMSYNC,
|
|
641
|
-
gft: process.env.GFT,
|
|
642
|
-
gitsite: process.env.GITSITE,
|
|
643
|
-
lebonchar: process.env.LEBONCHAR,
|
|
644
|
-
logger: process.env.LOGGER,
|
|
645
|
-
logs: process.env.LOGS,
|
|
646
|
-
logvault: process.env.LOGVAULT,
|
|
647
|
-
lyah: process.env.LYAH,
|
|
648
|
-
minify: process.env.MINIFY,
|
|
649
|
-
mn: process.env.MN,
|
|
650
|
-
monitoring: process.env.MONITORING,
|
|
651
|
-
morpion: process.env.MORPION,
|
|
652
|
-
nitrogen: process.env.NITROGEN,
|
|
653
|
-
old_database: process.env.OLD_DATABASE,
|
|
654
|
-
password: process.env.PASSWORD,
|
|
655
|
-
php: process.env.PHP,
|
|
656
|
-
planning: process.env.PLANNING,
|
|
657
|
-
ping: process.env.PING,
|
|
658
|
-
portfolio: process.env.PORTFOLIO,
|
|
659
|
-
python_api: process.env.PYTHON_API,
|
|
660
|
-
readme: process.env.README,
|
|
661
|
-
timestamp: process.env.TIMESTAMP,
|
|
662
|
-
terminal: process.env.TERMINAL,
|
|
663
|
-
valentine: process.env.VALENTINE,
|
|
664
|
-
wrkit: process.env.WRKIT,
|
|
665
|
-
zpki: process.env.ZPKI
|
|
666
|
-
},
|
|
667
|
-
patched_projects: envList('PATCH'),
|
|
668
|
-
updated_projects: envList('RECENT'),
|
|
669
|
-
new_projects: envList('NEW'),
|
|
670
|
-
sub_domains: envList('DOMAINS'),
|
|
671
|
-
stats: {
|
|
672
|
-
1: process.env.STATS1,
|
|
673
|
-
2: process.env.STATS2,
|
|
674
|
-
3: process.env.STATS3,
|
|
675
|
-
4: process.env.STATS4,
|
|
676
|
-
5: Object.keys(ipLimits).length,
|
|
677
|
-
activity
|
|
678
|
-
},
|
|
679
|
-
tag: process.env.TAG,
|
|
680
|
-
active: process.env.ACTIVE === 'true'
|
|
681
|
-
};
|
|
682
|
-
|
|
683
|
-
const key = req.query.key;
|
|
684
|
-
if (key) {
|
|
685
|
-
const keys = key.split('.');
|
|
686
|
-
let result = response;
|
|
687
|
-
|
|
688
|
-
for (const k of keys) {
|
|
689
|
-
if (result == null || typeof result !== 'object' || !(k in result)) {
|
|
690
|
-
return res.status(404).jsonResponse({
|
|
691
|
-
error: `Key '${key}' not found.`,
|
|
692
|
-
available: Object.keys(result ?? {})
|
|
693
|
-
});
|
|
694
|
-
}
|
|
695
|
-
result = result[k];
|
|
696
|
-
}
|
|
697
|
-
|
|
698
|
-
return res.jsonResponse({ [key]: result });
|
|
699
|
-
}
|
|
700
|
-
|
|
701
|
-
res.jsonResponse(response);
|
|
702
|
-
});
|
|
703
|
-
|
|
704
|
-
// ----------- ----------- POST ENDPOINTS ----------- ----------- //
|
|
705
|
-
|
|
706
|
-
// Store chat messages
|
|
707
|
-
app.post('/:version/chat', (req, res) => {
|
|
708
|
-
const { username, message, timestamp, session, token } = req.body || {};
|
|
709
|
-
|
|
710
|
-
if (!username) return res.jsonResponse({ error: 'Please provide a username (?username={username})' });
|
|
711
|
-
if (!message) return res.jsonResponse({ error: 'Please provide a message (&message={message})' });
|
|
712
|
-
if (!session) return res.jsonResponse({ error: 'Please provide a valid session ID (&session={ID})' });
|
|
713
|
-
|
|
714
|
-
try {
|
|
715
|
-
const result = req.module.chat('message', {
|
|
716
|
-
username,
|
|
717
|
-
message,
|
|
718
|
-
timestamp,
|
|
719
|
-
session,
|
|
720
|
-
token,
|
|
721
|
-
storage: chatStorage
|
|
722
|
-
});
|
|
723
|
-
res.jsonResponse(result);
|
|
724
|
-
} catch (err) {
|
|
725
|
-
res.jsonResponse({ error: err.message });
|
|
726
|
-
}
|
|
727
|
-
});
|
|
728
|
-
|
|
729
|
-
// Display a private chat with a token
|
|
730
|
-
app.post('/:version/chat/private', (req, res) => {
|
|
731
|
-
const { username, token } = req.body || {};
|
|
732
|
-
|
|
733
|
-
if (!username) return res.jsonResponse({ error: 'Please provide a username (?username={username})' });
|
|
734
|
-
if (!token) return res.jsonResponse({ error: 'Please provide a valid token (&token={key}).' });
|
|
735
|
-
|
|
736
|
-
try {
|
|
737
|
-
const messages = req.module.chat('private', {
|
|
738
|
-
username,
|
|
739
|
-
token,
|
|
740
|
-
storage: chatStorage
|
|
741
|
-
});
|
|
742
|
-
res.jsonResponse(messages);
|
|
743
|
-
} catch (err) {
|
|
744
|
-
res.jsonResponse({ error: err.message });
|
|
745
|
-
}
|
|
746
|
-
});
|
|
747
|
-
|
|
748
|
-
// Generate hash
|
|
749
|
-
app.post('/:version/hash', (req, res) => {
|
|
750
|
-
const { text, method } = req.body || {};
|
|
751
|
-
|
|
752
|
-
if (!text) return res.jsonResponse({ error: 'Please provide a text (?text={text})' });
|
|
753
|
-
if (!method) return res.jsonResponse({
|
|
754
|
-
error: 'Please provide a valid hash algorithm (&method={algorithm})',
|
|
755
|
-
documentation: `${documentation}/${req.version}/en/hash`
|
|
756
|
-
});
|
|
757
|
-
|
|
758
|
-
try {
|
|
759
|
-
const result = req.module.hash(text, method);
|
|
760
|
-
res.jsonResponse(result);
|
|
761
|
-
} catch (err) {
|
|
762
|
-
res.jsonResponse({
|
|
763
|
-
error: err.message,
|
|
764
|
-
documentation: `${documentation}/${req.version}/en/hash`
|
|
765
|
-
})
|
|
766
|
-
}
|
|
767
|
-
});
|
|
768
|
-
|
|
769
|
-
// Display a planning from an ICS file
|
|
770
|
-
app.post('/:version/hyperplanning', async (req, res) => {
|
|
771
|
-
const { url, detail } = req.body || {};
|
|
772
|
-
|
|
773
|
-
if (!url) return res.jsonResponse({ error: 'Please provide a valid ICS file URL (?url={URL})' });
|
|
774
|
-
|
|
775
|
-
try {
|
|
776
|
-
const hyperplanning = await req.module.hyperplanning(url, detail);
|
|
777
|
-
res.jsonResponse(hyperplanning);
|
|
778
|
-
} catch (err){
|
|
779
|
-
res.jsonResponse({
|
|
780
|
-
error: err.message,
|
|
781
|
-
documentation: `${documentation}/${req.version}/en/hyperplanning`
|
|
782
|
-
});
|
|
783
|
-
}
|
|
784
|
-
});
|
|
785
|
-
|
|
786
|
-
// Store tic tac toe games
|
|
787
|
-
app.post('/:version/tic-tac-toe', (req, res) => {
|
|
788
|
-
const { username, move, session, game } = req.body || {};
|
|
789
|
-
|
|
790
|
-
if (!username) return res.jsonResponse({ error: 'Please provide a username (?username={username})' });
|
|
791
|
-
if (!move) return res.jsonResponse({ error: 'Please provide a valid move (&move={move})' });
|
|
792
|
-
if (!session) return res.jsonResponse({ error: 'Please provide a valid session ID (&session={ID})' });
|
|
793
|
-
if (!game) return res.jsonResponse({ error: 'Please provide a valid game ID (&game={ID})' });
|
|
794
|
-
|
|
795
|
-
try {
|
|
796
|
-
const result = req.module.tic_tac_toe('play', {
|
|
797
|
-
username,
|
|
798
|
-
move,
|
|
799
|
-
session,
|
|
800
|
-
game,
|
|
801
|
-
storage: ticTacToeStorage
|
|
802
|
-
});
|
|
803
|
-
res.jsonResponse(result);
|
|
804
|
-
} catch (err) {
|
|
805
|
-
res.jsonResponse({ error: err.message });
|
|
806
|
-
}
|
|
807
|
-
});
|
|
808
|
-
|
|
809
|
-
// Display a tic tac toe game with a token
|
|
810
|
-
app.post('/:version/tic-tac-toe/fetch', (req, res) => {
|
|
811
|
-
const { username, game } = req.body || {};
|
|
812
|
-
const privateGame = (req.body || {}).private;
|
|
813
|
-
|
|
814
|
-
if (!username) return res.jsonResponse({ error: 'Please provide a username (?username={username})' });
|
|
815
|
-
|
|
816
|
-
try {
|
|
817
|
-
const result = req.module.tic_tac_toe('fetch', {
|
|
818
|
-
username,
|
|
819
|
-
game,
|
|
820
|
-
private: privateGame,
|
|
821
|
-
storage: ticTacToeStorage
|
|
822
|
-
});
|
|
823
|
-
res.jsonResponse(result);
|
|
824
|
-
} catch (err) {
|
|
825
|
-
res.jsonResponse({ error: err.message });
|
|
826
|
-
}
|
|
827
|
-
});
|
|
828
|
-
|
|
829
|
-
// List public tic tac toe games
|
|
830
|
-
app.post('/:version/tic-tac-toe/list', (req, res) => {
|
|
831
|
-
try {
|
|
832
|
-
const result = req.module.tic_tac_toe('list', {
|
|
833
|
-
storage: ticTacToeStorage
|
|
834
|
-
});
|
|
835
|
-
res.jsonResponse(result);
|
|
836
|
-
} catch (err) {
|
|
837
|
-
res.jsonResponse({ error: err.message });
|
|
838
|
-
}
|
|
839
|
-
});
|
|
840
|
-
|
|
841
|
-
// Generate Token
|
|
842
|
-
app.post('/:version/token', (req, res) => {
|
|
843
|
-
let { len, type } = req.body || {};
|
|
844
|
-
|
|
845
|
-
len = parseInt(len || 24, 10);
|
|
846
|
-
type = type ? type.toLowerCase() : 'alpha';
|
|
847
|
-
|
|
848
|
-
if (isNaN(len) || len < 0) return res.jsonResponse({ error: 'Invalid number.' });
|
|
849
|
-
if (len > 4096) return res.jsonResponse({ error: 'Length cannot exceed 4096.' });
|
|
850
|
-
if (len < 12) return res.jsonResponse({ error: 'Length cannot be less than 12.' });
|
|
851
|
-
|
|
852
|
-
try {
|
|
853
|
-
const token = req.module.token(len, type);
|
|
854
|
-
res.jsonResponse({ token });
|
|
855
|
-
} catch (err) {
|
|
856
|
-
return res.jsonResponse({
|
|
857
|
-
error: err.message,
|
|
858
|
-
documentation: `${documentation}/${req.version}/en/token`
|
|
859
|
-
});
|
|
860
|
-
}
|
|
861
|
-
});
|
|
862
|
-
|
|
863
|
-
// ----------- ----------- SERVER SETUP ----------- ----------- //
|
|
864
|
-
|
|
865
|
-
app.listen(3000, () => console.log('API is running on\n - http://127.0.0.1:3000\n - http://localhost:3000'));
|