@ip2geo/sdk 0.0.1

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.
@@ -0,0 +1,59 @@
1
+ import { DOCS_URL } from './Constants.js';
2
+ import Console from '../helpers/Console.js';
3
+ import Translation from '../helpers/Translation.js';
4
+
5
+ /* eslint-disable no-control-regex */
6
+ /* eslint-disable quotes */
7
+ const MessageUpdate = (currentVersion, latestVersion) => {
8
+ const boxWidth = 70;
9
+ const topBorder = `┌${'─'.repeat(boxWidth - 2)}┐`;
10
+ const bottomBorder = `└${'─'.repeat(boxWidth - 2)}┘`;
11
+ const stripAnsi = (str) => str.replace(/\x1b\[[0-9;]*m/g, '');
12
+ const centerText = (text) => {
13
+ const visibleLength = stripAnsi(text).length;
14
+ const padding = Math.max(0, Math.floor((boxWidth - 2 - visibleLength) / 2));
15
+ const rightPadding = Math.max(0, boxWidth - 2 - padding - visibleLength);
16
+ return `│${' '.repeat(padding)}${text}${' '.repeat(rightPadding)}│`;
17
+ };
18
+ const updateText = `\x1b[1m\x1b[33m${Translation('console.update-available')}\x1b[0m \x1b[2m${currentVersion} → ${latestVersion}\x1b[0m`;
19
+ const changelogText = `${Translation('console.changelogs')} \x1b[36m${DOCS_URL}/sdk/changelogs\x1b[0m`;
20
+ const commandText = `${Translation('console.run')} \x1b[32m\`npm i @ip2geo/sdk@latest\`\x1b[0m ${Translation('console.to-update')}.`;
21
+ const disableText = `${Translation('console.disable-warning-using')}: \x1b[36m{ versionUpdateMessage: false }\x1b[0m ${Translation('console.on-init')}.`;
22
+ Console.Info('MessageUpdate', '', true);
23
+ Console.Info('MessageUpdate', topBorder, true);
24
+ Console.Info('MessageUpdate', centerText(updateText), true);
25
+ Console.Info('MessageUpdate', centerText(changelogText), true);
26
+ Console.Info('MessageUpdate', centerText(commandText), true);
27
+ Console.Info('MessageUpdate', centerText(''), true);
28
+ Console.Info('MessageUpdate', centerText(disableText), true);
29
+ Console.Info('MessageUpdate', bottomBorder, true);
30
+ Console.Info('MessageUpdate', '', true);
31
+ };
32
+ const MessageClientRuntime = () => {
33
+ const boxWidth = 70;
34
+ const topBorder = `┌${'─'.repeat(boxWidth - 2)}┐`;
35
+ const bottomBorder = `└${'─'.repeat(boxWidth - 2)}┘`;
36
+ const stripAnsi = (str) => str.replace(/\x1b\[[0-9;]*m/g, '');
37
+ const centerText = (text) => {
38
+ const visibleLength = stripAnsi(text).length;
39
+ const padding = Math.max(0, Math.floor((boxWidth - 2 - visibleLength) / 2));
40
+ const rightPadding = Math.max(0, boxWidth - 2 - padding - visibleLength);
41
+ return `│${' '.repeat(padding)}${text}${' '.repeat(rightPadding)}│`;
42
+ };
43
+ const titleText = `\x1b[1m\x1b[31m⚠ ${Translation('console.client-runtime-detected')}!\x1b[0m`;
44
+ const line1 = `${Translation('generals.ip2geo')} ${Translation('console.must-run-in')} ${Translation('console.a')} \x1b[32m${Translation('console.server-side-environment')}\x1b[0m ${Translation('console.only')}.`;
45
+ const line2 = Translation('console.client-side-usage-warning');
46
+ const disableText = `${Translation('console.disable-warning-using')}: \x1b[36m{ clientRuntimeMessage: false }\x1b[0m ${Translation('console.on-init')}.`;
47
+ Console.Info('MessageClientRuntime', '', true);
48
+ Console.Info('MessageClientRuntime', topBorder, true);
49
+ Console.Info('MessageClientRuntime', centerText(titleText), true);
50
+ Console.Info('MessageClientRuntime', centerText(''), true);
51
+ Console.Info('MessageClientRuntime', centerText(line1), true);
52
+ Console.Info('MessageClientRuntime', centerText(line2), true);
53
+ Console.Info('MessageClientRuntime', centerText(''), true);
54
+ Console.Info('MessageClientRuntime', centerText(disableText), true);
55
+ Console.Info('MessageClientRuntime', bottomBorder, true);
56
+ Console.Info('MessageClientRuntime', '', true);
57
+ };
58
+
59
+ export { MessageClientRuntime, MessageUpdate };
@@ -0,0 +1,8 @@
1
+ const STATE = {
2
+ AUTH_KEY: null,
3
+ CLIENT_RUNTIME_MESSAGE: true,
4
+ VERSION_UPDATE_MESSAGE: true,
5
+ IS_VERSION_CHECKED: false
6
+ };
7
+
8
+ export { STATE };
@@ -0,0 +1,13 @@
1
+ import { MessageClientRuntime } from '../data/Messages.js';
2
+ import { STATE } from '../data/States.js';
3
+ import IsBrowser from './IsBrowser.js';
4
+
5
+ const CheckRuntime = () => {
6
+ if (STATE.CLIENT_RUNTIME_MESSAGE) {
7
+ const isBrowser = IsBrowser();
8
+ if (isBrowser)
9
+ MessageClientRuntime();
10
+ }
11
+ };
12
+
13
+ export { CheckRuntime as default };
@@ -0,0 +1,19 @@
1
+ import { version } from '../prebuild/package.json.js';
2
+ import { MessageUpdate } from '../data/Messages.js';
3
+ import { STATE } from '../data/States.js';
4
+ import GetNpmVersion from './GetNpmVersion.js';
5
+
6
+ const CheckVersion = async () => {
7
+ if (!STATE.IS_VERSION_CHECKED && STATE.VERSION_UPDATE_MESSAGE) {
8
+ STATE.IS_VERSION_CHECKED = true;
9
+ const currentVersion = version;
10
+ const latestVersion = await GetNpmVersion();
11
+ if (typeof latestVersion === 'string') {
12
+ const isDifferent = currentVersion !== latestVersion;
13
+ if (isDifferent)
14
+ MessageUpdate(currentVersion, latestVersion);
15
+ }
16
+ }
17
+ };
18
+
19
+ export { CheckVersion as default };
@@ -0,0 +1,22 @@
1
+ const Console = {
2
+ Error: (func, message, ignoreFunName = false) => {
3
+ if (ignoreFunName)
4
+ console.error(message);
5
+ else
6
+ console.error(func, message);
7
+ },
8
+ Info: (func, message, ignoreFunName = false) => {
9
+ if (ignoreFunName)
10
+ console.info(message);
11
+ else
12
+ console.info(func, message);
13
+ },
14
+ Warning: (func, message, ignoreFunName = false) => {
15
+ if (ignoreFunName)
16
+ console.warn(message);
17
+ else
18
+ console.warn(func, message);
19
+ }
20
+ };
21
+
22
+ export { Console as default };
@@ -0,0 +1,16 @@
1
+ import { NPM_REGISTRY_URL } from '../data/Constants.js';
2
+ import Console from './Console.js';
3
+
4
+ const GetNpmVersion = async () => {
5
+ try {
6
+ const response = await fetch(NPM_REGISTRY_URL);
7
+ const data = await response.json();
8
+ return data['dist-tags']?.latest;
9
+ }
10
+ catch (error) {
11
+ Console.Error('GetNpmVersion', error);
12
+ return null;
13
+ }
14
+ };
15
+
16
+ export { GetNpmVersion as default };
@@ -0,0 +1,46 @@
1
+ import { FILE_FORMATS, HTTP_METHODS, HEADER_KEYS, HEADER_TYPES } from '../data/Constants.js';
2
+ import { STATE } from '../data/States.js';
3
+ import Console from './Console.js';
4
+ import IsBrowser from './IsBrowser.js';
5
+
6
+ const Http = async (endpoint, ipAddresses) => {
7
+ const options = {
8
+ method: HTTP_METHODS.GET,
9
+ headers: {
10
+ [HEADER_TYPES.CONTENT_TYPE]: FILE_FORMATS.APPLICATION_JSON,
11
+ [HEADER_KEYS.X_API_KEY]: STATE.AUTH_KEY
12
+ }
13
+ };
14
+ if (Array.isArray(ipAddresses) && ipAddresses?.length > 0) {
15
+ options['method'] = HTTP_METHODS.POST;
16
+ options['body'] = JSON.stringify({
17
+ ips: ipAddresses
18
+ });
19
+ }
20
+ const isBrowser = IsBrowser();
21
+ if (!isBrowser) {
22
+ try {
23
+ const fetchModule = await import('node-fetch');
24
+ const fetch = fetchModule.default;
25
+ const request = await fetch(endpoint, options);
26
+ return await request.json();
27
+ }
28
+ catch (error) {
29
+ Console.Error('Http', error);
30
+ return null;
31
+ }
32
+ }
33
+ else {
34
+ try {
35
+ const request = await fetch(endpoint, options);
36
+ const response = await request.json();
37
+ return response;
38
+ }
39
+ catch (error) {
40
+ Console.Error('Http', error);
41
+ return null;
42
+ }
43
+ }
44
+ };
45
+
46
+ export { Http as default };
@@ -0,0 +1,27 @@
1
+ import Console from './Console.js';
2
+
3
+ const IpValidation = (ipAddress) => {
4
+ const isString = typeof ipAddress === 'string';
5
+ if (!isString)
6
+ return {
7
+ ip4: false,
8
+ ip6: false
9
+ };
10
+ try {
11
+ const ip4 = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ipAddress);
12
+ const ip6 = /^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$|^([0-9a-fA-F]{1,4}:){1,7}:$|^([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}$|^([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}$|^([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}$|^([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}$|^([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}$|^[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6}|:)$|^:((:[0-9a-fA-F]{1,4}){1,7}|:)$/.test(ipAddress);
13
+ return {
14
+ ip4,
15
+ ip6
16
+ };
17
+ }
18
+ catch (error) {
19
+ Console.Error('IpAddressValidation', error);
20
+ return {
21
+ ip4: false,
22
+ ip6: false
23
+ };
24
+ }
25
+ };
26
+
27
+ export { IpValidation as default };
@@ -0,0 +1,5 @@
1
+ const IsBrowser = () => {
2
+ return typeof window !== 'undefined' && typeof window.document !== 'undefined';
3
+ };
4
+
5
+ export { IsBrowser as default };
@@ -0,0 +1,17 @@
1
+ import en_US from '../langs/en_US.js';
2
+
3
+ const Translation = (translationKey) => {
4
+ const lang = en_US;
5
+ const keys = translationKey.split('.');
6
+ let result = lang;
7
+ for (const key of keys) {
8
+ if (result && typeof result === 'object' && result !== null && key in result) {
9
+ result = result[key];
10
+ }
11
+ else
12
+ return translationKey;
13
+ }
14
+ return typeof result === 'string' ? result : translationKey;
15
+ };
16
+
17
+ export { Translation as default };
package/esm/index.js ADDED
@@ -0,0 +1,11 @@
1
+ import ConvertIP from './methods/ConvertIP.js';
2
+ import ConvertIPs from './methods/ConvertIPs.js';
3
+ import Init from './methods/Init.js';
4
+
5
+ const Ip2Geo = {
6
+ ConvertIPs,
7
+ ConvertIP,
8
+ Init
9
+ };
10
+
11
+ export { ConvertIP, ConvertIPs, Init, Ip2Geo as default };
@@ -0,0 +1,28 @@
1
+ const en_US = {
2
+ 'generals': {
3
+ 'ip2geo': 'Ip2Geo'
4
+ },
5
+ 'console': {
6
+ 'update-available': 'Update available!',
7
+ 'changelogs': 'Changelogs:',
8
+ 'run': 'Run',
9
+ 'to-update': 'to update',
10
+ 'disable-warning-using': 'Disable warning using',
11
+ 'on-init': 'on init',
12
+ 'client-runtime-detected': 'Client Runtime Detected',
13
+ 'client-side-usage-warning': 'Client-side usage exposes your API key publicly.',
14
+ 'only': 'only',
15
+ 'server-side-environment': 'Server-side environment',
16
+ 'must-run-in': 'must run in',
17
+ 'a': 'a'
18
+ },
19
+ 'conversions': {
20
+ 'ip-address-is-required-to-convert-it': 'IP address is required to convert it.',
21
+ 'ip-addresses-are-required-to-convert-them': 'IP addresses are required to convert them.',
22
+ 'invalid-ip-address-so-we-could-not-convert-it': 'Invalid IP address so we could not convert it.',
23
+ 'all-ip-addresses-are-invalid': 'All IP addresses are invalid.',
24
+ 'some-ip-addresses-are-invalid': 'Some IP addresses are invalid.'
25
+ }
26
+ };
27
+
28
+ export { en_US as default };
@@ -0,0 +1,46 @@
1
+ import { API_ENDPOINT, ENDPOINTS } from '../data/Constants.js';
2
+ import Console from '../helpers/Console.js';
3
+ import Http from '../helpers/Http.js';
4
+ import IpValidation from '../helpers/IpValidation.js';
5
+ import Translation from '../helpers/Translation.js';
6
+
7
+ const ConvertIP = async (options) => {
8
+ const { ip } = options || {};
9
+ if (!ip)
10
+ return {
11
+ success: false,
12
+ message: Translation('conversions.ip-address-is-required-to-convert-it'),
13
+ code: 400,
14
+ data: null,
15
+ _req: {
16
+ reqId: null,
17
+ resTime: 0
18
+ }
19
+ };
20
+ const { ip4, ip6 } = IpValidation(ip);
21
+ if (!ip4 && !ip6)
22
+ return {
23
+ success: false,
24
+ message: Translation('conversions.invalid-ip-address-so-we-could-not-convert-it'),
25
+ code: 400,
26
+ data: null,
27
+ _req: {
28
+ reqId: null,
29
+ resTime: 0
30
+ }
31
+ };
32
+ try {
33
+ const ipLink = `${API_ENDPOINT}${ENDPOINTS.CONVERT}?ip=${ip}`;
34
+ const ipResponse = await Http(ipLink);
35
+ if (ipResponse)
36
+ return ipResponse;
37
+ else
38
+ return null;
39
+ }
40
+ catch (error) {
41
+ Console.Error('ConvertIP', error);
42
+ return null;
43
+ }
44
+ };
45
+
46
+ export { ConvertIP as default };
@@ -0,0 +1,55 @@
1
+ import { API_ENDPOINT, ENDPOINTS } from '../data/Constants.js';
2
+ import Console from '../helpers/Console.js';
3
+ import Http from '../helpers/Http.js';
4
+ import IpValidation from '../helpers/IpValidation.js';
5
+ import Translation from '../helpers/Translation.js';
6
+
7
+ const ConvertIPs = async (props) => {
8
+ const { ips } = props;
9
+ if (!ips || !Array.isArray(ips) || ips.length === 0)
10
+ return {
11
+ success: false,
12
+ message: Translation('conversions.ip-addresses-are-required-to-convert-them'),
13
+ code: 400,
14
+ data: null,
15
+ _req: {
16
+ reqId: null,
17
+ resTime: 0
18
+ }
19
+ };
20
+ const invalidIps = [];
21
+ for (const ip of ips) {
22
+ const { ip4, ip6 } = IpValidation(ip);
23
+ if (!ip4 && !ip6)
24
+ invalidIps.push(ip);
25
+ }
26
+ const isAllInvalid = invalidIps?.length === ips?.length;
27
+ if (invalidIps?.length > 0)
28
+ return {
29
+ success: false,
30
+ message: isAllInvalid ? Translation('conversions.all-ip-addresses-are-invalid') : Translation('conversions.some-ip-addresses-are-invalid'),
31
+ code: 400,
32
+ data: invalidIps?.map(ip => ({
33
+ ip,
34
+ conversion: null
35
+ })),
36
+ _req: {
37
+ reqId: null,
38
+ resTime: 0
39
+ }
40
+ };
41
+ try {
42
+ const ipLink = `${API_ENDPOINT}${ENDPOINTS.CONVERT}`;
43
+ const ipResponse = await Http(ipLink, ips);
44
+ if (ipResponse)
45
+ return ipResponse;
46
+ else
47
+ return null;
48
+ }
49
+ catch (error) {
50
+ Console.Error('ConvertIPs', error);
51
+ return null;
52
+ }
53
+ };
54
+
55
+ export { ConvertIPs as default };
@@ -0,0 +1,28 @@
1
+ import CheckVersion from '../helpers/CheckVersion.js';
2
+ import CheckRuntime from '../helpers/CheckRuntime.js';
3
+ import Console from '../helpers/Console.js';
4
+ import { STATE } from '../data/States.js';
5
+
6
+ const Init = async (key, options) => {
7
+ try {
8
+ const crm = options?.clientRuntimeMessage;
9
+ const vum = options?.versionUpdateMessage;
10
+ STATE.AUTH_KEY = key;
11
+ STATE.CLIENT_RUNTIME_MESSAGE = typeof crm === 'boolean' ? crm : true;
12
+ STATE.VERSION_UPDATE_MESSAGE = typeof vum === 'boolean' ? vum : true;
13
+ return Promise.resolve(true);
14
+ }
15
+ catch (error) {
16
+ STATE.AUTH_KEY = null;
17
+ STATE.CLIENT_RUNTIME_MESSAGE = true;
18
+ STATE.VERSION_UPDATE_MESSAGE = true;
19
+ Console.Error('Init', error);
20
+ return Promise.resolve(false);
21
+ }
22
+ finally {
23
+ CheckRuntime();
24
+ CheckVersion();
25
+ }
26
+ };
27
+
28
+ export { Init as default };
@@ -0,0 +1,3 @@
1
+ var version = "0.0.1";
2
+
3
+ export { version };