ibird-core
Version:
The core module of ibird.
27 lines (26 loc) • 903 B
JavaScript
const model = require('../model');
const utility = require('ibird-utils');
const hooks = require('../hooks');
const common = require('../common');
/**
* 默认列表查询路由
* @param name 模型名称
* @param [pre] 前置处理函数(数组)
* @param [post] 后置处理函数(数组)
*/
module.exports = (name, pre, post) => {
return async ctx => {
//定义通用返回结构
const response = common.response;
try {
const id = await model.id(name, ctx.params.id, async (query) => {
await hooks(pre, { ctx, query });
});
const result = Object.assign({}, response, { data: id });
await hooks(post, { ctx, data: result });
ctx.body = result;
} catch (e) {
ctx.body = Object.assign({}, response, { errmsg: utility.errors(e), errcode: '500' });
}
};
};