Skip to content

Latest commit

 

History

History
193 lines (127 loc) · 6.46 KB

README.md

File metadata and controls

193 lines (127 loc) · 6.46 KB

geolocate

Fetch IP geolocation data from the CLI.


⚡ Installation

As a global utility:

$ npm install -g @adamlui/geolocate

As a dev dependency, from your project root:

$ npm install -D @adamlui/geolocate

As a runtime dependency, from your project root:

$ npm install @adamlui/geolocate

💻 Command line usage

The basic global command is:

$ geolocate

Sample output:

💡 Note: If no IPv4 address is passed, your own one will be used.

Command line options

Boolean options:
 -q, --quiet                 Suppress all logging except errors.

Info commands:
 -h, --help                  Display help screen.
 -v, --version               Show version number.

🔌 Importing the API

You can also import geolocate into your app to use its API methods.

Node.js

ES module*:

import * as geo from '@adamlui/geolocate';

CommonJS:

const geo = require('@adamlui/geolocate');
*Node.js version 14 or higher required

Web

<> HTML script tag:

<script src="https://cdn.jsdelivr.net/npm/@adamlui/geolocate@1.0.0/dist/generate-ip.min.js"></script>

ES6:

(async () => {
    await import('https://cdn.jsdelivr.net/npm/@adamlui/geolocate@1.0.0/dist/generate-ip.min.js');
    // Your code here...
})();

Greasemonkey

...
// @require https://cdn.jsdelivr.net/npm/@adamlui/geolocate@1.0.0/dist/geolocate.min.js
// ==/UserScript==

// Your code here...

📝 Note: To always import the latest version (not recommended in production!) remove the @1.0.0 version tag from the jsDelivr URL: https://cdn.jsdelivr.net/npm/@adamlui/geolocate/dist/geolocate.min.js


📋 API usage

locate([ip])

Asynchronous method to fetch geolocation data for the ip passed, returned as an object.

💡 Note: If no ip is supplied, your own one will be used.

// Using await syntax
const location = await geo.locate('192.12.85.186');
console.log(location);

// Using .then() syntax
geo.locate('192.12.85.186').then(location => {
    console.log(location);
});

/* outputs:
{
  ip: '8.8.8.8',
  country: 'United States',
  countryCode: 'US',
  region: 'VA',
  regionName: 'Virginia',
  city: 'Ashburn',
  zip: '20149',
  lat: 39.03,
  lon: -77.5,
  timezone: 'America/New_York',
  isp: 'Google LLC'
}
*/

getOwnIP()

Asynchronous method to fetch/return your own IP as a string:

// Using await syntax
const ip = await geo.getOwnIP();
console.log(ip); // outputs your IP

// Using .then() syntax
geo.getOwnIP().then(ip => {
    console.log(ip); // outputs your IP
});

🏛️ MIT License

Copyright © 2023 Adam Lui

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


JavaScript utilities / Discuss / Back to top ↑