(ip?: string)
| 67 | }); |
| 68 | |
| 69 | export async function getGeoLocation(ip?: string): Promise<GeoLocation> { |
| 70 | if (!ip || ignore.includes(ip)) { |
| 71 | return DEFAULT_GEO; |
| 72 | } |
| 73 | |
| 74 | const cached = cache.get(ip); |
| 75 | if (cached) { |
| 76 | return cached; |
| 77 | } |
| 78 | |
| 79 | const reader = await getReader(); |
| 80 | |
| 81 | try { |
| 82 | const response = reader?.city(ip); |
| 83 | const res = { |
| 84 | city: response?.city?.names.en, |
| 85 | country: response?.country?.isoCode, |
| 86 | region: response?.subdivisions?.[0]?.names.en, |
| 87 | longitude: response?.location?.longitude, |
| 88 | latitude: response?.location?.latitude, |
| 89 | }; |
| 90 | cache.set(ip, res); |
| 91 | return res; |
| 92 | } catch (error) { |
| 93 | return DEFAULT_GEO; |
| 94 | } |
| 95 | } |
no test coverage detected