sendgrid-template-helper
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -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(() => {}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
12495
252
0
84
7
+ Added@sendgrid/client@7.7.0(transitive)
+ Added@sendgrid/helpers@7.7.0(transitive)
+ Added@sendgrid/mail@7.7.0(transitive)
+ Addedaxios@0.26.1(transitive)
+ Addedfollow-redirects@1.15.9(transitive)
- Removed@sendgrid/client@6.5.5(transitive)
- Removed@sendgrid/helpers@6.5.5(transitive)
- Removed@sendgrid/mail@6.5.5(transitive)
- Removed@types/caseless@0.12.5(transitive)
- Removed@types/node@22.10.2(transitive)
- Removed@types/request@2.48.12(transitive)
- Removed@types/tough-cookie@4.0.5(transitive)
- Removedajv@6.12.6(transitive)
- Removedansi-styles@3.2.1(transitive)
- Removedasn1@0.2.6(transitive)
- Removedassert-plus@1.0.0(transitive)
- Removedasynckit@0.4.0(transitive)
- Removedaws-sign2@0.7.0(transitive)
- Removedaws4@1.13.2(transitive)
- Removedbcrypt-pbkdf@1.0.2(transitive)
- Removedcaseless@0.12.0(transitive)
- Removedchalk@2.4.2(transitive)
- Removedcolor-convert@1.9.3(transitive)
- Removedcolor-name@1.1.3(transitive)
- Removedcombined-stream@1.0.8(transitive)
- Removedcore-util-is@1.0.2(transitive)
- Removeddashdash@1.14.1(transitive)
- Removeddelayed-stream@1.0.0(transitive)
- Removedecc-jsbn@0.1.2(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedextend@3.0.2(transitive)
- Removedextsprintf@1.3.0(transitive)
- Removedfast-deep-equal@3.1.3(transitive)
- Removedfast-json-stable-stringify@2.1.0(transitive)
- Removedforever-agent@0.6.1(transitive)
- Removedform-data@2.3.32.5.2(transitive)
- Removedgetpass@0.1.7(transitive)
- Removedhar-schema@2.0.0(transitive)
- Removedhar-validator@5.1.5(transitive)
- Removedhas-flag@3.0.0(transitive)
- Removedhttp-signature@1.2.0(transitive)
- Removedis-typedarray@1.0.0(transitive)
- Removedisstream@0.1.2(transitive)
- Removedjsbn@0.1.1(transitive)
- Removedjson-schema@0.4.0(transitive)
- Removedjson-schema-traverse@0.4.1(transitive)
- Removedjson-stringify-safe@5.0.1(transitive)
- Removedjsprim@1.4.2(transitive)
- Removedmime-db@1.52.0(transitive)
- Removedmime-types@2.1.35(transitive)
- Removedoauth-sign@0.9.0(transitive)
- Removedperformance-now@2.1.0(transitive)
- Removedpsl@1.15.0(transitive)
- Removedpunycode@2.3.1(transitive)
- Removedqs@6.5.3(transitive)
- Removedrequest@2.88.2(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedsafer-buffer@2.1.2(transitive)
- Removedsshpk@1.18.0(transitive)
- Removedsupports-color@5.5.0(transitive)
- Removedtough-cookie@2.5.0(transitive)
- Removedtunnel-agent@0.6.0(transitive)
- Removedtweetnacl@0.14.5(transitive)
- Removedundici-types@6.20.0(transitive)
- Removeduri-js@4.4.1(transitive)
- Removeduuid@3.4.0(transitive)
- Removedverror@1.10.0(transitive)
Updated@sendgrid/client@^7.0.1
Updated@sendgrid/mail@^7.0.1