@tomei/mailer 0.5.8 → 0.5.10

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.
Files changed (49) hide show
  1. package/package.json +44 -44
  2. package/src/enum/email-status.enum.ts +4 -4
  3. package/src/enum/index.ts +1 -1
  4. package/src/interfaces/log-transaction-options.interface.ts +18 -18
  5. package/src/interfaces/mail-config.interface.ts +8 -8
  6. package/src/interfaces/mail-log.interface.ts +12 -12
  7. package/src/mailer/index.ts +2 -2
  8. package/src/mailer/mailer.base.ts +110 -110
  9. package/src/mailer/smtp-mailer.ts +57 -57
  10. package/dist/__tests__/unit/mailer/mailer.spec.d.ts +0 -1
  11. package/dist/__tests__/unit/mailer/mailer.spec.js +0 -200
  12. package/dist/__tests__/unit/mailer/mailer.spec.js.map +0 -1
  13. package/dist/index.d.ts +0 -1
  14. package/dist/index.js +0 -18
  15. package/dist/index.js.map +0 -1
  16. package/dist/src/enum/email-status.enum.d.ts +0 -4
  17. package/dist/src/enum/email-status.enum.js +0 -9
  18. package/dist/src/enum/email-status.enum.js.map +0 -1
  19. package/dist/src/enum/index.d.ts +0 -1
  20. package/dist/src/enum/index.js +0 -6
  21. package/dist/src/enum/index.js.map +0 -1
  22. package/dist/src/index.d.ts +0 -3
  23. package/dist/src/index.js +0 -20
  24. package/dist/src/index.js.map +0 -1
  25. package/dist/src/interfaces/index.d.ts +0 -3
  26. package/dist/src/interfaces/index.js +0 -3
  27. package/dist/src/interfaces/index.js.map +0 -1
  28. package/dist/src/interfaces/log-transaction-options.interface.d.ts +0 -18
  29. package/dist/src/interfaces/log-transaction-options.interface.js +0 -3
  30. package/dist/src/interfaces/log-transaction-options.interface.js.map +0 -1
  31. package/dist/src/interfaces/mail-config.interface.d.ts +0 -8
  32. package/dist/src/interfaces/mail-config.interface.js +0 -3
  33. package/dist/src/interfaces/mail-config.interface.js.map +0 -1
  34. package/dist/src/interfaces/mail-log.interface.d.ts +0 -11
  35. package/dist/src/interfaces/mail-log.interface.js +0 -3
  36. package/dist/src/interfaces/mail-log.interface.js.map +0 -1
  37. package/dist/src/mailer/helpers/helpers.d.ts +0 -1
  38. package/dist/src/mailer/helpers/helpers.js +0 -36
  39. package/dist/src/mailer/helpers/helpers.js.map +0 -1
  40. package/dist/src/mailer/index.d.ts +0 -2
  41. package/dist/src/mailer/index.js +0 -8
  42. package/dist/src/mailer/index.js.map +0 -1
  43. package/dist/src/mailer/mailer.base.d.ts +0 -9
  44. package/dist/src/mailer/mailer.base.js +0 -113
  45. package/dist/src/mailer/mailer.base.js.map +0 -1
  46. package/dist/src/mailer/smtp-mailer.d.ts +0 -7
  47. package/dist/src/mailer/smtp-mailer.js +0 -53
  48. package/dist/src/mailer/smtp-mailer.js.map +0 -1
  49. package/dist/tsconfig.tsbuildinfo +0 -1
