0.1.1 • Published 2 years ago

@nayotta/mta-fission-onion v0.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

mta-fission-onion

Build and LintNode.js Package

EN

fission/nodejs 内使用的,仿 koajs 风格洋葱式中间件处理模块.

安装

$ 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: 权限验证
	const authPass = true
	if (!authPass) {
		ctx.status = 401
		ctx.body = 'unauthrization'
		return
	}
	await next()
})

onion.use(async (ctx, next) => {
	// TODO: 处理数据
	// ctx.request即为fission/nodejs提供的express/request对象
	const data = 'hello' + ctx.request.body.name
	ctx.status = 200
	ctx.body = {
		data
	}
})

// 或者批量注入中间件函数
onion.inject([async (ctx, next) => {
	// do something
	await next()
}, () => {
	// do something
	await next()
}])

module.export = onion.go()