-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetIPData.ts
54 lines (47 loc) · 1.13 KB
/
getIPData.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import axios from "axios";
import { cacheResponse } from "./cache";
import { Log } from "./main";
/*
- https://ip-api.com/
- Free for non-commercial use, no API key required
- Limited to 45 HTTP requests per minute from an IP address
*/
export const url = "http://ip-api.com/json";
export interface IPDataResponse {
query: string;
status: string;
country: string;
countryCode: string;
region: string;
regionName: string;
city: string;
zip: string;
lat: number;
lon: number;
timezone: string;
isp: string;
org: string;
as: string;
}
export interface IPData {
lat: number;
lon: number;
}
export const fetchIPData = async (log: Log): Promise<IPData> => {
try {
log("Fetching IP data...");
const response = await axios.get<IPDataResponse>(url);
log(response.data);
log("");
return {
lat: response.data.lat,
lon: response.data.lon,
};
} catch (error) {
log(error);
throw new Error("Failed to fetch IP data");
}
};
export const getIPData: (log: Log) => Promise<IPData> = async (log: Log) => {
return await cacheResponse<IPData>("ipData", async () => fetchIPData(log));
};