dymo-api 1.0.73 → 1.0.74
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 +44 -4
- package/dist/esm/dymo-api.js +44 -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,45 @@ 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("../data/cache.json"))
|
|
95
|
+
return null;
|
|
96
|
+
const parsedData = JSON.parse(fs_1.default.readFileSync("../data/cache.json", "utf-8"));
|
|
97
|
+
const lastInitialized = new Date(parsedData.lastInitialized);
|
|
98
|
+
const now = new Date();
|
|
99
|
+
const diffInDays = (now.getTime() - lastInitialized.getTime()) / (1000 * 3600 * 24);
|
|
100
|
+
if (diffInDays > 5) {
|
|
101
|
+
fs_1.default.unlinkSync("../data/cache.json");
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
return parsedData.tokens;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Writes the given tokens to a file called "cache.json" in the same directory where the
|
|
108
|
+
* script is running. The file is a JSON object with two properties: "tokens", which contains
|
|
109
|
+
* the authentication tokens, and "lastInitialized", which contains the date and time when
|
|
110
|
+
* the tokens were last generated. The file is formatted with two spaces of indentation.
|
|
111
|
+
*
|
|
112
|
+
* @param {TokensResponse} tokens - The authentication tokens to be written to the file.
|
|
113
|
+
*/
|
|
114
|
+
writeTokensToFile(tokens) {
|
|
115
|
+
fs_1.default.writeFileSync("../data/cache.json", JSON.stringify({
|
|
116
|
+
tokens,
|
|
117
|
+
lastInitialized: new Date().toISOString()
|
|
118
|
+
}, null, 2));
|
|
119
|
+
}
|
|
80
120
|
/**
|
|
81
121
|
* Retrieves and caches authentication tokens.
|
|
82
122
|
*
|
|
@@ -93,12 +133,12 @@ class DymoAPI {
|
|
|
93
133
|
* with the token retrieval process.
|
|
94
134
|
*/
|
|
95
135
|
async getTokens() {
|
|
96
|
-
const
|
|
97
|
-
if (
|
|
136
|
+
const cachedTokens = this.readTokensFromFile();
|
|
137
|
+
if (cachedTokens) {
|
|
98
138
|
console.log(`[${config_1.default.lib.name}] Using cached tokens response.`);
|
|
139
|
+
this.tokensResponse = cachedTokens;
|
|
99
140
|
return this.tokensResponse;
|
|
100
141
|
}
|
|
101
|
-
;
|
|
102
142
|
const tokens = {};
|
|
103
143
|
if (this.rootApiKey)
|
|
104
144
|
tokens.root = `Bearer ${this.rootApiKey}`;
|
|
@@ -113,7 +153,7 @@ class DymoAPI {
|
|
|
113
153
|
if (tokens.api && response.data.api === false)
|
|
114
154
|
throw customError(3000, "Invalid API token.");
|
|
115
155
|
this.tokensResponse = response.data;
|
|
116
|
-
this.
|
|
156
|
+
this.writeTokensToFile(response.data);
|
|
117
157
|
console.log(`[${config_1.default.lib.name}] Tokens initialized successfully.`);
|
|
118
158
|
return this.tokensResponse;
|
|
119
159
|
}
|
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,45 @@ 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("../data/cache.json"))
|
|
57
|
+
return null;
|
|
58
|
+
const parsedData = JSON.parse(fs.readFileSync("../data/cache.json", "utf-8"));
|
|
59
|
+
const lastInitialized = new Date(parsedData.lastInitialized);
|
|
60
|
+
const now = new Date();
|
|
61
|
+
const diffInDays = (now.getTime() - lastInitialized.getTime()) / (1000 * 3600 * 24);
|
|
62
|
+
if (diffInDays > 5) {
|
|
63
|
+
fs.unlinkSync("../data/cache.json");
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
return parsedData.tokens;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Writes the given tokens to a file called "cache.json" in the same directory where the
|
|
70
|
+
* script is running. The file is a JSON object with two properties: "tokens", which contains
|
|
71
|
+
* the authentication tokens, and "lastInitialized", which contains the date and time when
|
|
72
|
+
* the tokens were last generated. The file is formatted with two spaces of indentation.
|
|
73
|
+
*
|
|
74
|
+
* @param {TokensResponse} tokens - The authentication tokens to be written to the file.
|
|
75
|
+
*/
|
|
76
|
+
writeTokensToFile(tokens) {
|
|
77
|
+
fs.writeFileSync("../data/cache.json", JSON.stringify({
|
|
78
|
+
tokens,
|
|
79
|
+
lastInitialized: new Date().toISOString()
|
|
80
|
+
}, null, 2));
|
|
81
|
+
}
|
|
42
82
|
/**
|
|
43
83
|
* Retrieves and caches authentication tokens.
|
|
44
84
|
*
|
|
@@ -55,12 +95,12 @@ class DymoAPI {
|
|
|
55
95
|
* with the token retrieval process.
|
|
56
96
|
*/
|
|
57
97
|
async getTokens() {
|
|
58
|
-
const
|
|
59
|
-
if (
|
|
98
|
+
const cachedTokens = this.readTokensFromFile();
|
|
99
|
+
if (cachedTokens) {
|
|
60
100
|
console.log(`[${config.lib.name}] Using cached tokens response.`);
|
|
101
|
+
this.tokensResponse = cachedTokens;
|
|
61
102
|
return this.tokensResponse;
|
|
62
103
|
}
|
|
63
|
-
;
|
|
64
104
|
const tokens = {};
|
|
65
105
|
if (this.rootApiKey)
|
|
66
106
|
tokens.root = `Bearer ${this.rootApiKey}`;
|
|
@@ -75,7 +115,7 @@ class DymoAPI {
|
|
|
75
115
|
if (tokens.api && response.data.api === false)
|
|
76
116
|
throw customError(3000, "Invalid API token.");
|
|
77
117
|
this.tokensResponse = response.data;
|
|
78
|
-
this.
|
|
118
|
+
this.writeTokensToFile(response.data);
|
|
79
119
|
console.log(`[${config.lib.name}] Tokens initialized successfully.`);
|
|
80
120
|
return this.tokensResponse;
|
|
81
121
|
}
|
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
|
*
|