lundi 15 février 2021

How to register a user that can be of a number of types in nestjs?

The situation is plain and clear. I've got a user that can be a fighter, judge, location provider etc. All these users have some common props, like first name, second name, phone number, email etc. It's logical to have a dto called RegisterBasicUserDto that would have all those props. Now I want to have the fighter-related props on RegisterFighterDto, similarly for all the other types of user.

Then I would like to use the nestjs swagger and the class-validator library. The problem is that , to my surprise, typescript does not allow multiple inheritance and swagger decorators don't work with mixins meaning that I can't have RegisterUserDto that would extend all those types of users inheriting all the necessary props from those classes...

It forces me to dump all props into a single class, let's say RegisterUserDto and use it like that:

@Post('register')
public async registerUser(@Body() registerUserDto: RegisterUserDto): Promise<void> {}

I hate the idea that I will have to dump them all together to the point where I'm starting to think of making separate endpoints for each type of user. Even though that would lead to code duplication...

Maybe you've got better ideas. Would appreciate your help.

Aucun commentaire:

Enregistrer un commentaire