From b6450955716eb7d965c357058cf5b93f9e49353f Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Sun, 16 Jun 2024 21:41:10 +0800 Subject: [PATCH] fix: should support urls with controller string (#15) ## Summary by CodeRabbit - **New Features** - Enhanced routing functionality to handle arrays, strings, and regex inputs more effectively. - **Tests** - Added a new test case to verify routing behavior for multiple URLs and controller strings. --- src/Router.ts | 9 +++++++++ test/EggRouter.test.ts | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/Router.ts b/src/Router.ts index c857569..f62dd4b 100644 --- a/src/Router.ts +++ b/src/Router.ts @@ -619,6 +619,15 @@ export class Router { // set controller name to router name options.name = pathOrMiddleware; } + } else if (Array.isArray(nameOrPath)) { + // verb(method, paths, ...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; diff --git a/test/EggRouter.test.ts b/test/EggRouter.test.ts index e795b8a..32199d9 100644 --- a/test/EggRouter.test.ts +++ b/test/EggRouter.test.ts @@ -178,6 +178,44 @@ describe('test/EggRouter.test.ts', () => { assert.equal(router.stack[1].stack.length, 1); }); + it('should app.verb(urls, controllerString) work', () => { + const app = { + controller: { + async foo() { return; }, + hello: { + world() { return; }, + }, + }, + }; + + const router = new EggRouter({}, app); + router.get([ '/foo', '/bar' ], 'foo'); + router.post('/hello/world', 'hello.world'); + router.put('other', [ '/other1', '/other2' ], 'foo'); + + 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, 'foo'); + assert.equal(router.stack[1].path, '/bar'); + assert.deepEqual(router.stack[1].methods, [ 'HEAD', 'GET' ]); + assert.equal(router.stack[1].stack.length, 1); + assert.equal(router.stack[2].name, 'hello.world'); + assert.equal(router.stack[2].path, '/hello/world'); + assert.deepEqual(router.stack[2].methods, [ 'POST' ]); + assert.equal(router.stack[2].stack.length, 1); + + assert.equal(router.stack[3].name, 'other'); + assert.equal(router.stack[3].path, '/other1'); + assert.deepEqual(router.stack[3].methods, [ 'PUT' ]); + assert.equal(router.stack[3].stack.length, 1); + assert.equal(router.stack[4].name, 'other'); + assert.equal(router.stack[4].path, '/other2'); + assert.deepEqual(router.stack[4].methods, [ 'PUT' ]); + assert.equal(router.stack[4].stack.length, 1); + }); + it('should app.verb(urlRegex, controllerString) work', () => { const app = { controller: {