Last active
July 31, 2020 13:17
-
-
Save rimiti/4fb3fa033348f308043d8570794aac79 to your computer and use it in GitHub Desktop.
Typescript implementation of ipstack
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
import axios, { AxiosResponse } from 'axios'; | |
import { config } from '../config'; | |
import { Sentry } from '../libs/sentry'; | |
/** | |
* @description Ip Stack API response | |
*/ | |
interface IIpStackResponse { | |
latitude: number; | |
longitude: number; | |
} | |
/** | |
* @description Return IP geolocation | |
* @param ip {string} IP address | |
* @returns | |
*/ | |
async function getGeolocationFromIp(ip: string): Promise<IIpStackResponse> { | |
try { | |
const { data }: AxiosResponse = await axios.get(`https://api.ipstack.com/${ip}`, { | |
headers: { | |
access_key: config.ipStack.token, | |
}, | |
}); | |
return data; | |
} catch (error) { | |
Sentry.captureException(error); | |
throw error; | |
} | |
} | |
export { getGeolocationFromIp, IIpStackResponse }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment