feature: Add tags to include/exclude fields conditionally at a more global level
Description
We use class-transformer with nest.js and typeORM. Sometimes we have use-cases where we want to get the typeORM entity with some relations, but depending on the permission some data should or should not be sent to the client. There are already groups, but as far as I can see groups can not be used to "exclude" data but are only opt-in.
Proposed solution
I thought of kind of tags which can be applied to the corresponding properties. E.g. @Tags('sensitive') emailAddress: string. Now when using nests class transformer interceptor I could e.g. make sure this data is only included/excluded depending on my role and/or permissions, by adding a the tags to the classToPlain options
- as opt-in
{ includeTags: ['sensitive'] }or - as opt-out
{ excludeTags: ['sensitive'] }
One solution is to define a RespsoneDto class that is returned vs your Entity. Your Entity includes all properties that are required for your business (service) layer to function and then at the controller layer you return a new ResponseDto(bussinessEntity);
Additionally, if your controller has access to your user's permissions/role, you can choose which RepsoneDto is returned - UserResponseDto, AdminUserResponseDto, etc.
I'm believe this would work with the out-of-the-box Nest ClassSerializerInterceptor; I use a custom serialization interceptor that blocks a plain object response.
I am aware of that, but sometimes it produces more code than necessary since there is only one single member which changes which makes me doing class inheritance and consequently another file and so on... Since the tags are already available I thought it should be easy to add this include/exclude tags feature...