mirror of
https://github.com/matt-fidd/stratos.git
synced 2026-01-01 18:39:32 +00:00
Added documentation for EmailBuilder class
This commit is contained in:
@@ -1,16 +1,61 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* removeDupes() Removes item from src array that appear in dest
|
||||||
|
*
|
||||||
|
* @param {Array} src - The source array to remove duplicates from
|
||||||
|
* @param {Array} dest - The array to check against
|
||||||
|
*
|
||||||
|
* @return {Array} - The filtered array
|
||||||
|
*/
|
||||||
function removeDupes(src, dest) {
|
function removeDupes(src, dest) {
|
||||||
return src.filter(item => !dest.includes(item));
|
return src.filter(item => !dest.includes(item));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A class to allow for easy construction of emails
|
||||||
|
*/
|
||||||
class EmailBuilder {
|
class EmailBuilder {
|
||||||
|
/**
|
||||||
|
* The subject of the email
|
||||||
|
* @type {string}
|
||||||
|
*/
|
||||||
subject;
|
subject;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The plaintext body of the email
|
||||||
|
* @type {string}
|
||||||
|
*/
|
||||||
body;
|
body;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The html body of the email
|
||||||
|
* @type {string}
|
||||||
|
*/
|
||||||
HTMLBody;
|
HTMLBody;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The from address for the email
|
||||||
|
* @type {string}
|
||||||
|
*/
|
||||||
from = 'Stratos <contact@stratos.com>';
|
from = 'Stratos <contact@stratos.com>';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The addresses to send to
|
||||||
|
* @type {Array<string>}
|
||||||
|
*/
|
||||||
to;
|
to;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The addresses to carbon copy to
|
||||||
|
* @type {Array<string>}
|
||||||
|
*/
|
||||||
cc;
|
cc;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The addresses to blind carbon copy to
|
||||||
|
* @type {Array<string>}
|
||||||
|
*/
|
||||||
bcc;
|
bcc;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
@@ -19,54 +64,104 @@ class EmailBuilder {
|
|||||||
this.bcc = [];
|
this.bcc = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the subject of the email
|
||||||
|
* @param {string} subject - The subject to set
|
||||||
|
* @returns {EmailBuilder}
|
||||||
|
*/
|
||||||
setSubject(subject) {
|
setSubject(subject) {
|
||||||
this.subject = subject;
|
this.subject = subject;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the plaintext body of the email
|
||||||
|
* @param {string} body - The body to set
|
||||||
|
* @returns {EmailBuilder}
|
||||||
|
*/
|
||||||
setBody(body) {
|
setBody(body) {
|
||||||
this.body = body;
|
this.body = body;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the HTML body of the email
|
||||||
|
* @param {string} HTMLBody - The body to set
|
||||||
|
* @returns {EmailBuilder}
|
||||||
|
*/
|
||||||
setHTMLBody(HTMLBody) {
|
setHTMLBody(HTMLBody) {
|
||||||
this.HTMLBody = HTMLBody;
|
this.HTMLBody = HTMLBody;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the from address of the email
|
||||||
|
* @param {string} from - The address to set
|
||||||
|
* @returns {EmailBuilder}
|
||||||
|
*/
|
||||||
setFrom(from) {
|
setFrom(from) {
|
||||||
this.from = from;
|
this.from = from;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a list of addresses to the 'to' addresses
|
||||||
|
* @param {Array<string>} addresses - The addresses to add
|
||||||
|
* @returns {EmailBuilder}
|
||||||
|
*/
|
||||||
addTo(addresses) {
|
addTo(addresses) {
|
||||||
const newAddresses = removeDupes(addresses, this.to);
|
const newAddresses = removeDupes(addresses, this.to);
|
||||||
this.to.push(...newAddresses);
|
this.to.push(...newAddresses);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes a list of addresses from the 'to' addresses
|
||||||
|
* @param {Array<string>} addresses - The addresses to remove
|
||||||
|
* @returns {EmailBuilder}
|
||||||
|
*/
|
||||||
removeTo(addresses) {
|
removeTo(addresses) {
|
||||||
this.to = removeDupes(this.to, addresses);
|
this.to = removeDupes(this.to, addresses);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a list of addresses to the 'cc' addresses
|
||||||
|
* @param {Array<string>} addresses - The addresses to add
|
||||||
|
* @returns {EmailBuilder}
|
||||||
|
*/
|
||||||
addCC(addresses) {
|
addCC(addresses) {
|
||||||
const newAddresses = removeDupes(addresses, this.to);
|
const newAddresses = removeDupes(addresses, this.to);
|
||||||
this.cc.push(...newAddresses);
|
this.cc.push(...newAddresses);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes a list of addresses from the 'cc' addresses
|
||||||
|
* @param {Array<string>} addresses - The addresses to remove
|
||||||
|
* @returns {EmailBuilder}
|
||||||
|
*/
|
||||||
removeCC(addresses) {
|
removeCC(addresses) {
|
||||||
this.cc = removeDupes(this.cc, addresses);
|
this.cc = removeDupes(this.cc, addresses);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a list of addresses to the 'bcc' addresses
|
||||||
|
* @param {Array<string>} addresses - The addresses to add
|
||||||
|
* @returns {EmailBuilder}
|
||||||
|
*/
|
||||||
addBCC(addresses) {
|
addBCC(addresses) {
|
||||||
const newAddresses = removeDupes(addresses, this.to);
|
const newAddresses = removeDupes(addresses, this.to);
|
||||||
this.bcc.push(...newAddresses);
|
this.bcc.push(...newAddresses);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes a list of addresses from the 'bcc' addresses
|
||||||
|
* @param {Array<string>} addresses - The addresses to remove
|
||||||
|
* @returns {EmailBuilder}
|
||||||
|
*/
|
||||||
removeBCC(addresses) {
|
removeBCC(addresses) {
|
||||||
this.bcc = removeDupes(this.bcc, addresses);
|
this.bcc = removeDupes(this.bcc, addresses);
|
||||||
return this;
|
return this;
|
||||||
|
|||||||
Reference in New Issue
Block a user