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] router.use method doesn't work with RegExp #172

Open
Gabrirf opened this issue Sep 14, 2023 · 0 comments
Open

[fix] router.use method doesn't work with RegExp #172

Gabrirf opened this issue Sep 14, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@Gabrirf
Copy link

Gabrirf commented Sep 14, 2023

Describe the bug

Node.js version: 18.17.1

@koa/router: 12.0.0

Typescript target: ES2020

OS version: Windows 10 Enterprise

Description: router.use method doesn't work with RegExp

Actual behavior

I'm trying to set a middleware for just some routes in a path by using RegExp. It appears the error

([^/]*)`: `middleware` must be a function, not `object

I think that there is a bug with selecting the RegExp type because it does not match against the defined method in the Router class and it uses the first one. Should be using the second definition:

declare class Router<StateT = Koa.DefaultState, ContextT = Koa.DefaultContext> {
    use(...middleware: Array<Router.Middleware<StateT, ContextT>>): Router<StateT, ContextT>;

    use(
        path: string | string[] | RegExp,
        ...middleware: Array<Router.Middleware<StateT, ContextT>>
    ): Router<StateT, ContextT>;
}

Expected behavior

Should work the same as using:

router.use(['/a1', '/a2'], middleware);

Code to reproduce

import Router from '@koa/router';
const router= new Router();

const regexp = new RegExp('/^a.*/');
router.use(regexp, middleware);

router.post('/a1', func1);
router.post('/a2', func2);
router.post('/b1', func3);

export default router;

Checklist

  • [ X] I have searched through GitHub issues for similar issues.
  • [ X] I have completely read through the README and documentation.
  • [ X] I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.
@Gabrirf Gabrirf added the bug Something isn't working label Sep 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant