@api-buddy/sendgrid
Version:
API Buddy integration for SendGrid - Email delivery service for transactional and marketing emails
138 lines • 5.29 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendMultipleEmails = exports.sendEmail = exports.sendGridClient = exports.SendGridClient = void 0;
const mail_1 = __importDefault(require("@sendgrid/mail"));
class SendGridClient {
constructor(config) {
this.initialized = false;
this.config = config;
this.initialize();
}
/**
* Initialize the SendGrid client with the API key
*/
initialize() {
if (!this.initialized) {
if (!this.config.apiKey && !process.env.SENDGRID_API_KEY) {
throw new Error('SendGrid API key is required. Either provide it in the config or set the SENDGRID_API_KEY environment variable.');
}
mail_1.default.setApiKey(this.config.apiKey || process.env.SENDGRID_API_KEY);
this.initialized = true;
}
}
/**
* Send an email using SendGrid
* @param options Email options
* @returns Promise with the SendGrid response
*/
async sendEmail(options) {
if (!this.initialized) {
this.initialize();
}
try {
const msg = {
to: options.to,
from: options.from || this.config.defaultFrom || 'no-reply@example.com',
subject: options.subject,
text: options.text,
html: options.html,
templateId: options.templateId,
dynamicTemplateData: options.dynamicTemplateData,
attachments: options.attachments,
categories: options.categories,
customArgs: options.customArgs,
headers: options.headers,
mailSettings: options.mailSettings || this.config.mailSettings,
trackingSettings: options.trackingSettings || this.config.trackingSettings,
replyTo: options.replyTo || this.config.defaultReplyTo,
sendAt: options.sendAt,
batchId: options.batchId,
asm: options.asm || this.config.asm,
ipPoolName: options.ipPoolName || this.config.ipPoolName,
};
const [response] = await mail_1.default.send(msg);
return {
success: true,
statusCode: response.statusCode,
headers: response.headers,
body: response.body,
};
}
catch (error) {
console.error('Error sending email:', error);
throw error;
}
}
/**
* Send multiple emails in a single request
* @param messages Array of email options
* @returns Promise with the SendGrid response
*/
async sendMultipleEmails(messages) {
if (!this.initialized) {
this.initialize();
}
try {
const formattedMessages = messages.map((msg) => ({
to: msg.to,
from: msg.from || this.config.defaultFrom || 'no-reply@example.com',
subject: msg.subject,
text: msg.text,
html: msg.html,
templateId: msg.templateId,
dynamicTemplateData: msg.dynamicTemplateData,
attachments: msg.attachments,
categories: msg.categories,
customArgs: msg.customArgs,
headers: msg.headers,
mailSettings: msg.mailSettings || this.config.mailSettings,
trackingSettings: msg.trackingSettings || this.config.trackingSettings,
replyTo: msg.replyTo || this.config.defaultReplyTo,
sendAt: msg.sendAt,
batchId: msg.batchId,
asm: msg.asm || this.config.asm,
ipPoolName: msg.ipPoolName || this.config.ipPoolName,
}));
const response = await mail_1.default.send(formattedMessages, true);
return {
success: true,
responses: response.map((res) => ({
statusCode: res.statusCode,
headers: res.headers,
body: res.body,
})),
};
}
catch (error) {
console.error('Error sending multiple emails:', error);
throw error;
}
}
}
exports.SendGridClient = SendGridClient;
// Create a singleton instance
exports.sendGridClient = new SendGridClient({
apiKey: process.env.SENDGRID_API_KEY || '',
});
/**
* Send an email using the default SendGrid client
* @param options Email options
* @returns Promise with the SendGrid response
*/
const sendEmail = (options) => {
return exports.sendGridClient.sendEmail(options);
};
exports.sendEmail = sendEmail;
/**
* Send multiple emails in a single request using the default SendGrid client
* @param messages Array of email options
* @returns Promise with the SendGrid response
*/
const sendMultipleEmails = (messages) => {
return exports.sendGridClient.sendMultipleEmails(messages);
};
exports.sendMultipleEmails = sendMultipleEmails;
//# sourceMappingURL=client.js.map