Skip to content

Commit

Permalink
fix: support router.verb(method, path, controllerString)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jun 16, 2024
1 parent 38eb803 commit a84dbf0
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,25 @@ export class Router {
middlewares: (MiddlewareFunc | string | ResourcesController)[]) {
const options: RegisterOptions = {};
let path: string | RegExp | (string | RegExp)[];
if (typeof pathOrMiddleware === 'string' || pathOrMiddleware instanceof RegExp) {
if (typeof nameOrPath === 'string' && nameOrPath.startsWith('/')) {
// verb(method, path, ...middlewares)
path = nameOrPath;
middlewares = [ pathOrMiddleware as string, ...middlewares ];
if (typeof pathOrMiddleware === 'string') {
// verb(method, path, controllerString)
// set controller name to router name
options.name = pathOrMiddleware;
}
} else if (nameOrPath instanceof RegExp) {
// verb(method, pathRegex, ...middlewares)
path = nameOrPath;
middlewares = [ pathOrMiddleware as string, ...middlewares ];
if (typeof pathOrMiddleware === 'string') {
// verb(method, pathRegex, controllerString)
// set controller name to router name
options.name = pathOrMiddleware;
}
} else if (typeof pathOrMiddleware === 'string' || pathOrMiddleware instanceof RegExp) {
// verb(method, name, path, ...middlewares)
path = pathOrMiddleware;
assert(typeof nameOrPath === 'string', 'route name should be string');
Expand Down
57 changes: 57 additions & 0 deletions test/EggRouter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,63 @@ describe('test/EggRouter.test.ts', () => {
assert(router.stack[1].stack.length === 1);
});

it('should app.verb(url, controllerString) work', () => {
const app = {
controller: {
async foo() { return; },
hello: {
world() { return; },
},
},
};

const router = new EggRouter({}, app);
router.get('/foo', 'foo');
router.post('/hello/world', 'hello.world');

assert.equal(router.stack[0].name, 'foo');
assert.equal(router.stack[0].path, '/foo');
assert.deepEqual(router.stack[0].methods, [ 'HEAD', 'GET' ]);
assert.equal(router.stack[0].stack.length, 1);
assert.equal(router.stack[1].name, 'hello.world');
assert.equal(router.stack[1].path, '/hello/world');
assert.deepEqual(router.stack[1].methods, [ 'POST' ]);
assert.equal(router.stack[1].stack.length, 1);
});

it('should app.verb(urlRegex, controllerString) work', () => {
const app = {
controller: {
async foo() { return; },
hello: {
world() { return; },
},
},
};

const router = new EggRouter({}, app);
router.get(/^\/foo/, 'foo');
router.post(/^\/hello\/world/, 'hello.world');
router.post(/^\/hello\/world2/, () => {}, 'hello.world');

assert.equal(router.stack[0].name, 'foo');
assert(router.stack[0].path instanceof RegExp);
assert.equal(router.stack[0].path.toString(), String(/^\/foo/));
assert.deepEqual(router.stack[0].methods, [ 'HEAD', 'GET' ]);
assert.equal(router.stack[0].stack.length, 1);
assert.equal(router.stack[1].name, 'hello.world');
assert(router.stack[1].path instanceof RegExp);
assert.equal(router.stack[1].path.toString(), String(/^\/hello\/world/));
assert.deepEqual(router.stack[1].methods, [ 'POST' ]);
assert.equal(router.stack[1].stack.length, 1);

assert.equal(router.stack[2].name, undefined);
assert(router.stack[2].path instanceof RegExp);
assert.equal(router.stack[2].path.toString(), String(/^\/hello\/world2/));
assert.deepEqual(router.stack[2].methods, [ 'POST' ]);
assert.equal(router.stack[2].stack.length, 2);
});

it('should app.verb() throw if not found controller', () => {
const app = {
controller: {
Expand Down

0 comments on commit a84dbf0

Please sign in to comment.