Skip to content

Conversation

@malkovitc
Copy link

PR Checklist

PR Type

  • Bugfix
  • Feature

What is the current behavior?

Issue Number: #16095

setGlobalPrefix() only accepts a single string prefix. Users who need to expose the same API under multiple base paths (e.g., for different deployment environments) previously used regex-style prefixes like (prefixOne|prefixTwo), which are no longer supported by path-to-regexp v8 in NestJS v11.

What is the new behavior?

setGlobalPrefix() now accepts either a string or an array of strings:

// Single prefix (existing behavior)
app.setGlobalPrefix('api');

// Multiple prefixes (new)
app.setGlobalPrefix(['api', 'v1']);
// Routes will be registered under both /api/... and /v1/...

This provides a migration path for users who previously relied on regex-style prefixes.

Changes:

  • Updated INestApplication.setGlobalPrefix() interface to accept string | string[]
  • Added getGlobalPrefixes() method to ApplicationConfig to retrieve all prefixes
  • Updated RoutePathFactory to generate paths for each prefix in the array
  • Updated RouteInfoPathExtractor to generate middleware paths for all prefixes
  • Added unit tests for the new functionality

Does this PR introduce a breaking change?

  • Yes
  • No

The change is backward compatible - existing code using a single string prefix will continue to work.

@coveralls
Copy link

Pull Request Test Coverage Report for Build 052bf0c4-da67-43b3-ba46-c7a94794509b

Details

  • 19 of 21 (90.48%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.01%) to 89.671%

Changes Missing Coverage Covered Lines Changed/Added Lines %
packages/core/middleware/route-info-path-extractor.ts 9 11 81.82%
Totals Coverage Status
Change from base Build e3800a39-758b-42fd-9609-068501388108: -0.01%
Covered Lines: 7423
Relevant Lines: 8278

💛 - Coveralls

@kamilmysliwiec
Copy link
Member

This will break packages like @nestjs/swagger IIRC

@malkovitc
Copy link
Author

@kamilmysliwiec Thanks for the review!

I analyzed the @nestjs/swagger compatibility:

Current behavior with this PR:

  1. getGlobalPrefix() still returns string (the first element of the array) - no breaking change
  2. RoutePathFactory.create() accepts both string and string[] and handles both correctly
  3. Added new getGlobalPrefixes() method for packages that want to support multiple prefixes

Swagger analysis:

Swagger uses getGlobalPrefix(app) which calls applicationConfig.getGlobalPrefix(). With this PR:

  • If user sets single prefix: app.setGlobalPrefix('api') → swagger works exactly as before
  • If user sets array: app.setGlobalPrefix(['api', 'v1']) → swagger sees 'api' (first element) and generates docs for that prefix only

This is a functional limitation, not a breaking change - swagger will continue to work, but won't automatically know about additional prefixes.

Proposed path forward:

  1. This PR maintains full backward compatibility
  2. @nestjs/swagger can later be updated to use getGlobalPrefixes() to generate documentation for all prefixes
  3. Users who need multiple prefixes can use them now; swagger support can be added incrementally

Would this approach work? Or would you prefer a different solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants