2021-04-01 23:51:00 +08:00
|
|
|
|
import { actions, middlewares } 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());
|
|
|
|
|
|
2021-04-01 23:51:00 +08:00
|
|
|
|
app.resourcer.registerActionHandlers({ ...actions.common, ...actions.associate });
|
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();
|
|
|
|
|
});
|
|
|
|
|
|
2021-04-01 23:51:00 +08:00
|
|
|
|
app.resourcer.use(middlewares.associated);
|
|
|
|
|
app.use(middlewares.dataWrapping);
|
2020-11-11 15:23:39 +08:00
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|