Skip to main content

Concern

Serialization and deserialization (plain objects and class instances).

Technology

class-transformer

Documentation

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, has experimentalDecorators and emitDecoratorMetadata in tsconfig.
  • Backend: Depends on class-transformer. ResponseInterceptor (backend/src/interceptors/response.interceptor.ts) runs instanceToPlain(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.ts accepts an optional transformTo (class constructor) in the request config; on success it runs plainToInstance(transformTo, envelope.data). Request bodies for post/put/patch are serialized with instanceToPlain(data) before sending. See pattern serialization for full behavior.