@johnmmackey/ms-utils 4.1.1 → 4.2.1
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/CHANGELOG.md +25 -0
- package/README.md +1 -1
- package/index.js +2 -1
- package/lib/awsSendMail.js +99 -0
- package/lib/config.js +8 -7
- package/package.json +4 -3
- package/tests/awsSendMailTest.js +36 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,28 @@
|
|
|
1
|
+
## Version 4.2.1
|
|
2
|
+
* Bug fix on load
|
|
3
|
+
|
|
4
|
+
## Version 4.2.0
|
|
5
|
+
|
|
6
|
+
Added a self contained, simpler mail function:
|
|
7
|
+
```
|
|
8
|
+
const awsSendMail = require('@johnmmackey/ms-utils').awsSendMail;
|
|
9
|
+
|
|
10
|
+
await awsSendMail({
|
|
11
|
+
from: ...
|
|
12
|
+
to: ...
|
|
13
|
+
subject: ...
|
|
14
|
+
textBody: ...
|
|
15
|
+
htmlBody: ...
|
|
16
|
+
replyTo?: ...
|
|
17
|
+
})
|
|
18
|
+
```
|
|
19
|
+
The following environment variables are required:
|
|
20
|
+
* AWS_SES_REGION
|
|
21
|
+
* AWS_SES_ACCESS_KEY_ID
|
|
22
|
+
* AWS_SES_SECRET_ACCESS_KEY
|
|
23
|
+
* AWS_SES_IDENTITY_ARN
|
|
24
|
+
* AWS_SES_FEEDBACK_ADDRESS
|
|
25
|
+
|
|
1
26
|
## Version 4.1.0
|
|
2
27
|
Added functionality:
|
|
3
28
|
* uses ```dotenv``` to load a local .env file
|
package/README.md
CHANGED
|
@@ -47,7 +47,7 @@ sendmail(
|
|
|
47
47
|
* uses ```dotenv``` to load a .env file in the current working directory into the environment
|
|
48
48
|
* pushes all items loaded from etcd into the environment.
|
|
49
49
|
|
|
50
|
-
The order of precedence:
|
|
50
|
+
The order of precedence: etcd > .env > external set environment
|
|
51
51
|
|
|
52
52
|
## Future
|
|
53
53
|
* Add local queueing for log events when AMQP is not operative.
|
package/index.js
CHANGED
|
@@ -4,5 +4,6 @@ const Config = require('./lib/config');
|
|
|
4
4
|
const winstonMSLoggerFactory = require('./lib/winston-mslogger');
|
|
5
5
|
const Utils = require('./lib/utils');
|
|
6
6
|
const Mailer = require('./lib/mail');
|
|
7
|
+
const awsSendMail = require('./lib/awsSendMail')
|
|
7
8
|
|
|
8
|
-
module.exports = { Utils, Config, winstonMSLoggerFactory, Mailer };
|
|
9
|
+
module.exports = { Utils, Config, winstonMSLoggerFactory, Mailer, awsSendMail };
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
const { SESv2Client, SendEmailCommand } = require("@aws-sdk/client-sesv2");
|
|
2
|
+
|
|
3
|
+
// Create SES service object.
|
|
4
|
+
|
|
5
|
+
const awsSendMail = async (m) => {
|
|
6
|
+
const client = new SESv2Client({
|
|
7
|
+
region: process.env.AWS_SES_REGION,
|
|
8
|
+
credentials: ({
|
|
9
|
+
accessKeyId: process.env.AWS_SES_ACCESS_KEY_ID,
|
|
10
|
+
secretAccessKey: process.env.AWS_SES_SECRET_ACCESS_KEY,
|
|
11
|
+
})
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
const input = { // SendEmailRequest
|
|
15
|
+
FromEmailAddress: m.from,
|
|
16
|
+
FromEmailAddressIdentityArn: process.env.AWS_SES_IDENTITY_ARN,
|
|
17
|
+
Destination: { // Destination
|
|
18
|
+
ToAddresses: m.to instanceof Array ? m.to : [m.to],
|
|
19
|
+
CcAddresses: [],
|
|
20
|
+
BccAddresses: [],
|
|
21
|
+
},
|
|
22
|
+
ReplyToAddresses: [m.replyTo || m.from],
|
|
23
|
+
FeedbackForwardingEmailAddress: process.env.AWS_SES_FEEDBACK_ADDRESS,
|
|
24
|
+
FeedbackForwardingEmailAddressIdentityArn: process.env.AWS_SES_IDENTITY_ARN,
|
|
25
|
+
Content: { // EmailContent
|
|
26
|
+
Simple: { // Message
|
|
27
|
+
Subject: { // Content
|
|
28
|
+
Data: m.subject,
|
|
29
|
+
Charset: "UTF-8",
|
|
30
|
+
},
|
|
31
|
+
Body: { // Body
|
|
32
|
+
...(m.textBody &&
|
|
33
|
+
{
|
|
34
|
+
Text: {
|
|
35
|
+
Data: m.textBody ,
|
|
36
|
+
Charset: "UTF-8",
|
|
37
|
+
},
|
|
38
|
+
}
|
|
39
|
+
),
|
|
40
|
+
...(m.htmlBody &&
|
|
41
|
+
{
|
|
42
|
+
Html: {
|
|
43
|
+
Data: m.htmlBody, // required
|
|
44
|
+
Charset: "UTF-8",
|
|
45
|
+
},
|
|
46
|
+
}
|
|
47
|
+
)
|
|
48
|
+
}
|
|
49
|
+
/*
|
|
50
|
+
Headers: [ // MessageHeaderList
|
|
51
|
+
{ // MessageHeader
|
|
52
|
+
Name: "STRING_VALUE", // required
|
|
53
|
+
Value: "STRING_VALUE", // required
|
|
54
|
+
},
|
|
55
|
+
],
|
|
56
|
+
*/
|
|
57
|
+
},
|
|
58
|
+
/*
|
|
59
|
+
Raw: { // RawMessage
|
|
60
|
+
Data: new Uint8Array(), // e.g. Buffer.from("") or new TextEncoder().encode("") // required
|
|
61
|
+
},
|
|
62
|
+
Template: { // Template
|
|
63
|
+
TemplateName: "STRING_VALUE",
|
|
64
|
+
TemplateArn: "STRING_VALUE",
|
|
65
|
+
TemplateContent: { // EmailTemplateContent
|
|
66
|
+
Subject: "STRING_VALUE",
|
|
67
|
+
Text: "STRING_VALUE",
|
|
68
|
+
Html: "STRING_VALUE",
|
|
69
|
+
},
|
|
70
|
+
TemplateData: "STRING_VALUE",
|
|
71
|
+
Headers: [
|
|
72
|
+
{
|
|
73
|
+
Name: "STRING_VALUE", // required
|
|
74
|
+
Value: "STRING_VALUE", // required
|
|
75
|
+
},
|
|
76
|
+
],
|
|
77
|
+
},*/
|
|
78
|
+
},
|
|
79
|
+
/*
|
|
80
|
+
EmailTags: [ // MessageTagList
|
|
81
|
+
{ // MessageTag
|
|
82
|
+
Name: "STRING_VALUE", // required
|
|
83
|
+
Value: "STRING_VALUE", // required
|
|
84
|
+
},
|
|
85
|
+
],
|
|
86
|
+
ConfigurationSetName: "STRING_VALUE",
|
|
87
|
+
EndpointId: "STRING_VALUE",
|
|
88
|
+
ListManagementOptions: { // ListManagementOptions
|
|
89
|
+
ContactListName: "STRING_VALUE", // required
|
|
90
|
+
TopicName: "STRING_VALUE",
|
|
91
|
+
},
|
|
92
|
+
*/
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const command = new SendEmailCommand(input);
|
|
96
|
+
return await client.send(command);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
module.exports = awsSendMail;
|
package/lib/config.js
CHANGED
|
@@ -2,8 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
const { Etcd3 } = require('etcd3');
|
|
4
4
|
const dotenv = require('dotenv');
|
|
5
|
-
const client = new Etcd3({ hosts: process.env.ETCD_URI || 'http://localhost:2379' });
|
|
6
|
-
|
|
7
5
|
|
|
8
6
|
// helpers
|
|
9
7
|
// https://stackoverflow.com/questions/42429590/retry-on-javascript-promise-reject-a-limited-number-of-times-or-until-success
|
|
@@ -12,7 +10,7 @@ Promise.retry = (cont, fn, delay) => fn().catch(err => cont > 0 ? Promise.wait(d
|
|
|
12
10
|
|
|
13
11
|
var config = {};
|
|
14
12
|
|
|
15
|
-
async function tryLoad(serviceName) {
|
|
13
|
+
async function tryLoad(client, serviceName) {
|
|
16
14
|
let scoped = await client
|
|
17
15
|
.namespace(`${process.env.CONFIG_SET || ''}/${serviceName}/`)
|
|
18
16
|
.getAll()
|
|
@@ -25,13 +23,16 @@ async function tryLoad(serviceName) {
|
|
|
25
23
|
}
|
|
26
24
|
|
|
27
25
|
async function load(serviceName) {
|
|
28
|
-
await Promise.retry(10, tryLoad.bind(this, serviceName), 5000);
|
|
29
|
-
|
|
30
|
-
// as a side-effect, load these into process.env
|
|
31
|
-
dotenv.populate(process.env, config, { override: true });
|
|
32
26
|
|
|
33
27
|
// load any .env file that may be present into the environment
|
|
34
28
|
dotenv.config({ override: true });
|
|
29
|
+
|
|
30
|
+
const client = new Etcd3({ hosts: process.env.ETCD_URI || 'http://localhost:2379' });
|
|
31
|
+
|
|
32
|
+
await Promise.retry(10, tryLoad.bind(this, client, serviceName), 5000);
|
|
33
|
+
|
|
34
|
+
// as a side-effect, load these into process.env
|
|
35
|
+
dotenv.populate(process.env, config, { override: true });
|
|
35
36
|
return config;
|
|
36
37
|
}
|
|
37
38
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@johnmmackey/ms-utils",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.2.1",
|
|
4
4
|
"description": "Utility functions for Microservice Architecture",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -14,9 +14,10 @@
|
|
|
14
14
|
"license": "ISC",
|
|
15
15
|
"homepage": "https://bitbucket.org/52westlabs/ms-utils#readme",
|
|
16
16
|
"dependencies": {
|
|
17
|
+
"@aws-sdk/client-sesv2": "^3.758.0",
|
|
17
18
|
"dotenv": "^16.4.7",
|
|
18
19
|
"etcd3": "^1.1.2",
|
|
19
|
-
"redis": "^4.
|
|
20
|
-
"uuid": "^
|
|
20
|
+
"redis": "^4.7.0",
|
|
21
|
+
"uuid": "^11.1.0"
|
|
21
22
|
}
|
|
22
23
|
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const dotenv = require('dotenv');
|
|
2
|
+
const { awsSendMail } = require('../index.js');
|
|
3
|
+
|
|
4
|
+
dotenv.config();
|
|
5
|
+
|
|
6
|
+
(async () => {
|
|
7
|
+
/*
|
|
8
|
+
await awsSendMail(
|
|
9
|
+
{
|
|
10
|
+
from: 'info@countryglen.org',
|
|
11
|
+
to: [
|
|
12
|
+
"John Mackey <john.m.mackey@gmail.com>",
|
|
13
|
+
"johnmmackey@yahoo.ca",
|
|
14
|
+
"john.m.mackey@countryglen.org"
|
|
15
|
+
],
|
|
16
|
+
subject: "Test",
|
|
17
|
+
htmlBody: "<em>This</em> is progress...",
|
|
18
|
+
}
|
|
19
|
+
);
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
await awsSendMail(
|
|
23
|
+
{
|
|
24
|
+
from: 'CG <info@countryglen.org>',
|
|
25
|
+
to: [
|
|
26
|
+
"John Mackey <john.m.mackey@gmail.com>",
|
|
27
|
+
//"John Mackey <john.m.mackeasdfasdfay@gmail.com>",
|
|
28
|
+
],
|
|
29
|
+
replyTo: 'john.mackey@viasat.com',
|
|
30
|
+
subject: "Test",
|
|
31
|
+
htmlBody: "<em>This</em> is progress...",
|
|
32
|
+
textBody: "This is progress...",
|
|
33
|
+
addTextTimestamp: true
|
|
34
|
+
}
|
|
35
|
+
);
|
|
36
|
+
})();
|