Skip to content

Commit 5eadf70

Browse files
committed
docs: Replace ESM with CommonJS as default
1 parent d5cfe17 commit 5eadf70

File tree

1 file changed

+63
-13
lines changed

1 file changed

+63
-13
lines changed

README.md

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,61 @@ yarn add node-ipinfo
3333

3434
### Usage
3535

36+
1. Initialize an instance of `IPinfoWrapper` with [your token](https://ipinfo.io/account/token).
37+
3638
```typescript
37-
import { IPinfoWrapper } from "node-ipinfo";
39+
const { IPinfoWrapper } = require("node-ipinfo");
3840

3941
const ipinfoWrapper = new IPinfoWrapper("MY_TOKEN");
42+
```
43+
44+
> [!TIP]
45+
> If you are using ESM, import like this instead:
46+
> ```
47+
> import { IPinfoWrapper } from "node-ipinfo";
48+
> ```
49+
50+
2. Perform a lookup for an IP address or ASN
4051
52+
```typescript
4153
const ipinfo = await ipinfoWrapper.lookupIp("1.1.1.1");
4254
```
4355
56+
<details><summary>Standalone example</summary>
57+
1. Create a new file, `ipinfo.js`
58+
59+
```typescript
60+
const { IPinfoWrapper } = require("node-ipinfo");
61+
62+
const ipinfoWrapper = new IPinfoWrapper("MY_TOKEN");
63+
64+
const ipAddress = process.argv[2] || "1.1.1.1";
65+
66+
ipinfoWrapper.lookupIp(ipAddress).then(ipinfo => console.log(ipinfo))
67+
```
68+
69+
2. Run `ipinfo.js` (without an IP) to lookup 1.1.1.1
70+
71+
```shell
72+
node ipinfo.js
73+
{
74+
ip: '1.1.1.1',
75+
// ...
76+
```
77+
78+
3. Run `ipinfo.js` with an IP to lookup
79+
80+
```shell
81+
node ipinfo.js 2.2.2.2
82+
{
83+
ip: '2.2.2.2',
84+
// ...
85+
```
86+
87+
</details>
88+
89+
90+
4491
#### Best practices
4592
4693
Each `lookup` method will throw an error when the lookup does not complete
@@ -49,9 +96,9 @@ desirable for the error to bubble up. For example, if your program is performing
4996
a lookup to find the country code of an IP:
5097
5198
```typescript
52-
const ipinfo = await ipinfoWrapper.lookupIp("1.1.1.1").catch(error => null);
53-
54-
const countryCode = ipinfo ? ipinfo.countryCode : "N/A";
99+
const countryCode = ipinfoWrapper.lookupIp("1.1.1.1")
100+
.then(ipinfo => ipinfo.countryCode)
101+
.catch(error => "N/A");
55102
```
56103
57104
### Caching
@@ -63,13 +110,15 @@ If you prefer a different caching methodology, you may use the `IPCache` interfa
63110
The 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.
64111
65112
```typescript
66-
import { IPinfoWrapper, LruCache } from "node-ipinfo";
113+
const { IPinfoWrapper, LruCache } = require("node-ipinfo");
67114
68115
const cacheOptions = {
69116
max: 5000,
70117
ttl: 24 * 1000 * 60 * 60,
71118
};
119+
72120
const cache = new LruCache(cacheOptions);
121+
73122
const ipinfoWrapper = new IPinfoWrapper("MY_TOKEN", cache);
74123
```
75124
@@ -81,7 +130,7 @@ controls the timeout of requests. It defaults to `5000` i.e. 5 seconds.
81130
A timeout of `0` disables the timeout feature.
82131
83132
```typescript
84-
import IPinfoWrapper from "node-ipinfo";
133+
const { IPinfoWrapper } = require("node-ipinfo");
85134
86135
// 10 second timeout.
87136
const ipinfoWrapper = new IPinfoWrapper("MY_TOKEN", null, 10000);
@@ -92,7 +141,7 @@ const ipinfoWrapper = new IPinfoWrapper("MY_TOKEN", null, 10000);
92141
When 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.
93142
94143
```typescript
95-
import IPinfoWrapper from "node-ipinfo";
144+
const { IPinfoWrapper } = require("node-ipinfo");
96145
97146
const countries = {
98147
"US": "United States",
@@ -165,7 +214,7 @@ ipinfoWrapper.lookupIp("1.1.1.1").then(response => {
165214
`response.loc` will return a composite string of latitude and longitude values in the `"latitude,longitude"` format.
166215
167216
```typescript
168-
import IPinfoWrapper from "node-ipinfo";
217+
const { IPinfoWrapper } = require("node-ipinfo");
169218
170219
const ipinfoWrapper = new IPinfoWrapper("MY_TOKEN");
171220
@@ -180,7 +229,7 @@ ipinfoWrapper.lookupIp("1.1.1.1").then(response => {
180229
A world map can be generated with locations of all input IPs using `getMap`. It returns the URL of the map in the response.
181230
182231
```typescript
183-
import IPinfoWrapper from "node-ipinfo";
232+
const { IPinfoWrapper } = require("node-ipinfo");
184233
185234
const ipinfoWrapper = new IPinfoWrapper("MY_TOKEN");
186235
@@ -195,14 +244,15 @@ ipinfoWrapper.getMap(ips).then(response => {
195244
Looking 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.
196245
197246
```typescript
198-
import IPinfoWrapper from "node-ipinfo";
247+
const { IPinfoWrapper } = require("node-ipinfo");
199248
200249
const ipinfoWrapper = new IPinfoWrapper("MY_TOKEN");
201250
202251
const ips = ["1.1.1.1", "8.8.8.8", "1.2.3.4/country"];
203-
ipinfoWrapper.getBatch(ips).then(response => {
204-
console.log(response);
205-
});
252+
253+
ipinfoWrapper
254+
.getBatch(ips)
255+
.then(batch => console.log(batch));
206256
```
207257
208258
The input size is not limited, as the interface will chunk operations for you

0 commit comments

Comments
 (0)