@nestjsx/api-verion
has been designed to quickly help you with the access logic if you're using global prefix
in your application and want to restrict an access to different API endpoints with different API versions. For now it works with Express
and Fastify
adapters.
npm i @nestjsx/api-version --save
import { NestFactory } from '@nestjs/core';
import { ApplicationModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(ApplicationModule);
app.setGlobalPrefix('v1'); // <-- here
await app.listen(3000);
}
bootstrap();
import { ApiVersionGuard } from '@nestjsx/api-version';
import { Module } from '@nestjs/common';
import { APP_GUARD } from '@nestjs/core';
@Module({
providers: [
{
provide: APP_GUARD,
useClass: ApiVersionGuard,
},
],
})
export class ApplicationModule {}
import { ApiVersion } from '@nestjsx/api-version';
import { Controller, Get } from '@nestjs/common';
@Controller('cats')
export class CatsController {
@Get()
@ApiVersion()
findAllV1() {
return 'This action will work in all versions';
}
@Get()
@ApiVersion('v1')
findAllV1() {
return 'This action will work in v1 version';
}
@Get()
@ApiVersion('v2')
findAllNext() {
return 'This action will work in v2 version only';
}
}
npm run test:e2e
MIT