dymo-api 1.0.74 → 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,52 +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("../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
- }
120
80
  /**
121
81
  * Retrieves and caches authentication tokens.
122
82
  *
@@ -133,12 +93,11 @@ class DymoAPI {
133
93
  * with the token retrieval process.
134
94
  */
135
95
  async getTokens() {
136
- const cachedTokens = this.readTokensFromFile();
137
- if (cachedTokens) {
96
+ if (this.tokensResponse && !this.tokensVerified) {
138
97
  console.log(`[${config_1.default.lib.name}] Using cached tokens response.`);
139
- this.tokensResponse = cachedTokens;
140
98
  return this.tokensResponse;
141
99
  }
100
+ ;
142
101
  const tokens = {};
143
102
  if (this.rootApiKey)
144
103
  tokens.root = `Bearer ${this.rootApiKey}`;
@@ -153,7 +112,7 @@ class DymoAPI {
153
112
  if (tokens.api && response.data.api === false)
154
113
  throw customError(3000, "Invalid API token.");
155
114
  this.tokensResponse = response.data;
156
- this.writeTokensToFile(response.data);
115
+ this.tokensVerified = true;
157
116
  console.log(`[${config_1.default.lib.name}] Tokens initialized successfully.`);
158
117
  return this.tokensResponse;
159
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,52 +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("../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
- }
82
42
  /**
83
43
  * Retrieves and caches authentication tokens.
84
44
  *
@@ -95,12 +55,11 @@ class DymoAPI {
95
55
  * with the token retrieval process.
96
56
  */
97
57
  async getTokens() {
98
- const cachedTokens = this.readTokensFromFile();
99
- if (cachedTokens) {
58
+ if (this.tokensResponse && !this.tokensVerified) {
100
59
  console.log(`[${config.lib.name}] Using cached tokens response.`);
101
- this.tokensResponse = cachedTokens;
102
60
  return this.tokensResponse;
103
61
  }
62
+ ;
104
63
  const tokens = {};
105
64
  if (this.rootApiKey)
106
65
  tokens.root = `Bearer ${this.rootApiKey}`;
@@ -115,7 +74,7 @@ class DymoAPI {
115
74
  if (tokens.api && response.data.api === false)
116
75
  throw customError(3000, "Invalid API token.");
117
76
  this.tokensResponse = response.data;
118
- this.writeTokensToFile(response.data);
77
+ this.tokensVerified = true;
119
78
  console.log(`[${config.lib.name}] Tokens initialized successfully.`);
120
79
  return this.tokensResponse;
121
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.74",
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",