Resource/DTO Mappers
- import:
@azure-net/kit/infra - Preferred location:
src/app/contexts/{ContextName}/infrastructure/http/dto
Base class for inheritance when creating resources and DTO mappers. Has a built-in toPlainObject method that excludes methods from the class. To use methods like mapCollectionUsing and mapUsing with ResponseBuilder, you need to inherit from this class and perform all transformations and method calls in the constructor. The actual transformation using this method is performed by ResponseBuilder itself.
DTOMapper
typescript
import { DTOMapper } from '@azure-net/kit/infra';
interface IUser {
id: number;
name: string;
email: string;
}
interface IMappedUser {
id: number;
firstName: string;
lastName: string;
email: string;
status: number;
fullName: string;
}
// You cannot pass an interface to DTOMapper, but then toPlainObject will return PlainObject<UserDTO>
// PlainObject<UserDTO> will simply consist of your class keys without methods.
class UserDTO extends DTOMapper<IMappedUser> {
id: number;
name: string;
lastName: string;
email: string;
status: number;
fullName: string;
constructor(data: IUser) {
super();
this.id = data.id;
this.name = data.name;
this.lastName = 'SomeTestLastname';
this.email = data.email;
this.status = 1;
// Call all processing in the constructor
this.fullName = this.getFullName();
}
getFullName() {
return `${this.name} ${this.lastName}`;
}
}
const userDto = new UserDTO(userData); // Our DTO
const plain = userDto.toPlainObject(); // getFullName is not included in PlainObject as it's a method