dymo-api 1.0.72 → 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.
@@ -42,7 +42,7 @@ const axios_1 = __importDefault(require("axios"));
42
42
  const react_1 = __importDefault(require("react"));
43
43
  const promises_1 = __importDefault(require("fs/promises"));
44
44
  const tw_to_css_1 = require("tw-to-css");
45
- const config_1 = __importStar(require("../config"));
45
+ const config_1 = __importStar(require("../config/index.cjs"));
46
46
  const render_1 = require("@react-email/render");
47
47
  const customError = (code, message) => {
48
48
  return Object.assign(new Error(), { code, message: `[${config_1.default.lib.name}] ${message}` });
@@ -38,7 +38,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
39
  exports.newURLEncrypt = exports.isValidPwd = exports.satinizer = exports.getPrayerTimes = void 0;
40
40
  const axios_1 = __importDefault(require("axios"));
41
- const config_1 = __importStar(require("../config"));
41
+ const config_1 = __importStar(require("../config/index.cjs"));
42
42
  const customError = (code, message) => {
43
43
  return Object.assign(new Error(), { code, message: `[${config_1.default.lib.name}] ${message}` });
44
44
  };
@@ -36,11 +36,12 @@ 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
- const PublicAPI = __importStar(require("./branches/public"));
41
- const PrivateAPI = __importStar(require("./branches/private"));
42
- const config_1 = __importStar(require("./config"));
43
- const autoupdate_1 = require("./services/autoupdate");
41
+ const PublicAPI = __importStar(require("./branches/public.cjs"));
42
+ const PrivateAPI = __importStar(require("./branches/private.cjs"));
43
+ const config_1 = __importStar(require("./config/index.cjs"));
44
+ const autoupdate_1 = require("./services/autoupdate.cjs");
44
45
  const customError = (code, message) => {
45
46
  return Object.assign(new Error(), { code, message: `[${config_1.default.lib.name}] ${message}` });
46
47
  };
@@ -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 currentTime = new Date();
97
- if (this.tokensResponse && this.lastFetchTime && (currentTime.getTime() - this.lastFetchTime.getTime()) < 5 * 60 * 1000) {
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.lastFetchTime = currentTime;
156
+ this.writeTokensToFile(response.data);
117
157
  console.log(`[${config_1.default.lib.name}] Tokens initialized successfully.`);
118
158
  return this.tokensResponse;
119
159
  }
@@ -3,7 +3,7 @@ import axios from "axios";
3
3
  import React from "react";
4
4
  import fs from "fs/promises";
5
5
  import { twi } from "tw-to-css";
6
- import config, { BASE_URL } from "../config.js";
6
+ import config, { BASE_URL } from "../config/index.js";
7
7
  import { render } from "@react-email/render";
8
8
  const customError = (code, message) => {
9
9
  return Object.assign(new Error(), { code, message: `[${config.lib.name}] ${message}` });
@@ -1,5 +1,5 @@
1
1
  import axios from "axios";
2
- import config, { BASE_URL } from "../config.js";
2
+ import config, { BASE_URL } from "../config/index.js";
3
3
  const customError = (code, message) => {
4
4
  return Object.assign(new Error(), { code, message: `[${config.lib.name}] ${message}` });
5
5
  };
@@ -1,7 +1,8 @@
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";
4
- import config, { BASE_URL, setBaseUrl } from "./config.js";
5
+ import config, { BASE_URL, setBaseUrl } from "./config/index.js";
5
6
  import { checkForUpdates } from "./services/autoupdate.js";
6
7
  const customError = (code, message) => {
7
8
  return Object.assign(new Error(), { code, message: `[${config.lib.name}] ${message}` });
@@ -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 currentTime = new Date();
59
- if (this.tokensResponse && this.lastFetchTime && (currentTime.getTime() - this.lastFetchTime.getTime()) < 5 * 60 * 1000) {
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.lastFetchTime = currentTime;
118
+ this.writeTokensToFile(response.data);
79
119
  console.log(`[${config.lib.name}] Tokens initialized successfully.`);
80
120
  return this.tokensResponse;
81
121
  }
@@ -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
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dymo-api",
3
- "version": "1.0.72",
3
+ "version": "1.0.74",
4
4
  "description": "Flow system for Dymo API.",
5
5
  "main": "dist/cjs/dymo-api.js",
6
6
  "module": "dist/esm/dymo-api.js",