@middy/core
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -147,9 +147,11 @@ const middy = require('../') | ||
const executedBefore = [] | ||
const executedAfter = [] | ||
const m1 = () => ({ | ||
before: (handler, next) => { | ||
handler.executedBefore = ['m1'] | ||
executedBefore.push('m1') | ||
next() | ||
}, | ||
after: (handler, next) => { | ||
handler.executedAfter.push('m1') | ||
executedAfter.push('m1') | ||
next() | ||
@@ -161,7 +163,7 @@ } | ||
before: (handler, next) => { | ||
handler.executedBefore.push('m2') | ||
executedBefore.push('m2') | ||
next() | ||
}, | ||
after: (handler, next) => { | ||
handler.executedAfter = ['m2'] | ||
executedAfter.push('m2') | ||
next() | ||
@@ -177,4 +179,4 @@ } | ||
handler({}, {}, (_, response) => { | ||
expect(handler.executedBefore).toEqual(['m1', 'm2']) | ||
expect(handler.executedAfter).toEqual(['m2', 'm1']) | ||
expect(executedBefore).toEqual(['m1', 'm2']) | ||
expect(executedAfter).toEqual(['m2', 'm1']) | ||
expect(response).toEqual({ foo: 'bar' }) | ||
@@ -186,3 +188,5 @@ endTest() | ||
test('"before" middlewares should be able to change event', (endTest) => { | ||
let handlerEvent | ||
const handler = middy((event, context, callback) => { | ||
handlerEvent = event | ||
return callback(null, { foo: 'bar' }) | ||
@@ -199,3 +203,3 @@ }) | ||
handler({}, {}, () => { | ||
expect(handler.event.modified).toBe(true) | ||
expect(handlerEvent.modified).toBe(true) | ||
endTest() | ||
@@ -205,16 +209,19 @@ }) | ||
test('"after" middlewares should be able to change response', (endTest) => { | ||
test('"before" middleware should be able to modify context', (endTest) => { | ||
const handler = middy((event, context, callback) => { | ||
expect(context.modified).toBeTruthy() | ||
return callback(null, { foo: 'bar' }) | ||
}) | ||
const changeResponseMiddleware = (handler, next) => { | ||
handler.response.modified = true | ||
const getLambdaContext = (handler, next) => { | ||
handler.context = { | ||
...handler.context, | ||
modified: true | ||
} | ||
next() | ||
} | ||
handler.after(changeResponseMiddleware) | ||
handler.before(getLambdaContext) | ||
handler({}, {}, () => { | ||
expect(handler.response.modified).toBe(true) | ||
endTest() | ||
@@ -224,5 +231,3 @@ }) | ||
test('"before" middleware should be able to access context', (endTest) => { | ||
const context = {} | ||
test('"after" middlewares should be able to change response', (endTest) => { | ||
const handler = middy((event, context, callback) => { | ||
@@ -232,10 +237,12 @@ return callback(null, { foo: 'bar' }) | ||
const getLambdaContext = (handler, next) => { | ||
expect(handler.context).toEqual(context) | ||
const changeResponseMiddleware = (handler, next) => { | ||
handler.response.modified = true | ||
next() | ||
} | ||
handler.before(getLambdaContext) | ||
handler.after(changeResponseMiddleware) | ||
handler({}, context, () => { | ||
handler({}, {}, (err, response) => { | ||
expect(err).toBeNull() | ||
expect(response.modified).toBe(true) | ||
endTest() | ||
@@ -245,5 +252,6 @@ }) | ||
test('"before" middleware should be able to modify context', (endTest) => { | ||
test('"before" middleware should be able to access context', (endTest) => { | ||
const context = {} | ||
const handler = middy((event, context, callback) => { | ||
expect(context.modified).toBeTruthy() | ||
return callback(null, { foo: 'bar' }) | ||
@@ -253,3 +261,3 @@ }) | ||
const getLambdaContext = (handler, next) => { | ||
handler.context.modified = true | ||
expect(handler.context).toEqual(context) | ||
next() | ||
@@ -260,3 +268,3 @@ } | ||
handler({}, {}, () => { | ||
handler({}, context, () => { | ||
endTest() | ||
@@ -618,10 +626,5 @@ }) | ||
test('It should handle async middlewares', (endTest) => { | ||
const asyncBefore = async (handler) => { | ||
handler.event.asyncBefore = true | ||
} | ||
const asyncBefore = jest.fn(async () => {}) | ||
const asyncAfter = jest.fn(async () => {}) | ||
const asyncAfter = async (handler) => { | ||
handler.event.asyncAfter = true | ||
} | ||
const handler = middy((event, context, callback) => { | ||
@@ -637,4 +640,4 @@ return callback(null, { some: 'response' }) | ||
expect(err).toBeNull() | ||
expect(handler.event.asyncBefore).toBeTruthy() | ||
expect(handler.event.asyncAfter).toBeTruthy() | ||
expect(asyncBefore).toHaveBeenCalled() | ||
expect(asyncAfter).toHaveBeenCalled() | ||
endTest() | ||
@@ -641,0 +644,0 @@ }) |
45
index.js
@@ -67,3 +67,3 @@ const isPromise = require('./isPromise') | ||
const runMiddlewares = (middlewares, instance, done) => { | ||
const runMiddlewares = (middlewares, request, done) => { | ||
const stack = Array.from(middlewares) | ||
@@ -79,3 +79,3 @@ const runNext = (err) => { | ||
if (nextMiddleware) { | ||
const retVal = nextMiddleware(instance, runNext) | ||
const retVal = nextMiddleware(request, runNext) | ||
@@ -104,9 +104,9 @@ if (retVal) { | ||
const runErrorMiddlewares = (middlewares, instance, done) => { | ||
const runErrorMiddlewares = (middlewares, request, done) => { | ||
const stack = Array.from(middlewares) | ||
instance.__handledError = false | ||
request.__handledError = false | ||
const runNext = (err) => { | ||
try { | ||
if (!err) { | ||
instance.__handledError = true | ||
request.__handledError = true | ||
} | ||
@@ -117,3 +117,3 @@ | ||
if (nextMiddleware) { | ||
const retVal = nextMiddleware(instance, runNext) | ||
const retVal = nextMiddleware(request, runNext) | ||
@@ -136,3 +136,3 @@ if (retVal) { | ||
return done(instance.__handledError ? null : err) | ||
return done(request.__handledError ? null : err) | ||
} catch (err) { | ||
@@ -143,3 +143,3 @@ return done(err) | ||
runNext(instance.error) | ||
runNext(request.error) | ||
} | ||
@@ -158,7 +158,8 @@ | ||
const instance = (event, context, callback) => { | ||
instance.event = event | ||
instance.context = context | ||
instance.callback = callback | ||
instance.response = null | ||
instance.error = null | ||
const request = {} | ||
request.event = event | ||
request.context = context | ||
request.callback = callback | ||
request.response = null | ||
request.error = null | ||
@@ -171,15 +172,15 @@ const middyPromise = new Promise((resolve, reject) => { | ||
return callback ? callback(null, instance.response) : resolve(instance.response) | ||
return callback ? callback(null, request.response) : resolve(request.response) | ||
} | ||
const errorHandler = err => { | ||
instance.error = err | ||
return runErrorMiddlewares(errorMiddlewares, instance, terminate) | ||
request.error = err | ||
return runErrorMiddlewares(errorMiddlewares, request, terminate) | ||
} | ||
runMiddlewares(beforeMiddlewares, instance, (err) => { | ||
runMiddlewares(beforeMiddlewares, request, (err) => { | ||
if (err) return errorHandler(err) | ||
const onHandlerError = once((err) => { | ||
instance.response = null | ||
request.response = null | ||
errorHandler(err) | ||
@@ -189,4 +190,4 @@ }) | ||
const onHandlerSuccess = once((response) => { | ||
instance.response = response | ||
runMiddlewares(afterMiddlewares, instance, (err) => { | ||
request.response = response | ||
runMiddlewares(afterMiddlewares, request, (err) => { | ||
if (err) return errorHandler(err) | ||
@@ -198,3 +199,3 @@ | ||
const handlerReturnValue = handler.call(instance, instance.event, context, (err, response) => { | ||
const handlerReturnValue = handler.call(request, request.event, request.context, (err, response) => { | ||
if (err) return onHandlerError(err) | ||
@@ -216,3 +217,3 @@ onHandlerSuccess(response) | ||
}) | ||
if (!instance.callback) return middyPromise | ||
if (!request.callback) return middyPromise | ||
} | ||
@@ -219,0 +220,0 @@ |
{ | ||
"name": "@middy/core", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "🛵 The stylish Node.js middleware engine for AWS Lambda (core package)", | ||
@@ -48,3 +48,3 @@ "engines": { | ||
}, | ||
"gitHead": "250f39d83b26bb1b29f5786a3e3e764e236f1cd2" | ||
"gitHead": "920be48510d707e9ae017d90487923e14082f85f" | ||
} |
38354
960