@tomei/mailer 0.5.15 → 0.5.18

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.
@@ -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,94 +1,94 @@
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
- interface TransportConfig {
7
- host: string;
8
- port: number;
9
- secure: boolean;
10
- auth: {
11
- user: string;
12
- pass: string;
13
- };
14
- tls?: any;
15
- debug?: boolean;
16
- logger?: boolean;
17
- }
18
-
19
- export class SMTPMailer extends MailerBase {
20
- async sendMail(options: ISendMailConfig): Promise<void> {
21
- try {
22
- const host = ComponentConfig.getComponentConfigValue(
23
- '@tomei/mailer',
24
- 'host',
25
- );
26
- const port = ComponentConfig.getComponentConfigValue(
27
- '@tomei/mailer',
28
- 'port',
29
- );
30
- const secure = ComponentConfig.getComponentConfigValue(
31
- '@tomei/mailer',
32
- 'secure',
33
- );
34
- const user = ComponentConfig.getComponentConfigValue(
35
- '@tomei/mailer',
36
- 'user',
37
- );
38
- const pass = ComponentConfig.getComponentConfigValue(
39
- '@tomei/mailer',
40
- 'pass',
41
- );
42
- const tlsOptions = ComponentConfig.getComponentConfigValue(
43
- '@tomei/mailer',
44
- 'tlsOptions',
45
- );
46
- const isMailerDebugEnabled = ComponentConfig.getComponentConfigValue(
47
- '@tomei/mailer',
48
- 'isMailerDebugEnabled',
49
- );
50
- const isMailerLogEnabled = ComponentConfig.getComponentConfigValue(
51
- '@tomei/mailer',
52
- 'isMailerLogEnabled',
53
- );
54
-
55
- const transportConfig: TransportConfig = {
56
- host,
57
- port,
58
- secure,
59
- auth: {
60
- user,
61
- pass,
62
- },
63
- };
64
-
65
- if (tlsOptions) {
66
- transportConfig.tls = tlsOptions;
67
- }
68
-
69
- if (isMailerDebugEnabled) {
70
- transportConfig.debug = true;
71
- }
72
-
73
- if (isMailerLogEnabled) {
74
- transportConfig.logger = true;
75
- }
76
-
77
- const transporter = nodemailer.createTransport(transportConfig);
78
-
79
- await transporter.sendMail({
80
- from: options.from,
81
- to: options.to,
82
- subject: options.subject,
83
- html: options.html,
84
- cc: options.cc,
85
- attachments: options.attachments,
86
- });
87
- } catch (error) {
88
- console.log(error, '<<<<<<<<<< error caught from @tomei/mailer package');
89
- throw error;
90
- }
91
- }
92
- }
93
-
94
- 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
+ interface TransportConfig {
7
+ host: string;
8
+ port: number;
9
+ secure: boolean;
10
+ auth: {
11
+ user: string;
12
+ pass: string;
13
+ };
14
+ tls?: any;
15
+ debug?: boolean;
16
+ logger?: boolean;
17
+ }
18
+
19
+ export class SMTPMailer extends MailerBase {
20
+ async sendMail(options: ISendMailConfig): Promise<void> {
21
+ try {
22
+ const host = ComponentConfig.getComponentConfigValue(
23
+ '@tomei/mailer',
24
+ 'host',
25
+ );
26
+ const port = ComponentConfig.getComponentConfigValue(
27
+ '@tomei/mailer',
28
+ 'port',
29
+ );
30
+ const secure = ComponentConfig.getComponentConfigValue(
31
+ '@tomei/mailer',
32
+ 'secure',
33
+ );
34
+ const user = ComponentConfig.getComponentConfigValue(
35
+ '@tomei/mailer',
36
+ 'user',
37
+ );
38
+ const pass = ComponentConfig.getComponentConfigValue(
39
+ '@tomei/mailer',
40
+ 'pass',
41
+ );
42
+ const tlsOptions = ComponentConfig.getComponentConfigValue(
43
+ '@tomei/mailer',
44
+ 'tlsOptions',
45
+ );
46
+ const isMailerDebugEnabled = ComponentConfig.getComponentConfigValue(
47
+ '@tomei/mailer',
48
+ 'isMailerDebugEnabled',
49
+ );
50
+ const isMailerLogEnabled = ComponentConfig.getComponentConfigValue(
51
+ '@tomei/mailer',
52
+ 'isMailerLogEnabled',
53
+ );
54
+
55
+ const transportConfig: TransportConfig = {
56
+ host,
57
+ port,
58
+ secure,
59
+ auth: {
60
+ user,
61
+ pass,
62
+ },
63
+ };
64
+
65
+ if (tlsOptions) {
66
+ transportConfig.tls = tlsOptions;
67
+ }
68
+
69
+ if (isMailerDebugEnabled) {
70
+ transportConfig.debug = true;
71
+ }
72
+
73
+ if (isMailerLogEnabled) {
74
+ transportConfig.logger = true;
75
+ }
76
+
77
+ const transporter = nodemailer.createTransport(transportConfig);
78
+
79
+ await transporter.sendMail({
80
+ from: options.from,
81
+ to: options.to,
82
+ subject: options.subject,
83
+ html: options.html,
84
+ cc: options.cc,
85
+ attachments: options.attachments,
86
+ });
87
+ } catch (error) {
88
+ console.log(error, '<<<<<<<<<< error caught from @tomei/mailer package');
89
+ throw error;
90
+ }
91
+ }
92
+ }
93
+
94
+ export { nodemailer };
package/.eslintrc DELETED
@@ -1,16 +0,0 @@
1
- {
2
- "parser": "@typescript-eslint/parser",
3
- "parserOptions": {
4
- "ecmaVersion": "latest",
5
- "sourceType": "module" // Allows for the use of imports
6
- },
7
- "extends": ["plugin:@typescript-eslint/recommended"],
8
- "env": {
9
- "node": true // Enable Node.js global variables
10
- },
11
- "rules": {
12
- "no-console": "off",
13
- "import/prefer-default-export": "off",
14
- "@typescript-eslint/no-unused-vars": "warn"
15
- }
16
- }
package/.eslintrc.js DELETED
@@ -1,35 +0,0 @@
1
- module.exports = {
2
- parser: '@typescript-eslint/parser',
3
- plugins: ['@typescript-eslint'],
4
-
5
- parserOptions: {
6
- ecmaVersion: 'latest', // Allows the use of modern ECMAScript features
7
- sourceType: 'module', // Allows for the use of imports
8
- },
9
-
10
- extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
11
- env: {
12
- node: true, // Enable Node.js global variables
13
- },
14
- rules: {
15
- 'no-console': 'off',
16
- '@typescript-eslint/no-explicit-any': 'off',
17
- '@typescript-eslint/no-var-requires': 'off',
18
- '@typescript-eslint/explicit-module-boundary-types': 'off',
19
- 'import/prefer-default-export': 'off',
20
- '@typescript-eslint/no-unused-vars': 'warn',
21
- 'no-useless-catch': 'off',
22
- '@typescript-eslint/no-non-null-assertion': 'off',
23
- '@typescript-eslint/no-empty-function': 'off',
24
- '@typescript-eslint/no-empty-interface': 'off',
25
- '@typescript-eslint/no-inferrable-types': 'off',
26
- '@typescript-eslint/no-namespace': 'off',
27
- '@typescript-eslint/no-use-before-define': 'off',
28
- '@typescript-eslint/no-unsafe-assignment': 'off',
29
- '@typescript-eslint/no-unsafe-call': 'off',
30
- '@typescript-eslint/no-unsafe-member-access': 'off',
31
- '@typescript-eslint/no-unsafe-return': 'off',
32
- '@typescript-eslint/restrict-template-expressions': 'off',
33
- 'no-useless-escape': 'off',
34
- },
35
- };