UNPKG

ts-mailer

Version:

This lib provide a class and function as singleton instance to compile a hbs template adding data and send it as an email using aws-ses. ## Send email using hbs template and aws-ses

114 lines 5.21 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Mailer = void 0; const client_sesv2_1 = require("@aws-sdk/client-sesv2"); const tsyringe_1 = require("tsyringe"); const mailer_provider_1 = require("./mailer.provider"); let Mailer = class Mailer { constructor(awsMailer, templateCompiler) { var _a; this.templateCompiler = templateCompiler; const region = (_a = process.env.AWS_MAILER_REGION) !== null && _a !== void 0 ? _a : process.env.AWS_DEFAULT_REGION; if (!mailer_provider_1.validRegions.includes(`${region}`)) throw new Error(`Invalid aws region: ${region} on environment`); this.client = awsMailer.init({ region }); } /** * @description singleton client instance to send emails by aws ses * @param params as Object * @example * { * fromEmail: 'my-email@domain.com', * subject: 'some subject', * templatePath: resolve(__dirname, 'templates', 'my-template.hbs'), * toEmails: [ 'destination@domain.com' ], * data: { userName: 'John Doe' }, * bcc: [ 'financial@domain.com' ], * cc: [ 'my-email@domain.com' ] * } * @returns `{ $metadata, MessageId }` * @example * ` * "$metadata": { * "httpStatusCode": 200, * "requestId": "e6c808b4-4246-43a5-908d-bfb2d42b5de0", * "attempts": 1, * "totalRetryDelay": 0 * } * "MessageId": "0100017fa29f0e77-d32250df-e245-4bbc-b7f3-9d56a0a214ae-000000" * ` * * @augments data the values refer to what is being applied in the template * @example * // on template you can access data as example * ` * <h1>{{ userName }}</h1> * ` */ sendEmail(params) { return __awaiter(this, void 0, void 0, function* () { const templateExists = this.templateCompiler.exists(params.templatePath); if (!templateExists) throw new Error(`Template not found on path: ${params.templatePath}`); const template = this.templateCompiler.getTemplate(params.templatePath); const htmlWithData = this.templateCompiler.compileTemplate(template, params.data); const command = new client_sesv2_1.SendEmailCommand({ Destination: { ToAddresses: params.toEmails, CcAddresses: params.cc, BccAddresses: params.bcc, }, Content: { Simple: { Body: { Html: { Data: htmlWithData, Charset: 'utf-8', }, Text: { Data: JSON.stringify(params.data), Charset: 'utf-8', }, }, Subject: { Data: params.subject, Charset: 'utf-8', }, }, }, FromEmailAddress: params.fromEmail, }); return this.client.send(command); }); } }; Mailer = __decorate([ (0, tsyringe_1.singleton)(), __param(0, (0, tsyringe_1.inject)('AwsMailerProvider')), __param(1, (0, tsyringe_1.inject)('TemplateCompiler')), __metadata("design:paramtypes", [Object, Object]) ], Mailer); exports.Mailer = Mailer; exports.default = Mailer; //# sourceMappingURL=mail-sender.js.map