smtpapi
Advanced tools
Comparing version 0.0.2 to 1.0.0
139
lib/main.js
"use strict"; | ||
var package_json = require('./../package.json'); | ||
var _ = require('lodash'); | ||
function Header() { | ||
this.to = []; | ||
this.sub = {}; | ||
this.unique_args = {}; | ||
this.category = []; | ||
this.section = {}; | ||
this.filters = {}; | ||
function smtpapi(header) { | ||
this.version = package_json.version; | ||
header = header || {}; | ||
this.header = {}; | ||
this.header.to = header.to || []; | ||
this.header.sub = header.sub || {}; | ||
this.header.unique_args = header.unique_args || {}; | ||
this.header.category = header.category || []; | ||
this.header.section = header.section || {}; | ||
this.header.filters = header.filters || {}; | ||
} | ||
Header.prototype.addTo = function(to) { | ||
if (_.isArray(to)) { | ||
this.to = this.to.concat(to); | ||
smtpapi.prototype.addTo = function(to) { | ||
if (to instanceof Array) { | ||
this.header.to = this.header.to.concat(to); | ||
} else { | ||
this.to.push(to); | ||
this.header.to.push(to); | ||
} | ||
}; | ||
Header.prototype.setTos = function(tos) { | ||
this.to = tos; | ||
smtpapi.prototype.setTos = function(to) { | ||
if (to instanceof Array) { | ||
this.header.to = to; | ||
} else { | ||
this.header.to = [to]; | ||
} | ||
}; | ||
Header.prototype.addSubstitution = function(key, val) { | ||
if (_.isArray(val)) { | ||
this.sub[key] = val; | ||
smtpapi.prototype.addSubstitution = function(key, val) { | ||
if (this.header.sub[key] === undefined) this.header.sub[key] = []; | ||
if (val instanceof Array) { | ||
this.header.sub = this.header.sub[key].concat(val); | ||
} else { | ||
this.sub[key] = [val]; | ||
this.header.sub[key].push(val); | ||
} | ||
}; | ||
Header.prototype.setSubstitutions = function(substitutions) { | ||
this.sub = substitutions; | ||
smtpapi.prototype.setSubstitutions = function(subs) { | ||
this.header.sub = subs; | ||
}; | ||
Header.prototype.addUniqueArg = function(object_literal) { | ||
if (_.isObject(object_literal)) { | ||
_.extend(this.unique_args, object_literal); | ||
} | ||
smtpapi.prototype.addUniqueArg = function(key, val) { | ||
this.header.unique_args[key] = val; | ||
}; | ||
Header.prototype.setUniqueArgs = function(object_literal) { | ||
if (_.isObject(object_literal)) { | ||
this.unique_args = object_literal; | ||
} | ||
smtpapi.prototype.setUniqueArgs = function(val) { | ||
this.header.unique_args = val; | ||
}; | ||
Header.prototype.addCategory = function(val) { | ||
if (_.isArray(val)) { | ||
this.category.concat(val); | ||
smtpapi.prototype.addCategory = function(cat) { | ||
if (cat instanceof Array) { | ||
this.header.category.concat(cat); | ||
} else { | ||
this.category.push(val); | ||
this.header.category.push(cat); | ||
} | ||
}; | ||
Header.prototype.setCategories = function(val) { | ||
if (_.isArray(val)) { | ||
this.category = val; | ||
smtpapi.prototype.setCategories = function(cats) { | ||
if (cats instanceof Array) { | ||
this.header.category = cats; | ||
} else { | ||
this.category = [val]; | ||
this.header.category = [cats]; | ||
} | ||
}; | ||
Header.prototype.addSection = function(object_literal){ | ||
if (_.isObject(object_literal)) { | ||
_.extend(this.section, object_literal); | ||
} | ||
smtpapi.prototype.addSection = function(sec, val) { | ||
this.header.section[sec] = val; | ||
}; | ||
Header.prototype.setSections = function(object_literal){ | ||
if (_.isObject(object_literal)) { | ||
this.section = object_literal; | ||
} | ||
smtpapi.prototype.setSections = function(sec) { | ||
this.header.section = sec; | ||
}; | ||
Header.prototype.addFilter = function(filter, setting, val) { | ||
if (!this.filters[filter]) { | ||
this.filters[filter] = {}; | ||
smtpapi.prototype.addFilter = function(filter, setting, val) { | ||
if (this.header.filters[filter] === undefined) { | ||
this.header.filters[filter] = {'settings': {}}; | ||
} | ||
if (!this.filters[filter]['settings']) { | ||
this.filters[filter]['settings'] = {}; | ||
} | ||
this.filters[filter]['settings'][setting] = val; | ||
this.header.filters[filter]['settings'][setting] = val; | ||
}; | ||
Header.prototype.setFilters = function(filters) { | ||
this.filters = filters; | ||
smtpapi.prototype.setFilters = function(filters) { | ||
this.header.filters = filters; | ||
}; | ||
Header.prototype.toJson = function() { | ||
var data = _.clone(this); | ||
_.each(data, function(v, k, list) { | ||
if (_.isEmpty(v)) { | ||
delete list[k]; | ||
smtpapi.prototype.jsonString = function() { | ||
var header = {}; | ||
for (var key in this.header) { | ||
if (this.header.hasOwnProperty(key) && Object.keys(this.header[key]).length) { | ||
header[key] = this.header[key]; | ||
} | ||
}); | ||
} | ||
var json = JSON.stringify(header); | ||
return data; | ||
}; | ||
Header.prototype.jsonString = function() { | ||
var json = JSON.stringify(this.toJson()); | ||
return escapeUnicode(json); | ||
@@ -120,14 +109,4 @@ }; | ||
}); | ||
} | ||
}; | ||
function smtpapi() { | ||
var smtpapi = { | ||
version: package_json.version, | ||
environment: process.env.NODE_ENV || "development", | ||
Header: Header | ||
} | ||
return smtpapi; | ||
} | ||
module.exports = smtpapi(); | ||
module.exports = smtpapi; |
{ | ||
"name": "smtpapi", | ||
"version": "0.0.2", | ||
"version": "1.0.0", | ||
"description": "Build SendGrid X-SMTPAPI headers in nodejs.", | ||
@@ -14,3 +14,3 @@ "main": "lib/main.js", | ||
"type": "git", | ||
"url": "git://github.com/scottmotte/smtpapi-nodejs.git" | ||
"url": "git://github.com/sendgrid/smtpapi-nodejs.git" | ||
}, | ||
@@ -29,3 +29,2 @@ "keywords": [ | ||
"dependencies": { | ||
"lodash": "2.4.1" | ||
}, | ||
@@ -35,4 +34,15 @@ "devDependencies": { | ||
"should": "" | ||
} | ||
}, | ||
"contributors": [ | ||
{ | ||
"name": "Yamil Asusta", | ||
"email": "yamil@sendgrid.com", | ||
"url": "yamilasusta.com" | ||
}, | ||
{ | ||
"name": "Scott Motte", | ||
"email": "scott@scottmotte.com", | ||
"url": "scottmotte.com" | ||
} | ||
] | ||
} | ||
@@ -10,3 +10,3 @@ # smtpapi-nodejs | ||
var smtpapi = require('smtpapi'); | ||
var header = smtpapi.Header(); | ||
var header = new smtpapi(); | ||
header.addTo('you@youremail.com'); | ||
@@ -31,3 +31,3 @@ header.setUniqueArgs({cow: 'chicken'}); | ||
... | ||
"smtpapi": "0.0.2" | ||
"smtpapi": "1.0.0" | ||
} | ||
@@ -49,3 +49,3 @@ } | ||
var smtpapi = require('smtpapi'); | ||
var header = new smtpapi.Header(); | ||
var header = new smtpapi(); | ||
``` | ||
@@ -59,3 +59,3 @@ | ||
var smtpapi = require('smtpapi'); | ||
var header = new smtpapi.Header(); | ||
var header = new smtpapi(); | ||
header.jsonString(); | ||
@@ -67,3 +67,3 @@ ``` | ||
```javascript | ||
var header = new smtpapi.Header(); | ||
var header = new smtpapi(); | ||
header.addTo('you@youremail.com'); | ||
@@ -76,3 +76,3 @@ header.addTo('other@otheremail.com'); | ||
```javascript | ||
var header = new smtpapi.Header(); | ||
var header = new smtpapi(); | ||
header.setTos(['you@youremail.com', 'other@otheremail.com'); | ||
@@ -84,12 +84,12 @@ ``` | ||
```javascript | ||
var header = new smtpapi.Header(); | ||
header.addSubstitution('keep', 'secret'); // sub = {keep: ['secret']} | ||
header.addSubstitution('other', ['one', 'two']); // sub = {keep: ['secret'], other: ['one', 'two']} | ||
var header = new smtpapi(); | ||
header.addSubstitution('keep', ['secret']); | ||
header.addSubstitution('other', ['one', 'two']); | ||
``` | ||
### setSubstitutions | ||
### setSubstitutions | ||
```javascript | ||
var header = new smtpapi.Header(); | ||
header.setSubstitution({'-charge-': 'This ship is useless.'}); // section = {'-charge-': 'This ship is useless.'} | ||
var header = new smtpapi(); | ||
header.setSubstitution({'keep': ['secret']); | ||
``` | ||
@@ -100,5 +100,4 @@ | ||
```javascript | ||
var header = new smtpapi.Header(); | ||
header.addUniqueArg({cow: 'chicken'}); // unique_args = {cow: 'chicken'} | ||
header.addUniqueArg({cat: 'dog'}); // unique_args = {cow: 'chicken', cat: 'dog'} | ||
var header = new smtpapi(); | ||
header.addUniqueArg('cat', 'dogs'); | ||
``` | ||
@@ -109,5 +108,5 @@ | ||
```javascript | ||
var header = new smtpapi.Header(); | ||
header.setUniqueArgs({cow: 'chicken'}); // unique_args = {cow: 'chicken'} | ||
header.setUniqueArgs({dad: 'proud'}); // unique_args = {dad: 'proud'} | ||
var header = new smtpapi(); | ||
header.setUniqueArgs({cow: 'chicken'}); | ||
header.setUniqueArgs({dad: 'proud'}); | ||
``` | ||
@@ -118,5 +117,5 @@ | ||
```javascript | ||
var header = new smtpapi.Header(); | ||
header.addCategory('tactics'); // category = ['tactics'] | ||
header.addCategory('advanced'); // category = ['tactics', 'advanced'] | ||
var header = new smtpapi(); | ||
header.addCategory('tactics'); | ||
header.addCategory('advanced'); | ||
``` | ||
@@ -127,4 +126,4 @@ | ||
```javascript | ||
var header = new smtpapi.Header(); | ||
header.setCategories(['snowball-fight', 'tactics']); // category = ['snowball-fight', 'tactics'] | ||
var header = new smtpapi(); | ||
header.setCategories(['tactics', 'advanced']); | ||
``` | ||
@@ -135,4 +134,4 @@ | ||
```javascript | ||
var header = new smtpapi.Header(); | ||
header.addSection({'-charge-': 'This ship is useless.'}); // section = {'-charge-': 'This ship is useless.'} | ||
var header = new smtpapi(); | ||
header.addSection('-charge-', 'This ship is useless.'); | ||
``` | ||
@@ -143,4 +142,4 @@ | ||
```javascript | ||
var header = new smtpapi.Header(); | ||
header.setSections({'-charge-': 'This ship is useless.', '-other': 'Another section here'}); // section = {'-charge-': 'This ship is useless.', '-other': 'Another section here'} | ||
var header = new smtpapi(); | ||
header.setSections({'-charge-': 'This ship is useless.', '-other': 'Another section here'}); | ||
``` | ||
@@ -153,3 +152,3 @@ | ||
```javascript | ||
var header = new smtpapi.Header(); | ||
var header = new smtpapi(); | ||
header.addFilter('footer', 'enable', 1); | ||
@@ -164,3 +163,3 @@ header.addFilter('footer', 'text/html', '<strong>boo</strong>'); | ||
```javascript | ||
var header = new smtpapi.Header(); | ||
var header = new smtpapi(); | ||
header.setFilters({ | ||
@@ -180,3 +179,3 @@ 'footer': { | ||
```javascript | ||
```javascript | ||
var nodemailer = require('nodemailer'); | ||
@@ -186,3 +185,3 @@ var smtpapi = require('smtpapi'); | ||
// Build the smtpapi header | ||
var header = new smtpapi.Header(); | ||
var header = new smtpapi(); | ||
header.addTo('you@youremail.com'); | ||
@@ -218,3 +217,3 @@ header.setUniqueArgs({cow: 'chicken'}); | ||
if (error) { | ||
if (error) { | ||
console.log(error); | ||
@@ -221,0 +220,0 @@ } else { |
166
test/main.js
@@ -6,124 +6,92 @@ var assert = require('assert'); | ||
var result; | ||
describe('smtapi', function() { | ||
before(function() { | ||
result = smtpapi; | ||
beforeEach(function() { | ||
header = new smtpapi(); | ||
}); | ||
it('version should be set', function() { | ||
result.version.should.eql("0.0.2"); | ||
header.version.should.eql("1.0.0"); | ||
}); | ||
describe('.Header', function() { | ||
it('has a jsonString method', function() { | ||
var header = new smtpapi.Header(); | ||
it('has a jsonString method', function() { | ||
header.jsonString().should.eql(t.json_string); | ||
}); | ||
header.jsonString().should.eql(t.json_string); | ||
}); | ||
it('addTo', function() { | ||
header.addTo('addTo@mailinator.com'); | ||
header.jsonString().should.eql(t.add_to); | ||
}); | ||
it('addTo', function() { | ||
var header = new smtpapi.Header(); | ||
it('setTos', function() { | ||
header.setTos(['setTos@mailinator.com']); | ||
header.jsonString().should.eql(t.set_tos); | ||
}); | ||
header.addTo('addTo@mailinator.com'); | ||
header.jsonString().should.eql(t.add_to); | ||
}); | ||
it('addSubstitution', function() { | ||
header.addSubstitution('sub', 'val'); | ||
header.jsonString().should.eql(t.add_substitution); | ||
}); | ||
it('setTos', function() { | ||
var header = new smtpapi.Header(); | ||
it('setSubstitutions', function() { | ||
header.setSubstitutions({'sub': ['val']}); | ||
header.jsonString().should.eql(t.set_substitutions); | ||
}); | ||
header.setTos(['setTos@mailinator.com']); | ||
header.jsonString().should.eql(t.set_tos); | ||
}); | ||
it('addUniqueArg', function() { | ||
header.addUniqueArg('add_unique_argument_key', 'add_unique_argument_value'); | ||
header.addUniqueArg('add_unique_argument_key_2', 'add_unique_argument_value_2'); | ||
header.jsonString().should.eql(t.add_unique_arg); | ||
}); | ||
it('addSubstitution', function() { | ||
var header = new smtpapi.Header(); | ||
it('setUniqueArgs', function() { | ||
header.setUniqueArgs({set_unique_argument_key: 'set_unique_argument_value'}); | ||
header.jsonString().should.eql(t.set_unique_args); | ||
}); | ||
header.addSubstitution('sub', 'val'); | ||
header.jsonString().should.eql(t.add_substitution); | ||
}); | ||
it('addCategory', function() { | ||
header.addCategory('addCategory'); | ||
header.addCategory('addCategory2'); | ||
header.jsonString().should.eql(t.add_category); | ||
}); | ||
it('setSubstitutions', function() { | ||
var header = new smtpapi.Header(); | ||
it('addCategoryUnicode', function() { | ||
header.addCategory('カテゴリUnicode'); | ||
header.addCategory('カテゴリ2Unicode'); | ||
header.jsonString().should.eql(t.add_category_unicode); | ||
}); | ||
header.setSubstitutions({'sub': ['val']}); | ||
header.jsonString().should.eql(t.set_substitutions); | ||
}); | ||
it('setCategories', function() { | ||
header.setCategories(['setCategories']); | ||
header.jsonString().should.eql(t.set_categories); | ||
}); | ||
it('addUniqueArg', function() { | ||
var header = new smtpapi.Header(); | ||
it('addSection', function() { | ||
header.addSection('set_section_key', 'set_section_value'); | ||
header.addSection('set_section_key_2', 'set_section_value_2'); | ||
header.jsonString().should.eql(t.add_section); | ||
}); | ||
header.addUniqueArg({add_unique_argument_key: 'add_unique_argument_value'}); | ||
header.addUniqueArg({add_unique_argument_key_2: 'add_unique_argument_value_2'}); | ||
header.jsonString().should.eql(t.add_unique_arg); | ||
}); | ||
it('setSections', function() { | ||
header.setSections({'set_section_key': 'set_section_value'}); | ||
header.jsonString().should.eql(t.set_sections); | ||
}); | ||
it('setUniqueArgs', function() { | ||
var header = new smtpapi.Header(); | ||
it('addFilter', function() { | ||
header.addFilter('footer', 'text/html', '<strong>boo</strong>'); | ||
header.jsonString().should.eql(t.add_filter); | ||
}); | ||
header.setUniqueArgs({set_unique_argument_key: 'set_unique_argument_value'}); | ||
header.jsonString().should.eql(t.set_unique_args); | ||
}); | ||
it('addCategory', function() { | ||
var header = new smtpapi.Header(); | ||
header.addCategory('addCategory'); | ||
header.addCategory('addCategory2'); | ||
header.jsonString().should.eql(t.add_category); | ||
}); | ||
it('addCategoryUnicode', function() { | ||
var header = new smtpapi.Header(); | ||
header.addCategory('カテゴリUnicode'); | ||
header.addCategory('カテゴリ2Unicode'); | ||
header.jsonString().should.eql(t.add_category_unicode); | ||
}); | ||
it('setCategories', function() { | ||
var header = new smtpapi.Header(); | ||
header.setCategories(['setCategories']); | ||
header.jsonString().should.eql(t.set_categories); | ||
}); | ||
it('addSection', function() { | ||
var header = new smtpapi.Header(); | ||
header.addSection({'set_section_key': 'set_section_value'}); | ||
header.addSection({'set_section_key_2': 'set_section_value_2'}); | ||
header.jsonString().should.eql(t.add_section); | ||
}); | ||
it('setSections', function() { | ||
var header = new smtpapi.Header(); | ||
header.setSections({'set_section_key': 'set_section_value'}); | ||
header.jsonString().should.eql(t.set_sections); | ||
}); | ||
it('addFilter', function() { | ||
var header = new smtpapi.Header(); | ||
header.addFilter('footer', 'text/html', '<strong>boo</strong>'); | ||
header.jsonString().should.eql(t.add_filter); | ||
}); | ||
it('setFilters', function() { | ||
var header = new smtpapi.Header(); | ||
var filter = { | ||
'footer': { | ||
'setting': { | ||
'enable': 1, | ||
'text/plain': 'You can haz footers!' | ||
} | ||
it('setFilters', function() { | ||
var filter = { | ||
'footer': { | ||
'setting': { | ||
'enable': 1, | ||
'text/plain': 'You can haz footers!' | ||
} | ||
} | ||
} | ||
header.setFilters(filter); | ||
header.jsonString().should.eql(t.set_filters); | ||
}); | ||
header.setFilters(filter); | ||
header.jsonString().should.eql(t.set_filters); | ||
}); | ||
}); |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
0
1
0
12549
187
221
- Removedlodash@2.4.1
- Removedlodash@2.4.1(transitive)