Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: should support urls with controller string #15

Merged
merged 1 commit into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading