@johnmmackey/ms-utils 4.1.0 → 4.2.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/CHANGELOG.md CHANGED
@@ -1,3 +1,25 @@
1
+ ## Version 4.2.0
2
+
3
+ Added a self contained, simpler mail function:
4
+ ```
5
+ const awsSendMail = require('@johnmmackey/ms-utils').awsSendMail;
6
+
7
+ await awsSendMail({
8
+ from: ...
9
+ to: ...
10
+ subject: ...
11
+ textBody: ...
12
+ htmlBody: ...
13
+ replyTo?: ...
14
+ })
15
+ ```
16
+ The following environment variables are required:
17
+ * AWS_SES_REGION
18
+ * AWS_SES_ACCESS_KEY_ID
19
+ * AWS_SES_SECRET_ACCESS_KEY
20
+ * AWS_SES_IDENTITY_ARN
21
+ * AWS_SES_FEEDBACK_ADDRESS
22
+
1
23
  ## Version 4.1.0
2
24
  Added functionality:
3
25
  * 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: .env > etcd > external set environment
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
@@ -25,14 +25,15 @@ async function tryLoad(serviceName) {
25
25
  }
26
26
 
27
27
  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
28
 
33
29
  // load any .env file that may be present into the environment
34
30
  dotenv.config({ override: true });
35
31
 
32
+ await Promise.retry(10, tryLoad.bind(this, serviceName), 5000);
33
+
34
+ // as a side-effect, load these into process.env
35
+ dotenv.populate(process.env, config, { override: true });
36
+ return config;
36
37
  }
37
38
 
38
39
  function get(k) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johnmmackey/ms-utils",
3
- "version": "4.1.0",
3
+ "version": "4.2.0",
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.6.12",
20
- "uuid": "^9.0.1"
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
+ })();