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