package/package.json CHANGED
@@ -1,44 +1,44 @@
1
- {
2
- "name": "@tomei/mailer",
3
- "version": "0.5.8",
4
- "description": "Tomei Mailer Package",
5
- "main": "dist/index.js",
6
- "scripts": {
7
- "start:dev": "tsc -w",
8
- "build": "tsc",
9
- "prepare": "husky install",
10
- "format": "prettier --write \"src/**/*.ts\"",
11
- "lint": "npx eslint . --fix",
12
- "test": "jest --forceExit --detectOpenHandles"
13
- },
14
- "author": "Tomei",
15
- "license": "ISC",
16
- "devDependencies": {
17
- "@commitlint/cli": "^17.6.3",
18
- "@commitlint/config-conventional": "^17.6.3",
19
- "@tsconfig/node18": "^2.0.1",
20
- "@types/jest": "^29.5.2",
21
- "@types/node": "^20.3.1",
22
- "@types/nodemailer": "^6.4.9",
23
- "@typescript-eslint/eslint-plugin": "^5.33.0",
24
- "eslint": "^8.40.0",
25
- "eslint-config-prettier": "^8.5.0",
26
- "eslint-plugin-prettier": "^4.2.1",
27
- "jest": "^29.5.0",
28
- "nodemailer-mock": "^2.0.1",
29
- "prettier": "^2.8.8",
30
- "ts-jest": "^29.1.0",
31
- "ts-node": "^10.9.1",
32
- "tsc-watch": "^6.0.4",
33
- "typescript": "^5.1.3"
34
- },
35
- "publishConfig": {
36
- "access": "public"
37
- },
38
- "peerDependencies": {
39
- "@tomei/config": "^0.3.6",
40
- "@tomei/general": "^0.6.0",
41
- "husky": "^8.0.3",
42
- "nodemailer": "^6.9.4"
43
- }
44
- }
1
+ {
2
+ "name": "@tomei/mailer",
3
+ "version": "0.5.10",
4
+ "description": "Tomei Mailer Package",
5
+ "main": "dist/index.js",
6
+ "scripts": {
7
+ "start:dev": "tsc -w",
8
+ "build": "tsc",
9
+ "prepare": "husky install",
10
+ "format": "prettier --write \"src/**/*.ts\"",
11
+ "lint": "npx eslint . --fix",
12
+ "test": "jest --forceExit --detectOpenHandles"
13
+ },
14
+ "author": "Tomei",
15
+ "license": "ISC",
16
+ "devDependencies": {
17
+ "@commitlint/cli": "^17.6.3",
18
+ "@commitlint/config-conventional": "^17.6.3",
19
+ "@tsconfig/node18": "^2.0.1",
20
+ "@types/jest": "^29.5.2",
21
+ "@types/node": "^20.3.1",
22
+ "@types/nodemailer": "^6.4.9",
23
+ "@typescript-eslint/eslint-plugin": "^5.33.0",
24
+ "eslint": "^8.40.0",
25
+ "eslint-config-prettier": "^8.5.0",
26
+ "eslint-plugin-prettier": "^4.2.1",
27
+ "jest": "^29.5.0",
28
+ "nodemailer-mock": "^2.0.1",
29
+ "prettier": "^2.8.8",
30
+ "ts-jest": "^29.1.0",
31
+ "ts-node": "^10.9.1",
32
+ "tsc-watch": "^6.0.4",
33
+ "typescript": "^5.1.3"
34
+ },
35
+ "publishConfig": {
36
+ "access": "public"
37
+ },
38
+ "peerDependencies": {
39
+ "@tomei/config": "^0.3.9",
40
+ "@tomei/general": "^0.10.4",
41
+ "husky": "^8.0.3",
42
+ "nodemailer": "^6.9.4"
43
+ }
44
+ }
@@ -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
+ }
@@ -1,57 +1,57 @@
1
- import { ComponentConfig } from '@tomei/config';
2
- import { MailerBase } from './mailer.base';
3
- import * as nodemailer from 'nodemailer';
4
- import { ISendMailConfig } from 'src/interfaces/mail-config.interface';
5
-
6
- export class SMTPMailer extends MailerBase {
7
- async sendMail(options: ISendMailConfig): Promise<void> {
8
- try {
9
- const host = ComponentConfig.getComponentConfigValue(
10
- '@tomei/mailer',
11
- 'host',
12
- );
13
- const port = ComponentConfig.getComponentConfigValue(
14
- '@tomei/mailer',
15
- 'port',
16
- );
17
- const secure = ComponentConfig.getComponentConfigValue(
18
- '@tomei/mailer',
19
- 'secure',
20
- );
21
- const user = ComponentConfig.getComponentConfigValue(
22
- '@tomei/mailer',
23
- 'user',
24
- );
25
- const pass = ComponentConfig.getComponentConfigValue(
26
- '@tomei/mailer',
27
- 'pass',
28
- );
29
-
30
- const transportConfig = {
31
- host,
32
- port,
33
- secure,
34
- auth: {
35
- user,
36
- pass,
37
- },
38
- };
39
-
40
- const transporter = nodemailer.createTransport(transportConfig);
41
-
42
- await transporter.sendMail({
43
- from: options.from,
44
- to: options.to,
45
- subject: options.subject,
46
- html: options.html,
47
- cc: options.cc,
48
- attachments: options.attachments,
49
- });
50
- } catch (error) {
51
- console.log(error);
52
- throw error;
53
- }
54
- }
55
- }
56
-
57
- export { nodemailer };
1
+ import { ComponentConfig } from '@tomei/config';
2
+ import { MailerBase } from './mailer.base';
3
+ import * as nodemailer from 'nodemailer';
4
+ import { ISendMailConfig } from 'src/interfaces/mail-config.interface';
5
+
6
+ export class SMTPMailer extends MailerBase {
7
+ async sendMail(options: ISendMailConfig): Promise<void> {
8
+ try {
9
+ const host = ComponentConfig.getComponentConfigValue(
10
+ '@tomei/mailer',
11
+ 'host',
12
+ );
13
+ const port = ComponentConfig.getComponentConfigValue(
14
+ '@tomei/mailer',
15
+ 'port',
16
+ );
17
+ const secure = ComponentConfig.getComponentConfigValue(
18
+ '@tomei/mailer',
19
+ 'secure',
20
+ );
21
+ const user = ComponentConfig.getComponentConfigValue(
22
+ '@tomei/mailer',
23
+ 'user',
24
+ );
25
+ const pass = ComponentConfig.getComponentConfigValue(
26
+ '@tomei/mailer',
27
+ 'pass',
28
+ );
29
+
30
+ const transportConfig = {
31
+ host,
32
+ port,
33
+ secure,
34
+ auth: {
35
+ user,
36
+ pass,
37
+ },
38
+ };
39
+
40
+ const transporter = nodemailer.createTransport(transportConfig);
41
+
42
+ await transporter.sendMail({
43
+ from: options.from,
44
+ to: options.to,
45
+ subject: options.subject,
46
+ html: options.html,
47
+ cc: options.cc,
48
+ attachments: options.attachments,
49
+ });
50
+ } catch (error) {
51
+ console.log(error);
52
+ throw error;
53
+ }
54
+ }
55
+ }
56
+
57
+ export { nodemailer };
@@ -1 +0,0 @@
1
- export {};