Skip to content

Instantly share code, notes, and snippets.

@jkutner
Last active June 11, 2019 22:54
Show Gist options
  • Save jkutner/8840c1745bb8d422b02bf9dc4f70689f to your computer and use it in GitHub Desktop.
Save jkutner/8840c1745bb8d422b02bf9dc4f70689f to your computer and use it in GitHub Desktop.
// In these examples
// transform args
module.exports = async (req, res, chain) => {
let ce = create_cloudevent(req, res)
chain.finalize(ce) // ends the chain, and calls the user function with these args
}
// or don't transform args
module.exports = async (req, res, chain) => {
report_metrics(req, res)
chain.next() // calls the next
}
// append args
module.exports = async (req, res, chain) => {
let context = create_context(req, res)
? // adds the context to the args
}
// In this example we have different types of middleware
// they explicitly do different types of things
// transform the function arguments before invoking
// also trap errors if you'd like
// question: what if more than one middleware wants to transform?
module.exports.$finalize = async (req, res, func) => {
let ce = create_cloudevent(req, res)
func(ce)
}
// or
module.exports.$finalize = async (req, res, func) => {
let c = create_context(req, res)
func.appendArg(c)
}
// like a preprocessor. can't change the args but can have side-effects
// this might log something, or report metrics
module.exports.$filter = async (req, res) => {
// report some metrics
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment