@tomei/mailer 0.5.21 → 0.6.0

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/eslint.config.mjs CHANGED
@@ -1,58 +1,58 @@
1
- import eslintPlugin from "@typescript-eslint/eslint-plugin";
2
- import parser from "@typescript-eslint/parser";
3
- import importPlugin from 'eslint-plugin-import'
4
- import path from 'path';
5
- import { fileURLToPath } from 'url';
6
-
7
- const __filename = fileURLToPath(import.meta.url);
8
- const __dirname = path.dirname(__filename);
9
-
10
- export default [
11
- {
12
- languageOptions: {
13
- parser: parser,
14
- ecmaVersion: "latest", // Allows modern ECMAScript features
15
- sourceType: "module", // Allows for the use of imports
16
- parserOptions: {
17
- tsconfigRootDir: __dirname,
18
- project: './tsconfig.json'
19
- },
20
- },
21
- plugins: {
22
- "@typescript-eslint": eslintPlugin,
23
- import: importPlugin
24
- },
25
- rules: {
26
- "no-console": "off",
27
- "@typescript-eslint/no-explicit-any": "off",
28
- "@typescript-eslint/no-var-requires": "off",
29
- "@typescript-eslint/explicit-module-boundary-types": "off",
30
- "import/prefer-default-export": "off",
31
- "@typescript-eslint/no-unused-vars": "warn",
32
- "no-useless-catch": "off",
33
- "@typescript-eslint/no-non-null-assertion": "off",
34
- "@typescript-eslint/no-empty-function": "off",
35
- "@typescript-eslint/no-empty-interface": "off",
36
- "@typescript-eslint/no-inferrable-types": "off",
37
- "@typescript-eslint/no-namespace": "off",
38
- "@typescript-eslint/no-use-before-define": "off",
39
- "@typescript-eslint/no-unsafe-assignment": "off",
40
- "@typescript-eslint/no-unsafe-call": "off",
41
- "@typescript-eslint/no-unsafe-member-access": "off",
42
- "@typescript-eslint/no-unsafe-return": "off",
43
- "@typescript-eslint/restrict-template-expressions": "off",
44
- "no-useless-escape": "off",
45
- "import/no-relative-parent-imports": "error", // Enforce relative imports only
46
- "import/no-absolute-path": "error", // Block absolute imports outside aliases
47
- "import/no-unresolved": ["error", { commonjs: true }],
48
- },
49
- settings: {
50
- "import/resolver": {
51
- typescript: {
52
- alwaysTryTypes: true,
53
- }
54
- }
55
- },
56
- ignores: ["node_modules", "dist/**/*", "eslint.config.mjs"],
57
- },
58
- ];
1
+ import eslintPlugin from "@typescript-eslint/eslint-plugin";
2
+ import parser from "@typescript-eslint/parser";
3
+ import importPlugin from 'eslint-plugin-import'
4
+ import path from 'path';
5
+ import { fileURLToPath } from 'url';
6
+
7
+ const __filename = fileURLToPath(import.meta.url);
8
+ const __dirname = path.dirname(__filename);
9
+
10
+ export default [
11
+ {
12
+ languageOptions: {
13
+ parser: parser,
14
+ ecmaVersion: "latest", // Allows modern ECMAScript features
15
+ sourceType: "module", // Allows for the use of imports
16
+ parserOptions: {
17
+ tsconfigRootDir: __dirname,
18
+ project: './tsconfig.json'
19
+ },
20
+ },
21
+ plugins: {
22
+ "@typescript-eslint": eslintPlugin,
23
+ import: importPlugin
24
+ },
25
+ rules: {
26
+ "no-console": "off",
27
+ "@typescript-eslint/no-explicit-any": "off",
28
+ "@typescript-eslint/no-var-requires": "off",
29
+ "@typescript-eslint/explicit-module-boundary-types": "off",
30
+ "import/prefer-default-export": "off",
31
+ "@typescript-eslint/no-unused-vars": "warn",
32
+ "no-useless-catch": "off",
33
+ "@typescript-eslint/no-non-null-assertion": "off",
34
+ "@typescript-eslint/no-empty-function": "off",
35
+ "@typescript-eslint/no-empty-interface": "off",
36
+ "@typescript-eslint/no-inferrable-types": "off",
37
+ "@typescript-eslint/no-namespace": "off",
38
+ "@typescript-eslint/no-use-before-define": "off",
39
+ "@typescript-eslint/no-unsafe-assignment": "off",
40
+ "@typescript-eslint/no-unsafe-call": "off",
41
+ "@typescript-eslint/no-unsafe-member-access": "off",
42
+ "@typescript-eslint/no-unsafe-return": "off",
43
+ "@typescript-eslint/restrict-template-expressions": "off",
44
+ "no-useless-escape": "off",
45
+ "import/no-relative-parent-imports": "error", // Enforce relative imports only
46
+ "import/no-absolute-path": "error", // Block absolute imports outside aliases
47
+ "import/no-unresolved": ["error", { commonjs: true }],
48
+ },
49
+ settings: {
50
+ "import/resolver": {
51
+ typescript: {
52
+ alwaysTryTypes: true,
53
+ }
54
+ }
55
+ },
56
+ ignores: ["node_modules", "dist/**/*", "eslint.config.mjs"],
57
+ },
58
+ ];
package/package.json CHANGED
@@ -1,47 +1,47 @@
1
- {
2
- "name": "@tomei/mailer",
3
- "version": "0.5.21",
4
- "description": "Tomei Mailer Package",
5
- "main": "dist/index.js",
6
- "scripts": {
7
- "start:dev": "tsc -w",
8
- "build": "node_modules/.bin/tsc",
9
- "format": "prettier --write \"src/**/*.ts\"",
10
- "lint": "npx eslint . --fix",
11
- "test": "jest --forceExit --detectOpenHandles"
12
- },
13
- "author": "Tomei",
14
- "license": "ISC",
15
- "devDependencies": {
16
- "@commitlint/cli": "^19.6.0",
17
- "@commitlint/config-conventional": "^19.6.0",
18
- "@eslint/js": "^9.15.0",
19
- "@tsconfig/node18": "^18.2.4",
20
- "@types/jest": "^29.5.14",
21
- "@types/node": "^22.9.3",
22
- "@types/nodemailer": "^6.4.17",
23
- "@typescript-eslint/eslint-plugin": "^8.15.0",
24
- "@typescript-eslint/parser": "^8.15.0",
25
- "eslint": "^9.15.0",
26
- "eslint-config-prettier": "^9.1.0",
27
- "eslint-plugin-import": "^2.31.0",
28
- "eslint-plugin-prettier": "^5.2.1",
29
- "globals": "^15.12.0",
30
- "husky": "^9.1.7",
31
- "jest": "^29.7.0",
32
- "nodemailer-mock": "^2.0.6",
33
- "prettier": "^3.3.3",
34
- "ts-jest": "^29.2.5",
35
- "ts-node": "^10.9.2",
36
- "tsc-watch": "^6.2.1",
37
- "typescript": "^5.7.2",
38
- "typescript-eslint": "^8.15.0"
39
- },
40
- "publishConfig": {
41
- "access": "public"
42
- },
43
- "peerDependencies": {
44
- "@tomei/config": "^0.3.21",
45
- "nodemailer": "^6.9.16"
46
- }
47
- }
1
+ {
2
+ "name": "@tomei/mailer",
3
+ "version": "0.6.0",
4
+ "description": "Tomei Mailer Package",
5
+ "main": "dist/index.js",
6
+ "scripts": {
7
+ "start:dev": "tsc -w",
8
+ "build": "tsc",
9
+ "format": "prettier --write \"src/**/*.ts\"",
10
+ "lint": "npx eslint . --fix",
11
+ "test": "jest --forceExit --detectOpenHandles",
12
+ "prepare": "husky"
13
+ },
14
+ "author": "Tomei",
15
+ "license": "ISC",
16
+ "devDependencies": {
17
+ "@commitlint/cli": "^19.8.1",
18
+ "@commitlint/config-conventional": "^19.8.1",
19
+ "@eslint/js": "^9.35.0",
20
+ "@tsconfig/node18": "^18.2.4",
21
+ "@types/jest": "^30.0.0",
22
+ "@types/node": "^24.3.1",
23
+ "@types/nodemailer": "^7.0.1",
24
+ "@typescript-eslint/eslint-plugin": "^8.43.0",
25
+ "@typescript-eslint/parser": "^8.43.0",
26
+ "eslint": "^9.35.0",
27
+ "eslint-config-prettier": "^10.1.8",
28
+ "eslint-plugin-import": "^2.31.0",
29
+ "eslint-plugin-prettier": "^5.5.4",
30
+ "globals": "^16.4.0",
31
+ "husky": "^9.1.7",
32
+ "jest": "^30.1.3",
33
+ "nodemailer-mock": "^2.0.9",
34
+ "prettier": "^3.6.2",
35
+ "ts-jest": "^29.4.1",
36
+ "ts-node": "^10.9.2",
37
+ "tsc-watch": "^7.1.1",
38
+ "typescript": "^5.9.2"
39
+ },
40
+ "publishConfig": {
41
+ "access": "public"
42
+ },
43
+ "peerDependencies": {
44
+ "@tomei/config": "^0.3.22",
45
+ "nodemailer": "^6.10.1"
46
+ }
47
+ }
@@ -1,13 +1,13 @@
1
- sonar.projectKey=all-tomei-projects_mailer
2
- sonar.organization=all-tomei-projects
3
-
4
- # This is the name and version displayed in the SonarCloud UI.
5
- #sonar.projectName=Mailer
6
- #sonar.projectVersion=1.0
7
-
8
-
9
- # Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
10
- #sonar.sources=.
11
-
12
- # Encoding of the source code. Default is default system encoding
1
+ sonar.projectKey=all-tomei-projects_mailer
2
+ sonar.organization=all-tomei-projects
3
+
4
+ # This is the name and version displayed in the SonarCloud UI.
5
+ #sonar.projectName=Mailer
6
+ #sonar.projectVersion=1.0
7
+
8
+
9
+ # Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
10
+ #sonar.sources=.
11
+
12
+ # Encoding of the source code. Default is default system encoding
13
13
  #sonar.sourceEncoding=UTF-8
