From 9117f32c53279867eb6f679862500c49d35eda5d Mon Sep 17 00:00:00 2001 From: Yotam Tanay Date: Sat, 1 Mar 2014 18:21:32 +0200 Subject: [PATCH] Fixed paramater merging and added optional values --- lib/ultrases.js | 34 ++++++++++++++++++++-------------- lib/util.js | 24 +++++++++++++++++------- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/lib/ultrases.js b/lib/ultrases.js index 3b05633..3b89205 100644 --- a/lib/ultrases.js +++ b/lib/ultrases.js @@ -12,6 +12,9 @@ var util = require('./util'); function UltraSES(config){ + if(!(this instanceof UltraSES)) + return new UltraSES(config); + util.checkRequiredParams(config); var AWS = require('aws-sdk'); @@ -22,9 +25,11 @@ function UltraSES(config){ } UltraSES.prototype.send = function(data, done) { - data = this._typer(this._merge(data)); + console.dir(data); + data = this._merge(data); + console.dir(data); util.checkRequiredParamsSend(data); - this.ses.sendEmail({ + var params = { Source: data.from, Destination: { ToAddresses: (typeof data.to === 'string') ? [data.to] : data.to, @@ -44,28 +49,29 @@ UltraSES.prototype.send = function(data, done) { } } }, - ReplyToAddresses: data.replyTo, - ReturnPath: data.returnPath - }, done); + }; + if(data.replyTo) params.ReplyToAddresses = data.replyTo; + this.ses.sendEmail(params, done); }; UltraSES.prototype.sendHTML = function(data, html, done) { - data = this._merge(data); data.html = html; data.text = require('html-to-text').fromString(html); this.send(data, done); }; UltraSES.prototype.sendTemplate = function(data, template, done) { - var compiled = require('jade').compile(template.template)(template.locals); - data.html = compiled; - data.text = require('html-to-text').fromString(compiled); - this.send(data, done); -}; + if(!/\.jade$/.test(template.file)){ + throw 'template file must be a jade template.'; + return; + } -UltraSES.prototype._prep = function(data) { - data = this._merge(data); - data = this._typer(data); + require('fs').readFile(template.file, function(err, templateFile){ + var compiled = require('jade').compile(templateFile)(template.locals); + data.html = compiled; + data.text = require('html-to-text').fromString(compiled); + this.send(data, done); + }) }; UltraSES.prototype._merge = function(data, defaults) { diff --git a/lib/util.js b/lib/util.js index bda4faf..c1f3eb3 100644 --- a/lib/util.js +++ b/lib/util.js @@ -21,16 +21,26 @@ exports.checkRequiredParams = function checkRequiredParams(config) { }; exports.checkRequiredParamsSend = function checkRequiredParamsSend(params) { - var props = {from: 'string', to: 'array', cc: 'array', bcc: 'array', subject: 'string', text: 'string', html: 'string', replyTo: 'array', returnPath: 'string'}; - for(var prop in props){ - if(props[prop] === 'array'){ - if(!Array.isArray(params[prop])){ - throw new TypeError('\'' + prop + '\' must be of array'); + var doCheck = function __UltraSES_util_checkRequiredParamsSend_doCheck(prop, type, value, isOptional){ + if(isOptional && typeof value === 'undefined') return; + if(type === 'array'){ + if(!Array.isArray(value)){ + params[prop] = [value]; } } else { - if(typeof params[prop] !== props[prop]){ - throw new TypeError('\'' + prop + '\' must be of ' + props[prop]); + if(typeof value !== type){ + if(!isOptional) throw new TypeError('\'' + prop + '\' must be of type ' + type); } } } + console.dir(params); + var required = {from: 'string', to: 'array', cc: 'array', bcc: 'array', subject: 'string', text: 'string', html: 'string'}; + var optional = {replyTo: 'array', returnPath: 'string'}; + for(var prop in required){ + doCheck(prop, required[prop], params[prop]); + } + for(var prop in optional){ + doCheck(prop, optional[prop], params[prop], true); + } + };