tachybase_todo/packages/app/src/api/index.ts

84 lines
2.7 KiB
TypeScript
Raw Normal View History

import api from './app';
import actions from '../../../actions/src';
2020-12-18 15:33:34 +08:00
2020-12-15 09:58:42 +08:00
api.resourcer.use(async (ctx: actions.Context, next) => {
const { actionName, resourceField, resourceName, fields = {} } = ctx.action.params;
2020-12-25 16:15:58 +08:00
const table = ctx.db.getTable(resourceField ? resourceField.options.target : resourceName);
2020-12-12 16:36:02 +08:00
// ctx.state.developerMode = {[Op.not]: null};
2020-12-08 14:33:28 +08:00
ctx.state.developerMode = false;
2020-12-12 16:36:02 +08:00
if (table && table.hasField('developerMode') && ctx.state.developerMode === false) {
ctx.action.mergeParams({ filter: { "developerMode.$isFalsy": true } }, { filter: 'and' });
2020-12-08 10:02:41 +08:00
}
if (table && ['get', 'list'].includes(actionName)) {
const except = [];
const appends = [];
2020-12-15 09:58:42 +08:00
for (const [name, field] of table.getFields()) {
if (field.options.hidden) {
except.push(field.options.name);
}
2021-01-07 09:31:26 +08:00
// if (field.options.appends) {
// appends.push(field.options.name);
// }
2020-12-15 09:58:42 +08:00
}
ctx.action.mergeParams({ fields: {
except,
appends
} }, { fields: 'append' });
2021-01-04 20:53:30 +08:00
// console.log('ctx.action.params.fields', ctx.action.params.fields, except, appends);
2020-12-15 09:58:42 +08:00
}
2020-12-08 10:02:41 +08:00
await next();
});
2020-12-15 14:32:56 +08:00
2020-12-31 21:04:03 +08:00
// api.resourcer.use(async (ctx: actions.Context, next) => {
// const { resourceField, resourceName, viewName, filter } = ctx.action.params;
// // TODO: 需要补充默认视图的情况
// let view: any;
// if (viewName) {
// view = await ctx.db.getModel('views').findOne({
// where: {
// collection_name: resourceField ? resourceField.options.target : resourceName,
// name: viewName,
// }
// });
// const viewFilter = view.get('filter');
// if (viewFilter) {
// const args = [viewFilter, filter].filter(Boolean);
// ctx.action.setParam('filter', {and: args});
// console.log(ctx.action.params.filter);
// }
// }
// await next();
// });
2020-12-18 19:54:53 +08:00
(async () => {
await api.loadPlugins();
2021-01-07 09:31:26 +08:00
api.database.getModel('users').addHook('beforeUpdate', function (model) {
console.log('users.beforeUpdate', model.get(), model.changed('password' as any));
});
api.resourcer.use(async (ctx, next) => {
if (process.env.NOCOBASE_ENV !== 'demo') {
return next();
}
const currentUser = ctx.state.currentUser;
if (currentUser && currentUser.username === 'admin') {
return next();
}
const { actionName } = ctx.action.params;
if (['create', 'update', 'destroy'].includes(actionName)) {
ctx.body = {
data: {},
meta: {},
};
return;
}
await next();
});
2020-12-31 21:04:03 +08:00
await api.database.getModel('collections').load({skipExisting: true});
api.listen(process.env.HTTP_PORT, () => {
console.log(`http://localhost:${process.env.HTTP_PORT}/`);
});
})();