Does nestjs support the pattern multiple DTO's are used to "Infer" which controller method to use on a single route?

Viewed 296

Disclaimer: My aspiration has obvious and good alternatives, but from what little I know about Nestjs I would not be surprised if supported the following "pattern" somehow.

Given these 2 DTOs:


    export class AddExistingMemberDto {
      
      @IsInt()
      memberId: number;
    }
    
    export class AddNonExistingMemberDto {
      
      @IsString()
      username: string;
      
      @IsEmail()
      email: string;
      
    }

Can Nestjs check which DTP the request body fulfils, and switch between different controller methods?

I tried:


@Controller('group')
export class GroupController {

  @Patch(':id/members/add')
  addExistingMember(
    @Body() addExistingMemberDto: AddExistingMemberDto,
  ) {
    console.log('Existing', addExistingMemberDt);
  }

  @Patch(':id/members/add')
  addNonExistingMember(
    @Body() addNonExistingMemberDto: AddNonExistingMemberDto,
  ) {
    console.log('Non-existing', addNonExistingMemberDto);
  }
}

But this always invokes with the first method.

Rationale: Having one controller method that checks the DTO is a fine alternative, but it does require me to merge my two different DTO classes. So I was just curious.

0 Answers
Related