Skip to content

Commit

Permalink
fix: should support urls with controller string (#15)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## 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.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
fengmk2 authored Jun 16, 2024
1 parent a06ae6c commit b645095
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
38 changes: 38 additions & 0 deletions test/EggRouter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit b645095

Please sign in to comment.