Skip to content

Commit

Permalink
1.0.9
Browse files Browse the repository at this point in the history
  • Loading branch information
sefinek committed Dec 7, 2024
1 parent f4daddf commit 4e44230
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 38 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
.idea
node_modules
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/discord.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/geoip2-api.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/jsLibraryMappings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/jsLinters/eslint.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 4 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@


## 📝 Information
This documentation provides information about the lightweight Node.js module designed for sending requests to a geolocation data API.
This module enables you to retrieve location information for a specified IP address and takes full advantage of native modules.
The [official API](https://api.sefinek.net) is thoroughly optimized ⚡ and secure 🔐.

> We use the [MaxMind database](https://www.maxmind.com) in the [geoip-lite2](https://github.com/sefinek/geoip-lite2) module for [our API](https://api.sefinek.net/docs/v2).
> Due to certain limitations, the database is not updated daily (and never will be) by the module's developer. Updates are issued periodically.
This documentation covers a lightweight Node.js module designed for interacting with a geolocation data API.
The module enables efficient retrieval of location details for specific IP addresses, utilizing native modules for optimal performance.

> We utilize the [MaxMind database](https://www.maxmind.com) via the [geoip-lite2](https://github.com/sefinek/geoip-lite2) module for [our API](https://api.sefinek.net/docs/v2).
## 💻 Locally
There is an alternative to this module that allows for local geolocation retrieval based on a specific IP address.
Expand Down Expand Up @@ -83,6 +80,4 @@ If you like this module, please **star** ⭐ the repository.
## 🔑 MIT License
This GeoIP API client script is provided under the MIT License. See the [LICENSE](LICENSE) file for more details.

Copyright 2023-2024 © by [Sefinek](https://sefinek.net). All Rights Reserved.

[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fsefinek%2Fgeoip2-api.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fsefinek%2Fgeoip2-api?ref=badge_large)
Copyright 2023-2024 © by [Sefinek](https://sefinek.net). All Rights Reserved.
53 changes: 30 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,38 @@ const headers = {

const timeout = 25000;

const makeRequest = ip => new Promise((resolve, reject) => {
if (!ip) return reject(new Error('IP address is required.'));

const req = https.get(`https://api.sefinek.net/api/v2/geoip/${ip}`, { headers, timeout }, res => {
const { statusCode } = res;
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 {
resolve(JSON.parse(data));
} catch (err) {
reject(err);
const get = async ip => {
if (!ip || typeof ip !== 'string') throw new Error('A valid IP address is required.');

return 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 !== 400) {
req.destroy();
return reject(new Error(`HTTP Status Code: ${statusCode}`));
}

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

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

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

module.exports = { get: makeRequest, version };
module.exports = { get, version };
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "geoip2-api",
"version": "1.0.8",
"description": "This module enables you to retrieve the customer's geolocation using their IP address, utilizing data obtained from an official and high-speed API.",
"version": "1.0.9",
"description": "This module allows obtaining the client's geolocation using their IP address, leveraging data retrieved from a fast API.",
"keywords": [
"geo api",
"geo ip",
Expand Down
2 changes: 1 addition & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,4 @@ describe('GeoIP Wrapper Module', () => {
expect(result.message).toBe('HTTP Status Code: 400');
}
});
});
});

0 comments on commit 4e44230

Please sign in to comment.