fix: parseRequest & registerHandlers (#10)

This commit is contained in:
chenos 2020-11-12 11:48:54 +08:00 committed by GitHub
parent 1023e317f3
commit 77dc7227a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 2 deletions

View File

@ -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();

View File

@ -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' });

View File

@ -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) {