2020-11-11 15:23:39 +08:00
|
|
|
import Database from '@nocobase/database';
|
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-11 18:16:18 +08:00
|
|
|
import Application from './applicatiion';
|
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
|
|
|
|
|
|
|
export default {
|
|
|
|
create(options: any): Application {
|
|
|
|
console.log(options);
|
|
|
|
|
|
|
|
const app = new Application();
|
|
|
|
const database = new Database(options.database);
|
|
|
|
const resourcer = new Resourcer();
|
|
|
|
|
|
|
|
app.database = database;
|
|
|
|
app.resourcer = resourcer;
|
|
|
|
|
|
|
|
app.use(bodyParser());
|
|
|
|
app.use(cors());
|
|
|
|
|
|
|
|
resourcer.registerHandlers(actions.common);
|
|
|
|
|
|
|
|
app.use(async (ctx, next) => {
|
|
|
|
ctx.db = database;
|
|
|
|
ctx.database = database;
|
|
|
|
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-11 15:23:39 +08:00
|
|
|
prefix: '/api',
|
2020-11-11 18:16:18 +08:00
|
|
|
database,
|
|
|
|
resourcer,
|
|
|
|
...(options.resourcer||{}),
|
2020-11-11 15:23:39 +08:00
|
|
|
}));
|
|
|
|
|
|
|
|
return app;
|
|
|
|
}
|
|
|
|
}
|