feat: support register and call partial actions (#7)
This commit is contained in:
parent
dcdb21d398
commit
b00c24d5c6
@ -123,6 +123,53 @@ describe('resourcer', () => {
|
||||
expect(context.arr).toStrictEqual([11,22]);
|
||||
});
|
||||
|
||||
it('registerHandlers()', async () => {
|
||||
const resourcer = new Resourcer();
|
||||
|
||||
resourcer.registerHandlers({
|
||||
'test.list': async(ctx, next) => {
|
||||
ctx.arr.push(1);
|
||||
await next();
|
||||
ctx.arr.push(2);
|
||||
},
|
||||
'list': async(ctx, next) => {
|
||||
ctx.arr.push(11);
|
||||
await next();
|
||||
ctx.arr.push(22);
|
||||
},
|
||||
});
|
||||
|
||||
resourcer.define({
|
||||
name: 'test',
|
||||
});
|
||||
|
||||
resourcer.define({
|
||||
name: 'test2',
|
||||
});
|
||||
|
||||
let context = {
|
||||
arr: [],
|
||||
};
|
||||
|
||||
await resourcer.execute({
|
||||
resource: 'test',
|
||||
action: 'list',
|
||||
}, context);
|
||||
|
||||
expect(context.arr).toStrictEqual([1,2]);
|
||||
|
||||
context = {
|
||||
arr: [],
|
||||
};
|
||||
|
||||
await resourcer.execute({
|
||||
resource: 'test2',
|
||||
action: 'list',
|
||||
}, context);
|
||||
|
||||
expect(context.arr).toStrictEqual([11,22]);
|
||||
});
|
||||
|
||||
it('only', async () => {
|
||||
const resourcer = new Resourcer();
|
||||
|
||||
|
@ -194,6 +194,10 @@ export class Resourcer {
|
||||
}
|
||||
|
||||
getAction(name: string, action: ActionName): Action {
|
||||
// 支持注册局部 action
|
||||
if (this.handlers.has(`${name}.${action}`)) {
|
||||
return this.getResource(name).getAction(`${name}.${action}`);
|
||||
}
|
||||
return this.getResource(name).getAction(action);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user