Skip to content

Instantly share code, notes, and snippets.

@danderson00
Created February 26, 2019 02:02
Show Gist options
  • Save danderson00/5b37bd7c01bd9c109d5ebe5a2f19645c to your computer and use it in GitHub Desktop.
Save danderson00/5b37bd7c01bd9c109d5ebe5a2f19645c to your computer and use it in GitHub Desktop.
Simple middleware execution pipeline
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