dymo-api 1.0.1 → 1.0.3
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/config.cjs +6 -0
- package/index.js +13 -1
- package/lib/dymo-api.cjs +63 -0
- package/lib/private-api.cjs +38 -0
- package/lib/public-api.cjs +43 -0
- package/package.json +7 -2
- package/test/preview.js +12 -4
- package/lib/dymo-api.ts +0 -38
package/config.cjs
ADDED
package/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import dymoAPI from './lib/dymo-api.js';
|
|
1
|
+
/*import dymoAPI from './lib/dymo-api.js';
|
|
2
2
|
|
|
3
3
|
// This module is intended to unwrap Dymo API default export as named.
|
|
4
4
|
// Keep top-level export same with static properties.
|
|
@@ -10,4 +10,16 @@ const {
|
|
|
10
10
|
|
|
11
11
|
export {
|
|
12
12
|
dymoAPI as default
|
|
13
|
+
}*/
|
|
14
|
+
// Importar para ECMAScript Modules
|
|
15
|
+
import pkg from './lib/dymo-api.cjs';
|
|
16
|
+
|
|
17
|
+
const { Configuration: _Configuration, DymoAPI: _DymoAPI } = pkg;
|
|
18
|
+
|
|
19
|
+
// Exportar para ECMAScript Modules
|
|
20
|
+
export { _Configuration as Configuration, _DymoAPI as DymoAPI };
|
|
21
|
+
|
|
22
|
+
// Exportar para CommonJS
|
|
23
|
+
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
|
|
24
|
+
module.exports = { Configuration: _Configuration, DymoAPI: _DymoAPI };
|
|
13
25
|
}
|
package/lib/dymo-api.cjs
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
const axios = require("axios");
|
|
2
|
+
const config = require("../config.cjs");
|
|
3
|
+
const customError = (code, message) => Object.assign(new Error(), { code, message: `[${config.lib.name}] ${message}` });
|
|
4
|
+
|
|
5
|
+
/* INTERNALS */
|
|
6
|
+
const privateAPI = require("./private-api.cjs");
|
|
7
|
+
const publicAPI = require("./public-api.cjs");
|
|
8
|
+
|
|
9
|
+
class Configuration {
|
|
10
|
+
constructor({ rootApiKey, apiKey } = {}) {
|
|
11
|
+
// If neither is provided, set them as empty strings.
|
|
12
|
+
this.rootApiKey = rootApiKey || null;
|
|
13
|
+
this.apiKey = apiKey || null;
|
|
14
|
+
this.tokensResponse = null; // Variable to store the response.
|
|
15
|
+
this.lastFetchTime = null; // Variable to store the time of last request.
|
|
16
|
+
}
|
|
17
|
+
async getTokens() {
|
|
18
|
+
// Checks if the response is available and if 5 minutes have passed since the last request.
|
|
19
|
+
const currentTime = new Date();
|
|
20
|
+
if (this.tokensResponse && this.lastFetchTime && currentTime - this.lastFetchTime < 5 * 60 * 1000) {
|
|
21
|
+
console.log(`[${config.lib.name}] Using cached tokens response.`);
|
|
22
|
+
return this.tokensResponse;
|
|
23
|
+
}
|
|
24
|
+
const tokens = {};
|
|
25
|
+
if (this.rootApiKey !== null) tokens.root = `Bearer ${this.rootApiKey}`;
|
|
26
|
+
if (this.apiKey !== null) tokens.api = `Bearer ${this.apiKey}`;
|
|
27
|
+
try {
|
|
28
|
+
if (Object.keys(tokens).length < 1) return;
|
|
29
|
+
const response = await axios.post('https://api.tpeoficial.com/dvr/tokens', { tokens });
|
|
30
|
+
if (tokens.root && response.data.root === false) throw customError(3000, "Invalid root token.");
|
|
31
|
+
if (tokens.private && response.data.private === false) throw customError(3000, "Invalid private token.");
|
|
32
|
+
// Saves the response and the current request time.
|
|
33
|
+
this.tokensResponse = response.data;
|
|
34
|
+
this.lastFetchTime = currentTime;
|
|
35
|
+
console.log(`[${config.lib.name}] Tokens initialized successfully.`);
|
|
36
|
+
return response.data;
|
|
37
|
+
} catch (error) {
|
|
38
|
+
throw customError(5000, error.message);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
class DymoAPI {
|
|
44
|
+
constructor(configuration) {
|
|
45
|
+
this.configuration = configuration;
|
|
46
|
+
this.initializeTokens(); // Calls the function to obtain tokens when creating the object.
|
|
47
|
+
}
|
|
48
|
+
async initializeTokens() {
|
|
49
|
+
try {
|
|
50
|
+
await this.configuration.getTokens();
|
|
51
|
+
} catch (error) {
|
|
52
|
+
throw customError(5000, `Error initializing tokens: ${error.message}`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
// FUNCTIONS / Private.
|
|
56
|
+
async isValidData(data) { return await privateAPI.isValidData(this.configuration.apiKey, data); }
|
|
57
|
+
// FUNCTIONS / Public.
|
|
58
|
+
async getPrayerTimes(data) { return await publicAPI.getPrayerTimes(data); }
|
|
59
|
+
async isValidPwd(data) { return await publicAPI.isValidPwd(data); }
|
|
60
|
+
async newURLEncrypt(data) { return await publicAPI.newURLEncrypt(data); }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
module.exports = { Configuration, DymoAPI };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
const axios = require("axios");
|
|
2
|
+
const config = require("../config.cjs");
|
|
3
|
+
const customError = (code, message) => Object.assign(new Error(), { code, message: `[${config.lib.name}] ${message}` });
|
|
4
|
+
|
|
5
|
+
exports.isValidData = async (token, data) => {
|
|
6
|
+
if (token === null) throw customError(3000, "Invalid private token.");
|
|
7
|
+
let i = false;
|
|
8
|
+
for (const key in data) {
|
|
9
|
+
if (data.hasOwnProperty(key) && (key === 'email' || key === 'tel' || key === 'domain' || key === 'creditCard' || key === 'ip')) {
|
|
10
|
+
i = true;
|
|
11
|
+
break;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
if (!i) throw customError(1500, "You must provide at least one parameter.");
|
|
15
|
+
try {
|
|
16
|
+
const response = await axios.get(`https://api.tpeoficial.com/private/secure/verify`, { params: data, headers: { 'Authorization': token } });
|
|
17
|
+
const dicc = {
|
|
18
|
+
"0000": "email",
|
|
19
|
+
"0001": "tel",
|
|
20
|
+
"0002": "domain",
|
|
21
|
+
"0003": "creditCard",
|
|
22
|
+
"0004": "ip"
|
|
23
|
+
}
|
|
24
|
+
const result = {};
|
|
25
|
+
response.data.forEach(obj => {
|
|
26
|
+
Object.keys(dicc).forEach(code => {
|
|
27
|
+
if (obj.hasOwnProperty('code') && obj.code === code) {
|
|
28
|
+
const type = dicc[code];
|
|
29
|
+
if (!result.hasOwnProperty(type)) result[type] = {};
|
|
30
|
+
result[type] = { ...result[type], ...obj };
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
return result;
|
|
35
|
+
} catch (error) {
|
|
36
|
+
throw customError(5000, error.message);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
const axios = require("axios");
|
|
2
|
+
const config = require("../config.cjs");
|
|
3
|
+
const customError = (code, message) => Object.assign(new Error(), { code, message: `[${config.lib.name}] ${message}` });
|
|
4
|
+
|
|
5
|
+
exports.getPrayerTimes = async (data) => {
|
|
6
|
+
const { lat, lon } = data;
|
|
7
|
+
if (!lat || !lon) throw customError(1000, "You must provide a latitude and longitude.");
|
|
8
|
+
try {
|
|
9
|
+
const response = await axios.get(`https://api.tpeoficial.com/public/islam/prayertimes`, { params: data });
|
|
10
|
+
return response.data;
|
|
11
|
+
} catch (error) {
|
|
12
|
+
throw customError(5000, error.message);
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
exports.isValidPwd = async (data) => {
|
|
17
|
+
const { email, password } = data;
|
|
18
|
+
if (!password) throw customError(1000, "You must specify at least the password.");
|
|
19
|
+
const params = {
|
|
20
|
+
password: encodeURIComponent(password),
|
|
21
|
+
};
|
|
22
|
+
if (email) {
|
|
23
|
+
if (!/^[a-zA-Z0-9._\-+]+@?[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/.test(email)) throw customError(1500, "If you provide an email address it must be valid.");
|
|
24
|
+
params.email = encodeURIComponent(email);
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
const response = await axios.get(`https://api.tpeoficial.com/public/validPwd`, { params });
|
|
28
|
+
return response.data;
|
|
29
|
+
} catch (error) {
|
|
30
|
+
throw customError(5000, error.message);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
exports.newURLEncrypt = async (data) => {
|
|
35
|
+
const { url } = data;
|
|
36
|
+
if (!url || (!url.startsWith("https://") && !url.startsWith("http://"))) throw customError(1500, "You must provide a valid url.");
|
|
37
|
+
try {
|
|
38
|
+
const response = await axios.get(`https://api.tpeoficial.com/public/url-encrypt`, { params: data });
|
|
39
|
+
return response.data;
|
|
40
|
+
} catch (error) {
|
|
41
|
+
throw customError(5000, error.message);
|
|
42
|
+
}
|
|
43
|
+
}
|
package/package.json
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dymo-api",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "Flow system for Dymo API.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
9
9
|
},
|
|
10
|
-
"keywords": [
|
|
10
|
+
"keywords": [
|
|
11
|
+
"Dymo",
|
|
12
|
+
"Dymo API",
|
|
13
|
+
"TPEOficial",
|
|
14
|
+
"Ciphera"
|
|
15
|
+
],
|
|
11
16
|
"author": "TPEOficial LLC",
|
|
12
17
|
"license": "MIT",
|
|
13
18
|
"dependencies": {
|
package/test/preview.js
CHANGED
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
// npm install dymo-api
|
|
2
2
|
// /* 1. */ const { Configuration, DymoAPI } = require("dymo-api");
|
|
3
3
|
// /* 2. */ import { Configuration, DymoAPI } from "dymo-api";
|
|
4
|
+
import { Configuration, DymoAPI } from "../index.js";
|
|
4
5
|
|
|
5
6
|
const configuration = new Configuration({
|
|
6
|
-
rootApiKey: "ROOT_TOKEN_HERE",
|
|
7
|
-
apiKey: "PRIVATE_TOKEN_HERE"
|
|
7
|
+
/*rootApiKey: "ROOT_TOKEN_HERE",
|
|
8
|
+
apiKey: "PRIVATE_TOKEN_HERE",*/
|
|
8
9
|
});
|
|
9
10
|
|
|
10
|
-
// Creating the client for use
|
|
11
|
-
const dymo = new DymoAPI(configuration);
|
|
11
|
+
// Creating the client for use.
|
|
12
|
+
const dymo = new DymoAPI(configuration);
|
|
13
|
+
|
|
14
|
+
/*
|
|
15
|
+
console.log(await dymo.isValidData({ email: "test@test.com", tel: "+34617509462", domain: "test.com", creditCard: "5110929780543845", ip: "52.94.236.248" })); // Private authorization required.
|
|
16
|
+
console.log(await dymo.newURLEncrypt({ url: "https://www.tpeoficial.com/" })); // No authorization required.
|
|
17
|
+
console.log(await dymo.getPrayerTimes({ lat: "37.3755847689721", lon: "-4.457957889422379" })); // No authorization required.
|
|
18
|
+
console.log(await dymo.isValidPwd({ email: "username@test.com", password: "ThisIsATest" })); // No authorization required.
|
|
19
|
+
*/
|
package/lib/dymo-api.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import axios from 'axios';
|
|
2
|
-
|
|
3
|
-
export interface TokenResponse {
|
|
4
|
-
accessToken: string;
|
|
5
|
-
refreshToken: string;
|
|
6
|
-
private?: boolean; // We define private as an optional property in TokenResponse
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export interface ConfigurationOptions {
|
|
10
|
-
rootApiKey?: string;
|
|
11
|
-
apiKey?: string;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export class Configuration {
|
|
15
|
-
private rootApiKey?: string;
|
|
16
|
-
private apiKey?: string;
|
|
17
|
-
|
|
18
|
-
constructor(options: ConfigurationOptions = {}) {
|
|
19
|
-
this.rootApiKey = options.rootApiKey;
|
|
20
|
-
this.apiKey = options.apiKey;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
async getTokens(): Promise<TokenResponse> {
|
|
24
|
-
const response = await axios.post<TokenResponse>('https://api.tpeoficial.com/dvr/tokens', {
|
|
25
|
-
tokens: {
|
|
26
|
-
root: `Bearer ${this.rootApiKey}`,
|
|
27
|
-
private: `Bearer ${this.apiKey}`
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
//
|
|
32
|
-
if (response.data.private !== undefined && response.data.private === false) {
|
|
33
|
-
throw new Error('Invalid private token.');
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return response.data;
|
|
37
|
-
}
|
|
38
|
-
}
|