Skip to content

feat: add API versioning enforcement middleware#914

Open
Xaxxoo wants to merge 1 commit into
rinafcode:mainfrom
Xaxxoo:feat/875-api-version-middleware
Open

feat: add API versioning enforcement middleware#914
Xaxxoo wants to merge 1 commit into
rinafcode:mainfrom
Xaxxoo:feat/875-api-version-middleware

Conversation

@Xaxxoo

@Xaxxoo Xaxxoo commented Jun 27, 2026

Copy link
Copy Markdown

Closes #875

Summary

  • Creates ApiVersionMiddleware in src/common/middleware/api-version.middleware.ts that enforces the API version lifecycle at runtime
  • Requests to sunset versions receive 410 Gone with a Sunset header and a Link to migration docs
  • Requests to deprecated (grace-period) versions pass through but carry Deprecation: true and Sunset: <date> headers
  • Non-versioned paths are completely unaffected
  • Version sets are driven by two environment variables so no code change is needed to retire a version:
    • SUNSET_VERSIONS — comma-separated version:ISO-date pairs (e.g. v1:2024-01-01)
    • DEPRECATED_VERSIONS — same format for the grace-period warning
    • API_MIGRATION_DOCS_URL — URL included in 410 bodies and Link headers
  • Middleware is wired into AppModule.configure() and applied to all v* routes

Test plan

  • extractVersion correctly parses /v1/users, /v2, /health, etc.
  • Sunset version returns 410 with Sunset and Link headers; next() is never called
  • Deprecated version calls next() and sets Deprecation: true + Sunset headers
  • Non-versioned path calls next() without touching response headers
  • Empty config (no versions configured) passes all requests through
  • All 9 unit tests pass (npm test)

Create ApiVersionMiddleware that enforces version lifecycle policy at runtime.

- Requests to sunset versions receive 410 Gone with Sunset header and a link
  to migration docs (configured via SUNSET_VERSIONS env var)
- Requests to deprecated-but-active versions pass through with Deprecation and
  Sunset headers so consumers can track end-of-life dates (DEPRECATED_VERSIONS)
- Non-versioned paths are unaffected
- Apply middleware to all v* routes in AppModule via NestModule.configure()
- 9 unit tests covering sunset, deprecated, non-versioned, and empty-config cases
@drips-wave

drips-wave Bot commented Jun 27, 2026

Copy link
Copy Markdown

@Xaxxoo Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@RUKAYAT-CODER

Copy link
Copy Markdown
Contributor

Great job so far

There’s just one blocker — the workflow is failing. Could you take a look and fix it so all checks pass?

Happy to review again once that’s done.

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.

Add API versioning enforcement middleware to reject requests to deprecated versions

2 participants