Created
February 26, 2019 02:02
-
-
Save danderson00/5b37bd7c01bd9c109d5ebe5a2f19645c to your computer and use it in GitHub Desktop.
Simple middleware execution pipeline
This file contains hidden or 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
module.exports = (api, middleware, context) => ( | |
middleware.reverse() | |
.reduce( | |
(next, current) => executor(current, next, context), | |
(...args) => Promise.resolve(api.handler.apply(api.hostObject, args)) | |
) | |
) | |
const executor = (middleware, next, context) => ( | |
(...args) => ( | |
Promise.resolve(middleware.handler({ | |
...context, | |
args, | |
next: (...suppliedArgs) => next.apply( | |
null, | |
suppliedArgs.length === 0 ? args : suppliedArgs | |
) | |
})) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment