I am facing an issue with exposing an array of objects in my code.
Even though I have exposed the Followers array in the UserDto
, it is not getting displayed as expected.
This is the output I am currently seeing,
{
"id": "5ff4ec30-d3f4-43d3-b5ad-82b03e1c5481",
"userName": "jdbfjl",
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="29434d4f4b4843694e44484045074a4644">[email protected]</a>",
"bio": "Duuude",
"avatar": "sjldflaeulajsnlnaefb",
"followerCount": 0,
"followeeCount": 0,
"verified": false,
"followers": [
{},
{},
{}
],
"followees": [
{}
]
}
The expected output should look like this,
{
"id": "5ff4ec30-d3f4-43d3-b5ad-82b03e1c5481",
"createdAt": "2021-08-11T11:07:11.688Z",
"updatedAt": "2021-08-11T11:07:11.688Z",
"userName": "ashdviah",
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="204853445648415360474d41494c0e434f4d">[email protected]</a>",
"bio": "I am Handsome",
"avatar": "sjldflaeulajsnlnaefb",
"followerCount": 0,
"followeeCount": 0,
"verified": false,
"followers": [
{
"id": "db1d30c6-5607-4d87-8838-69f906c3c44e",
"createdAt": "2021-08-11T11:09:33.018Z",
"updatedAt": "2021-08-11T11:09:33.018Z"
},
{
"id": "31492cd6-7c56-48f6-aff3-792a980b5100",
"createdAt": "2021-08-11T11:11:01.288Z",
"updatedAt": "2021-08-11T11:11:01.288Z"
},
],
"followees": [
{
"id": "ab095d0d-b9fa-41a4-be35-13fe9dd6f7a1",
"createdAt": "2021-08-11T12:55:18.139Z",
"updatedAt": "2021-08-11T12:55:18.139Z"
}
]
}
However, when I do not specify an interceptor for that route, I get this output which exposes password information unintentionally.
My current approach involves using the following code in my mock class which is not working as intended. What could be the issue here?
class mock {
@Expose() id : string;
@Expose() createdAt : Date;
@Expose() updatedAt : Date;
}
export class UserDto {
@Expose()
id : string;
@Expose()
userName : string;
@Expose()
email : string;
@Expose()
bio : string;
@Expose()
avatar : string;
@Expose()
followerCount : number;
@Expose()
followeeCount : number;
@Expose()
verified : boolean;
@Expose()
followers : Array<mock>;
@Expose()
followees : Array<mock>;
}
The transformation is handled by an interceptor used at the controller level.
Usage : @Serialize(UserDto)
decorator
export function Serialize(dto: ClassConstructor) {
return UseInterceptors(new Serializeinterceptor(dto));
}
export class Serializeinterceptor implements NestInterceptor {
constructor(private dto: any) {}
intercept(context: ExecutionContext, handler: CallHandler) {
return handler.handle().pipe(
map((data: any) => {
return plainToClass(this.dto, data, {
excludeExtraneousValues: true,
});
}),
);
}
}