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: support router.verb(method, path, controllerString) #12

Merged
merged 2 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
Loading