@@ -33,36 +33,25 @@ yarn add node-ipinfo
3333
3434### Usage
3535
36- ##### TypeScript
37-
3836``` typescript
39- import IPinfoWrapper , { IPinfo , AsnResponse } from " node-ipinfo" ;
37+ import { IPinfoWrapper } from " node-ipinfo" ;
4038
4139const ipinfoWrapper = new IPinfoWrapper (" MY_TOKEN" );
4240
43- ipinfoWrapper .lookupIp (" 1.1.1.1" ).then ((response : IPinfo ) => {
44- console .log (response );
45- });
46-
47- ipinfoWrapper .lookupASN (" AS7922" ).then ((response : AsnResponse ) => {
48- console .log (response );
49- });
41+ const ipinfo = await ipinfoWrapper .lookupIp (" 1.1.1.1" );
5042```
5143
52- ##### JavaScript
53-
54- ``` javascript
55- const { IPinfoWrapper } = require (" node-ipinfo" );
44+ #### Best practices
5645
57- const ipinfo = new IPinfoWrapper (" MY_TOKEN" );
46+ Each ` lookup ` method will throw an error when the lookup does not complete
47+ successfully. A program that performs a lookup should catch errors and return
48+ a default value. For example, if your program is performing a lookup to find the
49+ country code of an IP:
5850
59- ipinfo .lookupIp (" 1.1.1.1" ).then ((response ) => {
60- console .log (response);
61- });
51+ ``` typescript
52+ const ipinfo = await ipinfoWrapper .lookupIp (" 1.1.1.1" ).catch (error => null );
6253
63- ipinfo .lookupASN (" AS7922" ).then ((response ) => {
64- console .log (response);
65- });
54+ const countryCode = ipinfo ? ipinfo .countryCode : " N/A" ;
6655```
6756
6857### Caching
@@ -73,30 +62,15 @@ If you prefer a different caching methodology, you may use the `IPCache` interfa
7362
7463The default cache length is 1 day and the default max number of items allowed in the cache is 5000. This can be changed by passing an ` Option ` to the ` LruCache ` constructor.
7564
76- ##### TypeScript
77-
7865``` typescript
79- import IPinfoWrapper , { LruCache , Options } from " node-ipinfo" ;
80-
81- const cacheOptions: Options <string , any > = {
82- max: 5000 ,
83- ttl: 24 * 1000 * 60 * 60 ,
84- };
85- const cache = new LruCache (cacheOptions );
86- const ipinfoWrapper = new IPinfoWrapper (" MY_TOKEN" , cache );
87- ```
88-
89- ##### JavaScript
90-
91- ``` javascript
92- const { IPinfoWrapper , LruCache } = require (" node-ipinfo" );
66+ import { IPinfoWrapper , LruCache } from " node-ipinfo" ;
9367
9468const cacheOptions = {
9569 max: 5000 ,
9670 ttl: 24 * 1000 * 60 * 60 ,
9771};
9872const cache = new LruCache (cacheOptions );
99- const ipinfo = new IPinfoWrapper (" MY_TOKEN" , cache);
73+ const ipinfoWrapper = new IPinfoWrapper (" MY_TOKEN" , cache );
10074```
10175
10276### Timeouts
@@ -106,74 +80,19 @@ controls the timeout of requests. It defaults to `5000` i.e. 5 seconds.
10680
10781A timeout of ` 0 ` disables the timeout feature.
10882
109- ##### TypeScript
110-
11183``` typescript
11284import IPinfoWrapper from " node-ipinfo" ;
11385
11486// 10 second timeout.
11587const ipinfoWrapper = new IPinfoWrapper (" MY_TOKEN" , null , 10000 );
11688```
11789
118- ##### JavaScript
119-
120- ``` javascript
121- const { IPinfoWrapper } = require (" node-ipinfo" );
122-
123- // 10 second timeout.
124- const ipinfo = new IPinfoWrapper (" MY_TOKEN" , null , 10000 );
125- ```
126-
127- ### Errors
128-
129- ##### TypeScript
130-
131- ``` typescript
132- import IPinfoWrapper , { IPinfo , ApiLimitError } from " node-ipinfo" ;
133-
134- const ipinfoWrapper = new IPinfoWrapper (" MY_TOKEN" );
135-
136- ipinfoWrapper .lookupIp (" 1.1.1.1" ).then ((response : IPinfo ) => {
137- console .log (response );
138- })
139- .catch ((error ) => {
140- console .log (error );
141- if (error instanceof ApiLimitError ) {
142- // handle api limit exceed error
143- } else {
144- // handle other errors
145- }
146- });
147- ```
148-
149- ##### JavaScript
150-
151- ``` javascript
152- const { IPinfoWrapper , ApiLimitError } = require (" node-ipinfo" );
153-
154- const ipinfo = new IPinfoWrapper (" MY_TOKEN" );
155-
156- ipinfo .lookupIp (" 1.1.1.1" ).then ((response ) => {
157- console .log (response);
158- },
159- (error ) => {
160- console .log (error);
161- if (error instanceof ApiLimitError){
162- // handle api limit exceed error
163- } else {
164- // handle other errors
165- }
166- });
167- ```
168-
16990### Internationalization
17091
17192When looking up an IP address, the response object includes ` response.country ` will return the country name, ` response.countryCode ` can be used to fetch the country code, Additionally ` response.isEU ` will return ` true ` if the country is a member of the European Union (EU), ` response.countryFlag ` will return the emoji and Unicode of the country's flag, ` response.countryFlagURL ` will return a public link to the country's flag image as an SVG which can be used anywhere, ` response.countryCurrency ` will return the code and symbol of the country's currency and ` response.continent ` will return the continent of the IP. It is possible to return the country name in other languages, change the EU countries, countries flags, countries currencies, and continents by setting the ` countries ` , ` euCountries ` , ` countriesFlags ` , ` countriesCurrencies ` and ` continents ` settings when creating the IPinfo object.
17293
173- ##### TypeScript
174-
17594``` typescript
176- import IPinfoWrapper , { IPinfo } from " node-ipinfo" ;
95+ import IPinfoWrapper from " node-ipinfo" ;
17796
17897const countries = {
17998 " US" : " United States" ,
@@ -217,7 +136,7 @@ const ipinfoWrapper = new IPinfoWrapper(
217136 }
218137);
219138
220- ipinfoWrapper .lookupIp (" 1.1.1.1" ).then (( response : IPinfo ) => {
139+ ipinfoWrapper .lookupIp (" 1.1.1.1" ).then (response => {
221140 // country code, e.g. 'US'
222141 console .log (response .countryCode );
223142
@@ -241,99 +160,16 @@ ipinfoWrapper.lookupIp("1.1.1.1").then((response: IPinfo) => {
241160});
242161```
243162
244- ##### JavaScript
245-
246- ``` javascript
247- const { IPinfoWrapper } = require (" node-ipinfo" );
248-
249- const countries = {
250- " US" : " United States" ,
251- " FR" : " France" ,
252- " BD" : " Bangladesh" ,
253- ...
254- }
255-
256- const countriesFlags = {
257- " US" : {" emoji" : " 🇺🇸" ," unicode" : " U+1F1FA U+1F1F8" },
258- " AD" : {" emoji" : " 🇦🇩" , " unicode" : " U+1F1E6 U+1F1E9" },
259- " AE" : {" emoji" : " 🇦🇪" , " unicode" : " U+1F1E6 U+1F1EA" },
260- ...
261- }
262-
263- const countriesCurrencies = {
264- " US" : { " code" : " USD" ," symbol" : " $" },
265- " AD" : {" code" : " EUR" , " symbol" : " €" },
266- " AE" : {" code" : " AED" , " symbol" : " د.إ" },
267- ...
268- }
269-
270- const continents = {
271- " US" : {" code" : " NA" , " name" : " North America" },
272- " BD" : {" code" : " AS" , " name" : " Asia" },
273- " BE" : {" code" : " EU" , " name" : " Europe" },
274- ...
275- }
276-
277- const euCountries = [" FR" ," ES" ," BE" , ... ]
278-
279- const ipinfo = new IPinfoWrapper (
280- " MY_TOKEN" ,
281- undefined ,
282- undefined ,
283- {
284- countries: countries,
285- countriesFlags: countriesFlags,
286- countriesCurrencies: countriesCurrencies,
287- ...
288- }
289- );
290-
291- ipinfo .lookupIp (" 1.1.1.1" ).then ((response ) => {
292- // country code, e.g. 'US'
293- console .log (response .countryCode );
294-
295- // country name, e.g. 'United States'
296- console .log (response .country );
297-
298- // whether part of the EU, e.g. false
299- console .log (response .isEU );
300-
301- // emoji and unicode of country flag { emoji: '🇺🇸', unicode: 'U+1F1FA U+1F1F8' }
302- console .log (response .countryFlag )
303-
304- // country's flag image URL e.g. https://cdn.ipinfo.io/static/images/countries-flags/US.svg
305- console .log (response .countryFlagURL )
306-
307- // code and symbol of country currency { code: 'USD', symbol: '$' }
308- console .log (response .countryCurrency )
309- });
310- ```
311-
312163### Location Information
313164
314165` response.loc ` will return a composite string of latitude and longitude values in the ` "latitude,longitude" ` format.
315166
316- ##### TypeScript
317-
318167``` typescript
319- import IPinfoWrapper , { IPinfo } from " node-ipinfo" ;
168+ import IPinfoWrapper from " node-ipinfo" ;
320169
321170const ipinfoWrapper = new IPinfoWrapper (" MY_TOKEN" );
322171
323- ipinfoWrapper .lookupIp (" 1.1.1.1" ).then ((response : IPinfo ) => {
324- // '34.0522,-118.2437'
325- console .log (response .loc );
326- });
327- ```
328-
329- ##### JavaScript
330-
331- ``` javascript
332- const { IPinfoWrapper } = require (" node-ipinfo" );
333-
334- const ipinfo = new IPinfoWrapper (" MY_TOKEN" );
335-
336- ipinfo .lookupIp (" 1.1.1.1" ).then ((response ) => {
172+ ipinfoWrapper .lookupIp (" 1.1.1.1" ).then (response => {
337173 // '34.0522,-118.2437'
338174 console .log (response .loc );
339175});
@@ -343,28 +179,13 @@ ipinfo.lookupIp("1.1.1.1").then((response) => {
343179
344180A world map can be generated with locations of all input IPs using ` getMap ` . It returns the URL of the map in the response.
345181
346- ##### TypeScript
347-
348182``` typescript
349- import IPinfoWrapper , { MapResponse } from " node-ipinfo" ;
183+ import IPinfoWrapper from " node-ipinfo" ;
350184
351185const ipinfoWrapper = new IPinfoWrapper (" MY_TOKEN" );
352186
353187const ips = [" 1.1.1.1" , " 8.8.8.8" , " 1.2.3.4" ];
354- ipinfoWrapper .getMap (ips ).then ((response : MapResponse ) => {
355- console .log (response );
356- });
357- ```
358-
359- ##### JavaScript
360-
361- ``` javascript
362- const { IPinfoWrapper } = require (" node-ipinfo" );
363-
364- const ipinfo = new IPinfoWrapper (" MY_TOKEN" );
365-
366- const ips = [" 1.1.1.1" , " 8.8.8.8" , " 1.2.3.4" ];
367- ipinfo .getMap (ips).then ((response ) => {
188+ ipinfoWrapper .getMap (ips ).then (response => {
368189 console .log (response );
369190});
370191```
@@ -373,28 +194,13 @@ ipinfo.getMap(ips).then((response) => {
373194
374195Looking up a single IP at a time can be slow. It could be done concurrently from the client side, but IPinfo supports a batch endpoint to allow you to group together IPs and let us handle retrieving details for them in bulk for you.
375196
376- ##### TypeScript
377-
378197``` typescript
379- import IPinfoWrapper , { BatchResponse } from " node-ipinfo" ;
198+ import IPinfoWrapper from " node-ipinfo" ;
380199
381200const ipinfoWrapper = new IPinfoWrapper (" MY_TOKEN" );
382201
383202const ips = [" 1.1.1.1" , " 8.8.8.8" , " 1.2.3.4/country" ];
384- ipinfoWrapper .getBatch (ips ).then ((response : BatchResponse ) => {
385- console .log (response );
386- });
387- ```
388-
389- ##### JavaScript
390-
391- ``` javascript
392- const { IPinfoWrapper } = require (" node-ipinfo" );
393-
394- const ipinfo = new IPinfoWrapper (" MY_TOKEN" );
395-
396- const ips = [" 1.1.1.1" , " 8.8.8.8" , " 1.2.3.4/country" ];
397- ipinfo .getBatch (ips).then ((response ) => {
203+ ipinfoWrapper .getBatch (ips ).then (response => {
398204 console .log (response );
399205});
400206```
0 commit comments