Used in
fission/node-env
, likekoajs
onion code style middleware module.
$ npm install @nayotta/mta-fission-onion
// fission function js file
const { Onion } = require('@nayotta/mta-fission-onion')
const onion = new Onion()
onion.use(async (ctx, next) => {
// TODO: authrization
const authPass = true
if (!authPass) {
ctx.status = 401
ctx.body = 'unauthrization'
return
}
await next()
})
onion.use(async (ctx, next) => {
// TODO: deal with data
// ctx.context -> fission/nodejs express/request object
const data = 'hello' + ctx.request.body.name
ctx.status = 200
ctx.body = {
data
}
})
// or inject middlewares funcs
onion.inject([async (ctx, next) => {
// do something
await next()
}, () => {
// do something
await next()
}])
module.export = onion.go()