1
- import * as axios from 'axios' ;
2
- import * as nodemailer from 'nodemailer' ;
1
+ // import * as axios from 'axios';
2
+ // import * as nodemailer from 'nodemailer';
3
3
4
- // const alert = {};
4
+ // // const alert = {};
5
+
6
+ // interface SlackSettings {
7
+ // webhook: any; // possibly 'string'?
8
+ // }
9
+
10
+ // interface EmailSettings {
11
+ // emails: string;
12
+ // emailHost: string;
13
+ // emailPort: string | number;
14
+ // user: string;
15
+ // password: string;
16
+ // }
17
+
18
+ // interface Alert {
19
+ // sendSlack: (code: number, message: string, slackSettings: SlackSettings) => void;
20
+ // sendEmail: (code: number, message: string, emailSettings: EmailSettings) => void;
21
+ // }
22
+
23
+ // const alert: Alert = {
24
+ // /**
25
+ // * Sends slack notifications to the provided slackurl with the status code
26
+ // * and message via an axios POST request
27
+ // // * @param {integer } code Response status code
28
+ // // * @param {string } message Response message
29
+ // // * @param {Object } slackSettings User provided slack settings
30
+ // */
31
+
32
+ // sendSlack : (code: number, message: string, slackSettings: any) => {
33
+ // const { webhook } = slackSettings;
34
+
35
+ // // Data for POST request
36
+ // const data = { text: `${code}, ${message}, ${Date.now()}` };
37
+
38
+ // // Options for POST request
39
+ // const config = {
40
+ // method: 'post',
41
+ // headers: {
42
+ // 'Content-Type': 'application/json',
43
+ // },
44
+ // };
45
+
46
+ // axios
47
+ // .post(webhook, data, config)
48
+ // .then(res => console.log('Status Code >= 400...\nError message sent'))
49
+ // .catch(error => console.log('test------>', error.message));
50
+ // },
51
+
52
+ // /**
53
+ // * Sends email notifications using the provided email information with the
54
+ // * status code and message via an axios POST request
55
+ // // * @param {integer } code Response status code
56
+ // // * @param {string } message Response message
57
+ // // * @param {Object } emailSettings User provided email settings
58
+ // // */
59
+
60
+ // sendEmail : (code: number, message: string, emailSettings: any) => {
61
+ // const { emails, emailHost, emailPort, user, password } = emailSettings;
62
+
63
+ // // Message object contains recipient email list and email text body
64
+ // const data = {
65
+ // to: `${emails}`,
66
+ // subject: 'Error from Middleware',
67
+ // text: `${code}, ${message}`,
68
+ // };
69
+
70
+ // // Configuration settings for email notifications
71
+ // const config = {
72
+ // host: `${emailHost}`,
73
+ // port: `${emailPort}`,
74
+ // auth: {
75
+ // user: `${user}`,
76
+ // pass: `${password}`,
77
+ // },
78
+ // };
79
+ // const transport = nodemailer.createTransport(config);
80
+
81
+ // transport.sendMail(data, function (err, info) {
82
+ // if (err) {
83
+ // console.log(err);
84
+ // } else {
85
+ // console.log(info);
86
+ // }
87
+ // });
88
+ // },
89
+ // };
90
+
91
+ // export default alert
92
+ // alert.ts
93
+
94
+ // Option A: Preferred if your tsconfig.json has "esModuleInterop": true
95
+ import axios from 'axios' ;
96
+ // Option B (if you can’t enable esModuleInterop):
97
+ // import * as axiosImport from 'axios';
98
+ // const axios = axiosImport.default;
99
+
100
+ import nodemailer from 'nodemailer' ;
5
101
6
102
interface SlackSettings {
7
- webhook : any ; // possibly ' string'?
103
+ webhook : string ; // assuming webhook is a URL string
8
104
}
9
105
10
106
interface EmailSettings {
@@ -21,71 +117,73 @@ interface Alert {
21
117
}
22
118
23
119
const alert : Alert = {
24
- /**
25
- * Sends slack notifications to the provided slackurl with the status code
26
- * and message via an axios POST request
27
- // * @param {integer } code Response status code
28
- // * @param {string } message Response message
29
- // * @param {Object } slackSettings User provided slack settings
30
- */
31
-
32
- sendSlack : ( code : number , message : string , slackSettings : any ) => {
33
- const { webhook } = slackSettings ;
34
-
35
- // Data for POST request
36
- const data = { text : `${ code } , ${ message } , ${ Date . now ( ) } ` } ;
37
-
38
- // Options for POST request
39
- const config = {
40
- method : 'post' ,
41
- headers : {
42
- 'Content-Type' : 'application/json' ,
43
- } ,
44
- } ;
45
-
46
- axios
47
- . post ( webhook , data , config )
48
- . then ( res => console . log ( 'Status Code >= 400...\nError message sent' ) )
49
- . catch ( error => console . log ( 'test------>' , error . message ) ) ;
50
- } ,
51
-
52
- /**
53
- * Sends email notifications using the provided email information with the
54
- * status code and message via an axios POST request
55
- // * @param {integer } code Response status code
56
- // * @param {string } message Response message
57
- // * @param {Object } emailSettings User provided email settings
58
- // */
120
+ /**
121
+ * Sends slack notifications to the provided slack webhook URL with the status code
122
+ * and message via an Axios POST request.
123
+ *
124
+ * @param code Response status code
125
+ * @param message Response message
126
+ * @param slackSettings User provided slack settings
127
+ */
128
+ sendSlack : ( code : number , message : string , slackSettings : SlackSettings ) => {
129
+ const { webhook } = slackSettings ;
130
+ // Data for POST request
131
+ const data = { text : `${ code } , ${ message } , ${ Date . now ( ) } ` } ;
132
+ // Options for POST request
133
+ const config = {
134
+ headers : {
135
+ 'Content-Type' : 'application/json' ,
136
+ } ,
137
+ } ;
138
+
139
+ axios
140
+ . post ( webhook , data , config )
141
+ . then ( ( res ) => console . log ( 'Status Code >= 400...\nError message sent' ) )
142
+ . catch ( ( error ) => console . log ( 'Error sending Slack message:' , error . message ) ) ;
143
+ } ,
144
+
145
+ /**
146
+ * Sends email notifications using the provided email information with the
147
+ * status code and message via Nodemailer.
148
+ *
149
+ * @param code Response status code
150
+ * @param message Response message
151
+ * @param emailSettings User provided email settings
152
+ */
153
+ sendEmail : ( code : number , message : string , emailSettings : EmailSettings ) => {
154
+ const { emails, emailHost, emailPort, user, password } = emailSettings ;
155
+
156
+ // Message object for the email
157
+ const mailOptions = {
158
+ to : emails ,
159
+ subject : 'Error from Middleware' ,
160
+ text : `${ code } , ${ message } ` ,
161
+ } ;
162
+
163
+ // Convert port to number if necessary
164
+ const portNumber =
165
+ typeof emailPort === 'string' ? parseInt ( emailPort , 10 ) : emailPort ;
166
+
167
+ // Configuration settings for Nodemailer
168
+ const transportConfig = {
169
+ host : emailHost ,
170
+ port : portNumber ,
171
+ auth : {
172
+ user : user ,
173
+ pass : password ,
174
+ } ,
175
+ } ;
176
+
177
+ const transport = nodemailer . createTransport ( transportConfig ) ;
59
178
60
- sendEmail : ( code : number , message : string , emailSettings : any ) => {
61
- const { emails, emailHost, emailPort, user, password } = emailSettings ;
62
-
63
- // Message object contains recipient email list and email text body
64
- const data = {
65
- to : `${ emails } ` ,
66
- subject : 'Error from Middleware' ,
67
- text : `${ code } , ${ message } ` ,
68
- } ;
69
-
70
- // Configuration settings for email notifications
71
- const config = {
72
- host : `${ emailHost } ` ,
73
- port : `${ emailPort } ` ,
74
- auth : {
75
- user : `${ user } ` ,
76
- pass : `${ password } ` ,
77
- } ,
78
- } ;
79
- const transport = nodemailer . createTransport ( config ) ;
80
-
81
- transport . sendMail ( data , function ( err , info ) {
82
- if ( err ) {
83
- console . log ( err ) ;
84
- } else {
85
- console . log ( info ) ;
86
- }
87
- } ) ;
88
- } ,
179
+ transport . sendMail ( mailOptions , ( err , info ) => {
180
+ if ( err ) {
181
+ console . log ( 'Error sending email:' , err ) ;
182
+ } else {
183
+ console . log ( 'Email sent:' , info ) ;
184
+ }
185
+ } ) ;
186
+ } ,
89
187
} ;
90
188
91
- export default alert
189
+ export default alert ;
0 commit comments