UNPKG

@maxmind/geoip2-node

Version:

Node.js API for GeoIP2 webservice client and database reader

47 lines (46 loc) 1.56 kB
import { camelcaseKeys } from '../utils.js'; export default class Country { continent; country; maxmind; registeredCountry; representedCountry; traits; constructor(response, ipAddress, network) { const camelcaseResponse = camelcaseKeys(response); this.continent = camelcaseResponse.continent || undefined; this.country = camelcaseResponse.country || undefined; this.maxmind = camelcaseResponse.maxmind || undefined; this.registeredCountry = this.setBooleanRegisteredCountry(camelcaseResponse.registeredCountry) || undefined; this.representedCountry = camelcaseResponse.representedCountry || undefined; this.traits = this.setBooleanTraits(camelcaseResponse.traits || {}); this.traits.ipAddress ??= ipAddress; this.traits.network ??= network; } setBooleanTraits(traits) { const booleanTraits = [ 'isAnonymous', 'isAnonymousProxy', 'isAnonymousVpn', 'isAnycast', 'isHostingProvider', 'isLegitimateProxy', 'isPublicProxy', 'isResidentialProxy', 'isSatelliteProvider', 'isTorExitNode', ]; booleanTraits.forEach((trait) => { traits[trait] = !!traits[trait]; }); return traits; } setBooleanRegisteredCountry(country) { if (country) { country.isInEuropeanUnion = !!country.isInEuropeanUnion; } return country; } }