|
2 | 2 |
|
3 | 3 | [](https://github.com/ConnerTechnology/ipdata-js-library/actions) |
4 | 4 |
|
5 | | -JavaScript library that can be used in a web browser or Node.js application to gather information using https://ipdata.co. |
| 5 | +JavaScript library that can be used in a web browser or Node.js application to gather information for an IP address using https://ipdata.co. |
6 | 6 |
|
7 | 7 | **Table of Contents** |
8 | 8 |
|
9 | 9 | - [Install](#install) |
10 | | -- [Import the library](#import-the-library) |
11 | 10 | - [Use](#use) |
| 11 | + - [Import Library](#import-library) |
| 12 | + - [Create an Instance](#create-an-instance) |
12 | 13 | - [Lookup](#lookup) |
13 | | - - [Lookup Bulk](#lookup-bulk) |
| 14 | + - [Bulk Lookup](#bulk-lookup) |
14 | 15 |
|
15 | 16 | ## Install |
16 | 17 |
|
17 | 18 | ```sh |
18 | 19 | $ npm install ipdata |
19 | 20 | ``` |
20 | 21 |
|
21 | | -## Import the library |
| 22 | +## Use |
| 23 | + |
| 24 | +### Import library |
22 | 25 |
|
23 | 26 | Import the library. |
24 | 27 |
|
25 | 28 | ```js |
26 | | -import ipdata from 'ipdata'; |
| 29 | +import IPData from 'ipdata'; |
| 30 | +``` |
| 31 | + |
| 32 | +**Note:** If you are using `require()` then you will need to use the default value exported from the library. |
| 33 | + |
| 34 | +```js |
| 35 | +const IPData = require('ipdata').default; |
27 | 36 | ``` |
28 | 37 |
|
| 38 | +### Create an Instance |
| 39 | + |
29 | 40 | Create an instance of the `IPData` class and pass your api key for IPData as the first parameter. |
30 | 41 |
|
31 | 42 | ```js |
@@ -56,59 +67,67 @@ const ipdata = new IPData('<apiKey>', cacheConfig); |
56 | 67 | The library will lookup the ip address of the host computer if no ip address is provided. |
57 | 68 |
|
58 | 69 | ```js |
59 | | -ipdata.lookup().then(function(info) { |
60 | | - // info.ip === '<hostcomputerip>' |
61 | | - // ... |
62 | | -}); |
| 70 | +ipdata.lookup() |
| 71 | + .then(function(info) { |
| 72 | + // info.ip === '<hostcomputerip>' |
| 73 | + // ... |
| 74 | + }); |
63 | 75 | ``` |
64 | 76 |
|
65 | 77 | You can pass an ip address as the first parameter to the `lookup()` method to lookup information about the ip address using IPData. |
66 | 78 |
|
67 | 79 | ```js |
68 | | -ipdata.lookup('1.1.1.1').then(function(info) { |
69 | | - // info.ip === 1.1.1.1 |
70 | | - // ... |
71 | | -}); |
| 80 | +const ip = '1.1.1.1'; |
| 81 | +ipdata.lookup(ip) |
| 82 | + .then(function(info) { |
| 83 | + // info.ip === 1.1.1.1 |
| 84 | + // ... |
| 85 | + }); |
72 | 86 | ``` |
73 | 87 |
|
74 | 88 | You can specify only a select field to be returned when looking up an ip address by passing a field as the second parameter to the `lookup()` method. |
75 | 89 |
|
76 | 90 | ```js |
77 | 91 | const ip = '1.1.1.1'; |
78 | 92 | const selectField = 'ip'; |
79 | | -ipdata.lookup(ip, selectField).then(function(info) { |
80 | | - // info.select_field === 1.1.1.1 |
81 | | - // ... |
82 | | -}); |
| 93 | +ipdata.lookup(ip, selectField) |
| 94 | + .then(function(info) { |
| 95 | + // info.select_field === 1.1.1.1 |
| 96 | + // ... |
| 97 | + }); |
83 | 98 | ``` |
84 | 99 |
|
85 | 100 | You can specify only certain fields to be returned when looking up an ip address by passing an array of fields as the third parameter to the `lookup()` method. |
86 | 101 |
|
87 | 102 | ```js |
88 | 103 | const ip = '1.1.1.1'; |
89 | 104 | const fields = ['ip', 'city']; |
90 | | -ipdata.lookup(ip, null, fields).then(function(info) { |
91 | | - // ... |
92 | | -}); |
| 105 | +ipdata.lookup(ip, null, fields) |
| 106 | + .then(function(info) { |
| 107 | + // ... |
| 108 | + }); |
93 | 109 | ``` |
94 | 110 |
|
95 | 111 | ### Bulk Lookup |
96 | 112 |
|
97 | 113 | You can lookup multiple ip addresses with one API call using the `bulkLookup()` method. |
98 | 114 |
|
99 | 115 | ```js |
100 | | -ipdata.bulkLookup(['1.1.1.1', '1.0.0.1']).then(function(info) { |
101 | | - // info.responses[0].ip === 1.1.1.1 |
102 | | - // ... |
103 | | -}); |
| 116 | +const ips = ['1.1.1.1', '1.0.0.1']; |
| 117 | +ipdata.bulkLookup(ips) |
| 118 | + .then(function(info) { |
| 119 | + // info[0].ip === 1.1.1.1 |
| 120 | + // ... |
| 121 | + }); |
104 | 122 | ``` |
105 | 123 |
|
106 | 124 | You can specify only certain fields to be returned when looking up multiple ip addresses by passing an array of fields as the second parameter to the `bulkLookup()` method. |
107 | 125 |
|
108 | 126 | ```js |
109 | 127 | const ips = ['1.1.1.1', '1.0.0.1']; |
110 | 128 | const fields = ['ip', 'city']; |
111 | | -ipdata.bulkLookup(ips, fields).then(function(info) { |
112 | | - // ... |
113 | | -}); |
| 129 | +ipdata.bulkLookup(ips, fields) |
| 130 | + .then(function(info) { |
| 131 | + // ... |
| 132 | + }); |
114 | 133 | ``` |
0 commit comments