Last active
June 4, 2018 13:19
-
-
Save uday-rayala/25a21be72e116cb0c42f9cd6ef78d3f0 to your computer and use it in GitHub Desktop.
Middleware framework for AWS lambdas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const withMiddlewares = (handler, middlewares = []) => (event, context, callback) => { | |
const chainMiddlewares = ([firstMiddleware, ...restOfMiddlewares]) => { | |
if (firstMiddleware) { | |
return (e, c) => { | |
try { | |
return firstMiddleware(e, c, chainMiddlewares(restOfMiddlewares)) | |
} catch (error) { | |
return Promise.reject(error) | |
} | |
} | |
} | |
return handler | |
} | |
chainMiddlewares(middlewares)(event, context) | |
.then(result => callback(null, result)) | |
.catch((err) => { | |
callback(err, null) | |
}) | |
} | |
module.exports = withMiddlewares |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment