dymo-api 1.0.73 → 1.0.75
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/dist/cjs/dymo-api.cjs +46 -4
- package/dist/esm/dymo-api.js +46 -4
- package/dist/types/dymo-api.d.ts +22 -0
- package/package.json +1 -1
package/dist/cjs/dymo-api.cjs
CHANGED
|
@@ -36,6 +36,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
36
36
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
const fs_1 = __importDefault(require("fs"));
|
|
39
40
|
const axios_1 = __importDefault(require("axios"));
|
|
40
41
|
const PublicAPI = __importStar(require("./branches/public.cjs"));
|
|
41
42
|
const PrivateAPI = __importStar(require("./branches/private.cjs"));
|
|
@@ -77,6 +78,47 @@ class DymoAPI {
|
|
|
77
78
|
this.autoupdate();
|
|
78
79
|
this.initializeTokens(); // Calls the function to obtain tokens when creating the object.
|
|
79
80
|
}
|
|
81
|
+
/**
|
|
82
|
+
* Reads the authentication tokens from a cache file if it exists and is still valid.
|
|
83
|
+
*
|
|
84
|
+
* This method checks for a "cache.json" file in the current directory. If the file
|
|
85
|
+
* exists, it parses the JSON content to retrieve the stored tokens and the last
|
|
86
|
+
* initialization date. It then calculates the difference in days between the current
|
|
87
|
+
* date and the last initialization date. If this difference exceeds 5 days, the cache
|
|
88
|
+
* file is deleted, and null is returned. Otherwise, it returns the tokens from the file.
|
|
89
|
+
*
|
|
90
|
+
* @returns {TokensResponse | null} The cached tokens if valid, or null if the cache file
|
|
91
|
+
* does not exist or is outdated.
|
|
92
|
+
*/
|
|
93
|
+
readTokensFromFile() {
|
|
94
|
+
if (!fs_1.default.existsSync("./node_modules/dymo-api/data/cache.json"))
|
|
95
|
+
fs_1.default.mkdirSync("./node_modules/dymo-api/data/cache.json", { recursive: true });
|
|
96
|
+
if (!fs_1.default.existsSync("./node_modules/dymo-api/data/cache.json"))
|
|
97
|
+
return null;
|
|
98
|
+
const parsedData = JSON.parse(fs_1.default.readFileSync("./node_modules/dymo-api/data/cache.json", "utf-8"));
|
|
99
|
+
const lastInitialized = new Date(parsedData.lastInitialized);
|
|
100
|
+
const now = new Date();
|
|
101
|
+
const diffInDays = (now.getTime() - lastInitialized.getTime()) / (1000 * 3600 * 24);
|
|
102
|
+
if (diffInDays > 5) {
|
|
103
|
+
fs_1.default.unlinkSync("./node_modules/dymo-api/data/cache.json");
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
return parsedData.tokens;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Writes the given tokens to a file called "cache.json" in the same directory where the
|
|
110
|
+
* script is running. The file is a JSON object with two properties: "tokens", which contains
|
|
111
|
+
* the authentication tokens, and "lastInitialized", which contains the date and time when
|
|
112
|
+
* the tokens were last generated. The file is formatted with two spaces of indentation.
|
|
113
|
+
*
|
|
114
|
+
* @param {TokensResponse} tokens - The authentication tokens to be written to the file.
|
|
115
|
+
*/
|
|
116
|
+
writeTokensToFile(tokens) {
|
|
117
|
+
fs_1.default.writeFileSync("./node_modules/dymo-api/data/cache.json", JSON.stringify({
|
|
118
|
+
tokens,
|
|
119
|
+
lastInitialized: new Date().toISOString()
|
|
120
|
+
}, null, 2));
|
|
121
|
+
}
|
|
80
122
|
/**
|
|
81
123
|
* Retrieves and caches authentication tokens.
|
|
82
124
|
*
|
|
@@ -93,12 +135,12 @@ class DymoAPI {
|
|
|
93
135
|
* with the token retrieval process.
|
|
94
136
|
*/
|
|
95
137
|
async getTokens() {
|
|
96
|
-
const
|
|
97
|
-
if (
|
|
138
|
+
const cachedTokens = this.readTokensFromFile();
|
|
139
|
+
if (cachedTokens) {
|
|
98
140
|
console.log(`[${config_1.default.lib.name}] Using cached tokens response.`);
|
|
141
|
+
this.tokensResponse = cachedTokens;
|
|
99
142
|
return this.tokensResponse;
|
|
100
143
|
}
|
|
101
|
-
;
|
|
102
144
|
const tokens = {};
|
|
103
145
|
if (this.rootApiKey)
|
|
104
146
|
tokens.root = `Bearer ${this.rootApiKey}`;
|
|
@@ -113,7 +155,7 @@ class DymoAPI {
|
|
|
113
155
|
if (tokens.api && response.data.api === false)
|
|
114
156
|
throw customError(3000, "Invalid API token.");
|
|
115
157
|
this.tokensResponse = response.data;
|
|
116
|
-
this.
|
|
158
|
+
this.writeTokensToFile(response.data);
|
|
117
159
|
console.log(`[${config_1.default.lib.name}] Tokens initialized successfully.`);
|
|
118
160
|
return this.tokensResponse;
|
|
119
161
|
}
|
package/dist/esm/dymo-api.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import fs from "fs";
|
|
1
2
|
import axios from "axios";
|
|
2
3
|
import * as PublicAPI from "./branches/public.js";
|
|
3
4
|
import * as PrivateAPI from "./branches/private.js";
|
|
@@ -39,6 +40,47 @@ class DymoAPI {
|
|
|
39
40
|
this.autoupdate();
|
|
40
41
|
this.initializeTokens(); // Calls the function to obtain tokens when creating the object.
|
|
41
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* Reads the authentication tokens from a cache file if it exists and is still valid.
|
|
45
|
+
*
|
|
46
|
+
* This method checks for a "cache.json" file in the current directory. If the file
|
|
47
|
+
* exists, it parses the JSON content to retrieve the stored tokens and the last
|
|
48
|
+
* initialization date. It then calculates the difference in days between the current
|
|
49
|
+
* date and the last initialization date. If this difference exceeds 5 days, the cache
|
|
50
|
+
* file is deleted, and null is returned. Otherwise, it returns the tokens from the file.
|
|
51
|
+
*
|
|
52
|
+
* @returns {TokensResponse | null} The cached tokens if valid, or null if the cache file
|
|
53
|
+
* does not exist or is outdated.
|
|
54
|
+
*/
|
|
55
|
+
readTokensFromFile() {
|
|
56
|
+
if (!fs.existsSync("./node_modules/dymo-api/data/cache.json"))
|
|
57
|
+
fs.mkdirSync("./node_modules/dymo-api/data/cache.json", { recursive: true });
|
|
58
|
+
if (!fs.existsSync("./node_modules/dymo-api/data/cache.json"))
|
|
59
|
+
return null;
|
|
60
|
+
const parsedData = JSON.parse(fs.readFileSync("./node_modules/dymo-api/data/cache.json", "utf-8"));
|
|
61
|
+
const lastInitialized = new Date(parsedData.lastInitialized);
|
|
62
|
+
const now = new Date();
|
|
63
|
+
const diffInDays = (now.getTime() - lastInitialized.getTime()) / (1000 * 3600 * 24);
|
|
64
|
+
if (diffInDays > 5) {
|
|
65
|
+
fs.unlinkSync("./node_modules/dymo-api/data/cache.json");
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
return parsedData.tokens;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Writes the given tokens to a file called "cache.json" in the same directory where the
|
|
72
|
+
* script is running. The file is a JSON object with two properties: "tokens", which contains
|
|
73
|
+
* the authentication tokens, and "lastInitialized", which contains the date and time when
|
|
74
|
+
* the tokens were last generated. The file is formatted with two spaces of indentation.
|
|
75
|
+
*
|
|
76
|
+
* @param {TokensResponse} tokens - The authentication tokens to be written to the file.
|
|
77
|
+
*/
|
|
78
|
+
writeTokensToFile(tokens) {
|
|
79
|
+
fs.writeFileSync("./node_modules/dymo-api/data/cache.json", JSON.stringify({
|
|
80
|
+
tokens,
|
|
81
|
+
lastInitialized: new Date().toISOString()
|
|
82
|
+
}, null, 2));
|
|
83
|
+
}
|
|
42
84
|
/**
|
|
43
85
|
* Retrieves and caches authentication tokens.
|
|
44
86
|
*
|
|
@@ -55,12 +97,12 @@ class DymoAPI {
|
|
|
55
97
|
* with the token retrieval process.
|
|
56
98
|
*/
|
|
57
99
|
async getTokens() {
|
|
58
|
-
const
|
|
59
|
-
if (
|
|
100
|
+
const cachedTokens = this.readTokensFromFile();
|
|
101
|
+
if (cachedTokens) {
|
|
60
102
|
console.log(`[${config.lib.name}] Using cached tokens response.`);
|
|
103
|
+
this.tokensResponse = cachedTokens;
|
|
61
104
|
return this.tokensResponse;
|
|
62
105
|
}
|
|
63
|
-
;
|
|
64
106
|
const tokens = {};
|
|
65
107
|
if (this.rootApiKey)
|
|
66
108
|
tokens.root = `Bearer ${this.rootApiKey}`;
|
|
@@ -75,7 +117,7 @@ class DymoAPI {
|
|
|
75
117
|
if (tokens.api && response.data.api === false)
|
|
76
118
|
throw customError(3000, "Invalid API token.");
|
|
77
119
|
this.tokensResponse = response.data;
|
|
78
|
-
this.
|
|
120
|
+
this.writeTokensToFile(response.data);
|
|
79
121
|
console.log(`[${config.lib.name}] Tokens initialized successfully.`);
|
|
80
122
|
return this.tokensResponse;
|
|
81
123
|
}
|
package/dist/types/dymo-api.d.ts
CHANGED
|
@@ -43,6 +43,28 @@ declare class DymoAPI {
|
|
|
43
43
|
local?: boolean;
|
|
44
44
|
serverEmailConfig?: ServerEmailConfig;
|
|
45
45
|
});
|
|
46
|
+
/**
|
|
47
|
+
* Reads the authentication tokens from a cache file if it exists and is still valid.
|
|
48
|
+
*
|
|
49
|
+
* This method checks for a "cache.json" file in the current directory. If the file
|
|
50
|
+
* exists, it parses the JSON content to retrieve the stored tokens and the last
|
|
51
|
+
* initialization date. It then calculates the difference in days between the current
|
|
52
|
+
* date and the last initialization date. If this difference exceeds 5 days, the cache
|
|
53
|
+
* file is deleted, and null is returned. Otherwise, it returns the tokens from the file.
|
|
54
|
+
*
|
|
55
|
+
* @returns {TokensResponse | null} The cached tokens if valid, or null if the cache file
|
|
56
|
+
* does not exist or is outdated.
|
|
57
|
+
*/
|
|
58
|
+
private readTokensFromFile;
|
|
59
|
+
/**
|
|
60
|
+
* Writes the given tokens to a file called "cache.json" in the same directory where the
|
|
61
|
+
* script is running. The file is a JSON object with two properties: "tokens", which contains
|
|
62
|
+
* the authentication tokens, and "lastInitialized", which contains the date and time when
|
|
63
|
+
* the tokens were last generated. The file is formatted with two spaces of indentation.
|
|
64
|
+
*
|
|
65
|
+
* @param {TokensResponse} tokens - The authentication tokens to be written to the file.
|
|
66
|
+
*/
|
|
67
|
+
private writeTokensToFile;
|
|
46
68
|
/**
|
|
47
69
|
* Retrieves and caches authentication tokens.
|
|
48
70
|
*
|