dymo-api 1.0.75 → 1.0.76

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.
@@ -36,7 +36,6 @@ 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"));
40
39
  const axios_1 = __importDefault(require("axios"));
41
40
  const PublicAPI = __importStar(require("./branches/public.cjs"));
42
41
  const PrivateAPI = __importStar(require("./branches/private.cjs"));
@@ -71,54 +70,13 @@ class DymoAPI {
71
70
  this.rootApiKey = rootApiKey;
72
71
  this.apiKey = apiKey;
73
72
  this.tokensResponse = null;
74
- this.lastFetchTime = null;
73
+ this.tokensVerified = false;
75
74
  this.serverEmailConfig = serverEmailConfig;
76
75
  this.local = rootApiKey ? local : false; // Only allow setting local if rootApiKey is defined.
77
76
  (0, config_1.setBaseUrl)(this.local);
78
77
  this.autoupdate();
79
78
  this.initializeTokens(); // Calls the function to obtain tokens when creating the object.
80
79
  }
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
- }
122
80
  /**
123
81
  * Retrieves and caches authentication tokens.
124
82
  *
@@ -135,12 +93,11 @@ class DymoAPI {
135
93
  * with the token retrieval process.
136
94
  */
137
95
  async getTokens() {
138
- const cachedTokens = this.readTokensFromFile();
139
- if (cachedTokens) {
96
+ if (this.tokensResponse && !this.tokensVerified) {
140
97
  console.log(`[${config_1.default.lib.name}] Using cached tokens response.`);
141
- this.tokensResponse = cachedTokens;
142
98
  return this.tokensResponse;
143
99
  }
100
+ ;
144
101
  const tokens = {};
145
102
  if (this.rootApiKey)
146
103
  tokens.root = `Bearer ${this.rootApiKey}`;
@@ -155,7 +112,7 @@ class DymoAPI {
155
112
  if (tokens.api && response.data.api === false)
156
113
  throw customError(3000, "Invalid API token.");
157
114
  this.tokensResponse = response.data;
158
- this.writeTokensToFile(response.data);
115
+ this.tokensVerified = true;
159
116
  console.log(`[${config_1.default.lib.name}] Tokens initialized successfully.`);
160
117
  return this.tokensResponse;
161
118
  }
@@ -1,4 +1,3 @@
1
- import fs from "fs";
2
1
  import axios from "axios";
3
2
  import * as PublicAPI from "./branches/public.js";
4
3
  import * as PrivateAPI from "./branches/private.js";
@@ -33,54 +32,13 @@ class DymoAPI {
33
32
  this.rootApiKey = rootApiKey;
34
33
  this.apiKey = apiKey;
35
34
  this.tokensResponse = null;
36
- this.lastFetchTime = null;
35
+ this.tokensVerified = false;
37
36
  this.serverEmailConfig = serverEmailConfig;
38
37
  this.local = rootApiKey ? local : false; // Only allow setting local if rootApiKey is defined.
39
38
  setBaseUrl(this.local);
40
39
  this.autoupdate();
41
40
  this.initializeTokens(); // Calls the function to obtain tokens when creating the object.
42
41
  }
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
- }
84
42
  /**
85
43
  * Retrieves and caches authentication tokens.
86
44
  *
@@ -97,12 +55,11 @@ class DymoAPI {
97
55
  * with the token retrieval process.
98
56
  */
99
57
  async getTokens() {
100
- const cachedTokens = this.readTokensFromFile();
101
- if (cachedTokens) {
58
+ if (this.tokensResponse && !this.tokensVerified) {
102
59
  console.log(`[${config.lib.name}] Using cached tokens response.`);
103
- this.tokensResponse = cachedTokens;
104
60
  return this.tokensResponse;
105
61
  }
62
+ ;
106
63
  const tokens = {};
107
64
  if (this.rootApiKey)
108
65
  tokens.root = `Bearer ${this.rootApiKey}`;
@@ -117,7 +74,7 @@ class DymoAPI {
117
74
  if (tokens.api && response.data.api === false)
118
75
  throw customError(3000, "Invalid API token.");
119
76
  this.tokensResponse = response.data;
120
- this.writeTokensToFile(response.data);
77
+ this.tokensVerified = true;
121
78
  console.log(`[${config.lib.name}] Tokens initialized successfully.`);
122
79
  return this.tokensResponse;
123
80
  }
@@ -16,7 +16,7 @@ declare class DymoAPI {
16
16
  private rootApiKey;
17
17
  private apiKey;
18
18
  private tokensResponse;
19
- private lastFetchTime;
19
+ private tokensVerified;
20
20
  private serverEmailConfig?;
21
21
  private local;
22
22
  /**
@@ -43,28 +43,6 @@ 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;
68
46
  /**
69
47
  * Retrieves and caches authentication tokens.
70
48
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dymo-api",
3
- "version": "1.0.75",
3
+ "version": "1.0.76",
4
4
  "description": "Flow system for Dymo API.",
5
5
  "main": "dist/cjs/dymo-api.js",
6
6
  "module": "dist/esm/dymo-api.js",