Skip to content

Commit 2be1bc1

Browse files
authored
Merge pull request #87 from Shoaib-Ashfaq/shoaib/support-for-configurable-data-maps
Shoaib/support for configurable data maps
2 parents 35f45b5 + d2e3c64 commit 2be1bc1

File tree

6 files changed

+137
-41
lines changed

6 files changed

+137
-41
lines changed

README.md

Lines changed: 84 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,19 +166,56 @@ ipinfo.lookupIp("1.1.1.1").then((response) => {
166166
});
167167
```
168168

169-
### Country Name Lookup
169+
### Internationalization
170170

171-
`response.country` will return the country name, whereas `response.countryCode` can be used to fetch the country code.
172-
173-
Additionally `response.isEU` will return `true` if the country is a member of the European Union (EU), `response.countryFlag`
174-
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.
171+
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.
175172

176173
##### TypeScript
177174

178175
```typescript
179176
import IPinfoWrapper, { IPinfo } from "node-ipinfo";
180177

181-
const ipinfoWrapper = new IPinfoWrapper("MY_TOKEN");
178+
const countries = {
179+
"US": "United States",
180+
"FR": "France",
181+
"BD": "Bangladesh",
182+
...
183+
}
184+
185+
const countriesFlags = {
186+
"US": {"emoji": "🇺🇸","unicode": "U+1F1FA U+1F1F8"},
187+
"AD": {"emoji": "🇦🇩", "unicode": "U+1F1E6 U+1F1E9"},
188+
"AE": {"emoji": "🇦🇪", "unicode": "U+1F1E6 U+1F1EA"},
189+
...
190+
}
191+
192+
const countriesCurrencies = {
193+
"US" : { "code": "USD" ,"symbol": "$"},
194+
"AD": {"code": "EUR", "symbol": ""},
195+
"AE": {"code": "AED", "symbol": "د.إ"},
196+
...
197+
}
198+
199+
const continents = {
200+
"US": {"code": "NA", "name": "North America"},
201+
"BD": {"code": "AS", "name": "Asia"},
202+
"BE": {"code": "EU", "name": "Europe"},
203+
...
204+
}
205+
206+
const euCountries = ["FR","ES","BE", ...]
207+
208+
const ipinfoWrapper = new IPinfoWrapper(
209+
"MY_TOKEN",
210+
undefined,
211+
undefined,
212+
{
213+
countries: countries,
214+
countriesFlags: countriesFlags,
215+
countriesCurrencies: countriesCurrencies,
216+
...
217+
}
218+
);
182219

