refactor: change registerHandler to registerActionHandler
This commit is contained in:
parent
970f20016d
commit
868dd1aaaa
@ -52,7 +52,7 @@ const tableFiles = glob.sync(`${resolve(__dirname, './tables')}/*.ts`);
|
||||
// resourcer 在内存中是单例,需要谨慎使用
|
||||
export const resourcer = new Resourcer();
|
||||
resourcer.use(associated);
|
||||
resourcer.registerHandlers({...actions.associate, ...actions.common});
|
||||
resourcer.registerActionHandlers({...actions.associate, ...actions.common});
|
||||
resourcer.define({
|
||||
name: 'posts',
|
||||
actions: actions.common,
|
||||
|
@ -36,7 +36,7 @@ const api = Api.create({
|
||||
});
|
||||
|
||||
api.resourcer.use(associated);
|
||||
api.resourcer.registerHandlers({...actions.common, ...actions.associate});
|
||||
api.resourcer.registerActionHandlers({...actions.common, ...actions.associate});
|
||||
|
||||
const data = {
|
||||
title: '后台应用',
|
||||
|
@ -169,7 +169,7 @@ describe('base model', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it.only('update virtual attribute', async () => {
|
||||
it('update virtual attribute', async () => {
|
||||
await test.update({
|
||||
title: 'xxx', // 虚拟字段没 set 转存 options
|
||||
content: 'content123', // set 留空,这个 key 什么也不做
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { Agent, getAgent, getApp } from '.';
|
||||
import { Application } from '@nocobase/server';
|
||||
import * as types from '../interfaces/types';
|
||||
|
||||
jest.setTimeout(30000);
|
||||
describe('collection hooks', () => {
|
||||
let app: Application;
|
||||
let agent: Agent;
|
||||
@ -13,6 +13,14 @@ describe('collection hooks', () => {
|
||||
|
||||
afterEach(() => app.database.close());
|
||||
|
||||
it('import', async () => {
|
||||
const tables = app.database.getTables([]);
|
||||
for (const table of tables) {
|
||||
const Collection = app.database.getModel('collections');
|
||||
await Collection.import(table.getOptions(), { hooks: false });
|
||||
}
|
||||
});
|
||||
|
||||
it('create table', async () => {
|
||||
const response = await agent.resource('collections').create({
|
||||
values: {
|
||||
@ -20,7 +28,6 @@ describe('collection hooks', () => {
|
||||
title: 'tests',
|
||||
},
|
||||
});
|
||||
|
||||
const table = app.database.getTable('tests');
|
||||
expect(table).toBeDefined();
|
||||
});
|
||||
|
@ -55,7 +55,7 @@ export async function getApp() {
|
||||
},
|
||||
});
|
||||
app.resourcer.use(middlewares.associated);
|
||||
app.resourcer.registerHandlers({...actions.associate, ...actions.common});
|
||||
app.resourcer.registerActionHandlers({...actions.associate, ...actions.common});
|
||||
await app.plugins([plugin]);
|
||||
await app.database.sync({
|
||||
force: true,
|
||||
|
@ -14,8 +14,8 @@ export default async function (options = {}) {
|
||||
directory: path.resolve(__dirname, 'collections'),
|
||||
});
|
||||
|
||||
resourcer.registerHandler('getCollection', getCollection);
|
||||
resourcer.registerHandler('getView', getView);
|
||||
resourcer.registerHandler('getPageInfo', getPageInfo);
|
||||
resourcer.registerHandler('pages:getRoutes', getRoutes);
|
||||
resourcer.registerActionHandler('getCollection', getCollection);
|
||||
resourcer.registerActionHandler('getView', getView);
|
||||
resourcer.registerActionHandler('getPageInfo', getPageInfo);
|
||||
resourcer.registerActionHandler('pages:getRoutes', getRoutes);
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ export default async function (options = {}) {
|
||||
directory: path.resolve(__dirname, 'collections'),
|
||||
});
|
||||
|
||||
resourcer.registerHandlers({
|
||||
resourcer.registerActionHandlers({
|
||||
'users:login': login,
|
||||
'users:register': register,
|
||||
'users:logout': logout,
|
||||
|
@ -48,7 +48,7 @@ describe('koa middleware', () => {
|
||||
|
||||
const resourcer = new Resourcer();
|
||||
|
||||
resourcer.registerHandlers({
|
||||
resourcer.registerActionHandlers({
|
||||
async index(ctx, next) {
|
||||
ctx.body = ctx.body || {};
|
||||
ctx.body.arr = ctx.body.arr || [];
|
||||
@ -188,7 +188,7 @@ describe('koa middleware', () => {
|
||||
ctx.body = ctx.action.params;
|
||||
await next();
|
||||
};
|
||||
resourcer.registerHandlers({
|
||||
resourcer.registerActionHandlers({
|
||||
list: handler,
|
||||
create: handler,
|
||||
update: handler,
|
||||
|
@ -78,10 +78,10 @@ describe('resourcer', () => {
|
||||
expect(context.arr).toStrictEqual([11,22]);
|
||||
});
|
||||
|
||||
it('registerHandlers()', async () => {
|
||||
it('registerActionHandlers()', async () => {
|
||||
const resourcer = new Resourcer();
|
||||
|
||||
resourcer.registerHandlers({
|
||||
resourcer.registerActionHandlers({
|
||||
async list(ctx, next) {
|
||||
ctx.arr.push(1);
|
||||
await next();
|
||||
@ -123,10 +123,10 @@ describe('resourcer', () => {
|
||||
expect(context.arr).toStrictEqual([11,22]);
|
||||
});
|
||||
|
||||
it('registerHandlers()', async () => {
|
||||
it('registerActionHandlers()', async () => {
|
||||
const resourcer = new Resourcer();
|
||||
|
||||
resourcer.registerHandlers({
|
||||
resourcer.registerActionHandlers({
|
||||
'test:list': async(ctx, next) => {
|
||||
ctx.arr.push(1);
|
||||
await next();
|
||||
@ -170,10 +170,10 @@ describe('resourcer', () => {
|
||||
expect(context.arr).toStrictEqual([11,22]);
|
||||
});
|
||||
|
||||
it('registerHandlers()', async () => {
|
||||
it('registerActionHandlers()', async () => {
|
||||
const resourcer = new Resourcer();
|
||||
|
||||
resourcer.registerHandlers({
|
||||
resourcer.registerActionHandlers({
|
||||
'list': async(ctx, next) => {
|
||||
ctx.arr.push(11);
|
||||
await next();
|
||||
@ -181,7 +181,7 @@ describe('resourcer', () => {
|
||||
},
|
||||
});
|
||||
|
||||
resourcer.registerHandlers({
|
||||
resourcer.registerActionHandlers({
|
||||
'get': async(ctx, next) => {
|
||||
ctx.arr.push(33);
|
||||
await next();
|
||||
@ -219,7 +219,7 @@ describe('resourcer', () => {
|
||||
it('only', async () => {
|
||||
const resourcer = new Resourcer();
|
||||
|
||||
resourcer.registerHandlers({
|
||||
resourcer.registerActionHandlers({
|
||||
async list(ctx, next) {
|
||||
ctx.arr.push(1);
|
||||
await next();
|
||||
@ -263,7 +263,7 @@ describe('resourcer', () => {
|
||||
it('except', async () => {
|
||||
const resourcer = new Resourcer();
|
||||
|
||||
resourcer.registerHandlers({
|
||||
resourcer.registerActionHandlers({
|
||||
async list(ctx, next) {
|
||||
ctx.arr.push(1);
|
||||
await next();
|
||||
|
@ -122,6 +122,10 @@ export class Resourcer {
|
||||
*/
|
||||
protected handlers = new Map<ActionName, any>();
|
||||
|
||||
protected actionHandlers = new Map<ActionName, any>();
|
||||
|
||||
protected middlewareHandlers = new Map<string, any>();
|
||||
|
||||
protected paramsKey = 'params';
|
||||
|
||||
protected middlewares = [];
|
||||
@ -174,22 +178,22 @@ export class Resourcer {
|
||||
*
|
||||
* @param handlers
|
||||
*/
|
||||
registerHandlers(handlers: Handlers) {
|
||||
registerActionHandlers(handlers: Handlers) {
|
||||
for (const [name, handler] of Object.entries(handlers)) {
|
||||
this.registerHandler(name, handler);
|
||||
this.registerActionHandler(name, handler);
|
||||
}
|
||||
}
|
||||
|
||||
registerHandler(name: ActionName, handler: HandlerType) {
|
||||
this.handlers.set(name, handler);
|
||||
registerActionHandler(name: ActionName, handler: HandlerType) {
|
||||
this.actionHandlers.set(name, handler);
|
||||
}
|
||||
|
||||
getRegisteredHandler(name: ActionName) {
|
||||
return this.handlers.get(name);
|
||||
return this.actionHandlers.get(name);
|
||||
}
|
||||
|
||||
getRegisteredHandlers() {
|
||||
return this.handlers;
|
||||
return this.actionHandlers;
|
||||
}
|
||||
|
||||
getResource(name: string): Resource {
|
||||
@ -201,7 +205,7 @@ export class Resourcer {
|
||||
|
||||
getAction(name: string, action: ActionName): Action {
|
||||
// 支持注册局部 action
|
||||
if (this.handlers.has(`${name}:${action}`)) {
|
||||
if (this.actionHandlers.has(`${name}:${action}`)) {
|
||||
return this.getResource(name).getAction(`${name}:${action}`);
|
||||
}
|
||||
return this.getResource(name).getAction(action);
|
||||
|
@ -13,7 +13,7 @@ describe('middleware', () => {
|
||||
beforeAll(() => {
|
||||
app = new Koa();
|
||||
resourcer = new Resourcer();
|
||||
resourcer.registerHandlers({
|
||||
resourcer.registerActionHandlers({
|
||||
list: async (ctx, next) => {
|
||||
ctx.body = [1,2];
|
||||
await next();
|
||||
|
@ -23,7 +23,7 @@ export default {
|
||||
app.use(cors());
|
||||
|
||||
// 这段代码处理的不完整
|
||||
app.resourcer.registerHandlers(actions.common);
|
||||
app.resourcer.registerActionHandlers(actions.common);
|
||||
|
||||
app.use(async (ctx, next) => {
|
||||
ctx.db = app.database;
|
||||
|
Loading…
Reference in New Issue
Block a user