2021-10-18 12:49:37 +08:00
|
|
|
import cors from '@koa/cors';
|
|
|
|
import Database from '@nocobase/database';
|
|
|
|
import Resourcer from '@nocobase/resourcer';
|
2022-02-07 01:14:00 +08:00
|
|
|
import i18next from 'i18next';
|
|
|
|
import bodyParser from 'koa-bodyparser';
|
2021-10-18 12:49:37 +08:00
|
|
|
import Application, { ApplicationOptions } from './application';
|
|
|
|
import { dataWrapping } from './middlewares/data-wrapping';
|
2022-09-29 21:05:31 +08:00
|
|
|
import { db2resource } from './middlewares/db2resource';
|
2022-09-23 09:22:17 +08:00
|
|
|
import { i18n } from './middlewares/i18n';
|
2021-10-18 12:49:37 +08:00
|
|
|
|
2022-03-06 12:07:56 +08:00
|
|
|
export function createI18n(options: ApplicationOptions) {
|
|
|
|
const instance = i18next.createInstance();
|
|
|
|
instance.init({
|
|
|
|
lng: 'en-US',
|
|
|
|
resources: {},
|
|
|
|
...options.i18n,
|
|
|
|
});
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2021-10-18 12:49:37 +08:00
|
|
|
export function createDatabase(options: ApplicationOptions) {
|
|
|
|
if (options.database instanceof Database) {
|
|
|
|
return options.database;
|
|
|
|
} else {
|
|
|
|
return new Database(options.database);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createResourcer(options: ApplicationOptions) {
|
|
|
|
return new Resourcer({ ...options.resourcer });
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
export function registerMiddlewares(app: Application, options: ApplicationOptions) {
|
2022-09-29 21:05:31 +08:00
|
|
|
app.use(
|
|
|
|
cors({
|
|
|
|
exposeHeaders: ['content-disposition'],
|
|
|
|
...options.cors,
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
tag: 'cors',
|
|
|
|
after: 'bodyParser',
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-10-18 12:49:37 +08:00
|
|
|
if (options.bodyParser !== false) {
|
|
|
|
app.use(
|
|
|
|
bodyParser({
|
|
|
|
...options.bodyParser,
|
|
|
|
}),
|
2022-09-29 21:05:31 +08:00
|
|
|
{
|
|
|
|
tag: 'bodyParser',
|
2022-11-12 17:12:50 +08:00
|
|
|
after: 'logger',
|
2022-09-29 21:05:31 +08:00
|
|
|
},
|
2021-10-18 12:49:37 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-23 09:22:17 +08:00
|
|
|
app.use(async (ctx, next) => {
|
2022-04-09 15:30:43 +08:00
|
|
|
ctx.getBearerToken = () => {
|
2022-12-24 16:29:20 +08:00
|
|
|
const token = ctx.get('Authorization').replace(/^Bearer\s+/gi, '');
|
|
|
|
return token || ctx.query.token;
|
2022-04-09 15:30:43 +08:00
|
|
|
};
|
2021-10-18 12:49:37 +08:00
|
|
|
await next();
|
|
|
|
});
|
|
|
|
|
2022-09-29 21:05:31 +08:00
|
|
|
app.use(i18n, { tag: 'i18n', after: 'cors' });
|
2022-09-23 09:22:17 +08:00
|
|
|
|
2021-10-18 12:49:37 +08:00
|
|
|
if (options.dataWrapping !== false) {
|
2022-09-29 21:05:31 +08:00
|
|
|
app.use(dataWrapping(), { tag: 'dataWrapping', after: 'i18n' });
|
2021-10-18 12:49:37 +08:00
|
|
|
}
|
|
|
|
|
2022-09-29 21:05:31 +08:00
|
|
|
app.use(db2resource, { tag: 'db2resource', after: 'dataWrapping' });
|
|
|
|
app.use(app.resourcer.restApiMiddleware(), { tag: 'restApi', after: 'db2resource' });
|
2021-10-18 12:49:37 +08:00
|
|
|
}
|