Skip to content

serialize

awekrx edited this page May 29, 2026 · 1 revision

serialize

Import

import { serialize } from '@dev-suite/decorators/serialize'

Category

  • method

Use Case

Transform method response into stable DTO/transport shape.

Replaces

  • Inline mapping in every return path
  • Scattered serializer helper calls

Example 1

Without decorator

class UserService {
  async getUser(id: string) {
    const user = await this.repo.get(id);
    return {
      id: user.id,
      fullName: user.firstName + ' ' + user.lastName,
      createdAt: user.createdAt.toISOString(),
    };
  }
}

With decorator

import { serialize } from '@dev-suite/decorators/serialize';

class UserService {
  @serialize({
    serializer: (user: UserEntity) => ({
      id: user.id,
      fullName: user.firstName + ' ' + user.lastName,
      createdAt: user.createdAt.toISOString(),
    }),
  })
  async getUser(id: string) {
    return this.repo.get(id);
  }
}

Why better

  • Serializer policy is explicit and testable in one place.
  • Domain retrieval remains separate from transport shape.

Example 2

Without decorator

class ExportService {
  async file(id: string) {
    const file = await this.storage.fetch(id);
    return {
      id: file.id,
      size: file.buffer.length,
      checksum: file.checksum,
    };
  }
}

With decorator

import { serialize } from '@dev-suite/decorators/serialize';

class ExportService {
  @serialize({ serializer: (file: StoredFile) => ({ id: file.id, size: file.buffer.length, checksum: file.checksum }) })
  async file(id: string) {
    return this.storage.fetch(id);
  }
}

Why better

  • Supports multiple output contracts without method duplication.
  • Avoids repeated DTO mapping code.

Clone this wiki locally