-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleRequest.mjs
More file actions
58 lines (47 loc) · 1.44 KB
/
simpleRequest.mjs
File metadata and controls
58 lines (47 loc) · 1.44 KB
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
import https from 'https'; // Change every occurance of https to http if needed, and vice versa
import config from './conf.json' assert {type: 'json'};
const isValidNumber = (value) => !isNaN(Number(value));
const performGetRequest = (days) => {
const url = `https://${config.requesterConfig.targetHost}/sl-cda?days=${days}&format=json&secret=${config.secret}`;
return new Promise((resolve, reject) => {
const req = https.get(url, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
if (res.statusCode === 200) {
resolve(JSON.parse(data));
} else {
reject(new Error(`Unexpected response: ${res.statusCode}, ${data}`));
}
});
});
req.on('error', (error) => {
reject(error);
});
req.end();
});
};
const main = async () => {
// Check if a argument is provided
if (process.argv.length < 3) {
console.error('Please provide a number as a command line argument.');
process.exit(1);
}
// Extract argument and validate it
const daysArg = process.argv[2];
if (!isValidNumber(daysArg)) {
console.error('Invalid number provided.');
process.exit(1);
}
try {
const responseData = await performGetRequest(Number(daysArg));
console.log('Response:', responseData);
process.exit(0);
} catch (error) {
console.error('Error:', error.message);
process.exit(1);
}
};
main();