From 868dd1aaaa6dca29dc9b0fd7076401e6e66d3ef4 Mon Sep 17 00:00:00 2001 From: chenos Date: Wed, 2 Dec 2020 13:48:19 +0800 Subject: [PATCH] refactor: change registerHandler to registerActionHandler --- packages/actions/src/__tests__/index.ts | 2 +- packages/app/src/api/index.ts | 2 +- .../src/__tests__/base-model.test.ts | 2 +- .../src/__tests__/collections.test.ts | 11 +++++++++-- .../plugin-collections/src/__tests__/index.ts | 2 +- packages/plugin-pages/src/server.ts | 8 ++++---- packages/plugin-users/src/server.ts | 2 +- packages/resourcer/src/__tests__/koa.test.ts | 4 ++-- .../resourcer/src/__tests__/resourcer.test.ts | 18 +++++++++--------- packages/resourcer/src/resourcer.ts | 18 +++++++++++------- .../server/src/__tests__/middleware.test.ts | 2 +- packages/server/src/index.ts | 2 +- 12 files changed, 42 insertions(+), 31 deletions(-) diff --git a/packages/actions/src/__tests__/index.ts b/packages/actions/src/__tests__/index.ts index 4b8c225aa..e278478ad 100644 --- a/packages/actions/src/__tests__/index.ts +++ b/packages/actions/src/__tests__/index.ts @@ -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, diff --git a/packages/app/src/api/index.ts b/packages/app/src/api/index.ts index 5799fc682..64d506e9d 100644 --- a/packages/app/src/api/index.ts +++ b/packages/app/src/api/index.ts @@ -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: '后台应用', diff --git a/packages/plugin-collections/src/__tests__/base-model.test.ts b/packages/plugin-collections/src/__tests__/base-model.test.ts index 89242458e..c26f901a6 100644 --- a/packages/plugin-collections/src/__tests__/base-model.test.ts +++ b/packages/plugin-collections/src/__tests__/base-model.test.ts @@ -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 什么也不做 diff --git a/packages/plugin-collections/src/__tests__/collections.test.ts b/packages/plugin-collections/src/__tests__/collections.test.ts index f92b6dd1a..128a5f202 100644 --- a/packages/plugin-collections/src/__tests__/collections.test.ts +++ b/packages/plugin-collections/src/__tests__/collections.test.ts @@ -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(); }); diff --git a/packages/plugin-collections/src/__tests__/index.ts b/packages/plugin-collections/src/__tests__/index.ts index cafd2e04e..7e2a4ea69 100644 --- a/packages/plugin-collections/src/__tests__/index.ts +++ b/packages/plugin-collections/src/__tests__/index.ts @@ -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, diff --git a/packages/plugin-pages/src/server.ts b/packages/plugin-pages/src/server.ts index c39fac0b0..dc1872bdd 100644 --- a/packages/plugin-pages/src/server.ts +++ b/packages/plugin-pages/src/server.ts @@ -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); } diff --git a/packages/plugin-users/src/server.ts b/packages/plugin-users/src/server.ts index d6099caf7..90d497c05 100644 --- a/packages/plugin-users/src/server.ts +++ b/packages/plugin-users/src/server.ts @@ -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, diff --git a/packages/resourcer/src/__tests__/koa.test.ts b/packages/resourcer/src/__tests__/koa.test.ts index adac2e7f7..e26e58680 100644 --- a/packages/resourcer/src/__tests__/koa.test.ts +++ b/packages/resourcer/src/__tests__/koa.test.ts @@ -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, diff --git a/packages/resourcer/src/__tests__/resourcer.test.ts b/packages/resourcer/src/__tests__/resourcer.test.ts index 81aff4d4a..df76870f2 100644 --- a/packages/resourcer/src/__tests__/resourcer.test.ts +++ b/packages/resourcer/src/__tests__/resourcer.test.ts @@ -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(); diff --git a/packages/resourcer/src/resourcer.ts b/packages/resourcer/src/resourcer.ts index 408bf945e..c5f8ff216 100644 --- a/packages/resourcer/src/resourcer.ts +++ b/packages/resourcer/src/resourcer.ts @@ -122,6 +122,10 @@ export class Resourcer { */ protected handlers = new Map(); + protected actionHandlers = new Map(); + + protected middlewareHandlers = new Map(); + 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); diff --git a/packages/server/src/__tests__/middleware.test.ts b/packages/server/src/__tests__/middleware.test.ts index 765119fed..bf91b9a4a 100644 --- a/packages/server/src/__tests__/middleware.test.ts +++ b/packages/server/src/__tests__/middleware.test.ts @@ -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(); diff --git a/packages/server/src/index.ts b/packages/server/src/index.ts index 7e39267ea..930e6e021 100644 --- a/packages/server/src/index.ts +++ b/packages/server/src/index.ts @@ -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;