@pushwoosh/geo-ip
v0.0.3
Published
Lightweight library for accurate user location detection, utilizing IP address and other data sources to provide precise geographical information.
Readme
@pushwoosh/geo-ip
TypeScript library for detecting a user's country by IP address. Used in the Pushwoosh single-sign-on registration flow to pre-select the user's country.
Installation
npm install @pushwoosh/geo-ipUsage
import { getCountryCodeByIp, getCountryName, checkIsAvailableCountyCode } from '@pushwoosh/geo-ip';
// Get ISO country code from user's IP (calls ipinfo.io)
const code = await getCountryCodeByIp(); // e.g. "US"
// Validate the code
if (checkIsAvailableCountyCode(code)) {
const name = getCountryName(code); // "United States"
}Development
npm install
npm run build # Build CJS + ESM + type declarations
npm run check:types # TypeScript type check
npm run package # Build and pack as tarballAPI
| Function | Description |
|----------|-------------|
| getCountryCodeByIp() | Fetches the user's ISO country code via ipinfo.io/json |
| checkIsAvailableCountyCode(input) | Type guard that checks if a string is a valid CountryCode |
| getCountryName(code) | Returns the full country name for a given CountryCode |
| Country | Enum mapping ISO 3166-1 alpha-2 codes to country names (~230 entries) |
| CountryCode | Type alias for valid keys of the Country enum |
