Concern
Serialization and deserialization (plain objects and class instances).
Technology
class-transformer
Documentation
- Repository: https://github.com/typestack/class-transformer
- Documentation: https://github.com/typestack/class-transformer#usage
- Getting started: https://github.com/typestack/class-transformer#installation
Integration
Shared models use decorators (e.g. @Type()) for nested types and @Transform for Date (see pattern serialization). Backend uses plainToInstance to map ORM/Prisma results to shared class instances before returning, and the response interceptor uses instanceToPlain before sending. Frontend uses plainToInstance for API success data when a class is specified (e.g. transformTo) and instanceToPlain for request bodies; used alongside the shared package DTOs and with the same shapes as validation (zod) where applicable.
Implementation
- Shared: DTO/model classes with class-transformer decorators live in shared (e.g.
shared/src/dto/); the shared package depends on class-transformer and, when it contains decorator DTOs, hasexperimentalDecoratorsandemitDecoratorMetadatain tsconfig. - Backend: Depends on class-transformer. ResponseInterceptor (
backend/src/interceptors/response.interceptor.ts) runsinstanceToPlain(data)on the handler return value before wrapping in the envelope. - Frontend: frontends/shared depends on class-transformer. handleRequest in
frontends/shared/src/lib/api.tsaccepts an optionaltransformTo(class constructor) in the request config; on success it runsplainToInstance(transformTo, envelope.data). Request bodies for post/put/patch are serialized withinstanceToPlain(data)before sending. See patternserializationfor full behavior.