2021-10-18 12:49:37 +08:00
|
|
|
import cors from '@koa/cors';
|
|
|
|
import Database from '@nocobase/database';
|
2023-09-03 10:59:33 +08:00
|
|
|
import { Resourcer } from '@nocobase/resourcer';
|
2023-09-12 22:39:23 +08:00
|
|
|
import { uid } from '@nocobase/utils';
|
2023-08-24 17:47:45 +08:00
|
|
|
import { Command } from 'commander';
|
2023-09-12 22:39:23 +08:00
|
|
|
import fs from 'fs';
|
2022-02-07 01:14:00 +08:00
|
|
|
import i18next from 'i18next';
|
|
|
|
import bodyParser from 'koa-bodyparser';
|
2023-09-12 22:39:23 +08:00
|
|
|
import { resolve } from 'path';
|
2021-10-18 12:49:37 +08:00
|
|
|
import Application, { ApplicationOptions } from './application';
|
2023-03-30 23:49:57 +08:00
|
|
|
import { parseVariables } from './middlewares';
|
2023-04-16 14:22:46 +08:00
|
|
|
import { dateTemplate } from './middlewares/data-template';
|
2021-10-18 12:49:37 +08:00
|
|
|
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: {},
|
2023-02-07 11:57:40 +08:00
|
|
|
keySeparator: false,
|
|
|
|
nsSeparator: false,
|
2022-03-06 12:07:56 +08:00
|
|
|
...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) {
|
2023-09-04 22:21:31 +08:00
|
|
|
const bodyLimit = '10mb';
|
2021-10-18 12:49:37 +08:00
|
|
|
app.use(
|
|
|
|
bodyParser({
|
2023-09-04 22:21:31 +08:00
|
|
|
jsonLimit: bodyLimit,
|
|
|
|
formLimit: bodyLimit,
|
|
|
|
textLimit: bodyLimit,
|
2021-10-18 12:49:37 +08:00
|
|
|
...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
|
|
|
}
|
|
|
|
|
2023-03-30 23:49:57 +08:00
|
|
|
app.resourcer.use(parseVariables, { tag: 'parseVariables', after: 'acl' });
|
2023-04-16 14:22:46 +08:00
|
|
|
app.resourcer.use(dateTemplate, { tag: 'dateTemplate', after: 'acl' });
|
2023-03-30 23:49:57 +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
|
|
|
}
|
2023-08-24 17:47:45 +08:00
|
|
|
|
|
|
|
export const createAppProxy = (app: Application) => {
|
|
|
|
return new Proxy(app, {
|
|
|
|
get(target, prop, ...args) {
|
|
|
|
if (typeof prop === 'string' && ['on', 'once', 'addListener'].includes(prop)) {
|
|
|
|
return (eventName: string, listener: any) => {
|
|
|
|
listener['_reinitializable'] = true;
|
|
|
|
return target[prop](eventName, listener);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return Reflect.get(target, prop, ...args);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getCommandFullName = (command: Command) => {
|
|
|
|
const names = [];
|
|
|
|
names.push(command.name());
|
|
|
|
let parent = command?.parent;
|
|
|
|
while (parent) {
|
|
|
|
if (!parent?.parent) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
names.unshift(parent.name());
|
|
|
|
parent = parent.parent;
|
|
|
|
}
|
|
|
|
return names.join('.');
|
|
|
|
};
|
2023-09-12 22:39:23 +08:00
|
|
|
|
|
|
|
export const tsxRerunning = async () => {
|
|
|
|
const file = resolve(process.cwd(), 'storage/app.watch.ts');
|
|
|
|
await fs.promises.writeFile(file, `export const watchId = '${uid()}';`, 'utf-8');
|
|
|
|
};
|