nocobase/packages/core/server/src/helper.ts

81 lines
2.2 KiB
TypeScript
Raw Normal View History

import cors from '@koa/cors';
import Database from '@nocobase/database';
import Resourcer from '@nocobase/resourcer';
import i18next from 'i18next';
import bodyParser from 'koa-bodyparser';
import Application, { ApplicationOptions } from './application';
feat(parse-variables): support to parse variables in filter params (#1558) * fix: add field linkage on setting default datetime * fix: fix dateonly timezone problem * fix: improve test * docs(DatePicker): add demos * fix(DatePicker): should return the beginning of a second * feat(DatePicker): support non-UTC * refactor: rename * fix(RangePicker): get correct end date * test(mapDatePicker): add test * test(mapRangePicker): add test * feat(Filter): use non-UTC to filter * feat(FilterBlock): use non-UTC to filter * feat: add '$dateBetween' operator in datetime * feat: use RangePicker on toggled to 'dateBetween' operator * feat: set ranges for RangePicker * feat: backend support to parse 'dateBetween' operator * fix: fix build error * fix: adaptive content width * feat: support to use var on data scope * feat: add parse-variables plugin * feat: support to parse variables * feat: support only to set system variables * test: rename * feat: cover all * fix: fix build error * feat(RangePicker): extend more shortcut keys * feat(parse-variables): support more date var * feat: support user variables * feat: disable unmatched options * fix: use component name to filter option * fix: fix build error * feat: remove some operator of id * chore: remove useless operators * fix: built in plugin * refactor: move to core from plugin * refactor: remove code of plugin * refactor: remove useless code * fix: should after acl * Update server.ts * fix: compatible with old version * feat: test cases * refactor: rename to 'is between' * refactor: parse filter * fix: improve code * feat: test cases * fix: fix error * fix: improve parse date * fix: date variables * fix: day range * fix: test error * fix: typo * fix: test error * feat: $user variable * fix: toDate * fix: fix the value range of shortcuts * feat: add quarter and test * feat: support to use user's association fields to filter * refactor: use maxDepth * refactor: remove useless code * fix: make AssociationSelect.Designer to support variables * fix: getField * fix: parse utc * fix: remove only * fix: filter by ctx.db.getFieldByPath * fix: avoid error * fix: add translation * fix(RangePicker): can be set to empty * feat(utils): add hasEmptyValue * fix: should not save empty * fix: last few days should include today * fix: limit user variable type to display * fix: parse filter error * fix: empty * test: [skip ci] * fix: remove ';' * feat: improve code --------- Co-authored-by: chenos <chenlinxh@gmail.com>
2023-03-30 15:49:57 +00:00
import { parseVariables } from './middlewares';
import { dateTemplate } from './middlewares/data-template';
import { dataWrapping } from './middlewares/data-wrapping';
import { db2resource } from './middlewares/db2resource';
import { i18n } from './middlewares/i18n';
export function createI18n(options: ApplicationOptions) {
const instance = i18next.createInstance();
instance.init({
lng: 'en-US',
resources: {},
keySeparator: false,
nsSeparator: false,
...options.i18n,
});
return instance;
}
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) {
app.use(
cors({
exposeHeaders: ['content-disposition'],
...options.cors,
}),
{
tag: 'cors',
after: 'bodyParser',
},
);
if (options.bodyParser !== false) {
app.use(
bodyParser({
...options.bodyParser,
}),
{
tag: 'bodyParser',
after: 'logger',
},
);
}
app.use(async (ctx, next) => {
2022-04-09 07:30:43 +00:00
ctx.getBearerToken = () => {
const token = ctx.get('Authorization').replace(/^Bearer\s+/gi, '');
return token || ctx.query.token;
2022-04-09 07:30:43 +00:00
};
await next();
});
app.use(i18n, { tag: 'i18n', after: 'cors' });
if (options.dataWrapping !== false) {
app.use(dataWrapping(), { tag: 'dataWrapping', after: 'i18n' });
}
feat(parse-variables): support to parse variables in filter params (#1558) * fix: add field linkage on setting default datetime * fix: fix dateonly timezone problem * fix: improve test * docs(DatePicker): add demos * fix(DatePicker): should return the beginning of a second * feat(DatePicker): support non-UTC * refactor: rename * fix(RangePicker): get correct end date * test(mapDatePicker): add test * test(mapRangePicker): add test * feat(Filter): use non-UTC to filter * feat(FilterBlock): use non-UTC to filter * feat: add '$dateBetween' operator in datetime * feat: use RangePicker on toggled to 'dateBetween' operator * feat: set ranges for RangePicker * feat: backend support to parse 'dateBetween' operator * fix: fix build error * fix: adaptive content width * feat: support to use var on data scope * feat: add parse-variables plugin * feat: support to parse variables * feat: support only to set system variables * test: rename * feat: cover all * fix: fix build error * feat(RangePicker): extend more shortcut keys * feat(parse-variables): support more date var * feat: support user variables * feat: disable unmatched options * fix: use component name to filter option * fix: fix build error * feat: remove some operator of id * chore: remove useless operators * fix: built in plugin * refactor: move to core from plugin * refactor: remove code of plugin * refactor: remove useless code * fix: should after acl * Update server.ts * fix: compatible with old version * feat: test cases * refactor: rename to 'is between' * refactor: parse filter * fix: improve code * feat: test cases * fix: fix error * fix: improve parse date * fix: date variables * fix: day range * fix: test error * fix: typo * fix: test error * feat: $user variable * fix: toDate * fix: fix the value range of shortcuts * feat: add quarter and test * feat: support to use user's association fields to filter * refactor: use maxDepth * refactor: remove useless code * fix: make AssociationSelect.Designer to support variables * fix: getField * fix: parse utc * fix: remove only * fix: filter by ctx.db.getFieldByPath * fix: avoid error * fix: add translation * fix(RangePicker): can be set to empty * feat(utils): add hasEmptyValue * fix: should not save empty * fix: last few days should include today * fix: limit user variable type to display * fix: parse filter error * fix: empty * test: [skip ci] * fix: remove ';' * feat: improve code --------- Co-authored-by: chenos <chenlinxh@gmail.com>
2023-03-30 15:49:57 +00:00
app.resourcer.use(parseVariables, { tag: 'parseVariables', after: 'acl' });
app.resourcer.use(dateTemplate, { tag: 'dateTemplate', after: 'acl' });
feat(parse-variables): support to parse variables in filter params (#1558) * fix: add field linkage on setting default datetime * fix: fix dateonly timezone problem * fix: improve test * docs(DatePicker): add demos * fix(DatePicker): should return the beginning of a second * feat(DatePicker): support non-UTC * refactor: rename * fix(RangePicker): get correct end date * test(mapDatePicker): add test * test(mapRangePicker): add test * feat(Filter): use non-UTC to filter * feat(FilterBlock): use non-UTC to filter * feat: add '$dateBetween' operator in datetime * feat: use RangePicker on toggled to 'dateBetween' operator * feat: set ranges for RangePicker * feat: backend support to parse 'dateBetween' operator * fix: fix build error * fix: adaptive content width * feat: support to use var on data scope * feat: add parse-variables plugin * feat: support to parse variables * feat: support only to set system variables * test: rename * feat: cover all * fix: fix build error * feat(RangePicker): extend more shortcut keys * feat(parse-variables): support more date var * feat: support user variables * feat: disable unmatched options * fix: use component name to filter option * fix: fix build error * feat: remove some operator of id * chore: remove useless operators * fix: built in plugin * refactor: move to core from plugin * refactor: remove code of plugin * refactor: remove useless code * fix: should after acl * Update server.ts * fix: compatible with old version * feat: test cases * refactor: rename to 'is between' * refactor: parse filter * fix: improve code * feat: test cases * fix: fix error * fix: improve parse date * fix: date variables * fix: day range * fix: test error * fix: typo * fix: test error * feat: $user variable * fix: toDate * fix: fix the value range of shortcuts * feat: add quarter and test * feat: support to use user's association fields to filter * refactor: use maxDepth * refactor: remove useless code * fix: make AssociationSelect.Designer to support variables * fix: getField * fix: parse utc * fix: remove only * fix: filter by ctx.db.getFieldByPath * fix: avoid error * fix: add translation * fix(RangePicker): can be set to empty * feat(utils): add hasEmptyValue * fix: should not save empty * fix: last few days should include today * fix: limit user variable type to display * fix: parse filter error * fix: empty * test: [skip ci] * fix: remove ';' * feat: improve code --------- Co-authored-by: chenos <chenlinxh@gmail.com>
2023-03-30 15:49:57 +00:00
app.use(db2resource, { tag: 'db2resource', after: 'dataWrapping' });
app.use(app.resourcer.restApiMiddleware(), { tag: 'restApi', after: 'db2resource' });
}