@@ -1,4 +1,4 @@
1
- export enum EmailStatus {
2
- Sent = 'Sent',
3
- Failed = 'Failed',
4
- }
1
+ export enum EmailStatus {
2
+ Sent = 'Sent',
3
+ Failed = 'Failed',
4
+ }
package/src/enum/index.ts CHANGED
@@ -1 +1 @@
1
- export { EmailStatus } from './email-status.enum';
1
+ export { EmailStatus } from './email-status.enum';
@@ -1,18 +1,18 @@
1
- export interface ILogTransactionOption {
2
- options: {
3
- from: string;
4
- to: string;
5
- subject: string;
6
- };
7
- logData: {
8
- systemCode: string;
9
- apiUrl: string;
10
- transactionName: string;
11
- dbName: string;
12
- tableName: string;
13
- dataBefore: string;
14
- dataAfter: string;
15
- performAt: Date;
16
- note: string;
17
- };
18
- }
1
+ export interface ILogTransactionOption {
2
+ options: {
3
+ from: string;
4
+ to: string;
5
+ subject: string;
6
+ };
7
+ logData: {
8
+ systemCode: string;
9
+ apiUrl: string;
10
+ transactionName: string;
11
+ dbName: string;
12
+ tableName: string;
13
+ dataBefore: string;
14
+ dataAfter: string;
15
+ performAt: Date;
16
+ note: string;
17
+ };
18
+ }
@@ -1,8 +1,8 @@
1
- export interface ISendMailConfig {
2
- from: string;
3
- to: string | string[];
4
- cc?: string | string[];
5
- attachments?: any[];
6
- subject: string;
7
- html: any;
8
- }
1
+ export interface ISendMailConfig {
2
+ from: string;
3
+ to: string | string[];
4
+ cc?: string | string[];
5
+ attachments?: any[];
6
+ subject: string;
7
+ html: any;
8
+ }
@@ -1,12 +1,12 @@
1
- import { EmailStatus } from '../enum/email-status.enum';
2
-
3
- export interface IMailLog {
4
- duration: number;
5
- from: string;
6
- to: string;
7
- cc: string;
8
- subject: string;
9
- rawContent: string;
10
- date: Date;
11
- status: EmailStatus;
12
- }
1
+ import { EmailStatus } from '../enum/email-status.enum';
2
+
3
+ export interface IMailLog {
4
+ duration: number;
5
+ from: string;
6
+ to: string;
7
+ cc: string;
8
+ subject: string;
9
+ rawContent: string;
10
+ date: Date;
11
+ status: EmailStatus;
12
+ }
@@ -1,2 +1,2 @@
1
- export { MailerBase } from './mailer.base';
2
- export { SMTPMailer } from './smtp-mailer';
1
+ export { MailerBase } from './mailer.base';
2
+ export { SMTPMailer } from './smtp-mailer';
@@ -1,110 +1,110 @@
1
- import { IMailLog } from 'src/interfaces/mail-log.interface';
2
- import { ILogTransactionOption } from '../interfaces/log-transaction-options.interface';
3
- import { ISendMailConfig } from '../interfaces/mail-config.interface';
4
- import * as fs from 'fs';
5
- import * as path from 'path';
6
- import { EmailStatus } from '../enum/email-status.enum';
7
- import { ComponentConfig } from '@tomei/config';
8
-
9
- export abstract class MailerBase {
10
- abstract sendMail(options: any): Promise<any>;
11
-
12
- async send(options: ISendMailConfig): Promise<any> {
13
- // start timer
14
- const start = Date.now();
15
- let status: EmailStatus;
16
-
17
- // send email
18
- try {
19
- await this.sendMail(options);
20
- status = EmailStatus.Sent;
21
- } catch (error) {
22
- status = EmailStatus.Failed;
23
- } finally {
24
- const duration = Date.now() - start;
25
- const isLogEnabled = ComponentConfig.getComponentConfigValue(
26
- '@tomei/mailer',
27
- 'isLogEnabled',
28
- );
29
- if (isLogEnabled) {
30
- MailerBase.log({
31
- duration,
32
- from: options.from,
33
- to:
34
- typeof options.to === 'string' ? options.to : options.to.join(','),
35
- cc:
36
- typeof options.cc === 'string'
37
- ? options.cc
38
- : options.cc
39
- ? options.cc.join(',')
40
- : '',
41
- subject: options.subject,
42
- rawContent: options.html,
43
- date: new Date(),
44
- status: status,
45
- });
46
- }
47
- }
48
- }
49
-
50
- async logTransaction(logOptions: ILogTransactionOption): Promise<void> {
51
- const startTime = new Date().getTime();
52
- let endTime: number;
53
- const options: any = {
54
- from: logOptions.options.from,
55
- to: logOptions.options.to,
56
- subject: logOptions.options.subject,
57
- html: `<div>${JSON.stringify(logOptions.logData)}</div>`,
58
- cc: '',
59
- };
60
- let result: EmailStatus;
61
-
62
- try {
63
- await this.sendMail(options);
64
- result = EmailStatus.Sent;
65
- } catch (error) {
66
- //Retry once
67
- try {
68
- console.info('Retry to send mail');
69
- await this.sendMail(options);
70
- } catch (retryError) {
71
- result = EmailStatus.Failed;
72
- console.error(retryError);
73
- }
74
- } finally {
75
- endTime = new Date().getTime();
76
- const duration = endTime - startTime;
77
- const mailLogOptions: IMailLog = {
78
- duration,
79
- from: options.from,
80
- to: options.to,
81
- cc: '',
82
- subject: options.subject,
83
- rawContent: options.html,
84
- date: new Date(),
85
- status: result,
86
- };
87
- MailerBase.log(mailLogOptions);
88
- }
89
- }
90
-
91
- static log(mailLogOptions: IMailLog): void {
92
- try {
93
- const logFolderPath = path.join(process.cwd(), 'mail-log');
94
-
95
- if (!fs.existsSync(logFolderPath)) {
96
- fs.mkdirSync(logFolderPath);
97
- }
98
-
99
- const currentDate = new Date();
100
- const formattedDate = currentDate.toISOString().slice(0, 10);
101
- const logFileName = `${formattedDate}.log`;
102
- const logFilePath = path.join(logFolderPath, logFileName);
103
- const stringData = JSON.stringify(mailLogOptions);
104
-
105
- fs.appendFileSync(logFilePath, '\n' + stringData);
106
- } catch (error) {
107
- throw new Error('Error occurred while logging mail');
108
- }
109
- }
110
- }
1
+ import { IMailLog } from 'src/interfaces/mail-log.interface';
2
+ import { ILogTransactionOption } from '../interfaces/log-transaction-options.interface';
3
+ import { ISendMailConfig } from '../interfaces/mail-config.interface';
4
+ import * as fs from 'fs';
5
+ import * as path from 'path';
6
+ import { EmailStatus } from '../enum/email-status.enum';
7
+ import { ComponentConfig } from '@tomei/config';
8
+
9
+ export abstract class MailerBase {
10
+ abstract sendMail(options: any): Promise<any>;
11
+
12
+ async send(options: ISendMailConfig): Promise<any> {
13
+ // start timer
14
+ const start = Date.now();
15
+ let status: EmailStatus;
16
+
17
+ // send email
18
+ try {
19
+ await this.sendMail(options);
20
+ status = EmailStatus.Sent;
21
+ } catch (error) {
22
+ status = EmailStatus.Failed;
23
+ } finally {
24
+ const duration = Date.now() - start;
25
+ const isLogEnabled = ComponentConfig.getComponentConfigValue(
26
+ '@tomei/mailer',
27
+ 'isLogEnabled',
28
+ );
29
+ if (isLogEnabled) {
30
+ MailerBase.log({
31
+ duration,
32
+ from: options.from,
33
+ to:
34
+ typeof options.to === 'string' ? options.to : options.to.join(','),
35
+ cc:
36
+ typeof options.cc === 'string'
37
+ ? options.cc
38
+ : options.cc
39
+ ? options.cc.join(',')
40
+ : '',
41
+ subject: options.subject,
42
+ rawContent: options.html,
43
+ date: new Date(),
44
+ status: status,
45
+ });
46
+ }
47
+ }
48
+ }
49
+
50
+ async logTransaction(logOptions: ILogTransactionOption): Promise<void> {
51
+ const startTime = new Date().getTime();
52
+ let endTime: number;
53
+ const options: any = {
54
+ from: logOptions.options.from,
55
+ to: logOptions.options.to,
56
+ subject: logOptions.options.subject,
57
+ html: `<div>${JSON.stringify(logOptions.logData)}</div>`,
58
+ cc: '',
59
+ };
60
+ let result: EmailStatus;
61
+
62
+ try {
63
+ await this.sendMail(options);
64
+ result = EmailStatus.Sent;
65
+ } catch (error) {
66
+ //Retry once
67
+ try {
68
+ console.info('Retry to send mail');
69
+ await this.sendMail(options);
70
+ } catch (retryError) {
71
+ result = EmailStatus.Failed;
72
+ console.error(retryError);
73
+ }
74
+ } finally {
75
+ endTime = new Date().getTime();
76
+ const duration = endTime - startTime;
77
+ const mailLogOptions: IMailLog = {
78
+ duration,
79
+ from: options.from,
80
+ to: options.to,
81
+ cc: '',
82
+ subject: options.subject,
83
+ rawContent: options.html,
84
+ date: new Date(),
85
+ status: result,
86
+ };
87
+ MailerBase.log(mailLogOptions);
88
+ }
89
+ }
90
+
91
+ static log(mailLogOptions: IMailLog): void {
92
+ try {
93
+ const logFolderPath = path.join(process.cwd(), 'mail-log');
94
+
95
+ if (!fs.existsSync(logFolderPath)) {
96
+ fs.mkdirSync(logFolderPath);
97
+ }
98
+
99
+ const currentDate = new Date();
100
+ const formattedDate = currentDate.toISOString().slice(0, 10);
101
+ const logFileName = `${formattedDate}.log`;
102
+ const logFilePath = path.join(logFolderPath, logFileName);
103
+ const stringData = JSON.stringify(mailLogOptions);
104
+
105
+ fs.appendFileSync(logFilePath, '\n' + stringData);
106
+ } catch (error) {
107
+ throw new Error('Error occurred while logging mail');
108
+ }
109
+ }
110
+ }