@minilibs/ip2geo
Version:
Ip to Geo location instantly ⚡
98 lines (95 loc) • 4 kB
JavaScript
import CheckVersion from '../helpers/CheckVersion.js';
import { IPWHO_ENDPOINT } from '../data/Constants.js';
import Http from '../helpers/Http.js';
/**
* Get geo information from an IP address.
*
* The function fetches detailed geographical and connection-related information
* for a given IP address. The returned data includes properties such as continent,
* country, region, city, latitude, longitude, timezone, and connection details.
*
* @param ip - The IP address to retrieve geo information for.
* @returns A Promise resolving to an object of type `Ip` containing geo and connection details, or `null` if no data is found or something goes wrong.
*
* @typedef Ip
* @property ip - The IP address.
* @property type - The type of IP (e.g., IPv4, IPv6).
* @property geo - Geographical information:
* - continent: The continent name.
* - continentCode: The continent code.
* - country: The country name.
* - countryCode: The country code.
* - region: The region name.
* - regionCode: The region code.
* - city: The city name.
* - latitude: The latitude coordinate.
* - longitude: The longitude coordinate.
* - isEu: Whether the location is in the European Union.
* - postal: The postal code.
* - callingCode: The calling code.
* - capital: The capital city.
* - borders: An array of bordering countries.
* @property flag - Information about the country's flag:
* - img: The URL of the flag image.
* - emoji: The flag emoji.
* - emojiUnicode: The Unicode representation of the flag emoji.
* @property connection - Connection details:
* - asn: The autonomous system number.
* - org: The organization name.
* - isp: The internet service provider.
* - domain: The domain name.
* @property timezone - Timezone information:
* - id: The timezone ID.
* - abbr: The timezone abbreviation.
* - isDst: Whether daylight saving time is active.
* - offset: The timezone offset in hours.
* - utc: An array of UTC offsets.
* - currentTime: The current time in the timezone.
*/
const GetGeoFromIP = async (ip) => {
CheckVersion();
const ipLink = `${IPWHO_ENDPOINT}/${ip}`;
const ipResponse = await Http(ipLink);
if (ipResponse) {
const instance = {
ip: ipResponse?.ip || '',
type: ipResponse?.type || '',
continent: ipResponse?.continent || '',
continentCode: ipResponse?.continent_code || '',
country: ipResponse?.country || '',
countryCode: ipResponse?.country_code || '',
region: ipResponse?.region || '',
regionCode: ipResponse?.region_code || '',
city: ipResponse?.city || '',
latitude: ipResponse?.latitude || '',
longitude: ipResponse?.longitude || '',
isEu: ipResponse?.is_eu,
postal: ipResponse?.postal || '',
callingCode: ipResponse?.calling_code || '',
capital: ipResponse?.capital || '',
borders: ipResponse?.borders || '',
flag: {
img: ipResponse?.flag?.img || '',
emoji: ipResponse?.flag?.emoji || '',
emojiUnicode: ipResponse?.flag?.emoji_unicode || ''
},
connection: {
asn: ipResponse?.connection?.asn || '',
org: ipResponse?.connection?.org || '',
isp: ipResponse?.connection?.isp || '',
domain: ipResponse?.connection?.domain || ''
},
timezone: {
id: ipResponse?.timezone?.id || '',
abbr: ipResponse?.timezone?.abbr || '',
isDst: ipResponse?.timezone?.is_dst || '',
offset: ipResponse?.timezone?.offset || '',
utc: ipResponse?.timezone?.utc || '',
currentTime: ipResponse?.timezone?.current_time || ''
}
};
return instance;
}
return null;
};
export { GetGeoFromIP as default };