-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathipdata.ts
More file actions
259 lines (221 loc) · 6.22 KB
/
ipdata.ts
File metadata and controls
259 lines (221 loc) · 6.22 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import { isIP as netIsIP } from 'net';
import { LRUCache } from 'lru-cache';
const CACHE_MAX = 4096; // max number of items
const CACHE_TTL = 1000 * 60 * 60 * 24; // 24 hours
const DEFAULT_IP = 'DEFAULT_IP';
const VALID_FIELDS = [
'ip',
'is_eu',
'city',
'region',
'region_code',
'country_name',
'country_code',
'continent_name',
'continent_code',
'latitude',
'longitude',
'asn',
'company',
'organisation',
'postal',
'calling_code',
'flag',
'emoji_flag',
'emoji_unicode',
'carrier',
'languages',
'currency',
'time_zone',
'threat',
'count',
'status',
];
const BASE_URL = 'https://api.ipdata.co/';
export const EU_BASE_URL = 'https://eu-api.ipdata.co/';
function isValidIP(ip: string): boolean {
return ip === DEFAULT_IP || netIsIP(ip) !== 0;
}
function isValidSelectField(field: string): boolean {
const index = VALID_FIELDS.indexOf(field);
if (index === -1) {
throw new Error(`${field} is not a valid field.`);
}
return true;
}
function isValidFields(fields: string[]): boolean {
if (!Array.isArray(fields)) {
throw new Error('Fields should be an array.');
}
fields.forEach((field) => {
const index = VALID_FIELDS.indexOf(field);
if (index === -1) {
throw new Error(`${field} is not a valid field.`);
}
});
return true;
}
export interface CacheConfig {
max?: number;
ttl?: number;
}
export interface LookupResponse {
ip: string;
is_eu: boolean;
city?: string;
region?: string;
region_code?: string;
country_name: string;
country_code: string;
continent_name: string;
continent_code: string;
latitude: number;
longitude: number;
postal?: string;
calling_code: string;
flag: string;
emoji_flag: string;
emoji_unicode: string;
carrier?: {
name: string;
mcc: string;
mnc: string;
};
asn: {
asn: string;
name: string;
domain: string;
route: string;
type: string;
};
company?: {
name: string;
domain: string;
network: string;
type: string;
};
languages: { name: string; native: string; code?: string }[];
currency: {
name: string;
code: string;
symbol: string;
native: string;
plural: string;
};
time_zone: {
name: string;
abbr: string;
offset: string;
is_dst: boolean;
current_time: string;
};
threat: {
is_tor: boolean;
is_proxy: boolean;
is_anonymous: boolean;
is_known_attacker: boolean;
is_known_abuser: boolean;
is_threat: boolean;
is_bogon: boolean;
is_icloud_relay?: boolean;
is_datacenter?: boolean;
blocklists?: { name: string; site: string; type: string }[];
};
count: number;
status: number;
}
export class IPData {
apiKey: string;
baseUrl: string;
cache: LRUCache<string, LookupResponse>;
constructor(apiKey: string, cacheConfig?: CacheConfig, baseUrl?: string) {
if (typeof apiKey !== 'string') {
throw new Error('An API key is required.');
}
this.apiKey = apiKey;
this.baseUrl = baseUrl || BASE_URL;
this.cache = new LRUCache<string, LookupResponse>({ max: CACHE_MAX, ttl: CACHE_TTL, ...cacheConfig });
}
private buildUrl(...pathSegments: string[]): URL {
const base = this.baseUrl.endsWith('/') ? this.baseUrl : `${this.baseUrl}/`;
const path = pathSegments.filter(Boolean).join('/');
const url = path ? new URL(path, base) : new URL(base);
url.searchParams.set('api-key', this.apiKey);
return url;
}
async lookup(ip?: string, selectField?: string, fields?: string[]): Promise<LookupResponse> {
if (ip && !isValidIP(ip)) {
throw new Error(`${ip} is an invalid IP address.`);
}
if (this.cache.has(ip || DEFAULT_IP)) {
return this.cache.get(ip || DEFAULT_IP)!;
}
if (selectField && fields) {
throw new Error('The selectField and fields parameters cannot be used at the same time.');
}
let urlPath = ip || '';
if (selectField && isValidSelectField(selectField)) {
urlPath = urlPath ? `${urlPath}/${selectField}` : selectField;
}
const url = this.buildUrl(urlPath);
if (fields && isValidFields(fields)) {
url.searchParams.set('fields', fields.join(','));
}
const response = await fetch(url.toString());
if (!response.ok) {
const errorData = (await response.json().catch(() => ({}))) as object;
return { ...errorData, status: response.status } as LookupResponse;
}
const responseData: unknown = await response.json();
let data: LookupResponse;
if (selectField) {
data = { [selectField]: responseData, status: response.status } as unknown as LookupResponse;
} else {
data = { ...(responseData as Record<string, unknown>), status: response.status } as LookupResponse;
}
this.cache.set(ip || DEFAULT_IP, data);
return this.cache.get(ip || DEFAULT_IP)!;
}
async bulkLookup(ips: string[], fields?: string[]): Promise<LookupResponse[]> {
const responses: LookupResponse[] = [];
const bulk: string[] = [];
if (ips.length < 2) {
throw new Error('Bulk Lookup requires more than 1 IP Address in the payload.');
}
ips.forEach((ip) => {
if (!isValidIP(ip)) {
throw new Error(`${ip} is an invalid IP address.`);
}
if (this.cache.has(ip)) {
responses.push(this.cache.get(ip)!);
} else {
bulk.push(ip);
}
});
if (fields && isValidFields(fields)) {
// validation only — fields are passed as query params below
}
if (bulk.length > 0) {
const url = this.buildUrl('bulk');
if (fields) {
url.searchParams.set('fields', fields.join(','));
}
const response = await fetch(url.toString(), {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(bulk),
});
if (!response.ok) {
const errorData = (await response.json().catch(() => ({}))) as object;
return { ...errorData, status: response.status } as unknown as LookupResponse[];
}
const responseData = (await response.json()) as LookupResponse[];
responseData.forEach((info) => {
this.cache.set(info.ip, { ...info, status: response.status });
responses.push(this.cache.get(info.ip)!);
});
}
return responses;
}
}
export default IPData;