2021-04-07 09:10:52 +00:00
|
|
|
|
import { actions, middlewares as m } from '@nocobase/actions';
|
2020-11-12 03:48:27 +00:00
|
|
|
|
import Application from './application';
|
2020-11-11 07:23:39 +00:00
|
|
|
|
import bodyParser from 'koa-bodyparser';
|
2020-11-11 10:16:18 +00:00
|
|
|
|
import cors from '@koa/cors';
|
2021-04-07 09:10:52 +00:00
|
|
|
|
import { dbResourceRouter } from './middlewares';
|
2020-11-11 07:23:39 +00:00
|
|
|
|
|
2020-11-12 03:48:27 +00:00
|
|
|
|
export * from './application';
|
|
|
|
|
export * from './middleware';
|
2021-04-07 09:10:52 +00:00
|
|
|
|
export * as middlewares from './middlewares';
|
2020-11-12 03:48:27 +00:00
|
|
|
|
|
2020-11-11 07:23:39 +00:00
|
|
|
|
export default {
|
2020-11-12 03:48:27 +00:00
|
|
|
|
/**
|
|
|
|
|
* 这部分还比较杂,细节待改进
|
|
|
|
|
*
|
|
|
|
|
* @param options
|
|
|
|
|
*/
|
2020-11-11 07:23:39 +00:00
|
|
|
|
create(options: any): Application {
|
|
|
|
|
console.log(options);
|
|
|
|
|
|
2020-11-12 03:48:27 +00:00
|
|
|
|
const app = new Application(options);
|
2020-11-11 07:23:39 +00:00
|
|
|
|
|
|
|
|
|
app.use(bodyParser());
|
2021-08-13 01:57:21 +00:00
|
|
|
|
app.use(cors({
|
|
|
|
|
exposeHeaders: ['content-disposition'],
|
|
|
|
|
}));
|
2020-11-11 07:23:39 +00:00
|
|
|
|
|
2021-04-01 15:51:00 +00:00
|
|
|
|
app.resourcer.registerActionHandlers({ ...actions.common, ...actions.associate });
|
2020-11-11 07:23:39 +00:00
|
|
|
|
|
|
|
|
|
app.use(async (ctx, next) => {
|
2020-11-12 03:48:27 +00:00
|
|
|
|
ctx.db = app.database;
|
|
|
|
|
ctx.database = app.database;
|
2020-11-11 07:23:39 +00:00
|
|
|
|
await next();
|
|
|
|
|
});
|
|
|
|
|
|
2021-04-07 09:10:52 +00:00
|
|
|
|
app.resourcer.use(m.associated);
|
|
|
|
|
app.use(m.dataWrapping);
|
2020-11-11 07:23:39 +00:00
|
|
|
|
|
2021-04-07 09:10:52 +00:00
|
|
|
|
app.use(dbResourceRouter({
|
2020-11-12 03:48:27 +00:00
|
|
|
|
database: app.database,
|
|
|
|
|
resourcer: app.resourcer,
|
2020-11-11 10:16:18 +00:00
|
|
|
|
...(options.resourcer||{}),
|
2020-11-11 07:23:39 +00:00
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
return app;
|
|
|
|
|
}
|
|
|
|
|
}
|