@dbbs/strapi-stripe-payment
Version:
Strapi integration plugin for Stripe payment system
75 lines (74 loc) • 3.19 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SendgridService = void 0;
const SendGrid = __importStar(require("@sendgrid/mail"));
const errors_1 = require("../../dbbs-packages/common/errors");
/**
* Service for sending emails using SendGrid.
* @class
*/
class SendgridService {
/**
* Creates an instance of SendgridService.
* @constructor
* @param {string} [apiKey=process.env.SENGRID_API_KEY] - The SendGrid API key. If not provided, it will default to the value of the SENGRID_API_KEY environment variable.
* @param verifiedEmail
*/
constructor(apiKey, verifiedEmail) {
this.apiKey = apiKey;
this.verifiedEmail = verifiedEmail;
SendGrid.setApiKey(apiKey);
}
/**
* Sends an email using SendGrid.
* @async
* @param {ISendEmailInput} data - The data required to send the email.
* @param {string} data.recipientEmail - The recipient's email address.
* @param {string} data.senderEmail - The sender's email address.
* @param {string} data.subject - The subject of the email.
* @param {string} data.message - The HTML message content of the email.
* @param {string} data.text - The plain text content of the email.
* @throws {ValidationError} If any required field (recipientEmail, senderEmail, message, subject, or text) is missing.
*/
async sendEmail(data) {
if (!(data === null || data === void 0 ? void 0 : data.recipientEmail)) {
throw new errors_1.ValidationError('recipientEmail value not specified');
}
if (!(data === null || data === void 0 ? void 0 : data.message)) {
throw new errors_1.ValidationError('message value not specified');
}
if (!(data === null || data === void 0 ? void 0 : data.subject)) {
throw new errors_1.ValidationError('subject value not specified');
}
return SendGrid.send({
subject: data.subject,
to: data.recipientEmail,
from: this.verifiedEmail,
html: data.message
});
}
}
exports.SendgridService = SendgridService;