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