Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sendgrid-template-helper

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sendgrid-template-helper - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

87

lib/index.js

@@ -1,9 +0,9 @@

'use strict';
"use strict";
const fs = require('fs');
const fspath = require('path');
const crypto = require('crypto');
const { Client } = require('@sendgrid/client');
const { MailService } = require('@sendgrid/mail');
const _ = require('lodash');
const fs = require("fs");
const fspath = require("path");
const crypto = require("crypto");
const { Client } = require("@sendgrid/client");
const { MailService } = require("@sendgrid/mail");
const _ = require("lodash");

@@ -17,10 +17,10 @@ function Email(settings) {

if (_.isUndefined(settings.apiKey) || _.isEmpty(settings.apiKey)) {
throw new Error('SendGird API Key is required');
throw new Error("SendGird API Key is required");
}
this.apiKey = settings.apiKey;
this.prefix = settings.prefix ? settings.prefix : 'sendgrid_template_helper_';
this.prefix = settings.prefix ? settings.prefix : "sendgrid_template_helper_";
this.postfix = `_${this.createHash(this.prefix + this.apiKey)}`;
this.sgClient = new Client();
this.sgClient.setApiKey(this.apiKey);
this.sgClient.setDefaultRequest('baseUrl', 'https://api.sendgrid.com/v3');
this.sgClient.setDefaultRequest("baseUrl", "https://api.sendgrid.com/v3");
this.sgMail = new MailService();

@@ -41,3 +41,3 @@ this.sgMail.setApiKey(this.apiKey);

*/
Email.prototype.send = function(messages) {
Email.prototype.send = function (messages) {
return new Promise((resolve, reject) => {

@@ -53,3 +53,3 @@ setTimeout(() => {

return sequence.then(() =>
this.mangleMessage(msg).then(m => {
this.mangleMessage(msg).then((m) => {
msgs.push(m);

@@ -63,7 +63,7 @@ })

})
.then(msgs => this.sgMail.send(msgs))
.then((msgs) => this.sgMail.send(msgs))
.then(resolve)
.catch(err => {
.catch((err) => {
if (err.code === 401 || err.code === 403) {
err = new Error('Invalid SendGrid API Key');
err = new Error("Invalid SendGrid API Key");
}

@@ -84,3 +84,3 @@ reject(err);

*/
Email.prototype.mangleMessage = function(msg) {
Email.prototype.mangleMessage = function (msg) {
return Promise.resolve().then(() => {

@@ -95,3 +95,3 @@ msg = _.mapKeys(msg, (v, k) => _.camelCase(k));

return this.getTemplateId(msg.templatePath).then(templateId => {
return this.getTemplateId(msg.templatePath).then((templateId) => {
msg.templateId = templateId;

@@ -115,3 +115,3 @@ return msg;

*/
Email.prototype.sgRequest = function(method, url, body) {
Email.prototype.sgRequest = function (method, url, body) {
const request = { method, url, body };

@@ -129,5 +129,5 @@ return this.sgClient.request(request).then(([response, body]) => {

*/
Email.prototype.getTemplates = function() {
return this.sgRequest('GET', '/templates?generations=dynamic').then(
body => body.templates
Email.prototype.getTemplates = function () {
return this.sgRequest("GET", "/templates?generations=dynamic").then(
(body) => body.templates
);

@@ -144,6 +144,6 @@ };

*/
Email.prototype.createTemplate = function(templateName) {
return this.sgRequest('POST', '/templates', {
Email.prototype.createTemplate = function (templateName) {
return this.sgRequest("POST", "/templates", {
name: templateName,
generation: 'dynamic'
generation: "dynamic",
});

@@ -162,4 +162,8 @@ };

*/
Email.prototype.createVersion = function(templateId, versionName, htmlContent) {
return this.sgRequest('POST', `/templates/${templateId}/versions`, {
Email.prototype.createVersion = function (
templateId,
versionName,
htmlContent
) {
return this.sgRequest("POST", `/templates/${templateId}/versions`, {
template_id: templateId,

@@ -169,3 +173,3 @@ name: versionName,

active: 1,
subject: '{{subject}}'
subject: "{{subject}}",
});

@@ -185,3 +189,3 @@ };

*/
Email.prototype.updateVersion = function(
Email.prototype.updateVersion = function (
versionId,

@@ -193,7 +197,7 @@ templateId,

return this.sgRequest(
'PATCH',
"PATCH",
`/templates/${templateId}/versions/${versionId}`,
{
name: versionName,
html_content: htmlContent
html_content: htmlContent,
}

@@ -211,3 +215,3 @@ );

*/
Email.prototype.getTemplateId = function(templatePath) {
Email.prototype.getTemplateId = function (templatePath) {
const templateFilename = fspath.parse(templatePath).base;

@@ -219,9 +223,9 @@ const templateName = `${this.prefix}${templateFilename}${this.postfix}`;

}
const templateContent = fs.readFileSync(templatePath, 'utf-8');
const templateContent = fs.readFileSync(templatePath, "utf-8");
const versionName = this.createHash(templateContent);
return this.getTemplates().then(templates => {
const existingTemplate = _.find(templates, t => t.name === templateName);
return this.getTemplates().then((templates) => {
const existingTemplate = _.find(templates, (t) => t.name === templateName);
if (!existingTemplate) {
return this.createTemplate(templateName).then(template => {
return this.createTemplate(templateName).then((template) => {
return this.createVersion(

@@ -231,3 +235,3 @@ template.id,

templateContent
).then(version => {
).then((version) => {
Email.templateIds[templateName] = version.template_id;

@@ -251,3 +255,3 @@ return version.template_id;

templateContent
).then(version => {
).then((version) => {
Email.templateIds[templateName] = version.template_id;

@@ -262,3 +266,3 @@ return version.template_id;

templateContent
).then(version => {
).then((version) => {
Email.templateIds[templateName] = version.template_id;

@@ -278,9 +282,6 @@ return version.template_id;

*/
Email.prototype.createHash = function(message) {
return crypto
.createHash('md5')
.update(message)
.digest('hex');
Email.prototype.createHash = function (message) {
return crypto.createHash("md5").update(message).digest("hex");
};
module.exports = Email;
{
"name": "sendgrid-template-helper",
"version": "1.0.0",
"version": "1.0.1",
"description": "The Sendgrid wrapper helps to send an email with a dynamic template stored on disk.",

@@ -25,6 +25,6 @@ "main": "./lib/index.js",

},
"homepage": "https://github.tdev.app/sendgrid-template-helper",
"homepage": "https://tdev.app/sendgrid-template-helper",
"dependencies": {
"@sendgrid/client": "^6.5.3",
"@sendgrid/mail": "^6.5.3",
"@sendgrid/client": "^7.0.1",
"@sendgrid/mail": "^7.0.1",
"lodash": "^4.17.15"

@@ -34,6 +34,14 @@ },

"chai": "^4.2.0",
"mocha": "^7.1.0",
"sinon": "^9.0.0",
"husky": "^4.2.5",
"mocha": "^7.1.2",
"prettier": "^2.0.5",
"pretty-quick": "^2.0.1",
"sinon": "^9.0.2",
"sinon-chai": "^3.5.0"
},
"husky": {
"hooks": {
"pre-commit": "pretty-quick --staged"
}
}
}
[![npm version](https://badge.fury.io/js/sendgrid-template-helper.svg)](https://badge.fury.io/js/sendgrid-template-helper)
[![Node.js CI](https://github.com/t-ho/sendgrid-template-helper/workflows/Node.js%20CI/badge.svg?branch=master)](https://github.com/t-ho/sendgrid-template-helper/actions)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)

@@ -36,8 +37,8 @@ [![license](https://img.shields.io/npm/l/sendgrid-template-helper)](https://github.com/t-ho/sendgrid-template-helper/blob/master/LICENSE)

```javascript
const path = require('path');
const Email = require('sendgrid-template-helper');
const path = require("path");
const Email = require("sendgrid-template-helper");
const settings = {
apiKey: process.env.SENDGRID_API_KEY,
prefix: 'your_app_name_'
prefix: "your_app_name_",
};

@@ -49,10 +50,10 @@

.send({
to: 'user@tdev.app',
from: 'admin@tdev.app',
to: "user@tdev.app",
from: "admin@tdev.app",
subject: `[Test] Sendgrid template helper`,
templatePath: path.resolve(__dirname, './dynamic-email-template.html'), // absolute path to your template
templatePath: path.resolve(__dirname, "./dynamic-email-template.html"), // absolute path to your template
dynamicTemplateData: {
// your dynamic template data
username: 'user'
}
username: "user",
},
})

@@ -59,0 +60,0 @@ .then(() => {})

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc