lambda-wrap
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -5,32 +5,52 @@ 'use strict'; | ||
const defaultResponse = require('./default-response'); | ||
const defaultErrorResponse = require('./default-error-response'); | ||
const defaultResponse = require('./response'); | ||
const defaultErrorResponse = require('./error-response'); | ||
const types = { | ||
MIDDLEWARE: 'middleware', | ||
CATCH: 'catch', | ||
WRAP: 'wrap' | ||
}; | ||
const MIDDLEWARE = 'middleware'; | ||
const CATCH = 'catch'; | ||
const WRAP = 'wrap'; | ||
/** | ||
* Lorem ipsum. | ||
* | ||
* @constructor | ||
* @param {Object} [options={}] - Assign attributes to event object. | ||
*/ | ||
function LambdaWrap (options = {}) { | ||
const wrap = (fn) => { | ||
const wrapped = { type: types.WRAP, fn: co.wrap(fn) }; | ||
const wrapped = { | ||
type: WRAP, | ||
fn: co.wrap(fn) | ||
}; | ||
return function (event, context, callback) { | ||
const { | ||
responseHandler, errorResponseHandler, middlewareHandlers, catchHandlers, logger | ||
responseHandler, | ||
errorResponseHandler, | ||
_middlewareHandlers, | ||
_catchHandlers, | ||
logger | ||
} = wrap; | ||
Object.assign(context, { callbackWaitsForEmptyEventLoop: false }); | ||
Object.assign(event, { options }); | ||
const promisesToResolve = middlewareHandlers.concat(wrapped, catchHandlers) | ||
Object.keys(options).forEach((option) => { | ||
if (event[option]) { | ||
Object.assign(event[option], options[option]); | ||
} else { | ||
event[option] = options[option]; // eslint-disable-line no-param-reassign | ||
} | ||
}); | ||
const promisesToResolve = _middlewareHandlers.concat(wrapped, _catchHandlers) | ||
.reduce((acc, cur) => { | ||
switch (cur.type) { | ||
case types.MIDDLEWARE: | ||
case MIDDLEWARE: | ||
return acc.then(() => cur.fn(event, context)); | ||
case types.WRAP: | ||
case WRAP: | ||
return acc.then(() => cur.fn(event, context)); | ||
case types.CATCH: | ||
case CATCH: | ||
return acc.catch(err => cur.fn(err, event, context)); | ||
@@ -50,4 +70,8 @@ default: | ||
wrap.middlewareHandlers = []; | ||
wrap.catchHandlers = []; | ||
// Private, not meant to be accessed directly. | ||
// Use wrap.before() and wrap.catch() instead. | ||
wrap._middlewareHandlers = []; | ||
wrap._catchHandlers = []; | ||
// Public, meant to be accessed directly. | ||
wrap.responseHandler = defaultResponse; | ||
@@ -58,4 +82,4 @@ wrap.errorResponseHandler = defaultErrorResponse; | ||
wrap.before = (fn) => { | ||
wrap.middlewareHandlers.push({ | ||
type: types.MIDDLEWARE, | ||
wrap._middlewareHandlers.push({ | ||
type: MIDDLEWARE, | ||
fn: co.wrap(fn) | ||
@@ -66,4 +90,4 @@ }); | ||
wrap.catch = (fn) => { | ||
wrap.catchHandlers.push({ | ||
type: types.CATCH, | ||
wrap._catchHandlers.push({ | ||
type: CATCH, | ||
fn: co.wrap(fn) | ||
@@ -70,0 +94,0 @@ }); |
{ | ||
"name": "lambda-wrap", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha --opts ./mocha.opts ./test/**/*.test.js", | ||
"test:watch": "npm test -- --watch", | ||
"test:coverage": "istanbul cover _mocha -- --opts ./mocha.opts ./test/**/*.test.js", | ||
"test:coverage:threshold": "node ./node_modules/.bin/istanbul check --branches 100 --functions 100 --statements 100 --lines 100 ./coverage/coverage.json", | ||
"test:lint": "eslint ./lib/**/*.js" | ||
"test": "npm run test:lint && npm run test:unit && npm run test:coverage && npm run test:coverage:threshold", | ||
"test:unit": "mocha ./test/**/*.test.js", | ||
"test:unit:watch": "npm run test:unit -- --watch", | ||
"test:coverage": "istanbul cover _mocha -- ./test/**/*.test.js", | ||
"test:coverage:threshold": "node ./node_modules/.bin/istanbul check", | ||
"test:lint": "eslint ./lib/**/*.js ./test/**/*.js" | ||
}, | ||
@@ -13,0 +14,0 @@ "author": "Pragonauts <lukas.hykl@pragonauts.com> (https://pragonauts.com)", |
@@ -13,3 +13,2 @@ 'use strict'; | ||
let fn; | ||
let doSyncStuff; | ||
let doAsyncStuff; | ||
@@ -37,5 +36,2 @@ | ||
// Mock synchronous function | ||
doSyncStuff = bool => bool; | ||
// Mock asynchronous function | ||
@@ -55,10 +51,40 @@ doAsyncStuff = (outcome, bool) => ( | ||
describe('new', () => { | ||
describe('constructor', () => { | ||
it('should return an instance of LambdaWrap', () => { | ||
assert.instanceOf(wrap, LambdaWrap); | ||
}); | ||
it('should be able to pass options to event object', (done) => { | ||
it('should be able to assign new attributes to options', (done) => { | ||
event = { | ||
headers: { 'Access-Control-Allow-Origin': '*' } | ||
}; | ||
wrap = new LambdaWrap({ | ||
someOption: 'value' | ||
}); | ||
wrap.logger = loggerMock; | ||
const handler = wrap(function* (e) { | ||
const { headers, someOption } = e; | ||
assert.equal(headers['Access-Control-Allow-Origin'], '*'); | ||
assert.equal(someOption, 'value'); | ||
done(); | ||
return { | ||
statusCode: 200, | ||
message: 'test' | ||
}; | ||
}); | ||
handler(event, context, () => {}); | ||
}); | ||
it('should be able to assign new values options\' attributes', (done) => { | ||
event = { | ||
headers: { 'Access-Control-Allow-Origin': '*' } | ||
}; | ||
wrap = new LambdaWrap({ | ||
headers: { 'X-Auth-Token': '1234' } | ||
@@ -70,5 +96,6 @@ }); | ||
const handler = wrap(function* (e) { | ||
const { headers } = e.options; | ||
const { headers } = e; | ||
assert.equal(headers['X-Auth-Token'], '1234'); | ||
assert.equal(headers['Access-Control-Allow-Origin'], '*'); | ||
done(); | ||
@@ -84,4 +111,58 @@ | ||
}); | ||
it('should set default response handler', () => { | ||
const { name } = wrap.responseHandler; | ||
assert.equal(name, 'response'); | ||
}); | ||
it('should set default error response handler', () => { | ||
const { name } = wrap.errorResponseHandler; | ||
assert.equal(name, 'errorResponse'); | ||
}); | ||
it('should initialize empty middleware handlers array', () => { | ||
const { _middlewareHandlers } = wrap; | ||
assert.deepEqual(_middlewareHandlers, []); | ||
}); | ||
it('should initialize empty catch handlers array', () => { | ||
const { _catchHandlers } = wrap; | ||
assert.deepEqual(_catchHandlers, []); | ||
}); | ||
it('should log set default logger'); | ||
}); | ||
describe('wrap', () => { | ||
xit('should return a function'); | ||
}); | ||
describe('responseHandler', () => { | ||
it('should allow to override default response handler', () => { | ||
const newResponseHandler = () => {}; | ||
wrap.responseHandler = newResponseHandler; | ||
assert.equal(wrap.responseHandler, newResponseHandler); | ||
}); | ||
}); | ||
describe('errorResponseHandler', () => { | ||
it('should allow to override default error response handler', () => { | ||
const newErrorResponseHandler = () => {}; | ||
wrap.errorResponseHandler = newErrorResponseHandler; | ||
assert.equal(wrap.errorResponseHandler, newErrorResponseHandler); | ||
}); | ||
}); | ||
describe('logger', () => { | ||
it('should allow to override default logger'); | ||
}); | ||
describe('before', () => { | ||
@@ -130,5 +211,5 @@ it('should be able to add synchronous middleware', (done) => { | ||
wrap.before((event, context) => { | ||
assert.equal(event.name, 'event'); | ||
assert.equal(context.name, 'context'); | ||
wrap.before((e, con) => { | ||
assert.equal(e.name, 'event'); | ||
assert.equal(con.name, 'context'); | ||
done(); | ||
@@ -143,11 +224,11 @@ }); | ||
it('should execute middlewares in order', (done) => { | ||
const callback = (ctx, data) => { | ||
const callback = () => { | ||
done(); | ||
}; | ||
wrap.before(function* (event) { | ||
wrap.before(function* (e) { | ||
const result = yield doAsyncStuff('resolve', true); | ||
if (result) { | ||
event.options.headers = { | ||
e.options.headers = { | ||
'X-Auth-Token': 'xyz123' | ||
@@ -158,5 +239,5 @@ }; | ||
wrap.before(function* (event) { | ||
const result = yield doAsyncStuff('resolve', true); | ||
assert.equal(event.options.headers['X-Auth-Token'], 'xyz123'); | ||
wrap.before(function* (e) { | ||
yield doAsyncStuff('resolve', true); | ||
assert.equal(e.options.headers['X-Auth-Token'], 'xyz123'); | ||
}); | ||
@@ -214,4 +295,4 @@ | ||
it('should have access to error, event and context objects', (done) => { | ||
const event = { name: 'event' }; | ||
const context = { name: 'context' }; | ||
event = { name: 'event' }; | ||
context = { name: 'context' }; | ||
@@ -222,6 +303,6 @@ fn = function* () { | ||
wrap.catch((err, event, context) => { | ||
wrap.catch((err, e, con) => { | ||
assert.equal(err.message, 'Some test error'); | ||
assert.equal(event.name, 'event'); | ||
assert.equal(context.name, 'context'); | ||
assert.equal(e.name, 'event'); | ||
assert.equal(con.name, 'context'); | ||
done(); | ||
@@ -243,3 +324,3 @@ }); | ||
wrap.catch(function* (err) { | ||
wrap.catch(function* () { | ||
const result = yield doAsyncStuff('resolve', false); | ||
@@ -252,3 +333,3 @@ | ||
wrap.catch(function* (err) { | ||
wrap.catch(function* () { | ||
const result = yield doAsyncStuff('resolve', false); | ||
@@ -277,3 +358,3 @@ | ||
wrap.catch(function* (err) { | ||
wrap.catch(function* () { | ||
return { statusCode: 200, data: 'Some returned data' }; | ||
@@ -280,0 +361,0 @@ }); |
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
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
17710
403