-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.cjs
45 lines (45 loc) · 1.39 KB
/
index.cjs
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
const https = require('node:https');
function fetchRanking(region, server, nick) {
let path = '/v1/';
switch (region.toLowerCase()) {
case 'de':
path += 'de/';
break;
case 'fr':
path += 'fr/';
break;
case 'en':
case 'us':
case 'int':
break;
default:
throw new Error('Unsupported Region.');
}
let queryParams = '';
if (server) { queryParams += `server=${server}&` }
if (nick) { queryParams += `nick=${encodeURIComponent(nick)}` }
if (queryParams) { path += `?${queryParams}` }
const options = {
hostname: 'api.nosranking.com',
port: 443,
path: path,
method: 'GET',
headers: { 'Content-Type': 'application/json' }
};
return new Promise((resolve, reject) => {
const req = https.request(options, (res) => {
let data = '';
res.on('data', (chunk) => { data += chunk });
res.on('end', () => {
if (res.statusCode >= 200 && res.statusCode < 300) {
resolve(JSON.parse(data));
} else {
reject(new Error(`Request failed with status code ${res.statusCode}`));
}
});
});
req.on('error', (e) => { reject(e) });
req.end();
});
}
module.exports = { fetchRanking };