183220
ipinfoWrapper.lookupIp("1.1.1.1").then((response: IPinfo) => {
184221
// country code, e.g. 'US'
@@ -209,7 +246,47 @@ ipinfoWrapper.lookupIp("1.1.1.1").then((response: IPinfo) => {
209246
```javascript
210247
const { IPinfoWrapper } = require("node-ipinfo");
211248

212-
const ipinfo = new IPinfoWrapper("MY_TOKEN");
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+
);
213290

214291
ipinfo.lookupIp("1.1.1.1").then((response) => {
215292
// country code, e.g. 'US'

__tests__/ipinfoWrapper.test.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,13 @@ describe("IPinfoWrapper", () => {
109109
expect(data["8.8.8.8/hostname"]).toEqual("dns.google");
110110
expect(data["4.4.4.4"]).toEqual({
111111
ip: "4.4.4.4",
112-
city: "Paris",
113-
region: "Île-de-France",
114-
country: "France",
115-
loc: "48.8534,2.3488",
112+
city: "Honolulu",
113+
region: "Hawaii",
114+
country: "United States",
115+
loc: "21.3179,-157.8521",
116116
org: "AS3356 Level 3 Parent, LLC",
117-
postal: "75000",
118-
timezone: "Europe/Paris",
117+
postal: "96813",
118+
timezone: "Pacific/Honolulu",
119119
asn: {
120120
asn: "AS3356",
121121
name: "Level 3 Parent, LLC",
@@ -147,16 +147,16 @@ describe("IPinfoWrapper", () => {
147147
},
148148
domains: {
149149
ip: "4.4.4.4",
150-
total: 124,
150+
total: 110,
151151
domains: [
152-
"clearcloud.ru",
153-
"ddosxtesting.co.uk",
154-
"fleetzy.com",
155-
"datacenter-team.eu",
156-
"datacenterteam.net",
152+
"dyyunyou.com",
153+
"itmg.ch",
154+
"cloudmaas.net",
155+
"datacenter-team.de",
156+
"fisiodiagnosticaragionieri.it",
157157
]
158158
},
159-
countryCode: "FR"
159+
countryCode: "US"
160160
});
161161

162162
expect(data["AS123"]).toEqual({

config/utils.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const continents = {
1+
export const defaultContinents = {
22
"BD": {"code": "AS", "name": "Asia"},
33
"BE": {"code": "EU", "name": "Europe"},
44
"BF": {"code": "AF", "name": "Africa"},
@@ -249,9 +249,9 @@ export const continents = {
249249
"UA": {"code": "EU", "name": "Europe"},
250250
"QA": {"code": "AS", "name": "Asia"},
251251
"MZ": {"code": "AF", "name": "Africa"}
252-
}
252+
}
253253

254-
export const countriesCurrencies = {
254+
export const defaultCountriesCurrencies = {
255255
"AD" : { "code": "EUR" ,"symbol": "€"},
256256
"AE" : { "code": "AED" ,"symbol": "د.إ"},
257257
"AF" : { "code": "AFN" ,"symbol": "؋"},
@@ -502,9 +502,9 @@ export const countriesCurrencies = {
502502
"ZA" : { "code": "ZAR" ,"symbol": "R"},
503503
"ZM" : { "code": "ZMK" ,"symbol": "ZK"},
504504
"ZW" : { "code": "ZWL" ,"symbol": "$"}
505-
}
505+
}
506506

507-
export const countries = {
507+
export const defaultCountries = {
508508
"BD": "Bangladesh",
509509
"BE": "Belgium",
510510
"BF": "Burkina Faso",
@@ -757,7 +757,7 @@ export const countries = {
757757
"MZ": "Mozambique"
758758
}
759759

760-
export const countriesFlags = {
760+
export const defaultCountriesFlags = {
761761
"AD": {"emoji": "🇦🇩","unicode": "U+1F1E6 U+1F1E9"},
762762
"AE": {"emoji": "🇦🇪","unicode": "U+1F1E6 U+1F1EA"},
763763
"AF": {"emoji": "🇦🇫","unicode": "U+1F1E6 U+1F1EB"},
@@ -1010,4 +1010,4 @@ export const countriesFlags = {
10101010
"ZW": {"emoji": "🇿🇼","unicode": "U+1F1FF U+1F1FC"}
10111011
}
10121012

1013-
export const euCountries = ["IE","AT","LT","LU","LV","DE","DK","SE","SI","SK","CZ","CY","NL","FI","FR","MT","ES","IT","EE","PL","PT","HU","HR","GR","RO","BG","BE"]
1013+
export const defaultEuCountries = ["IE","AT","LT","LU","LV","DE","DK","SE","SI","SK","CZ","CY","NL","FI","FR","MT","ES","IT","EE","PL","PT","HU","HR","GR","RO","BG","BE"]

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-ipinfo",
3-
"version": "3.5.1",
3+
"version": "3.5.2",
44
"description": "Official Node client library for IPinfo",
55
"main": "dist/src/index.js",
66
"types": "dist/src/index.d.ts",

src/ipinfoWrapper.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { IncomingMessage } from "http";
22
import https, { RequestOptions } from "https";
33
import {
4-
continents,
5-
countriesCurrencies,
6-
countriesFlags,
7-
countries,
8-
euCountries
4+
defaultContinents,
5+
defaultCountriesCurrencies,
6+
defaultCountriesFlags,
7+
defaultCountries,
8+
defaultEuCountries
99
} from "../config/utils";
1010
import Cache from "./cache/cache";
1111
import LruCache from "./cache/lruCache";
@@ -48,14 +48,33 @@ export default class IPinfoWrapper {
4848
* then LruCache is used as default.
4949
* @param timeout Timeout in milliseconds that controls the timeout of requests.
5050
* It defaults to 5000 i.e. 5 seconds. A timeout of 0 disables the timeout feature.
51+
* @param i18nData Internationalization data for customizing countries-related information.
52+
* @param i18nData.countries Custom countries data. If not provided, default countries data will be used.
53+
* @param i18nData.countriesFlags Custom countries flags data. If not provided, default countries flags data will be used.
54+
* @param i18nData.countriesCurrencies Custom countries currencies data. If not provided, default countries currencies data will be used.
55+
* @param i18nData.continents Custom continents data. If not provided, default continents data will be used.
56+
* @param i18nData.euCountries Custom EU countries data. If not provided or an empty array, default EU countries data will be used.
5157
*/
52-
constructor(token: string, cache?: Cache, timeout?: number) {
58+
constructor(
59+
token: string,
60+
cache?: Cache,
61+
timeout?: number,
62+
i18nData?: {
63+
countries?: any,
64+
countriesFlags?: any,
65+
countriesCurrencies?: any,
66+
continents?: any,
67+
euCountries?: Array<string>,
68+
}
69+
) {
5370
this.token = token;
54-
this.countries = countries;
55-
this.countriesFlags = countriesFlags;
56-
this.countriesCurrencies = countriesCurrencies;
57-
this.continents = continents;
58-
this.euCountries = euCountries;
71+
this.countries = i18nData?.countries ? i18nData.countries : defaultCountries;
72+
this.countriesFlags = i18nData?.countriesFlags ? i18nData.countriesFlags: defaultCountriesFlags;
73+
this.countriesCurrencies = i18nData?.countriesCurrencies ? i18nData.countriesCurrencies: defaultCountriesCurrencies;
74+
this.continents = i18nData?.continents ? i18nData.continents : defaultContinents;
75+
this.euCountries =
76+
i18nData?.euCountries && i18nData?.euCountries.length !== 0
77+
? i18nData.euCountries : defaultEuCountries;
5978
this.cache = cache ? cache : new LruCache();
6079
this.timeout =
6180
timeout === null || timeout === undefined

0 commit comments

Comments
 (0)