greip.js 2.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/LICENSE +21 -0
- package/README.md +88 -0
- package/gre-geoip.js +184 -0
- package/index.js +280 -0
- package/main.js +8 -0
- package/package.json +38 -0
- package/src/util.js +33 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 GRE Development Ltd.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<h1>Greip Javascript Library</h1>
|
|
3
|
+
<p>The official JS library of Greip API</p>
|
|
4
|
+
<br />
|
|
5
|
+
<a href="https://github.com/gre-dev/GeoIP-JS/issues/new">Report Issue</a> ·
|
|
6
|
+
<a href="https://github.com/gre-dev/GeoIP-JS/discussions/new">Request Feature</a> ·
|
|
7
|
+
<a href="https://greip.io" target="_BLANK">Home Page</a> ·
|
|
8
|
+
<a href="https://docs.greip.io/sdks/js" target="_BLANK">API Docs</a>
|
|
9
|
+
<br />
|
|
10
|
+
<br />
|
|
11
|
+
<a href="https://www.npmjs.com/package/gre-geoip" title="NPM Package" href="_BLANK"><img src="https://img.shields.io/badge/npm-CB3837?style=for-the-badge&logo=npm&logoColor=white"></a>
|
|
12
|
+
<a href="https://github.com/gre-dev/Greip-JS" title="Github Repo" href="_BLANK"><img src="https://img.shields.io/badge/GitHub-100000?style=for-the-badge&logo=github&logoColor=white"></a>
|
|
13
|
+
<a href="https://www.patreon.com/gredev" title="Patreon Profile - GRE Development Ltd." href="_BLANK"><img src="https://img.shields.io/badge/Patreon-ff424e?style=for-the-badge&logo=patreon&logoColor=white"></a>
|
|
14
|
+
<img src="https://img.shields.io/badge/JavaScript-323330?style=for-the-badge&logo=javascript&logoColor=F7DF1E" title="Javascript">
|
|
15
|
+
</div>
|
|
16
|
+
<br />
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
<br />
|
|
20
|
+
|
|
21
|
+
[](https://badge.fury.io/js/gre-geoip)
|
|
22
|
+
|
|
23
|
+

|
|
24
|
+
|
|
25
|
+
[](https://opensource.org/licenses/MIT)
|
|
26
|
+
|
|
27
|
+

|
|
28
|
+
<br /><br />
|
|
29
|
+
|
|
30
|
+
# Requirements
|
|
31
|
+
No requirements for using this package.
|
|
32
|
+
<br /><br />
|
|
33
|
+
|
|
34
|
+
# Installation
|
|
35
|
+
For Node.js, React.js & React Native:
|
|
36
|
+
```
|
|
37
|
+
npm i gre-geoip --save
|
|
38
|
+
```
|
|
39
|
+
or
|
|
40
|
+
```
|
|
41
|
+
yarn add gre-geoip
|
|
42
|
+
```
|
|
43
|
+
<br />
|
|
44
|
+
|
|
45
|
+
For Browser:
|
|
46
|
+
```html
|
|
47
|
+
<script src="https://cdn.jsdelivr.net/gh/gre-dev/GeoIP-JS@latest/gre-geoip.min.js"></script>
|
|
48
|
+
```
|
|
49
|
+
<br /><br />
|
|
50
|
+
|
|
51
|
+
# Usage
|
|
52
|
+
Let's say that we want to get the visitor IP Address. So we do the following:
|
|
53
|
+
<br /><br />
|
|
54
|
+
|
|
55
|
+
For Node.js, React.js & React Native:
|
|
56
|
+
```javascript
|
|
57
|
+
const GREGeoIP = require('gre-geoip');
|
|
58
|
+
const GeoIP = new GREGeoIP('<API-Key>');
|
|
59
|
+
|
|
60
|
+
GeoIP.geoip({}).then(res => {
|
|
61
|
+
console.log(res?.data?.ip);
|
|
62
|
+
});
|
|
63
|
+
```
|
|
64
|
+
<br />
|
|
65
|
+
|
|
66
|
+
For Browser:
|
|
67
|
+
```html
|
|
68
|
+
<script>
|
|
69
|
+
const GeoIP = new GREGeoIP('<API-Key>');
|
|
70
|
+
|
|
71
|
+
GeoIP.geoip({}).then(res => {
|
|
72
|
+
console.log(res?.data?.ip);
|
|
73
|
+
});
|
|
74
|
+
</script>
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
<br /><br />
|
|
78
|
+
# Options, Methods and More
|
|
79
|
+
You can find the full guide of this package by visiting our [Documentation Page](https://geoip-docs.gredev.io/sdks/js).
|
|
80
|
+
|
|
81
|
+
<br /><br />
|
|
82
|
+
# Credits
|
|
83
|
+
* [GRE Development Ltd.](https://www.gredev.io/en/)
|
|
84
|
+
* [All Contributors](https://github.com/gre-dev/Greip-JS/graphs/contributors)
|
|
85
|
+
|
|
86
|
+
<br /><br />
|
|
87
|
+
# License
|
|
88
|
+
The MIT License (MIT). Please see [License](https://github.com/gre-dev/GeoIP-JS/blob/main/LICENSE) File for more information.
|
package/gre-geoip.js
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
class GREGeoIP {
|
|
2
|
+
#key;
|
|
3
|
+
#baseURL = 'https://gregeoip.com/';
|
|
4
|
+
#availableGeoIPParams = ['location', 'security', 'timezone', 'currency', 'device'];
|
|
5
|
+
#availableLanguages = ['EN', 'AR', 'DE', 'FR', 'ES', 'JA', 'ZH', 'RU'];
|
|
6
|
+
#availableFormats = ['JSON', 'XML', 'CSV', 'Newline'];
|
|
7
|
+
#availableCountryParams = ['language', 'flag', 'currency', 'timezone'];
|
|
8
|
+
|
|
9
|
+
constructor(key) {
|
|
10
|
+
if (key && key.length > 0){
|
|
11
|
+
this.#key = key;
|
|
12
|
+
}else{
|
|
13
|
+
throw new Error('You should pass the API Key.');
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
#serialize = function (obj) {
|
|
18
|
+
var str = [];
|
|
19
|
+
for (var p in obj)
|
|
20
|
+
if (obj.hasOwnProperty(p)) {
|
|
21
|
+
str.push(encodeURIComponent(p) + '=' + encodeURIComponent(obj[p]));
|
|
22
|
+
}
|
|
23
|
+
return str.join('&');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
#makeHttpRquest(endpoint, options, callback) {
|
|
27
|
+
options.source = 'JS-Package';
|
|
28
|
+
let xmlHttp = new XMLHttpRequest();
|
|
29
|
+
xmlHttp.onreadystatechange = function () {
|
|
30
|
+
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
|
|
31
|
+
callback(xmlHttp.responseText);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
xmlHttp.open('GET', this.#baseURL + '/' + endpoint + '?' + this.#serialize(options), true); // true for asynchronous
|
|
35
|
+
xmlHttp.send(null);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
geoip(options = {}) {
|
|
39
|
+
return new Promise((resolve, reject) => {
|
|
40
|
+
let params = options.params || [];
|
|
41
|
+
let format = options.format || 'JSON';
|
|
42
|
+
let lang = options.lang || 'EN';
|
|
43
|
+
let mode = options.mode || 'live';
|
|
44
|
+
lang = lang.toUpperCase();
|
|
45
|
+
|
|
46
|
+
// Validate the params variable items
|
|
47
|
+
params.forEach(perParam => {
|
|
48
|
+
if (perParam.length > 0) {
|
|
49
|
+
if (!this.#availableGeoIPParams.includes(perParam)) {
|
|
50
|
+
reject(new Error('The "' + perParam + '" module you used is unknown.\nYou can use: `location`, `security`, `timezone`, `currency` and/or `device`.\nRead more at: https://geoip-docs.gredev.io/sdks/js/geoip-method#options'));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// Validate the format variable
|
|
56
|
+
if (!this.#availableFormats.includes(format)) {
|
|
57
|
+
reject(new Error('The `format` option value "' + lang + '" you specified is unknown.\nYou can use: `JSON`, `XML`, `CSV` or `Newline`.\nRead more at: https://geoip-docs.gredev.io/sdks/js/geoip-method#options'));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Validate the lang variable
|
|
61
|
+
if (!this.#availableLanguages.includes(lang)) {
|
|
62
|
+
reject(new Error('The `lang` option value "' + lang + '" you specified is unknown.\nYou can use: `EN`, `AR`, `DE`, `FR`, `ES`, `JA`, `ZH` or `RU`.\nRead more at: https://geoip-docs.gredev.io/sdks/js/geoip-method#options'));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Validate the mode variable
|
|
66
|
+
if (mode !== 'live' && mode !== 'test') {
|
|
67
|
+
reject(new Error('The `mode` option value "' + lang + '" you specified is unknown.\nYou can use: `live` or `test`.\nRead more at: https://geoip-docs.gredev.io/sdks/js/geoip-method#options'));
|
|
68
|
+
}
|
|
69
|
+
this.#makeHttpRquest('GeoIP', {
|
|
70
|
+
'key': this.#key,
|
|
71
|
+
'params': params.join(','),
|
|
72
|
+
'format': format,
|
|
73
|
+
'lang': lang,
|
|
74
|
+
'mode': mode
|
|
75
|
+
}, (res) => {
|
|
76
|
+
if (typeof res !== 'object') res = JSON.parse(res);
|
|
77
|
+
resolve(res);
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
lookup(options = {}) {
|
|
83
|
+
return new Promise((resolve, reject) => {
|
|
84
|
+
let ip = options.ip || '';
|
|
85
|
+
let params = options.params || [];
|
|
86
|
+
let format = options.format || 'JSON';
|
|
87
|
+
let lang = options.lang || 'EN';
|
|
88
|
+
let mode = options.mode || 'live';
|
|
89
|
+
lang = lang.toUpperCase();
|
|
90
|
+
|
|
91
|
+
// Validate the ip variable
|
|
92
|
+
if (ip.length < 7){
|
|
93
|
+
reject(new Error('You should pass the `ip` parameter.'))
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Validate the params variable items
|
|
97
|
+
params.forEach(perParam => {
|
|
98
|
+
if (perParam.length > 0) {
|
|
99
|
+
if (!this.#availableGeoIPParams.includes(perParam)) {
|
|
100
|
+
reject(new Error('The "' + perParam + '" module you used is unknown.\nYou can use: `location`, `security`, `timezone`, `currency` and/or `device`.\nRead more at: https://geoip-docs.gredev.io/sdks/js/lookup-method#options'));
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// Validate the format variable
|
|
106
|
+
if (!this.#availableFormats.includes(format)) {
|
|
107
|
+
reject(new Error('The `format` option value "' + lang + '" you specified is unknown.\nYou can use: `JSON`, `XML`, `CSV` or `Newline`.\nRead more at: https://geoip-docs.gredev.io/sdks/js/lookup-method#options'));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Validate the lang variable
|
|
111
|
+
if (!this.#availableLanguages.includes(lang)) {
|
|
112
|
+
reject(new Error('The `lang` option value "' + lang + '" you specified is unknown.\nYou can use: `EN`, `AR`, `DE`, `FR`, `ES`, `JA`, `ZH` or `RU`.\nRead more at: https://geoip-docs.gredev.io/sdks/js/lookup-method#options'));
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Validate the mode variable
|
|
116
|
+
if (mode !== 'live' && mode !== 'test') {
|
|
117
|
+
reject(new Error('The `mode` option value "' + lang + '" you specified is unknown.\nYou can use: `live` or `test`.\nRead more at: https://geoip-docs.gredev.io/sdks/js/lookup-method#options'));
|
|
118
|
+
}
|
|
119
|
+
this.#makeHttpRquest('IPLookup', {
|
|
120
|
+
'ip': ip,
|
|
121
|
+
'key': this.#key,
|
|
122
|
+
'params': params.join(','),
|
|
123
|
+
'format': format,
|
|
124
|
+
'lang': lang,
|
|
125
|
+
'mode': mode
|
|
126
|
+
}, (res) => {
|
|
127
|
+
if (typeof res !== 'object') res = JSON.parse(res);
|
|
128
|
+
resolve(res);
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
country(options = {}) {
|
|
134
|
+
return new Promise((resolve, reject) => {
|
|
135
|
+
let countryCode = options.countryCode || '';
|
|
136
|
+
let params = options.params || [];
|
|
137
|
+
let format = options.format || 'JSON';
|
|
138
|
+
let lang = options.lang || 'EN';
|
|
139
|
+
let mode = options.mode || 'live';
|
|
140
|
+
countryCode = countryCode.toUpperCase();
|
|
141
|
+
lang = lang.toUpperCase();
|
|
142
|
+
|
|
143
|
+
// Validate the countryCode variable
|
|
144
|
+
if (countryCode.length !== 2){
|
|
145
|
+
reject(new Error('You should pass the `countryCode` parameter. Also, it should be a `ISO 3166-1 alpha-2` format.\nRead more at: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2'));
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Validate the params variable items
|
|
149
|
+
params.forEach(perParam => {
|
|
150
|
+
if (perParam.length > 0) {
|
|
151
|
+
if (!this.#availableCountryParams.includes(perParam)) {
|
|
152
|
+
reject(new Error('The "' + perParam + '" module you used is unknown.\nYou can use: `language`, `flag`, `currency` and/or `timezone`.\nRead more at: https://geoip-docs.gredev.io/sdks/js/country-method#options'));
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
// Validate the format variable
|
|
158
|
+
if (!this.#availableFormats.includes(format)) {
|
|
159
|
+
reject(new Error('The `format` option value "' + lang + '" you specified is unknown.\nYou can use: `JSON`, `XML`, `CSV` or `Newline`.\nRead more at: https://geoip-docs.gredev.io/sdks/js/country-method#options'));
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Validate the lang variable
|
|
163
|
+
if (!this.#availableLanguages.includes(lang)) {
|
|
164
|
+
reject(new Error('The `lang` option value "' + lang + '" you specified is unknown.\nYou can use: `EN`, `AR`, `DE`, `FR`, `ES`, `JA`, `ZH` or `RU`.\nRead more at: https://geoip-docs.gredev.io/sdks/js/country-method#options'));
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Validate the mode variable
|
|
168
|
+
if (mode !== 'live' && mode !== 'test') {
|
|
169
|
+
reject(new Error('The `mode` option value "' + lang + '" you specified is unknown.\nYou can use: `live` or `test`.\nRead more at: https://geoip-docs.gredev.io/sdks/js/country-method#options'));
|
|
170
|
+
}
|
|
171
|
+
this.#makeHttpRquest('Country', {
|
|
172
|
+
'CountryCode': countryCode,
|
|
173
|
+
'key': this.#key,
|
|
174
|
+
'params': params.join(','),
|
|
175
|
+
'format': format,
|
|
176
|
+
'lang': lang,
|
|
177
|
+
'mode': mode
|
|
178
|
+
}, (res) => {
|
|
179
|
+
if (typeof res !== 'object') res = JSON.parse(res);
|
|
180
|
+
resolve(res);
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
};
|
package/index.js
ADDED
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { availableGeoIPParams, availableLanguages, availableFormats, availableCountryParams, makeHttpRquest } from "./src/util.js";
|
|
2
|
+
|
|
3
|
+
export function GeoIP(options) {
|
|
4
|
+
if (typeof options !== "object") options = {};
|
|
5
|
+
|
|
6
|
+
if (!options.key || options.key.length < 1) {
|
|
7
|
+
throw new Error('You should pass the API Key.');
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return new Promise((resolve, reject) => {
|
|
11
|
+
let params = options.params || [];
|
|
12
|
+
let format = options.format || 'JSON';
|
|
13
|
+
let lang = options.lang || 'EN';
|
|
14
|
+
let mode = options.mode || 'live';
|
|
15
|
+
lang = lang.toUpperCase();
|
|
16
|
+
|
|
17
|
+
// Validate the params variable items
|
|
18
|
+
params.forEach(perParam => {
|
|
19
|
+
if (perParam.length > 0) {
|
|
20
|
+
if (!availableGeoIPParams.includes(perParam)) {
|
|
21
|
+
reject(new Error('The "' + perParam + '" module you used is unknown.\nYou can use: `location`, `security`, `timezone`, `currency` and/or `device`.\nRead more at: https://geoip-docs.gredev.io/sdks/js/geoip-method#options'));
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// Validate the format variable
|
|
27
|
+
if (!availableFormats.includes(format)) {
|
|
28
|
+
reject(new Error('The `format` option value "' + lang + '" you specified is unknown.\nYou can use: `JSON`, `XML`, `CSV` or `Newline`.\nRead more at: https://geoip-docs.gredev.io/sdks/js/geoip-method#options'));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Validate the lang variable
|
|
32
|
+
if (!availableLanguages.includes(lang)) {
|
|
33
|
+
reject(new Error('The `lang` option value "' + lang + '" you specified is unknown.\nYou can use: `EN`, `AR`, `DE`, `FR`, `ES`, `JA`, `ZH` or `RU`.\nRead more at: https://geoip-docs.gredev.io/sdks/js/geoip-method#options'));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Validate the mode variable
|
|
37
|
+
if (mode !== 'live' && mode !== 'test') {
|
|
38
|
+
reject(new Error('The `mode` option value "' + lang + '" you specified is unknown.\nYou can use: `live` or `test`.\nRead more at: https://geoip-docs.gredev.io/sdks/js/geoip-method#options'));
|
|
39
|
+
}
|
|
40
|
+
makeHttpRquest('GeoIP', {
|
|
41
|
+
'key': options.key,
|
|
42
|
+
'params': params.join(','),
|
|
43
|
+
'format': format,
|
|
44
|
+
'lang': lang,
|
|
45
|
+
'mode': mode
|
|
46
|
+
}, (res) => {
|
|
47
|
+
if (typeof res !== 'object') res = JSON.parse(res);
|
|
48
|
+
resolve(res);
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function Lookup(options) {
|
|
54
|
+
if (typeof options !== "object") options = {};
|
|
55
|
+
|
|
56
|
+
if (!options.key || options.key.length < 1) {
|
|
57
|
+
throw new Error('You should pass the API Key.');
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return new Promise((resolve, reject) => {
|
|
61
|
+
let ip = options.ip || '';
|
|
62
|
+
let params = options.params || [];
|
|
63
|
+
let format = options.format || 'JSON';
|
|
64
|
+
let lang = options.lang || 'EN';
|
|
65
|
+
let mode = options.mode || 'live';
|
|
66
|
+
lang = lang.toUpperCase();
|
|
67
|
+
|
|
68
|
+
// Validate the ip variable
|
|
69
|
+
if (ip.length < 7) {
|
|
70
|
+
reject(new Error('You should pass the `ip` parameter.'))
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Validate the params variable items
|
|
74
|
+
params.forEach(perParam => {
|
|
75
|
+
if (perParam.length > 0) {
|
|
76
|
+
if (!availableGeoIPParams.includes(perParam)) {
|
|
77
|
+
reject(new Error('The "' + perParam + '" module you used is unknown.\nYou can use: `location`, `security`, `timezone`, `currency` and/or `device`.\nRead more at: https://geoip-docs.gredev.io/sdks/js/lookup-method#options'));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// Validate the format variable
|
|
83
|
+
if (!availableFormats.includes(format)) {
|
|
84
|
+
reject(new Error('The `format` option value "' + lang + '" you specified is unknown.\nYou can use: `JSON`, `XML`, `CSV` or `Newline`.\nRead more at: https://geoip-docs.gredev.io/sdks/js/lookup-method#options'));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Validate the lang variable
|
|
88
|
+
if (!availableLanguages.includes(lang)) {
|
|
89
|
+
reject(new Error('The `lang` option value "' + lang + '" you specified is unknown.\nYou can use: `EN`, `AR`, `DE`, `FR`, `ES`, `JA`, `ZH` or `RU`.\nRead more at: https://geoip-docs.gredev.io/sdks/js/lookup-method#options'));
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Validate the mode variable
|
|
93
|
+
if (mode !== 'live' && mode !== 'test') {
|
|
94
|
+
reject(new Error('The `mode` option value "' + lang + '" you specified is unknown.\nYou can use: `live` or `test`.\nRead more at: https://geoip-docs.gredev.io/sdks/js/lookup-method#options'));
|
|
95
|
+
}
|
|
96
|
+
makeHttpRquest('IPLookup', {
|
|
97
|
+
'ip': ip,
|
|
98
|
+
'key': options.key,
|
|
99
|
+
'params': params.join(','),
|
|
100
|
+
'format': format,
|
|
101
|
+
'lang': lang,
|
|
102
|
+
'mode': mode
|
|
103
|
+
}, (res) => {
|
|
104
|
+
if (typeof res !== 'object') res = JSON.parse(res);
|
|
105
|
+
resolve(res);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function BulkLookup(options) {
|
|
111
|
+
if (typeof options !== "object") options = {};
|
|
112
|
+
|
|
113
|
+
if (!options.key || options.key.length < 1) {
|
|
114
|
+
throw new Error('You should pass the API Key.');
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return new Promise((resolve, reject) => {
|
|
118
|
+
let ips = options.ips || [];
|
|
119
|
+
let params = options.params || [];
|
|
120
|
+
let format = options.format || 'JSON';
|
|
121
|
+
let lang = options.lang || 'EN';
|
|
122
|
+
let mode = options.mode || 'live';
|
|
123
|
+
lang = lang.toUpperCase();
|
|
124
|
+
|
|
125
|
+
if (typeof ips !== "object") ips = [];
|
|
126
|
+
|
|
127
|
+
// Validate the ip variable
|
|
128
|
+
if (ips.length < 1) {
|
|
129
|
+
reject(new Error('You should pass the `ips` parameter.'))
|
|
130
|
+
}
|
|
131
|
+
ips.forEach(perParam => {
|
|
132
|
+
if (perParam.length < 7) {
|
|
133
|
+
reject(new Error('You should pass a valid IP Addresses in the `ips` parameter.'))
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
// Validate the params variable items
|
|
138
|
+
params.forEach(perParam => {
|
|
139
|
+
if (perParam.length > 0) {
|
|
140
|
+
if (!availableGeoIPParams.includes(perParam)) {
|
|
141
|
+
reject(new Error('The "' + perParam + '" module you used is unknown.\nYou can use: `location`, `security`, `timezone`, `currency` and/or `device`.\nRead more at: https://geoip-docs.gredev.io/sdks/js/lookup-method#options'));
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
// Validate the format variable
|
|
147
|
+
if (!availableFormats.includes(format)) {
|
|
148
|
+
reject(new Error('The `format` option value "' + lang + '" you specified is unknown.\nYou can use: `JSON`, `XML`, `CSV` or `Newline`.\nRead more at: https://geoip-docs.gredev.io/sdks/js/lookup-method#options'));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Validate the lang variable
|
|
152
|
+
if (!availableLanguages.includes(lang)) {
|
|
153
|
+
reject(new Error('The `lang` option value "' + lang + '" you specified is unknown.\nYou can use: `EN`, `AR`, `DE`, `FR`, `ES`, `JA`, `ZH` or `RU`.\nRead more at: https://geoip-docs.gredev.io/sdks/js/lookup-method#options'));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Validate the mode variable
|
|
157
|
+
if (mode !== 'live' && mode !== 'test') {
|
|
158
|
+
reject(new Error('The `mode` option value "' + lang + '" you specified is unknown.\nYou can use: `live` or `test`.\nRead more at: https://geoip-docs.gredev.io/sdks/js/lookup-method#options'));
|
|
159
|
+
}
|
|
160
|
+
makeHttpRquest('BulkLookup', {
|
|
161
|
+
'ips': ips,
|
|
162
|
+
'key': options.key,
|
|
163
|
+
'params': params.join(','),
|
|
164
|
+
'format': format,
|
|
165
|
+
'lang': lang,
|
|
166
|
+
'mode': mode
|
|
167
|
+
}, (res) => {
|
|
168
|
+
if (typeof res !== 'object') res = JSON.parse(res);
|
|
169
|
+
resolve(res);
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export function Country(options) {
|
|
175
|
+
if (typeof options !== "object") options = {};
|
|
176
|
+
|
|
177
|
+
if (!options.key || options.key.length < 1) {
|
|
178
|
+
throw new Error('You should pass the API Key.');
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return new Promise((resolve, reject) => {
|
|
182
|
+
var countryCode = options.countryCode || '';
|
|
183
|
+
var params = options.params || [];
|
|
184
|
+
var format = options.format || 'JSON';
|
|
185
|
+
var lang = options.lang || 'EN';
|
|
186
|
+
var mode = options.mode || 'live';
|
|
187
|
+
|
|
188
|
+
countryCode = countryCode.toUpperCase();
|
|
189
|
+
lang = lang.toUpperCase();
|
|
190
|
+
|
|
191
|
+
// Validate the countryCode variable
|
|
192
|
+
if (countryCode.length !== 2) {
|
|
193
|
+
reject(new Error('You should pass the `countryCode` parameter. Also, it should be a `ISO 3166-1 alpha-2` format.\nRead more at: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2'));
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Validate the params variable items
|
|
197
|
+
params.forEach(perParam => {
|
|
198
|
+
if (perParam.length > 0) {
|
|
199
|
+
if (!availableCountryParams.includes(perParam)) {
|
|
200
|
+
reject(new Error('The "' + perParam + '" module you used is unknown.\nYou can use: `language`, `flag`, `currency` and/or `timezone`.\nRead more at: https://geoip-docs.gredev.io/sdks/js/country-method#options'));
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
// Validate the format variable
|
|
206
|
+
if (!availableFormats.includes(format)) {
|
|
207
|
+
reject(new Error('The `format` option value "' + lang + '" you specified is unknown.\nYou can use: `JSON`, `XML`, `CSV` or `Newline`.\nRead more at: https://geoip-docs.gredev.io/sdks/js/country-method#options'));
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// Validate the lang variable
|
|
211
|
+
if (!availableLanguages.includes(lang)) {
|
|
212
|
+
reject(new Error('The `lang` option value "' + lang + '" you specified is unknown.\nYou can use: `EN`, `AR`, `DE`, `FR`, `ES`, `JA`, `ZH` or `RU`.\nRead more at: https://geoip-docs.gredev.io/sdks/js/country-method#options'));
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// Validate the mode variable
|
|
216
|
+
if (mode !== 'live' && mode !== 'test') {
|
|
217
|
+
reject(new Error('The `mode` option value "' + lang + '" you specified is unknown.\nYou can use: `live` or `test`.\nRead more at: https://geoip-docs.gredev.io/sdks/js/country-method#options'));
|
|
218
|
+
}
|
|
219
|
+
makeHttpRquest('Country', {
|
|
220
|
+
'CountryCode': countryCode,
|
|
221
|
+
'key': options.key,
|
|
222
|
+
'params': params.join(','),
|
|
223
|
+
'format': format,
|
|
224
|
+
'lang': lang,
|
|
225
|
+
'mode': mode
|
|
226
|
+
}, (res) => {
|
|
227
|
+
if (typeof res !== 'object') res = JSON.parse(res);
|
|
228
|
+
resolve(res);
|
|
229
|
+
});
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export function BadWord(options) {
|
|
234
|
+
if (typeof options !== "object") options = {};
|
|
235
|
+
|
|
236
|
+
if (!options.key || options.key.length < 1) {
|
|
237
|
+
throw new Error('You should pass the API Key.');
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return new Promise((resolve, reject) => {
|
|
241
|
+
var text = options.text || '';
|
|
242
|
+
var params = options.params || [];
|
|
243
|
+
var format = options.format || 'JSON';
|
|
244
|
+
var lang = options.lang || 'EN';
|
|
245
|
+
var mode = options.mode || 'live';
|
|
246
|
+
|
|
247
|
+
lang = lang.toUpperCase();
|
|
248
|
+
|
|
249
|
+
// Validate the text variable
|
|
250
|
+
if (text.length < 1) {
|
|
251
|
+
reject(new Error('You should pass the `text` parameter.'));
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Validate the format variable
|
|
255
|
+
if (!availableFormats.includes(format)) {
|
|
256
|
+
reject(new Error('The `format` option value "' + lang + '" you specified is unknown.\nYou can use: `JSON`, `XML` or `CSV`.\nRead more at: https://geoip-docs.gredev.io/sdks/js/country-method#options'));
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// Validate the lang variable
|
|
260
|
+
if (!availableLanguages.includes(lang)) {
|
|
261
|
+
reject(new Error('The `lang` option value "' + lang + '" you specified is unknown.\nYou can use: `EN`, `AR`, `DE`, `FR`, `ES`, `JA`, `ZH` or `RU`.\nRead more at: https://geoip-docs.gredev.io/sdks/js/country-method#options'));
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// Validate the mode variable
|
|
265
|
+
if (mode !== 'live' && mode !== 'test') {
|
|
266
|
+
reject(new Error('The `mode` option value "' + lang + '" you specified is unknown.\nYou can use: `live` or `test`.\nRead more at: https://geoip-docs.gredev.io/sdks/js/country-method#options'));
|
|
267
|
+
}
|
|
268
|
+
makeHttpRquest('badWords', {
|
|
269
|
+
'text': text,
|
|
270
|
+
'key': options.key,
|
|
271
|
+
'params': params.join(','),
|
|
272
|
+
'format': format,
|
|
273
|
+
'lang': lang,
|
|
274
|
+
'mode': mode
|
|
275
|
+
}, (res) => {
|
|
276
|
+
if (typeof res !== 'object') res = JSON.parse(res);
|
|
277
|
+
resolve(res);
|
|
278
|
+
});
|
|
279
|
+
});
|
|
280
|
+
}
|
package/main.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "greip.js",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "The official JS library of Greip.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "GRE Development Ltd.",
|
|
8
|
+
"email": "info@greip.io",
|
|
9
|
+
"url": "https://greip.io"
|
|
10
|
+
},
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"main": "index.js",
|
|
13
|
+
"keywords": [
|
|
14
|
+
"ip",
|
|
15
|
+
"api",
|
|
16
|
+
"geolocation",
|
|
17
|
+
"geoip"
|
|
18
|
+
],
|
|
19
|
+
"homepage": "https://github.com/gre-dev/Greip-JS#readme",
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/gre-dev/Greip-JS/issues",
|
|
22
|
+
"email": "info@greip.io"
|
|
23
|
+
},
|
|
24
|
+
"funding": {
|
|
25
|
+
"type": "patreon",
|
|
26
|
+
"url": "https://www.patreon.com/gredev"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"test": "echo \"No test specified\""
|
|
30
|
+
},
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "https://github.com/gre-dev/Greip-JS.git"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"axios": "^0.25.0"
|
|
37
|
+
}
|
|
38
|
+
}
|
package/src/util.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
|
|
3
|
+
export const baseURL = "https://gregeoip.com/";
|
|
4
|
+
export const availableGeoIPParams = ['location', 'security', 'timezone', 'currency', 'device'];
|
|
5
|
+
export const availableLanguages = ['EN', 'AR', 'DE', 'FR', 'ES', 'JA', 'ZH', 'RU'];
|
|
6
|
+
export const availableFormats = ['JSON', 'XML', 'CSV', 'Newline'];
|
|
7
|
+
export const availableCountryParams = ['language', 'flag', 'currency', 'timezone'];
|
|
8
|
+
|
|
9
|
+
export function serialize(obj) {
|
|
10
|
+
var str = [];
|
|
11
|
+
for (var p in obj)
|
|
12
|
+
if (obj.hasOwnProperty(p)) {
|
|
13
|
+
str.push(encodeURIComponent(p) + '=' + encodeURIComponent(obj[p]));
|
|
14
|
+
}
|
|
15
|
+
return str.join('&');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function makeHttpRquest(endpoint, options, callback) {
|
|
19
|
+
options.source = 'JS-Package';
|
|
20
|
+
|
|
21
|
+
axios.get(baseURL + '/' + endpoint + '?' + serialize(options))
|
|
22
|
+
.then(function (response) {
|
|
23
|
+
if (response.status === 200) {
|
|
24
|
+
callback(response.data);
|
|
25
|
+
} else {
|
|
26
|
+
throw new Error('An unknown error occurred while sending the request to GRE GeoIP API.');
|
|
27
|
+
}
|
|
28
|
+
})
|
|
29
|
+
.catch(function (error) {
|
|
30
|
+
console.error(error);
|
|
31
|
+
throw new Error('An unknown error occurred while sending the request to GRE GeoIP API.');
|
|
32
|
+
});
|
|
33
|
+
}
|