const cache = new Map(); const key = 'hulk_affef_telephone_geoIpLookup'; async function ipapi(): Promise { const response = await fetch('//ipapi.co/json'); if (!response.ok) { throw response; } const data = await response.json(); return data?.country_code ?? ''; } async function geojs(): Promise { const response = await fetch('//get.geojs.io/v1/ip/country.json'); if (!response.ok) { throw response; } const data = await response.json(); return data?.country ?? ''; } async function ipGuide(): Promise { const response = await fetch('//ip.guide'); if (!response.ok) { throw response; } const data = await response.json(); return data?.network?.autonomous_system?.country ?? ''; } async function bigdatacloud(): Promise { const response = await fetch('//api.bigdatacloud.net/data/reverse-geocode-client'); if (!response.ok) { throw response; } const data = await response.json(); return data?.countryCode ?? ''; } async function lookup(defaultCode: string): Promise { return ipapi() .then(value => value.toLowerCase()) .catch(() => geojs()) .then(value => value.toLowerCase()) .catch(() => ipGuide()) .then(value => value.toLowerCase()) .catch(() => bigdatacloud()) .then(value => value.toLowerCase()) .catch(() => defaultCode.toLowerCase()); } function geoIpLookup(defaultCode: string) { if (!cache.has(key)) { cache.set(key, lookup(defaultCode)); } return cache.get(key); } export default geoIpLookup