0

I'm using Nestjs v8.1.1, and trying to setup api versioning. My goal is to add in a new api version only those methods to the controller that where changed without duplicating other unchanged methods. Example:

api v1 TeamsController

@Controller('teams')
export class TeamsController {
 
 @Get()    
 findAll() {
    return this.teamsService.findAll();
 }

 @Get(':id')
 findOne(@Param('id') id: string) {
   return this.teamsService.findOne(id);
 }

}

In api v2 a new method findAllV2 is added to TeamsController

@Controller('teams')
export class TeamsController {

 @Get()    
 findAll() {
   return this.teamsService.findAll();
 }

 @Get(':id')
  findOne(@Param('id') id: string) {
   return this.teamsService.findOne(id);
 }

 @Version('2')
 @Get()
 findAllV2() {
   return this.teamsServiceV2.findAll();
 }

}

So far so good. In main.ts I've set v1 as default version:

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

Results:

http://localhost:8080/api/v1/teams -> works ok

http://localhost:8080/api/v1/teams/1 -> works ok

http://localhost:8080/api/v2/teams -> works ok

http://localhost:8080/api/v2/teams/1 -> doesn't work (url not found 404) I would like to get the same results as /api/v1/teams/1 here without duplicating findOne() method which is unchanged in v2.

aponski
  • 145
  • 3
  • 12

0 Answers0