Search code examples
nestjsnestjs-swagger

How to make URI versioning optional in a nestjs REST application


NestJS URI Versioning for HTTP REST applications can be easily enabled when following the docs here.

The docs do however not explain how to make URI versioning optional.

Example:

/api/v1/users

/api/v2/users

/api/users -> should be mapped to v1 to allow existing webhooks to keep working

Question:

How can we make the URI versioning optional in a NestJS REST application so that old versions (without any api version) keep working ?


Solution

  • You should use the VERSION_NEUTRAL version on defaultVersion option like:

    app.enableVersioning({
        type: VersioningType.URI,
        defaultVersion: [VERSION_NEUTRAL, '1', '2'],
    });
    

    https://docs.nestjs.com/techniques/versioning