Route Handler Fails to Reflect Middleware Modifications to request.originalUrl
for API Versioning
#13211
Open
5 of 15 tasks
Labels
needs triage
This issue has not been looked into
Is there an existing issue for this?
Current behavior
I wanted to add versioning support to an existing Nest.js REST API.
Previously, all the routes were handled without version in the URL (like a GET at
/users
) and now I want:v1
routes, means if I perform a GET at/users/
this needs to be handled by/v1/users
handlerMy solution was to:
v2
requests, here is an example:The purpose of the
VersionManagementMiddleware
is to ensure that all incoming requests are automatically directed to the appropriate version of our API based on the URL path. Specifically, it aims to:It's consumed in the app as following:
With this in place, i expected all the requests missing the version to be handled as v1, all with wrong version to be handled as v2 and everything else as expected, means a GET at
v1/
or/v2
should be ignored by the middleware, but this is what currently happens:When versions exist in the request -> everything works as expected -- see the screenshot below
When missing the version, it recognizes I am trying to hit the v1/ but sends a 404 error while the v1/ handler exist
When using a bad version, it recognizes I am trying to hit the v2/ but still send a 404 while v2/ handler also exists
Minimum reproduction code
https://github.com/pacyL2K19/request-original-url-mutation-middleware
Steps to reproduce
Expected behavior
I expected the middleware to mutate the
request.originalUrl
so that requests without version are handled by v1 and those with bad version to be handled by v2 that way, my clients won't have to change anything on their side, this means the app can still work with endpoints missing version(or with unsupported versions) and the ones with versionPackage
@nestjs/common
@nestjs/core
@nestjs/microservices
@nestjs/platform-express
@nestjs/platform-fastify
@nestjs/platform-socket.io
@nestjs/platform-ws
@nestjs/testing
@nestjs/websockets
Other package
express
NestJS version
10.3.2
Packages versions
Node.js version
20.11.0
In which operating systems have you tested?
Other
No response
The text was updated successfully, but these errors were encountered: