ipwhois 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +203 -0
- package/dist/index.d.ts +48 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +96 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +55 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +28 -0
- package/src/index.ts +113 -0
- package/src/types.ts +56 -0
- package/test/example.js +41 -0
- package/tsconfig.json +20 -0
package/README.md
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
# IPWhois Node Module
|
|
2
|
+
|
|
3
|
+
A lightweight Node.js module for fetching IP geolocation information using the [ipwhois.app](https://ipwhois.app) API.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🚀 Simple and easy to use
|
|
8
|
+
- 📦 Zero dependencies (uses native Node.js fetch)
|
|
9
|
+
- 🔒 TypeScript support with full type definitions
|
|
10
|
+
- ⚡ Works with Node.js 20+
|
|
11
|
+
- 🌍 Get geolocation info for any IP or your current IP
|
|
12
|
+
- ⏱️ Configurable timeout support
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install
|
|
18
|
+
npm run build
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
### JavaScript (CommonJS)
|
|
24
|
+
|
|
25
|
+
```javascript
|
|
26
|
+
const { ipwhois, getIpInfo, getMyIpInfo } = require("ipwhois");
|
|
27
|
+
|
|
28
|
+
// Get info for your current IP
|
|
29
|
+
async function example1() {
|
|
30
|
+
const info = await getMyIpInfo();
|
|
31
|
+
console.log(info);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Get info for a specific IP
|
|
35
|
+
async function example2() {
|
|
36
|
+
const info = await getIpInfo("8.8.8.8");
|
|
37
|
+
console.log(info.country); // "United States"
|
|
38
|
+
console.log(info.city); // "Mountain View"
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Using the main function with options
|
|
42
|
+
async function example3() {
|
|
43
|
+
const info = await ipwhois({
|
|
44
|
+
ip: "1.1.1.1",
|
|
45
|
+
timeout: 5000,
|
|
46
|
+
});
|
|
47
|
+
console.log(info);
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### TypeScript
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
import { ipwhois, getIpInfo, getMyIpInfo, IpWhoisResponse } from "ipwhois";
|
|
55
|
+
|
|
56
|
+
// Get info for your current IP
|
|
57
|
+
const myInfo: IpWhoisResponse = await getMyIpInfo();
|
|
58
|
+
|
|
59
|
+
// Get info for a specific IP
|
|
60
|
+
const googleDns: IpWhoisResponse = await getIpInfo("8.8.8.8");
|
|
61
|
+
|
|
62
|
+
// With options
|
|
63
|
+
const info: IpWhoisResponse = await ipwhois({
|
|
64
|
+
ip: "1.1.1.1",
|
|
65
|
+
timeout: 10000,
|
|
66
|
+
});
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## API
|
|
70
|
+
|
|
71
|
+
### `ipwhois(options?)`
|
|
72
|
+
|
|
73
|
+
Main function to fetch IP geolocation information.
|
|
74
|
+
|
|
75
|
+
**Parameters:**
|
|
76
|
+
|
|
77
|
+
- `options` (optional): Configuration object
|
|
78
|
+
- `ip` (string, optional): IP address to lookup. If not provided, returns info for your current IP
|
|
79
|
+
- `timeout` (number, optional): Request timeout in milliseconds (default: 10000)
|
|
80
|
+
|
|
81
|
+
**Returns:** `Promise<IpWhoisResponse>`
|
|
82
|
+
|
|
83
|
+
### `getIpInfo(ip, timeout?)`
|
|
84
|
+
|
|
85
|
+
Convenience function to fetch info for a specific IP address.
|
|
86
|
+
|
|
87
|
+
**Parameters:**
|
|
88
|
+
|
|
89
|
+
- `ip` (string): IP address to lookup
|
|
90
|
+
- `timeout` (number, optional): Request timeout in milliseconds
|
|
91
|
+
|
|
92
|
+
**Returns:** `Promise<IpWhoisResponse>`
|
|
93
|
+
|
|
94
|
+
### `getMyIpInfo(timeout?)`
|
|
95
|
+
|
|
96
|
+
Convenience function to fetch info for your current IP address.
|
|
97
|
+
|
|
98
|
+
**Parameters:**
|
|
99
|
+
|
|
100
|
+
- `timeout` (number, optional): Request timeout in milliseconds
|
|
101
|
+
|
|
102
|
+
**Returns:** `Promise<IpWhoisResponse>`
|
|
103
|
+
|
|
104
|
+
## Response Format
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
{
|
|
108
|
+
About_Us: string;
|
|
109
|
+
ip: string;
|
|
110
|
+
success: boolean;
|
|
111
|
+
type: string;
|
|
112
|
+
continent: string;
|
|
113
|
+
continent_code: string;
|
|
114
|
+
country: string;
|
|
115
|
+
country_code: string;
|
|
116
|
+
country_flag: string;
|
|
117
|
+
country_capital: string;
|
|
118
|
+
country_phone: string;
|
|
119
|
+
country_neighbours: string;
|
|
120
|
+
region: string;
|
|
121
|
+
city: string;
|
|
122
|
+
latitude: number;
|
|
123
|
+
longitude: number;
|
|
124
|
+
asn: string;
|
|
125
|
+
org: string;
|
|
126
|
+
isp: string;
|
|
127
|
+
timezone: string;
|
|
128
|
+
timezone_name: string;
|
|
129
|
+
timezone_dstOffset: number;
|
|
130
|
+
timezone_gmtOffset: number;
|
|
131
|
+
timezone_gmt: string;
|
|
132
|
+
currency: string;
|
|
133
|
+
currency_code: string;
|
|
134
|
+
currency_symbol: string;
|
|
135
|
+
currency_rates: number;
|
|
136
|
+
currency_plural: string;
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## Example Response
|
|
141
|
+
|
|
142
|
+
```json
|
|
143
|
+
{
|
|
144
|
+
"About_Us": "https://ipwhois.io",
|
|
145
|
+
"ip": "8.8.8.8",
|
|
146
|
+
"success": true,
|
|
147
|
+
"type": "IPv4",
|
|
148
|
+
"continent": "North America",
|
|
149
|
+
"continent_code": "NA",
|
|
150
|
+
"country": "United States",
|
|
151
|
+
"country_code": "US",
|
|
152
|
+
"country_flag": "https://cdn.ipwhois.io/flags/us.svg",
|
|
153
|
+
"country_capital": "Washington",
|
|
154
|
+
"country_phone": "+1",
|
|
155
|
+
"country_neighbours": "CA,MX",
|
|
156
|
+
"region": "California",
|
|
157
|
+
"city": "Mountain View",
|
|
158
|
+
"latitude": 37.405992,
|
|
159
|
+
"longitude": -122.078515,
|
|
160
|
+
"asn": "AS15169",
|
|
161
|
+
"org": "Google LLC",
|
|
162
|
+
"isp": "Google LLC",
|
|
163
|
+
"timezone": "America/Los_Angeles",
|
|
164
|
+
"timezone_name": "PST",
|
|
165
|
+
"timezone_dstOffset": 3600,
|
|
166
|
+
"timezone_gmtOffset": -28800,
|
|
167
|
+
"timezone_gmt": "-08:00",
|
|
168
|
+
"currency": "US Dollar",
|
|
169
|
+
"currency_code": "USD",
|
|
170
|
+
"currency_symbol": "$",
|
|
171
|
+
"currency_rates": 1,
|
|
172
|
+
"currency_plural": "US dollars"
|
|
173
|
+
}
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
## Error Handling
|
|
177
|
+
|
|
178
|
+
```javascript
|
|
179
|
+
try {
|
|
180
|
+
const info = await getIpInfo("8.8.8.8");
|
|
181
|
+
console.log(info);
|
|
182
|
+
} catch (error) {
|
|
183
|
+
console.error("Error:", error.message);
|
|
184
|
+
}
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## Running Tests
|
|
188
|
+
|
|
189
|
+
```bash
|
|
190
|
+
npm test
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
## Requirements
|
|
194
|
+
|
|
195
|
+
- Node.js 22 or higher (for native fetch support)
|
|
196
|
+
|
|
197
|
+
## License
|
|
198
|
+
|
|
199
|
+
MIT
|
|
200
|
+
|
|
201
|
+
## Credits
|
|
202
|
+
|
|
203
|
+
This module uses the [ipwhois.app](https://ipwhois.app) API.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { IpWhoisResponse, IpWhoisOptions } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Fetches IP geolocation information from ipwhois.app API
|
|
4
|
+
*
|
|
5
|
+
* @param options - Optional configuration
|
|
6
|
+
* @returns Promise resolving to IP geolocation data
|
|
7
|
+
* @throws Error if the API request fails or returns an error
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* // Get info for current IP
|
|
12
|
+
* const info = await ipwhois();
|
|
13
|
+
*
|
|
14
|
+
* // Get info for specific IP
|
|
15
|
+
* const info = await ipwhois({ ip: '8.8.8.8' });
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare function ipwhois(options?: IpWhoisOptions): Promise<IpWhoisResponse>;
|
|
19
|
+
/**
|
|
20
|
+
* Fetches IP geolocation information for a specific IP address
|
|
21
|
+
*
|
|
22
|
+
* @param ip - IP address to lookup
|
|
23
|
+
* @param timeout - Optional timeout in milliseconds
|
|
24
|
+
* @returns Promise resolving to IP geolocation data
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* const info = await getIpInfo('8.8.8.8');
|
|
29
|
+
* console.log(info.country); // "United States"
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export declare function getIpInfo(ip: string, timeout?: number): Promise<IpWhoisResponse>;
|
|
33
|
+
/**
|
|
34
|
+
* Fetches IP geolocation information for the caller's IP address
|
|
35
|
+
*
|
|
36
|
+
* @param timeout - Optional timeout in milliseconds
|
|
37
|
+
* @returns Promise resolving to IP geolocation data
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* const info = await getMyIpInfo();
|
|
42
|
+
* console.log(info.ip); // Your IP address
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export declare function getMyIpInfo(timeout?: number): Promise<IpWhoisResponse>;
|
|
46
|
+
export type { IpWhoisResponse, IpWhoisError, IpWhoisOptions } from './types';
|
|
47
|
+
export default ipwhois;
|
|
48
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAgB,cAAc,EAAE,MAAM,SAAS,CAAC;AAIxE;;;;;;;;;;;;;;;GAeG;AACH,wBAAsB,OAAO,CAAC,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,eAAe,CAAC,CA+ChF;AAED;;;;;;;;;;;;GAYG;AACH,wBAAsB,SAAS,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,CAAC,CAEtF;AAED;;;;;;;;;;;GAWG;AACH,wBAAsB,WAAW,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,CAAC,CAE5E;AAGD,YAAY,EAAE,eAAe,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAG7E,eAAe,OAAO,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ipwhois = ipwhois;
|
|
4
|
+
exports.getIpInfo = getIpInfo;
|
|
5
|
+
exports.getMyIpInfo = getMyIpInfo;
|
|
6
|
+
const BASE_URL = 'https://ipwhois.app/json';
|
|
7
|
+
/**
|
|
8
|
+
* Fetches IP geolocation information from ipwhois.app API
|
|
9
|
+
*
|
|
10
|
+
* @param options - Optional configuration
|
|
11
|
+
* @returns Promise resolving to IP geolocation data
|
|
12
|
+
* @throws Error if the API request fails or returns an error
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* // Get info for current IP
|
|
17
|
+
* const info = await ipwhois();
|
|
18
|
+
*
|
|
19
|
+
* // Get info for specific IP
|
|
20
|
+
* const info = await ipwhois({ ip: '8.8.8.8' });
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
async function ipwhois(options) {
|
|
24
|
+
const { ip, timeout = 10000 } = options || {};
|
|
25
|
+
// Build URL
|
|
26
|
+
const url = ip ? `${BASE_URL}/${ip}` : BASE_URL;
|
|
27
|
+
try {
|
|
28
|
+
// Create AbortController for timeout
|
|
29
|
+
const controller = new AbortController();
|
|
30
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
31
|
+
// Make request using native fetch (available in Node 20+)
|
|
32
|
+
const response = await fetch(url, {
|
|
33
|
+
method: 'GET',
|
|
34
|
+
headers: {
|
|
35
|
+
'Accept': 'application/json',
|
|
36
|
+
'User-Agent': 'ipwhois-node-module/1.0.0'
|
|
37
|
+
},
|
|
38
|
+
signal: controller.signal
|
|
39
|
+
});
|
|
40
|
+
clearTimeout(timeoutId);
|
|
41
|
+
// Check if response is ok
|
|
42
|
+
if (!response.ok) {
|
|
43
|
+
throw new Error(`HTTP error! status: ${response.status}`);
|
|
44
|
+
}
|
|
45
|
+
// Parse JSON response
|
|
46
|
+
const data = await response.json();
|
|
47
|
+
// Check if the API returned an error
|
|
48
|
+
if ('success' in data && data.success === false) {
|
|
49
|
+
throw new Error(data.message || 'API returned an error');
|
|
50
|
+
}
|
|
51
|
+
return data;
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
if (error instanceof Error) {
|
|
55
|
+
if (error.name === 'AbortError') {
|
|
56
|
+
throw new Error(`Request timeout after ${timeout}ms`);
|
|
57
|
+
}
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
throw new Error('Unknown error occurred');
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Fetches IP geolocation information for a specific IP address
|
|
65
|
+
*
|
|
66
|
+
* @param ip - IP address to lookup
|
|
67
|
+
* @param timeout - Optional timeout in milliseconds
|
|
68
|
+
* @returns Promise resolving to IP geolocation data
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```typescript
|
|
72
|
+
* const info = await getIpInfo('8.8.8.8');
|
|
73
|
+
* console.log(info.country); // "United States"
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
async function getIpInfo(ip, timeout) {
|
|
77
|
+
return ipwhois({ ip, timeout });
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Fetches IP geolocation information for the caller's IP address
|
|
81
|
+
*
|
|
82
|
+
* @param timeout - Optional timeout in milliseconds
|
|
83
|
+
* @returns Promise resolving to IP geolocation data
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* const info = await getMyIpInfo();
|
|
88
|
+
* console.log(info.ip); // Your IP address
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
async function getMyIpInfo(timeout) {
|
|
92
|
+
return ipwhois({ timeout });
|
|
93
|
+
}
|
|
94
|
+
// Default export
|
|
95
|
+
exports.default = ipwhois;
|
|
96
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAoBA,0BA+CC;AAeD,8BAEC;AAcD,kCAEC;AAlGD,MAAM,QAAQ,GAAG,0BAA0B,CAAC;AAE5C;;;;;;;;;;;;;;;GAeG;AACI,KAAK,UAAU,OAAO,CAAC,OAAwB;IACpD,MAAM,EAAE,EAAE,EAAE,OAAO,GAAG,KAAK,EAAE,GAAG,OAAO,IAAI,EAAE,CAAC;IAE9C,YAAY;IACZ,MAAM,GAAG,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,QAAQ,IAAI,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC;IAEhD,IAAI,CAAC;QACH,qCAAqC;QACrC,MAAM,UAAU,GAAG,IAAI,eAAe,EAAE,CAAC;QACzC,MAAM,SAAS,GAAG,UAAU,CAAC,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,EAAE,OAAO,CAAC,CAAC;QAEhE,0DAA0D;QAC1D,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,EAAE;YAChC,MAAM,EAAE,KAAK;YACb,OAAO,EAAE;gBACP,QAAQ,EAAE,kBAAkB;gBAC5B,YAAY,EAAE,2BAA2B;aAC1C;YACD,MAAM,EAAE,UAAU,CAAC,MAAM;SAC1B,CAAC,CAAC;QAEH,YAAY,CAAC,SAAS,CAAC,CAAC;QAExB,0BAA0B;QAC1B,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;YACjB,MAAM,IAAI,KAAK,CAAC,uBAAuB,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC;QAC5D,CAAC;QAED,sBAAsB;QACtB,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAoC,CAAC;QAErE,qCAAqC;QACrC,IAAI,SAAS,IAAI,IAAI,IAAI,IAAI,CAAC,OAAO,KAAK,KAAK,EAAE,CAAC;YAChD,MAAM,IAAI,KAAK,CAAE,IAAqB,CAAC,OAAO,IAAI,uBAAuB,CAAC,CAAC;QAC7E,CAAC;QAED,OAAO,IAAuB,CAAC;IAEjC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,YAAY,KAAK,EAAE,CAAC;YAC3B,IAAI,KAAK,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;gBAChC,MAAM,IAAI,KAAK,CAAC,yBAAyB,OAAO,IAAI,CAAC,CAAC;YACxD,CAAC;YACD,MAAM,KAAK,CAAC;QACd,CAAC;QACD,MAAM,IAAI,KAAK,CAAC,wBAAwB,CAAC,CAAC;IAC5C,CAAC;AACH,CAAC;AAED;;;;;;;;;;;;GAYG;AACI,KAAK,UAAU,SAAS,CAAC,EAAU,EAAE,OAAgB;IAC1D,OAAO,OAAO,CAAC,EAAE,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC;AAClC,CAAC;AAED;;;;;;;;;;;GAWG;AACI,KAAK,UAAU,WAAW,CAAC,OAAgB;IAChD,OAAO,OAAO,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC;AAC9B,CAAC;AAKD,iBAAiB;AACjB,kBAAe,OAAO,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Response from the ipwhois.app API
|
|
3
|
+
*/
|
|
4
|
+
export interface IpWhoisResponse {
|
|
5
|
+
About_Us: string;
|
|
6
|
+
ip: string;
|
|
7
|
+
success: boolean;
|
|
8
|
+
type: string;
|
|
9
|
+
continent: string;
|
|
10
|
+
continent_code: string;
|
|
11
|
+
country: string;
|
|
12
|
+
country_code: string;
|
|
13
|
+
country_flag: string;
|
|
14
|
+
country_capital: string;
|
|
15
|
+
country_phone: string;
|
|
16
|
+
country_neighbours: string;
|
|
17
|
+
region: string;
|
|
18
|
+
city: string;
|
|
19
|
+
latitude: number;
|
|
20
|
+
longitude: number;
|
|
21
|
+
asn: string;
|
|
22
|
+
org: string;
|
|
23
|
+
isp: string;
|
|
24
|
+
timezone: string;
|
|
25
|
+
timezone_name: string;
|
|
26
|
+
timezone_dstOffset: number;
|
|
27
|
+
timezone_gmtOffset: number;
|
|
28
|
+
timezone_gmt: string;
|
|
29
|
+
currency: string;
|
|
30
|
+
currency_code: string;
|
|
31
|
+
currency_symbol: string;
|
|
32
|
+
currency_rates: number;
|
|
33
|
+
currency_plural: string;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Error response when the API request fails
|
|
37
|
+
*/
|
|
38
|
+
export interface IpWhoisError {
|
|
39
|
+
success: false;
|
|
40
|
+
message: string;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Options for the ipwhois function
|
|
44
|
+
*/
|
|
45
|
+
export interface IpWhoisOptions {
|
|
46
|
+
/**
|
|
47
|
+
* IP address to lookup. If not provided, returns info for the caller's IP
|
|
48
|
+
*/
|
|
49
|
+
ip?: string;
|
|
50
|
+
/**
|
|
51
|
+
* Request timeout in milliseconds (default: 10000)
|
|
52
|
+
*/
|
|
53
|
+
timeout?: number;
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,QAAQ,EAAE,MAAM,CAAC;IACjB,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,OAAO,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;IAClB,cAAc,EAAE,MAAM,CAAC;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,YAAY,EAAE,MAAM,CAAC;IACrB,YAAY,EAAE,MAAM,CAAC;IACrB,eAAe,EAAE,MAAM,CAAC;IACxB,aAAa,EAAE,MAAM,CAAC;IACtB,kBAAkB,EAAE,MAAM,CAAC;IAC3B,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;IAClB,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,EAAE,MAAM,CAAC;IACjB,aAAa,EAAE,MAAM,CAAC;IACtB,kBAAkB,EAAE,MAAM,CAAC;IAC3B,kBAAkB,EAAE,MAAM,CAAC;IAC3B,YAAY,EAAE,MAAM,CAAC;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,aAAa,EAAE,MAAM,CAAC;IACtB,eAAe,EAAE,MAAM,CAAC;IACxB,cAAc,EAAE,MAAM,CAAC;IACvB,eAAe,EAAE,MAAM,CAAC;CACzB;AAED;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,OAAO,EAAE,KAAK,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B;;OAEG;IACH,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ipwhois",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A Node.js module to fetch IP geolocation information from ipwhois.app API (not official).",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"dev": "tsc --watch",
|
|
10
|
+
"test": "node test/example.js"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"ip",
|
|
14
|
+
"geolocation",
|
|
15
|
+
"ipwhois",
|
|
16
|
+
"ip-lookup",
|
|
17
|
+
"geoip"
|
|
18
|
+
],
|
|
19
|
+
"author": "",
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">=22.0.0"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/node": "^22.10.5",
|
|
26
|
+
"typescript": "^5.7.3"
|
|
27
|
+
}
|
|
28
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { IpWhoisResponse, IpWhoisError, IpWhoisOptions } from "./types";
|
|
2
|
+
|
|
3
|
+
const BASE_URL = "https://ipwhois.app/json";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Fetches IP geolocation information from ipwhois.app API
|
|
7
|
+
*
|
|
8
|
+
* @param options - Optional configuration
|
|
9
|
+
* @returns Promise resolving to IP geolocation data
|
|
10
|
+
* @throws Error if the API request fails or returns an error
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* // Get info for current IP
|
|
15
|
+
* const info = await ipwhois();
|
|
16
|
+
*
|
|
17
|
+
* // Get info for specific IP
|
|
18
|
+
* const info = await ipwhois({ ip: '8.8.8.8' });
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export async function ipwhois(
|
|
22
|
+
options?: IpWhoisOptions
|
|
23
|
+
): Promise<IpWhoisResponse> {
|
|
24
|
+
const { ip, timeout = 10000 } = options || {};
|
|
25
|
+
|
|
26
|
+
// Build URL
|
|
27
|
+
const url = ip ? `${BASE_URL}/${ip}` : BASE_URL;
|
|
28
|
+
|
|
29
|
+
try {
|
|
30
|
+
// Create AbortController for timeout
|
|
31
|
+
const controller = new AbortController();
|
|
32
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
33
|
+
|
|
34
|
+
// Make request using native fetch (available in Node 20+)
|
|
35
|
+
const response = await fetch(url, {
|
|
36
|
+
method: "GET",
|
|
37
|
+
headers: {
|
|
38
|
+
Accept: "application/json",
|
|
39
|
+
"User-Agent": "ipwhois-node-module/1.0.0",
|
|
40
|
+
},
|
|
41
|
+
signal: controller.signal,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
clearTimeout(timeoutId);
|
|
45
|
+
|
|
46
|
+
// Check if response is ok
|
|
47
|
+
if (!response.ok) {
|
|
48
|
+
throw new Error(`HTTP error! status: ${response.status}`);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Parse JSON response
|
|
52
|
+
const data = (await response.json()) as IpWhoisResponse | IpWhoisError;
|
|
53
|
+
|
|
54
|
+
// Check if the API returned an error
|
|
55
|
+
if ("success" in data && data.success === false) {
|
|
56
|
+
throw new Error(
|
|
57
|
+
(data as IpWhoisError).message || "API returned an error"
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return data as IpWhoisResponse;
|
|
62
|
+
} catch (error) {
|
|
63
|
+
if (error instanceof Error) {
|
|
64
|
+
if (error.name === "AbortError") {
|
|
65
|
+
throw new Error(`Request timeout after ${timeout}ms`);
|
|
66
|
+
}
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
throw new Error("Unknown error occurred");
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Fetches IP geolocation information for a specific IP address
|
|
75
|
+
*
|
|
76
|
+
* @param ip - IP address to lookup
|
|
77
|
+
* @param timeout - Optional timeout in milliseconds
|
|
78
|
+
* @returns Promise resolving to IP geolocation data
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```typescript
|
|
82
|
+
* const info = await getIpInfo('8.8.8.8');
|
|
83
|
+
* console.log(info.country); // "United States"
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
export async function getIpInfo(
|
|
87
|
+
ip: string,
|
|
88
|
+
timeout?: number
|
|
89
|
+
): Promise<IpWhoisResponse> {
|
|
90
|
+
return ipwhois({ ip, timeout });
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Fetches IP geolocation information for the caller's IP address
|
|
95
|
+
*
|
|
96
|
+
* @param timeout - Optional timeout in milliseconds
|
|
97
|
+
* @returns Promise resolving to IP geolocation data
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```typescript
|
|
101
|
+
* const info = await getMyIpInfo();
|
|
102
|
+
* console.log(info.ip); // Your IP address
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
export async function getMyIpInfo(timeout?: number): Promise<IpWhoisResponse> {
|
|
106
|
+
return ipwhois({ timeout });
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Export types
|
|
110
|
+
export type { IpWhoisResponse, IpWhoisError, IpWhoisOptions } from "./types";
|
|
111
|
+
|
|
112
|
+
// Default export
|
|
113
|
+
export default ipwhois;
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Response from the ipwhois.app API
|
|
3
|
+
*/
|
|
4
|
+
export interface IpWhoisResponse {
|
|
5
|
+
About_Us: string;
|
|
6
|
+
ip: string;
|
|
7
|
+
success: boolean;
|
|
8
|
+
type: string;
|
|
9
|
+
continent: string;
|
|
10
|
+
continent_code: string;
|
|
11
|
+
country: string;
|
|
12
|
+
country_code: string;
|
|
13
|
+
country_flag: string;
|
|
14
|
+
country_capital: string;
|
|
15
|
+
country_phone: string;
|
|
16
|
+
country_neighbours: string;
|
|
17
|
+
region: string;
|
|
18
|
+
city: string;
|
|
19
|
+
latitude: number;
|
|
20
|
+
longitude: number;
|
|
21
|
+
asn: string;
|
|
22
|
+
org: string;
|
|
23
|
+
isp: string;
|
|
24
|
+
timezone: string;
|
|
25
|
+
timezone_name: string;
|
|
26
|
+
timezone_dstOffset: number;
|
|
27
|
+
timezone_gmtOffset: number;
|
|
28
|
+
timezone_gmt: string;
|
|
29
|
+
currency: string;
|
|
30
|
+
currency_code: string;
|
|
31
|
+
currency_symbol: string;
|
|
32
|
+
currency_rates: number;
|
|
33
|
+
currency_plural: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Error response when the API request fails
|
|
38
|
+
*/
|
|
39
|
+
export interface IpWhoisError {
|
|
40
|
+
success: false;
|
|
41
|
+
message: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Options for the ipwhois function
|
|
46
|
+
*/
|
|
47
|
+
export interface IpWhoisOptions {
|
|
48
|
+
/**
|
|
49
|
+
* IP address to lookup. If not provided, returns info for the caller's IP
|
|
50
|
+
*/
|
|
51
|
+
ip?: string;
|
|
52
|
+
/**
|
|
53
|
+
* Request timeout in milliseconds (default: 10000)
|
|
54
|
+
*/
|
|
55
|
+
timeout?: number;
|
|
56
|
+
}
|
package/test/example.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const { ipwhois, getIpInfo, getMyIpInfo } = require('../dist/index');
|
|
2
|
+
|
|
3
|
+
async function runExamples() {
|
|
4
|
+
console.log('=== IP Whois Module Examples ===\n');
|
|
5
|
+
|
|
6
|
+
try {
|
|
7
|
+
// Example 1: Get info for current IP
|
|
8
|
+
console.log('1. Getting info for your current IP...');
|
|
9
|
+
const myInfo = await getMyIpInfo();
|
|
10
|
+
console.log(` IP: ${myInfo.ip}`);
|
|
11
|
+
console.log(` Country: ${myInfo.country} (${myInfo.country_code})`);
|
|
12
|
+
console.log(` City: ${myInfo.city}`);
|
|
13
|
+
console.log(` ISP: ${myInfo.isp}`);
|
|
14
|
+
console.log(` Timezone: ${myInfo.timezone}\n`);
|
|
15
|
+
|
|
16
|
+
// Example 2: Get info for a specific IP (Google DNS)
|
|
17
|
+
console.log('2. Getting info for Google DNS (8.8.8.8)...');
|
|
18
|
+
const googleInfo = await getIpInfo('8.8.8.8');
|
|
19
|
+
console.log(` IP: ${googleInfo.ip}`);
|
|
20
|
+
console.log(` Country: ${googleInfo.country} (${googleInfo.country_code})`);
|
|
21
|
+
console.log(` City: ${googleInfo.city}`);
|
|
22
|
+
console.log(` Organization: ${googleInfo.org}`);
|
|
23
|
+
console.log(` Timezone: ${googleInfo.timezone}\n`);
|
|
24
|
+
|
|
25
|
+
// Example 3: Using main function with options
|
|
26
|
+
console.log('3. Getting info for Cloudflare DNS (1.1.1.1)...');
|
|
27
|
+
const cloudflareInfo = await ipwhois({ ip: '1.1.1.1', timeout: 5000 });
|
|
28
|
+
console.log(` IP: ${cloudflareInfo.ip}`);
|
|
29
|
+
console.log(` Country: ${cloudflareInfo.country} (${cloudflareInfo.country_code})`);
|
|
30
|
+
console.log(` City: ${cloudflareInfo.city}`);
|
|
31
|
+
console.log(` Latitude: ${cloudflareInfo.latitude}`);
|
|
32
|
+
console.log(` Longitude: ${cloudflareInfo.longitude}\n`);
|
|
33
|
+
|
|
34
|
+
console.log('✅ All examples completed successfully!');
|
|
35
|
+
|
|
36
|
+
} catch (error) {
|
|
37
|
+
console.error('❌ Error:', error.message);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
runExamples();
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "commonjs",
|
|
5
|
+
"lib": ["ES2022"],
|
|
6
|
+
"outDir": "./dist",
|
|
7
|
+
"rootDir": "./src",
|
|
8
|
+
"declaration": true,
|
|
9
|
+
"declarationMap": true,
|
|
10
|
+
"sourceMap": true,
|
|
11
|
+
"strict": true,
|
|
12
|
+
"esModuleInterop": true,
|
|
13
|
+
"skipLibCheck": true,
|
|
14
|
+
"forceConsistentCasingInFileNames": true,
|
|
15
|
+
"moduleResolution": "node",
|
|
16
|
+
"resolveJsonModule": true
|
|
17
|
+
},
|
|
18
|
+
"include": ["src/**/*"],
|
|
19
|
+
"exclude": ["node_modules", "dist", "test"]
|
|
20
|
+
}
|