@compassdigital/middleware
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -19,2 +19,4 @@ "use strict"; | ||
this.emitter = emitter; | ||
this.options = options || {}; | ||
this.enabled = this.options.enabled || false; | ||
@@ -36,2 +38,12 @@ this.schema = new dynamoose.Schema({ | ||
}, | ||
"resource": { | ||
"type": "String", | ||
"index": [{ | ||
"global": true, | ||
"rangeKey": "date", | ||
"name": "ResourceIndex", | ||
"project": true, | ||
"throughput": "ON_DEMAND" | ||
}] | ||
}, | ||
"method": "String", | ||
@@ -53,28 +65,28 @@ "request": "Object", | ||
handle(req, res, next) { | ||
if (this.emitter.listenerCount(DATA_EVENT) === 0) { | ||
this.emitter.addListener(DATA_EVENT, (data) => { | ||
for (let property in data) { | ||
if (!this.model[property]) { | ||
this.model[property] = data[property]; | ||
} | ||
} | ||
}); | ||
} | ||
handle() { | ||
return (req, res, next) => { | ||
if (!this.enabled) return next(); | ||
if (this.emitter.listenerCount(SAVE_EVENT) === 0) { | ||
// Grab the response before the request ends and save | ||
this.emitter.addListener(SAVE_EVENT, (res) => { | ||
this.model.response = res; | ||
this.model.date = Date.now(); | ||
if (this.emitter.listenerCount(DATA_EVENT) === 0) { | ||
this.emitter.addListener(DATA_EVENT, (data) => { | ||
Object.assign(this.model, data); | ||
}); | ||
} | ||
if (this.model.method) { | ||
this.model.save((err) => { | ||
if (err) console.warn(err); | ||
}); | ||
} | ||
}); | ||
} | ||
if (this.emitter.listenerCount(SAVE_EVENT) === 0) { | ||
// Grab the response before the request ends and save | ||
this.emitter.addListener(SAVE_EVENT, (res) => { | ||
this.model.response = res; | ||
this.model.date = Date.now(); | ||
return next(); | ||
if (this.model.method) { | ||
this.model.save((err) => { | ||
if (err) console.warn(err); | ||
}); | ||
} | ||
}); | ||
} | ||
return next(); | ||
}; | ||
} | ||
@@ -87,12 +99,4 @@ } | ||
*/ | ||
module.exports = exports = function (req, res, next) { | ||
/** | ||
* @todo If configuration options are needed for the | ||
* middleware, either to change the AWS region, stage, | ||
* or other options, pass an options paramter to the | ||
* exported function above, and into the constructor | ||
* of the class below and return a middleware | ||
* function (req, res, next) from the handle method. | ||
*/ | ||
return new ActionLogger(this).handle(req, res, next); | ||
module.exports = exports = function (options) { | ||
return new ActionLogger(this, options).handle(); | ||
}; |
{ | ||
"name": "@compassdigital/middleware", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "CDL Provider middleware module", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -22,5 +22,6 @@ "use strict"; | ||
it("Should run the action logger and register data, error, and save listeners", async function () { | ||
let ActionLogger = MW.fn.ActionLogger; | ||
it("Should run the action logger and register the data and save listeners", async function () { | ||
let emitter = new EventEmitter(); | ||
let options = { "enabled": true }; | ||
let ActionLogger = MW.fn.ActionLogger.call(emitter, options); | ||
@@ -30,3 +31,3 @@ let req = {}; | ||
let next = sinon.spy(); | ||
ActionLogger.call(emitter, req, res, next); | ||
ActionLogger(req, res, next); | ||
emitter.listenerCount(DATA_EVENT).should.equal(1); | ||
@@ -37,5 +38,5 @@ emitter.listenerCount(SAVE_EVENT).should.equal(1); | ||
it("Should run the action logger and emit the data event", async function () { | ||
let ActionLogger = MW.fn.ActionLogger; | ||
it("Should run the action logger and not register the data and save listeners", async function () { | ||
let emitter = new EventEmitter(); | ||
let ActionLogger = MW.fn.ActionLogger.call(emitter); | ||
@@ -45,3 +46,17 @@ let req = {}; | ||
let next = sinon.spy(); | ||
ActionLogger.call(emitter, req, res, next); | ||
ActionLogger(req, res, next); | ||
emitter.listenerCount(DATA_EVENT).should.equal(0); | ||
emitter.listenerCount(SAVE_EVENT).should.equal(0); | ||
next.calledOnce.should.equal(true); | ||
}); | ||
it("Should run the action logger and process the data event", async function () { | ||
let emitter = new EventEmitter(); | ||
let options = { "enabled": true }; | ||
let ActionLogger = MW.fn.ActionLogger.call(emitter); | ||
let req = {}; | ||
let res = {}; | ||
let next = sinon.spy(); | ||
ActionLogger(req, res, next); | ||
emitter.emit(DATA_EVENT, { "testing": 123 }); | ||
@@ -51,5 +66,6 @@ next.calledOnce.should.equal(true); | ||
it("Should run the action logger and emit the save event", async function () { | ||
let ActionLogger = MW.fn.ActionLogger; | ||
it("Should run the action logger and process the save event", async function () { | ||
let emitter = new EventEmitter(); | ||
let options = { "enabled": true }; | ||
let ActionLogger = MW.fn.ActionLogger.call(emitter, options); | ||
@@ -59,3 +75,3 @@ let req = {}; | ||
let next = sinon.spy(); | ||
ActionLogger.call(emitter, req, res, next); | ||
ActionLogger(req, res, next); | ||
emitter.emit(SAVE_EVENT, { "response": {} }); | ||
@@ -62,0 +78,0 @@ next.calledOnce.should.equal(true); |
17089
442