dymo-api 1.0.42 → 1.0.44

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.
@@ -28,22 +28,34 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.getRandom = exports.sendEmail = exports.isValidData = void 0;
30
30
  const axios_1 = __importDefault(require("axios"));
31
+ const react_1 = __importDefault(require("react"));
32
+ const { twi, twj } = require("tw-to-css");
31
33
  const config_1 = __importStar(require("../config"));
32
34
  const render_1 = require("@react-email/render");
33
35
  const customError = (code, message) => {
34
36
  return Object.assign(new Error(), { code, message: `[${config_1.default.lib.name}] ${message}` });
35
37
  };
38
+ const convertTailwindToInlineCss = (htmlContent) => {
39
+ return htmlContent.replace(/class="([^"]+)"/g, (match, classList) => {
40
+ return match.replace(classList, twi(classList, { minify: true, merge: true }));
41
+ });
42
+ };
43
+ /**
44
+ * Validates the provided data using a secure verification endpoint.
45
+ *
46
+ * @param token - A string or null representing the authentication token. Must not be null.
47
+ * @param data - An object adhering to the Validator interface, containing at least one of the following fields:
48
+ * email, phone, domain, creditCard, ip, or wallet.
49
+ *
50
+ * @returns A promise that resolves to the response data from the verification endpoint.
51
+ *
52
+ * @throws Will throw an error if the token is null, if none of the required fields are present in the data,
53
+ * or if an error occurs during the verification request.
54
+ */
36
55
  const isValidData = async (token, data) => {
37
56
  if (token === null)
38
57
  throw customError(3000, "Invalid private token.");
39
- let i = false;
40
- for (const key in data) {
41
- if (data.hasOwnProperty(key) && (key === "email" || key === "phone" || key === "domain" || key === "creditCard" || key === "ip" || key === "wallet")) {
42
- i = true;
43
- break;
44
- }
45
- }
46
- if (!i)
58
+ if (!Object.keys(data).some((key) => ["email", "phone", "domain", "creditCard", "ip", "wallet"].includes(key) && data.hasOwnProperty(key)))
47
59
  throw customError(1500, "You must provide at least one parameter.");
48
60
  try {
49
61
  const response = await axios_1.default.post(`${config_1.BASE_URL}/v1/private/secure/verify`, data, { headers: { "Authorization": token } });
@@ -54,6 +66,19 @@ const isValidData = async (token, data) => {
54
66
  }
55
67
  };
56
68
  exports.isValidData = isValidData;
69
+ /**
70
+ * Sends an email using the configured email client settings.
71
+ *
72
+ * This method requires a valid private token to be set.
73
+ *
74
+ * @param token - A string or null representing the authentication token. Must not be null.
75
+ * @param data - An object adhering to the SendEmail interface, containing the following fields:
76
+ * from, to, subject, html, and optionally react and options.
77
+ *
78
+ * @returns A promise that resolves to the response from the server.
79
+ *
80
+ * @throws Will throw an error if there is an issue with the email sending process.
81
+ */
57
82
  const sendEmail = async (token, data) => {
58
83
  if (token === null)
59
84
  throw customError(3000, "Invalid private token.");
@@ -63,7 +88,7 @@ const sendEmail = async (token, data) => {
63
88
  throw customError(1500, "You must provide an email to be sent to.");
64
89
  if (!data.subject)
65
90
  throw customError(1500, "You must provide a subject for the email to be sent.");
66
- if (!data.html && !data.react)
91
+ if (!data.html && !data.react && !react_1.default.isValidElement(data.react))
67
92
  throw customError(1500, "You must provide HTML or a React component.");
68
93
  if (data.html && data.react)
69
94
  throw customError(1500, "You must provide only HTML or a React component, not both.");
@@ -73,6 +98,8 @@ const sendEmail = async (token, data) => {
73
98
  data.html = await (0, render_1.render)(data.react);
74
99
  delete data.react;
75
100
  }
101
+ if (data.options && data.options.composeTailwindClasses)
102
+ data.html = convertTailwindToInlineCss(data.html);
76
103
  }
77
104
  catch (error) {
78
105
  throw customError(1500, `An error occurred while rendering your React component. Details: ${error}`);
@@ -86,6 +113,19 @@ const sendEmail = async (token, data) => {
86
113
  }
87
114
  };
88
115
  exports.sendEmail = sendEmail;
116
+ /**
117
+ * Retrieves a random number within a specified range using a secure endpoint.
118
+ *
119
+ * @param token - A string or null representing the authentication token. Must not be null.
120
+ * @param data - An object adhering to the SRNG interface, containing 'min' and 'max' fields,
121
+ * which define the inclusive range within which the random number should be generated.
122
+ *
123
+ * @returns A promise that resolves to the response data containing the random number.
124
+ *
125
+ * @throws Will throw an error if the token is null, if 'min' or 'max' are not defined,
126
+ * if 'min' is not less than 'max', if 'min' or 'max' are out of the allowed range,
127
+ * or if an error occurs during the request to the random number generator endpoint.
128
+ */
89
129
  const getRandom = async (token, data) => {
90
130
  if (token === null)
91
131
  throw customError(3000, "Invalid private token.");
@@ -169,8 +169,10 @@ class DymoAPI {
169
169
  * @param {string} data.from - The email address from which the email will be sent.
170
170
  * @param {string} data.to - The email address to which the email will be sent.
171
171
  * @param {string} data.subject - The subject of the email.
172
- * @param {string} data.html - The HTML content of the email.
173
- * @param {React.ReactElement} data.react - The React component to be rendered as the email content.
172
+ * @param {string} [data.html] - The HTML content of the email.
173
+ * @param {React.ReactElement} [data.react] - The React component to be rendered as the email content.
174
+ * @param {Object} [data.options] - Content configuration options.
175
+ * @param {boolean} [data.options.composeTailwindClasses] - Whether to compose tailwind classes.
174
176
  * @returns {Promise<Object>} A promise that resolves to the response from the server.
175
177
  * @throws Will throw an error if there is an issue with the email sending process.
176
178
  */
@@ -261,3 +263,5 @@ class DymoAPI {
261
263
  }
262
264
  }
263
265
  exports.default = DymoAPI;
266
+ //@ts-ignore
267
+ module.exports = DymoAPI;
@@ -28,22 +28,34 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.getRandom = exports.sendEmail = exports.isValidData = void 0;
30
30
  const axios_1 = __importDefault(require("axios"));
31
+ const react_1 = __importDefault(require("react"));
32
+ const { twi, twj } = require("tw-to-css");
31
33
  const config_1 = __importStar(require("../config"));
32
34
  const render_1 = require("@react-email/render");
33
35
  const customError = (code, message) => {
34
36
  return Object.assign(new Error(), { code, message: `[${config_1.default.lib.name}] ${message}` });
35
37
  };
38
+ const convertTailwindToInlineCss = (htmlContent) => {
39
+ return htmlContent.replace(/class="([^"]+)"/g, (match, classList) => {
40
+ return match.replace(classList, twi(classList, { minify: true, merge: true }));
41
+ });
42
+ };
43
+ /**
44
+ * Validates the provided data using a secure verification endpoint.
45
+ *
46
+ * @param token - A string or null representing the authentication token. Must not be null.
47
+ * @param data - An object adhering to the Validator interface, containing at least one of the following fields:
48
+ * email, phone, domain, creditCard, ip, or wallet.
49
+ *
50
+ * @returns A promise that resolves to the response data from the verification endpoint.
51
+ *
52
+ * @throws Will throw an error if the token is null, if none of the required fields are present in the data,
53
+ * or if an error occurs during the verification request.
54
+ */
36
55
  const isValidData = async (token, data) => {
37
56
  if (token === null)
38
57
  throw customError(3000, "Invalid private token.");
39
- let i = false;
40
- for (const key in data) {
41
- if (data.hasOwnProperty(key) && (key === "email" || key === "phone" || key === "domain" || key === "creditCard" || key === "ip" || key === "wallet")) {
42
- i = true;
43
- break;
44
- }
45
- }
46
- if (!i)
58
+ if (!Object.keys(data).some((key) => ["email", "phone", "domain", "creditCard", "ip", "wallet"].includes(key) && data.hasOwnProperty(key)))
47
59
  throw customError(1500, "You must provide at least one parameter.");
48
60
  try {
49
61
  const response = await axios_1.default.post(`${config_1.BASE_URL}/v1/private/secure/verify`, data, { headers: { "Authorization": token } });
@@ -54,6 +66,19 @@ const isValidData = async (token, data) => {
54
66
  }
55
67
  };
56
68
  exports.isValidData = isValidData;
69
+ /**
70
+ * Sends an email using the configured email client settings.
71
+ *
72
+ * This method requires a valid private token to be set.
73
+ *
74
+ * @param token - A string or null representing the authentication token. Must not be null.
75
+ * @param data - An object adhering to the SendEmail interface, containing the following fields:
76
+ * from, to, subject, html, and optionally react and options.
77
+ *
78
+ * @returns A promise that resolves to the response from the server.
79
+ *
80
+ * @throws Will throw an error if there is an issue with the email sending process.
81
+ */
57
82
  const sendEmail = async (token, data) => {
58
83
  if (token === null)
59
84
  throw customError(3000, "Invalid private token.");
@@ -63,7 +88,7 @@ const sendEmail = async (token, data) => {
63
88
  throw customError(1500, "You must provide an email to be sent to.");
64
89
  if (!data.subject)
65
90
  throw customError(1500, "You must provide a subject for the email to be sent.");
66
- if (!data.html && !data.react)
91
+ if (!data.html && !data.react && !react_1.default.isValidElement(data.react))
67
92
  throw customError(1500, "You must provide HTML or a React component.");
68
93
  if (data.html && data.react)
69
94
  throw customError(1500, "You must provide only HTML or a React component, not both.");
@@ -73,6 +98,8 @@ const sendEmail = async (token, data) => {
73
98
  data.html = await (0, render_1.render)(data.react);
74
99
  delete data.react;
75
100
  }
101
+ if (data.options && data.options.composeTailwindClasses)
102
+ data.html = convertTailwindToInlineCss(data.html);
76
103
  }
77
104
  catch (error) {
78
105
  throw customError(1500, `An error occurred while rendering your React component. Details: ${error}`);
@@ -86,6 +113,19 @@ const sendEmail = async (token, data) => {
86
113
  }
87
114
  };
88
115
  exports.sendEmail = sendEmail;
116
+ /**
117
+ * Retrieves a random number within a specified range using a secure endpoint.
118
+ *
119
+ * @param token - A string or null representing the authentication token. Must not be null.
120
+ * @param data - An object adhering to the SRNG interface, containing 'min' and 'max' fields,
121
+ * which define the inclusive range within which the random number should be generated.
122
+ *
123
+ * @returns A promise that resolves to the response data containing the random number.
124
+ *
125
+ * @throws Will throw an error if the token is null, if 'min' or 'max' are not defined,
126
+ * if 'min' is not less than 'max', if 'min' or 'max' are out of the allowed range,
127
+ * or if an error occurs during the request to the random number generator endpoint.
128
+ */
89
129
  const getRandom = async (token, data) => {
90
130
  if (token === null)
91
131
  throw customError(3000, "Invalid private token.");
@@ -169,8 +169,10 @@ class DymoAPI {
169
169
  * @param {string} data.from - The email address from which the email will be sent.
170
170
  * @param {string} data.to - The email address to which the email will be sent.
171
171
  * @param {string} data.subject - The subject of the email.
172
- * @param {string} data.html - The HTML content of the email.
173
- * @param {React.ReactElement} data.react - The React component to be rendered as the email content.
172
+ * @param {string} [data.html] - The HTML content of the email.
173
+ * @param {React.ReactElement} [data.react] - The React component to be rendered as the email content.
174
+ * @param {Object} [data.options] - Content configuration options.
175
+ * @param {boolean} [data.options.composeTailwindClasses] - Whether to compose tailwind classes.
174
176
  * @returns {Promise<Object>} A promise that resolves to the response from the server.
175
177
  * @throws Will throw an error if there is an issue with the email sending process.
176
178
  */
@@ -261,3 +263,5 @@ class DymoAPI {
261
263
  }
262
264
  }
263
265
  exports.default = DymoAPI;
266
+ //@ts-ignore
267
+ module.exports = DymoAPI;
@@ -1,4 +1,42 @@
1
1
  import * as Interfaces from "../lib/interfaces";
2
+ /**
3
+ * Validates the provided data using a secure verification endpoint.
4
+ *
5
+ * @param token - A string or null representing the authentication token. Must not be null.
6
+ * @param data - An object adhering to the Validator interface, containing at least one of the following fields:
7
+ * email, phone, domain, creditCard, ip, or wallet.
8
+ *
9
+ * @returns A promise that resolves to the response data from the verification endpoint.
10
+ *
11
+ * @throws Will throw an error if the token is null, if none of the required fields are present in the data,
12
+ * or if an error occurs during the verification request.
13
+ */
2
14
  export declare const isValidData: (token: string | null, data: Interfaces.Validator) => Promise<any>;
15
+ /**
16
+ * Sends an email using the configured email client settings.
17
+ *
18
+ * This method requires a valid private token to be set.
19
+ *
20
+ * @param token - A string or null representing the authentication token. Must not be null.
21
+ * @param data - An object adhering to the SendEmail interface, containing the following fields:
22
+ * from, to, subject, html, and optionally react and options.
23
+ *
24
+ * @returns A promise that resolves to the response from the server.
25
+ *
26
+ * @throws Will throw an error if there is an issue with the email sending process.
27
+ */
3
28
  export declare const sendEmail: (token: string | null, data: Interfaces.SendEmail) => Promise<any>;
29
+ /**
30
+ * Retrieves a random number within a specified range using a secure endpoint.
31
+ *
32
+ * @param token - A string or null representing the authentication token. Must not be null.
33
+ * @param data - An object adhering to the SRNG interface, containing 'min' and 'max' fields,
34
+ * which define the inclusive range within which the random number should be generated.
35
+ *
36
+ * @returns A promise that resolves to the response data containing the random number.
37
+ *
38
+ * @throws Will throw an error if the token is null, if 'min' or 'max' are not defined,
39
+ * if 'min' is not less than 'max', if 'min' or 'max' are out of the allowed range,
40
+ * or if an error occurs during the request to the random number generator endpoint.
41
+ */
4
42
  export declare const getRandom: (token: string | null, data: Interfaces.SRNG) => Promise<any>;
@@ -100,8 +100,10 @@ declare class DymoAPI {
100
100
  * @param {string} data.from - The email address from which the email will be sent.
101
101
  * @param {string} data.to - The email address to which the email will be sent.
102
102
  * @param {string} data.subject - The subject of the email.
103
- * @param {string} data.html - The HTML content of the email.
104
- * @param {React.ReactElement} data.react - The React component to be rendered as the email content.
103
+ * @param {string} [data.html] - The HTML content of the email.
104
+ * @param {React.ReactElement} [data.react] - The React component to be rendered as the email content.
105
+ * @param {Object} [data.options] - Content configuration options.
106
+ * @param {boolean} [data.options.composeTailwindClasses] - Whether to compose tailwind classes.
105
107
  * @returns {Promise<Object>} A promise that resolves to the response from the server.
106
108
  * @throws Will throw an error if there is an issue with the email sending process.
107
109
  */
@@ -1,4 +1,3 @@
1
- import type * as React from "react";
2
1
  interface PhoneData {
3
2
  iso: any;
4
3
  phone: string;
@@ -22,17 +21,14 @@ export interface SRNG {
22
21
  max: number;
23
22
  quantity?: number;
24
23
  }
25
- export type SendEmail = {
24
+ export interface SendEmail {
26
25
  from: string;
27
26
  to: string;
28
27
  subject: string;
29
- html: string;
28
+ html?: string;
30
29
  react?: never;
31
- } | {
32
- from: string;
33
- to: string;
34
- subject: string;
35
- html?: never;
36
- react: React.ReactNode;
37
- };
30
+ options?: {
31
+ composeTailwindClasses?: boolean;
32
+ };
33
+ }
38
34
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dymo-api",
3
- "version": "1.0.42",
3
+ "version": "1.0.44",
4
4
  "description": "Flow system for Dymo API.",
5
5
  "main": "dist/cjs/dymo-api.js",
6
6
  "module": "dist/esm/dymo-api.js",
@@ -20,7 +20,7 @@
20
20
  "build": "rimraf dist && tsc -p tsconfig.cjs.json && tsc -p tsconfig.esm.json",
21
21
  "start": "ts-node src/dymo-api.ts",
22
22
  "prepublishOnly": "npm run build",
23
- "test": "echo \"Error: no test specified\" && exit 1"
23
+ "test": "node --max-old-space-size=4096 test/index.js"
24
24
  },
25
25
  "repository": {
26
26
  "url": "https://github.com/TPEOficial/dymo-api-node"
@@ -41,7 +41,8 @@
41
41
  "@react-email/render": "1.0.1",
42
42
  "@types/react": "^18.3.5",
43
43
  "axios": "^1.6.8",
44
- "rimraf": "^6.0.1"
44
+ "rimraf": "^6.0.1",
45
+ "tw-to-css": "0.0.12"
45
46
  },
46
47
  "contributors": [
47
48
  "TPEOficial (https://github.com/TPEOficial)",
@@ -49,7 +50,7 @@
49
50
  ],
50
51
  "devDependencies": {
51
52
  "@types/axios": "^0.9.36",
52
- "@types/node": "^22.0.2",
53
+ "@types/node": "^22.9.0",
53
54
  "ts-node": "^10.9.2",
54
55
  "typescript": "^5.5.4"
55
56
  }