Skip to content

Instantly share code, notes, and snippets.

@rimiti
Last active July 31, 2020 13:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rimiti/4fb3fa033348f308043d8570794aac79 to your computer and use it in GitHub Desktop.
Save rimiti/4fb3fa033348f308043d8570794aac79 to your computer and use it in GitHub Desktop.
Typescript implementation of ipstack
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