How to realize the DTO of the return value of a function

define a database model User

interface User {
    id: string,
    name: string,
    xxxx: string,
}

define a DTO UserDTO

interface UserDTO {
    id: string,
    name: string,
}

defines a method to get a user.
generally, if we do not want to expose all the information, we will fix the exposed attribute (id, name).


async function getUser(){
    const user = await UserService.findOne();
    return {
        id: user.id,
        name: user.name,
    };
}
// res= { id, name }

but the above writing is not suitable for automated scenarios, and I want to use the decorator to achieve the following functions.

@ReturnDto(UserDTO)
async function getUser({
    const user = await UserService.findOne();
    return user;
}
// res= { id, name }

I have tried a lot of methods, but there is still no way to modify it while getting the return value of the function asynchronously.
can you tell me how to implement it easily?

Menu