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.