mirror of
https://github.com/nocobase/nocobase
synced 2024-11-15 05:36:05 +00:00
fix: parseRequest & registerHandlers (#10)
This commit is contained in:
parent
1023e317f3
commit
77dc7227a0
@ -170,6 +170,52 @@ describe('resourcer', () => {
|
||||
expect(context.arr).toStrictEqual([11,22]);
|
||||
});
|
||||
|
||||
it('registerHandlers()', async () => {
|
||||
const resourcer = new Resourcer();
|
||||
|
||||
resourcer.registerHandlers({
|
||||
'list': async(ctx, next) => {
|
||||
ctx.arr.push(11);
|
||||
await next();
|
||||
ctx.arr.push(22);
|
||||
},
|
||||
});
|
||||
|
||||
resourcer.registerHandlers({
|
||||
'get': async(ctx, next) => {
|
||||
ctx.arr.push(33);
|
||||
await next();
|
||||
ctx.arr.push(44);
|
||||
},
|
||||
});
|
||||
|
||||
resourcer.define({
|
||||
name: 'test',
|
||||
});
|
||||
|
||||
let context = {
|
||||
arr: [],
|
||||
};
|
||||
|
||||
await resourcer.execute({
|
||||
resource: 'test',
|
||||
action: 'list',
|
||||
}, context);
|
||||
|
||||
expect(context.arr).toStrictEqual([11,22]);
|
||||
|
||||
context = {
|
||||
arr: [],
|
||||
};
|
||||
|
||||
await resourcer.execute({
|
||||
resource: 'test',
|
||||
action: 'get',
|
||||
}, context);
|
||||
|
||||
expect(context.arr).toStrictEqual([33,44]);
|
||||
});
|
||||
|
||||
it('only', async () => {
|
||||
const resourcer = new Resourcer();
|
||||
|
||||
|
@ -186,7 +186,7 @@ describe('utils', () => {
|
||||
|
||||
it('actions options', () => {
|
||||
const params = parseRequest({
|
||||
path: '/user.posts:list',
|
||||
path: '/resourcer/user.posts:list',
|
||||
method: 'GET',
|
||||
});
|
||||
expect(params).toEqual({ associatedName: 'user', resourceName: 'posts', actionName: 'list' });
|
||||
|
@ -175,7 +175,9 @@ export class Resourcer {
|
||||
* @param handlers
|
||||
*/
|
||||
registerHandlers(handlers: Handlers) {
|
||||
this.handlers = new Map(Object.entries(handlers));
|
||||
for (const [name, handler] of Object.entries(handlers)) {
|
||||
this.registerHandler(name, handler);
|
||||
}
|
||||
}
|
||||
|
||||
registerHandler(name: ActionName, handler: HandlerType) {
|
||||
|
Loading…
Reference in New Issue
Block a user