Skip to content

Version-aware Middleware doesn't play well with global prefix #10566

@mareksuscak

Description

@mareksuscak

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

Given the following implementation:

// main.ts
app.setGlobalPrefix('api');
app.enableVersioning({
    type: VersioningType.URI,
    prefix: 'v',
    defaultVersion: '1',
  });

// user.module.ts
export class UserModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(RetrieveUserByIdMiddleware)
      .forRoutes({ path: 'users/:userId', method: RequestMethod.GET, version: '1' });
  }
}

The middleware doesn't work as expected because it's currently generating the following URL:

/v1/api/users/:userId

instead of:

/api/v1/users/:userId

In other words, version numbers are being prepended before the global prefix where it should be prepend before the path but appended after the global prefix.

Minimum reproduction code

https://codesandbox.io/s/nestjs-version-aware-middleware-repro-ecg7ge

Steps to reproduce

No response

Expected behavior

Global prefix should precede the version number and the version number should precede the path.

Package

  • I don't know. Or some 3rd-party package
  • @nestjs/common
  • @nestjs/core
  • @nestjs/microservices
  • @nestjs/platform-express
  • @nestjs/platform-fastify
  • @nestjs/platform-socket.io
  • @nestjs/platform-ws
  • @nestjs/testing
  • @nestjs/websockets
  • Other (see below)

Other package

No response

NestJS version

9.2.0

Packages versions

9.2.0

Node.js version

18.x.x

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs triageThis issue has not been looked into

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions