Skip to content

Commit

Permalink
Update index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
sefinek committed Nov 1, 2024
1 parent be22a07 commit d942041
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,25 @@ const makeRequest = ip => new Promise((resolve, reject) => {

const req = https.get(`https://api.sefinek.net/api/v2/geoip/${ip}`, { headers, timeout }, res => {
const { statusCode } = res;
if ((statusCode < 200 || statusCode >= 300) && statusCode !== 400) return reject(new Error(`HTTP Status Code: ${statusCode}`));
if (statusCode !== 200 && statusCode !== 400) {
return reject(new Error(`HTTP Status Code: ${statusCode}`));
}

let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => {
try {
const parsedData = JSON.parse(data);
resolve(parsedData);
resolve(JSON.parse(data));
} catch (err) {
reject(err);
}
});
});

req.on('error', err => reject(err));
req.on('error', reject);
req.on('timeout', () => {
req.destroy();
reject(new Error(`Request timed out (${timeout} ms).`));
reject(new Error(`Request timed out after ${timeout} ms.`));
});
});

Expand Down

0 comments on commit d942041

Please sign in to comment.