Skip to content

Commit 44d7efb

Browse files
Merge pull request #16 from socketlabs/feature/metadata-tags
Feature/metadata tags
2 parents 1117955 + 2a25ad3 commit 44d7efb

14 files changed

+675
-251
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ This example demonstrates how to send a bulk message with an AMP Html body.
213213
For more information about AMP please see [AMP Project](https://amp.dev/documentation/)
214214

215215
<a name="version"></a>
216-
# Version
216+
* 1.4.0 - Adding Metadata and Tags
217217
* 1.2.1 - Adding optional retry logic for Http requests. If configured, the request will retry when certain 500 errors occur (500, 502, 503, 504)
218218
* 1.1.1 - Adding request timeout value on the client for Http requests
219219
* 1.1.0 - Adds Amp Html Support

examples/basic/basicSendComplexExample.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { SocketLabsClient, EmailAddress, BasicMessage, Attachment, CustomHeader } = require('../../src/socketlabsClient');
1+
const { SocketLabsClient, EmailAddress, BasicMessage, Attachment, CustomHeader, Metadata } = require('../../src/socketlabsClient');
22
const exampleConfig = require('../exampleConfig');
33
const resolve = require('path').resolve;
44

@@ -104,7 +104,7 @@ var attachment2 = new Attachment({
104104
message.addAttachments(attachment2)
105105

106106
// Add Attachment a filePath {string} to the array
107-
message.attachments.push(resolve("./html/sampleemail.html"));
107+
message.attachments.push(resolve("./../html/SampleEmail.html"));
108108

109109
/**
110110
* Adding Custom Headers
@@ -121,6 +121,37 @@ message.customHeaders.push(new CustomHeader("message-has-attachments", "true"));
121121
// Add Custom Headers using the addCustomHeaders function
122122
message.addCustomHeaders("testMessageHeader", "I am a message header");
123123

124+
/**
125+
* Adding Metadata
126+
*/
127+
// Add Metadata using an Array
128+
var metadata = [];
129+
metadata.push(new Metadata("example-type", "basic-send-complex"));
130+
metadata.push({ name: "message-contains", value: "attachments, headers" });
131+
message.metadata = metadata;
132+
133+
// Add Metadata directly to the Array
134+
message.metadata.push(new Metadata("message-has-attachments", "true"));
135+
136+
// Add Metadata using the addCustomHeaders function
137+
message.addMetadata("testMessageHeader", "I am metadata");
138+
139+
/**
140+
* Adding Tags
141+
*/
142+
// Add Tags using an Array
143+
var tags = [];
144+
tags.push("example-type:basic-send-complex");
145+
message.tags = tags;
146+
147+
// Add Tags directly to the Array
148+
message.tags.push("has-attachments:true");
149+
150+
// Add Tags using the addCustomHeaders function
151+
message.addTag("I am a test message");
152+
message.addTag("nodejs-Example");
153+
154+
124155
/**
125156
* Create the client
126157
*/

examples/bulk/bulkSendComplexExample.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { SocketLabsClient, EmailAddress, BulkMessage, BulkRecipient, Attachment, CustomHeader, MergeData } = require('../../src/socketlabsClient');
1+
const { SocketLabsClient, EmailAddress, BulkMessage, BulkRecipient, Attachment, CustomHeader, MergeData, Metadata } = require('../../src/socketlabsClient');
22
const exampleConfig = require('../exampleConfig');
33
const resolve = require('path').resolve;
44

@@ -161,6 +161,36 @@ message.customHeaders.push(new CustomHeader("message-has-attachments", "true"));
161161
// Add Custom Headers using the addCustomHeaders function
162162
message.addCustomHeaders("testMessageHeader", "I am a message header");
163163

164+
/**
165+
* Adding Metadata
166+
*/
167+
// Add Metadata using an Array
168+
var metadata = [];
169+
metadata.push(new Metadata("example-type", "basic-send-complex"));
170+
metadata.push({ name: "message-contains", value: "attachments, headers" });
171+
message.metadata = metadata;
172+
173+
// Add Metadata directly to the Array
174+
message.metadata.push(new Metadata("message-has-attachments", "true"));
175+
176+
// Add Metadata using the addCustomHeaders function
177+
message.addMetadata("testMessageHeader", "I am metadata");
178+
179+
/**
180+
* Adding Tags
181+
*/
182+
// Add Tags using an Array
183+
var tags = [];
184+
tags.push("example-type:bulk-send-complex");
185+
message.tags = tags;
186+
187+
// Add Tags directly to the Array
188+
message.tags.push("has-attachments:true");
189+
190+
// Add Tags using the addCustomHeaders function
191+
message.addTag("I am a test message");
192+
message.addTag("nodejs-Example");
193+
164194
/**
165195
* Create the client
166196
*/

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@socketlabs/email",
3-
"version": "1.2.2",
3+
"version": "1.4.0",
44
"description": "SocketLabs Email Delivery node.js client library",
55
"main": "./src/socketlabsClient.js",
66
"repository": {
@@ -31,4 +31,4 @@
3131
"axios": "^0.21.1",
3232
"promise-retry": "^2.0.1"
3333
}
34-
}
34+
}

src/core/injectionRequestFactory.js

Lines changed: 115 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { ToBulkRecipient, ToEmailAddress, ToCustomHeader, ToMergeData } = require('../helpers/helpersClasses');
1+
const { ToBulkRecipient, ToEmailAddress, ToCustomHeader, ToMergeData, ToMetadata } = require('../helpers/helpersClasses');
22
const { BasicMessage, BulkMessage } = require('../message/messageClasses');
33
const sendValidator = require('./sendValidator');
44
const sendResultEnum = require('../sendResultEnum');
@@ -8,111 +8,130 @@ class InjectionRequestFactory {
88
//constructor(){}
99
generateRequest(messageData) {
1010
return new Promise((resolve, reject) => {
11-
var {
12-
to,
13-
from,
14-
replyTo,
15-
subject,
16-
textBody,
17-
htmlBody,
18-
apiTemplate,
19-
cc,
20-
bcc,
21-
attachments,
22-
messageId,
23-
mailingId,
24-
charSet,
25-
customHeaders,
26-
globalMergeData,
27-
messageType
28-
} = messageData;
11+
var {
12+
to,
13+
from,
14+
replyTo,
15+
subject,
16+
textBody,
17+
htmlBody,
18+
apiTemplate,
19+
cc,
20+
bcc,
21+
attachments,
22+
messageId,
23+
mailingId,
24+
charSet,
25+
customHeaders,
26+
metadata,
27+
tags,
28+
globalMergeData,
29+
messageType
30+
} = messageData;
2931

30-
var result = {};
31-
var validator = new sendValidator();
32+
var result = {};
33+
var validator = new sendValidator();
3234

33-
if (messageType === "basic") {
35+
if (messageType === "basic") {
3436

35-
var basicMsg = new BasicMessage();
37+
var basicMsg = new BasicMessage();
3638

37-
if (to) {
38-
if (Array.isArray(to)) basicMsg.to = to;
39-
else basicMsg.to.push(ToEmailAddress.convert(to));
40-
}
41-
basicMsg.from = from;
42-
basicMsg.replyTo = replyTo;
43-
basicMsg.subject = subject;
44-
basicMsg.textBody = textBody;
45-
basicMsg.htmlBody = htmlBody;
46-
if (attachments) {
47-
if (Array.isArray(attachments)) basicMsg.attachments = attachments;
48-
else basicMsg.attachments.push(attachments);
49-
}
50-
if (cc) {
51-
if (Array.isArray(cc)) basicMsg.cc = cc;
52-
else basicMsg.cc.push(ToEmailAddress.convert(cc));
53-
}
54-
if (bcc) {
55-
if (Array.isArray(bcc)) basicMsg.bcc = bcc;
56-
else basicMsg.bcc.push(ToEmailAddress.convert(bcc));
57-
}
58-
basicMsg.apiTemplate = apiTemplate;
59-
basicMsg.messageId = messageId;
60-
basicMsg.mailingId = mailingId;
61-
basicMsg.charSet = charSet;
62-
if (customHeaders) {
63-
if (Array.isArray(customHeaders)) basicMsg.customHeaders = customHeaders;
64-
else basicMsg.customHeaders.push(ToEmailAddress.convert(customHeaders));
65-
}
66-
67-
result = validator.validateBasicMessage(basicMsg);
68-
if (result.result !== sendResultEnum.Success) {
69-
reject(result);
70-
}
39+
if (to) {
40+
if (Array.isArray(to)) basicMsg.to = to;
41+
else basicMsg.to.push(ToEmailAddress.convert(to));
42+
}
43+
basicMsg.from = from;
44+
basicMsg.replyTo = replyTo;
45+
basicMsg.subject = subject;
46+
basicMsg.textBody = textBody;
47+
basicMsg.htmlBody = htmlBody;
48+
if (attachments) {
49+
if (Array.isArray(attachments)) basicMsg.attachments = attachments;
50+
else basicMsg.attachments.push(attachments);
51+
}
52+
if (cc) {
53+
if (Array.isArray(cc)) basicMsg.cc = cc;
54+
else basicMsg.cc.push(ToEmailAddress.convert(cc));
55+
}
56+
if (bcc) {
57+
if (Array.isArray(bcc)) basicMsg.bcc = bcc;
58+
else basicMsg.bcc.push(ToEmailAddress.convert(bcc));
59+
}
60+
basicMsg.apiTemplate = apiTemplate;
61+
basicMsg.messageId = messageId;
62+
basicMsg.mailingId = mailingId;
63+
basicMsg.charSet = charSet;
64+
if (customHeaders) {
65+
if (Array.isArray(customHeaders)) basicMsg.customHeaders = customHeaders;
66+
else basicMsg.customHeaders.push(ToEmailAddress.convert(customHeaders));
67+
}
68+
if (metadata) {
69+
if (Array.isArray(metadata)) basicMsg.metadata = metadata;
70+
else basicMsg.metadata.push(ToMetadata.convert(metadata));
71+
}
72+
if (tags) {
73+
if (Array.isArray(tags)) basicMsg.tags = tags;
74+
else basicMsg.tags.push(tags);
75+
}
7176

72-
resolve(basicMsg.toJSON());
73-
} else if (messageType === "bulk") {
74-
var bulkMsg = new BulkMessage();
77+
result = validator.validateBasicMessage(basicMsg);
78+
if (result.result !== sendResultEnum.Success) {
79+
reject(result);
80+
}
7581

76-
if (to) {
77-
if (Array.isArray(to)) bulkMsg.to = to;
78-
else bulkMsg.to.push(ToBulkRecipient.convert(to));
79-
}
80-
bulkMsg.from = from;
81-
bulkMsg.replyTo = replyTo;
82-
bulkMsg.subject = subject;
83-
bulkMsg.textBody = textBody;
84-
bulkMsg.htmlBody = htmlBody;
85-
if (attachments) {
86-
if (Array.isArray(attachments)) bulkMsg.attachments = attachments;
87-
else bulkMsg.attachments.push(attachments);
88-
}
89-
bulkMsg.apiTemplate = apiTemplate;
90-
bulkMsg.messageId = messageId;
91-
bulkMsg.mailingId = mailingId;
92-
bulkMsg.charSet = charSet;
93-
if (customHeaders) {
94-
if (Array.isArray(customHeaders)) bulkMsg.customHeaders = customHeaders;
95-
else bulkMsg.customHeaders.push(ToCustomHeader.convert(customHeaders));
96-
}
97-
if (globalMergeData) {
98-
if (Array.isArray(globalMergeData)) bulkMsg.globalMergeData = globalMergeData;
99-
else bulkMsg.globalMergeData.push(ToMergeData.convert(globalMergeData));
100-
}
82+
resolve(basicMsg.toJSON());
83+
} else if (messageType === "bulk") {
84+
var bulkMsg = new BulkMessage();
10185

102-
result = validator.validateBulkMessage(bulkMsg);
103-
if (result.result !== sendResultEnum.Success) {
104-
reject(result);
105-
}
86+
if (to) {
87+
if (Array.isArray(to)) bulkMsg.to = to;
88+
else bulkMsg.to.push(ToBulkRecipient.convert(to));
89+
}
90+
bulkMsg.from = from;
91+
bulkMsg.replyTo = replyTo;
92+
bulkMsg.subject = subject;
93+
bulkMsg.textBody = textBody;
94+
bulkMsg.htmlBody = htmlBody;
95+
if (attachments) {
96+
if (Array.isArray(attachments)) bulkMsg.attachments = attachments;
97+
else bulkMsg.attachments.push(attachments);
98+
}
99+
bulkMsg.apiTemplate = apiTemplate;
100+
bulkMsg.messageId = messageId;
101+
bulkMsg.mailingId = mailingId;
102+
bulkMsg.charSet = charSet;
103+
if (customHeaders) {
104+
if (Array.isArray(customHeaders)) bulkMsg.customHeaders = customHeaders;
105+
else bulkMsg.customHeaders.push(ToCustomHeader.convert(customHeaders));
106+
}
107+
if (metadata) {
108+
if (Array.isArray(metadata)) bulkMsg.metadata = metadata;
109+
else bulkMsg.metadata.push(ToMetadata.convert(metadata));
110+
}
111+
if (tags) {
112+
if (Array.isArray(tags)) bulkMsg.tags = tags;
113+
else bulkMsg.tags.push(tags);
114+
}
115+
if (globalMergeData) {
116+
if (Array.isArray(globalMergeData)) bulkMsg.globalMergeData = globalMergeData;
117+
else bulkMsg.globalMergeData.push(ToMergeData.convert(globalMergeData));
118+
}
106119

107-
resolve(bulkMsg.toJSON());
108-
} else {
109-
result = new sendResponse({
110-
result: sendResultEnum.MessageValidationInvalidMessageType
111-
});
120+
result = validator.validateBulkMessage(bulkMsg);
121+
if (result.result !== sendResultEnum.Success) {
112122
reject(result);
113123
}
124+
125+
resolve(bulkMsg.toJSON());
126+
} else {
127+
result = new sendResponse({
128+
result: sendResultEnum.MessageValidationInvalidMessageType
129+
});
130+
reject(result);
114131
}
115-
)}
132+
}
133+
)
116134
}
135+
}
117136

118-
module.exports = new InjectionRequestFactory();
137+
module.exports = new InjectionRequestFactory();

0 commit comments

Comments
 (0)