nestjs-mailable
Version:
A comprehensive NestJS mail package with design patterns for email handling, templating, and multi-provider support
123 lines • 4.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MailableAttachmentBuilder = exports.AttachmentBuilder = exports.Mailable = void 0;
const fs = require("fs/promises");
const path = require("path");
class Mailable {
attachments() {
return [];
}
headers() {
return {};
}
async build() {
const envelope = this.envelope();
const content = this.content();
const headers = this.headers();
const attachments = await this.buildAttachments();
const mailContent = {
subject: envelope.subject,
tags: envelope.tags,
metadata: envelope.metadata,
headers: headers.text,
attachments,
};
if (content.html) {
mailContent.html = content.html;
}
if (content.text) {
mailContent.text = content.text;
}
if (content.template) {
mailContent.template = content.template;
mailContent.context = content.with;
}
if (content.markdown) {
mailContent.template = 'markdown';
mailContent.context = {
markdown: content.markdown,
...(content.with || {}),
};
}
if (envelope.using) {
mailContent.metadata = {
...mailContent.metadata,
envelopeCustomizations: envelope.using,
};
}
if (headers.messageId || headers.references) {
const customHeaders = { ...(mailContent.headers || {}) };
if (headers.messageId) {
customHeaders['Message-ID'] = headers.messageId;
}
if (headers.references && headers.references.length > 0) {
customHeaders['References'] = headers.references.join(' ');
}
mailContent.headers = customHeaders;
}
return mailContent;
}
async buildAttachments() {
const mailableAttachments = this.attachments();
const attachments = [];
for (const attachment of mailableAttachments) {
if (attachment.path) {
const content = await fs.readFile(attachment.path);
attachments.push({
filename: attachment.as || path.basename(attachment.path),
content,
contentType: attachment.mime,
});
}
else if (attachment.storage) {
const storagePath = path.join(process.cwd(), 'storage', attachment.storage);
const content = await fs.readFile(storagePath);
attachments.push({
filename: attachment.as || path.basename(attachment.storage),
content,
contentType: attachment.mime,
});
}
else if (attachment.data) {
const content = attachment.data();
attachments.push({
filename: attachment.as || attachment.filename || 'attachment',
content: typeof content === 'string' ? Buffer.from(content) : content,
contentType: attachment.mime,
});
}
}
return attachments;
}
}
exports.Mailable = Mailable;
class AttachmentBuilder {
static fromPath(filePath) {
return new MailableAttachmentBuilder({ path: filePath });
}
static fromStorage(storagePath) {
return new MailableAttachmentBuilder({ storage: storagePath });
}
static fromData(dataFn, filename) {
return new MailableAttachmentBuilder({ data: dataFn, filename });
}
}
exports.AttachmentBuilder = AttachmentBuilder;
class MailableAttachmentBuilder {
constructor(attachment) {
this.attachment = { ...attachment };
}
as(filename) {
this.attachment.as = filename;
return this;
}
withMime(mimeType) {
this.attachment.mime = mimeType;
return this;
}
build() {
return this.attachment;
}
}
exports.MailableAttachmentBuilder = MailableAttachmentBuilder;
//# sourceMappingURL=mailable.js.map