-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(geolocate): return error where there is no ip info
- Loading branch information
Showing
3 changed files
with
32 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
import noIpInfoErrorMsg from './utils/constants'; | ||
|
||
export default class GeolocateService { | ||
static async geolocateFreeGeoIp() { | ||
const response = await fetch('https://freegeoip.net/json/'); | ||
const ipInfo = await response.json(); | ||
static async geolocateFreeGeoIp(ip) { | ||
const ipQuery = ip || ''; | ||
const response = await fetch(`https://freegeoip.net/json/${ipQuery}`); | ||
const ipInfo = await response.json().catch(() => new Error(noIpInfoErrorMsg)); | ||
return ipInfo; | ||
} | ||
|
||
static async geolocateIPAPI() { | ||
const response = await fetch('https://ipapi.co/country'); | ||
const countryCode = await response.text(); | ||
return countryCode; | ||
static async geolocateIPAPI(ip) { | ||
const ipQuery = ip ? `${ip}/` : ''; | ||
const response = await fetch(`https://ipapi.co/${ipQuery}country`); | ||
const countryCode = await response.text().catch(() => new Error(noIpInfoErrorMsg)); | ||
return countryCode !== 'Undefined' ? countryCode : new Error(noIpInfoErrorMsg); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const noIpInfoErrorMsg = 'no ip information'; | ||
|
||
export default noIpInfoErrorMsg; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters