2020-11-11 18:16:18 +08:00
|
|
|
|
import Resourcer, { Action } from '@nocobase/resourcer';
|
2020-11-11 15:23:39 +08:00
|
|
|
|
import actions from '@nocobase/actions';
|
2020-11-12 11:48:27 +08:00
|
|
|
|
import Application from './application';
|
2020-11-11 15:23:39 +08:00
|
|
|
|
import bodyParser from 'koa-bodyparser';
|
2020-11-11 18:16:18 +08:00
|
|
|
|
import cors from '@koa/cors';
|
|
|
|
|
import middleware from './middleware';
|
2020-11-11 15:23:39 +08:00
|
|
|
|
|
2020-11-12 11:48:27 +08:00
|
|
|
|
export * from './application';
|
|
|
|
|
export * from './middleware';
|
|
|
|
|
|
2020-11-11 15:23:39 +08:00
|
|
|
|
export default {
|
2020-11-12 11:48:27 +08:00
|
|
|
|
/**
|
|
|
|
|
* 这部分还比较杂,细节待改进
|
|
|
|
|
*
|
|
|
|
|
* @param options
|
|
|
|
|
*/
|
2020-11-11 15:23:39 +08:00
|
|
|
|
create(options: any): Application {
|
|
|
|
|
console.log(options);
|
|
|
|
|
|
2020-11-12 11:48:27 +08:00
|
|
|
|
const app = new Application(options);
|
2020-11-11 15:23:39 +08:00
|
|
|
|
|
|
|
|
|
app.use(bodyParser());
|
|
|
|
|
app.use(cors());
|
|
|
|
|
|
2020-11-12 11:48:27 +08:00
|
|
|
|
// 这段代码处理的不完整
|
2020-12-02 13:48:19 +08:00
|
|
|
|
app.resourcer.registerActionHandlers(actions.common);
|
2020-11-11 15:23:39 +08:00
|
|
|
|
|
|
|
|
|
app.use(async (ctx, next) => {
|
2020-11-12 11:48:27 +08:00
|
|
|
|
ctx.db = app.database;
|
|
|
|
|
ctx.database = app.database;
|
2020-11-11 15:23:39 +08:00
|
|
|
|
await next();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.use(async (ctx, next) => {
|
|
|
|
|
await next();
|
|
|
|
|
if (ctx.action instanceof Action) {
|
|
|
|
|
if (!ctx.body) {
|
|
|
|
|
ctx.body = {};
|
|
|
|
|
}
|
|
|
|
|
const { rows, ...meta } = ctx.body||{};
|
|
|
|
|
if (rows) {
|
|
|
|
|
ctx.body = {
|
|
|
|
|
data: rows,
|
|
|
|
|
meta,
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
ctx.body = {
|
|
|
|
|
data: ctx.body,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2020-11-11 18:16:18 +08:00
|
|
|
|
app.use(middleware({
|
2020-11-12 11:48:27 +08:00
|
|
|
|
database: app.database,
|
|
|
|
|
resourcer: app.resourcer,
|
2020-11-11 18:16:18 +08:00
|
|
|
|
...(options.resourcer||{}),
|
2020-11-11 15:23:39 +08:00
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
return app;
|
|
|
|
|
}
|
|
|
|
|
}
|