Open AbanobNageh opened 1 month ago
there is another configuration where it does not work: this will ignore previous exclude calls
consumer.apply(someMiddleware)
.exclude('id')
.exclude('somethingelse')
this works
consumer.apply(someMiddleware)
.exclude('id', 'somethingelse')
@satanshiro I guess that's another issue. Would you like to create a PR to fix that one?
@AbanobNageh @micalevisk this works! I have confirmed that I can use such regular expressions to exclude specific pattern paths from the middleware.
consumer
.apply(AppMiddleware)
.exclude({ path: '/:id(\\d+)', method: RequestMethod.GET })
.forRoutes(AppController);
and @satanshiro this code works, too!
consumer
.apply(AppMiddleware)
.exclude({ path: 'all', method: RequestMethod.GET })
.exclude({ path: '/:id(\\d+)', method: RequestMethod.GET })
.forRoutes(AppController);
@micalevisk Hello, I am a university student from South Korea developing with NestJS. I would like to contribute to NestJS, but I am not sure where to start. Could you help me? Someday, I hope to make a significant contribution to the NestJS framework! Thanks for reading!
@EeeasyCode
@EeeasyCode Thank you. However, How did you test this? The code you included doesn't work for me. I tried it on my reproduction repo above and the tests still fail.
Also, my knowledge of Regex is lacking so excuse me if I am wrong but are you saying that for every path with a param (ex: /:id
) we would need to explicitly add the paths that should not match (ex: /all
)? If this is the case then this could be an ok temporary solution for small codebases but it doesn't seem like a feasible solution for large codebases where there could be many such endpoints.
@AbanobNageh
I think I found the cause of the issue. When /:id
is entered, it receives the id in the format of /1
or /test
, and in such cases, it cannot distinguish between @Get('/1')
and @Get('/test')
. To handle this, you should either use the regular expression I initially suggested or specify the path with a prefix in the Controller.
i have also same issue
Constructing an exclude pattern using regular expressions to omit “all” could be a viable solution like below.
consumer
.apply(AppMiddleware)
.exclude({ path: '/:id((?!all$).*)', method: RequestMethod.GET })
.forRoutes(AppController);
However, unfortunately, the version of path-to-regexp in nest is 3.2.0. It seems that we cannot currently use the negative lookahead regular expression.
SyntaxError: Invalid regular expression:
... Invalid group
@micalevisk, do you have any plans to update the version of path-to-regexp to the latest one?
We can't upgrade it as it would introduce a significant breaking change to dozens of existing Nest applications. There are no plans to move from v3 in the coming months/years.
@kamilmysliwiec In this case, what will happen to this issue? Will this issue be blocked because path-to-regexp
can't be upgraded? or do you mean that this middleware issue would be solved in some other way?
Is there an existing issue for this?
Current behavior
This issue happens when there are 2 endpoints:
/all
/:id
If the
/:id
endpoint is excluded from the middleware then the middleware is not called for both of the 2 endpoints.Minimum reproduction code
https://github.com/AbanobNageh/nestjs-middleware-exclude-issue
Steps to reproduce
The above repository has a middleware and the following endpoints:
/all
. The middleware should run for this endpoint./:id
. The middleware should not run for this endpoint.You can reproduce the issue by using the tests in the repository.
/all
endpoint test fails and the/:id
endpoint test succeeds when only the/:id
endpoint is excluded.Expected behavior
Only the excluded endpoints should be excluded from the middleware. The middleware should run for the
/all
endpoint and shouldn't run for the/:id
endpoint.Package
Other package
No response
NestJS version
10.3.8
Packages versions
Node.js version
20.10.0
In which operating systems have you tested?
Other
No response