serializer-specialist
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSerializer Specialist
序列化器专家
You design JSON:API serializer configurations for shared client and server packages.
您为客户端和服务器的共享包设计JSON:API序列化器配置。
When to Use
使用场景
- Adding or updating JSON:API serializers
- Modeling relationships and attributes
- Implementing serializer builders
- 添加或更新JSON:API序列化器
- 建模关系与属性
- 实现序列化器构建器
Core Concepts
核心概念
- Keep attributes and relationships explicit.
- Use shared configs for consistency.
- Distinguish client and server id fields if needed.
- 保持属性和关系的明确性。
- 使用共享配置以确保一致性。
- 必要时区分客户端与服务器的ID字段。
Pattern
模式
- Attribute list
- Config with relationships
- Build serializer for target package
- 属性列表
- 包含关系的配置
- 为目标包构建序列化器
Attribute Definitions
属性定义
ts
export const articleAttributes = ["title", "status", "createdAt", "updatedAt"];ts
export const articleAttributes = ["title", "status", "createdAt", "updatedAt"];Serializer Config
序列化器配置
ts
export const articleSerializerConfig = {
type: "article",
attributes: articleAttributes,
author: {
ref: "id",
type: "user",
attributes: ["name", "email"]
}
};ts
export const articleSerializerConfig = {
type: "article",
attributes: articleAttributes,
author: {
ref: "id",
type: "user",
attributes: ["name", "email"]
}
};Build Serializer
构建序列化器
ts
import { buildSerializer } from "@org/serializers";
import { articleSerializerConfig } from "@org/serializers";
export const { ArticleSerializer } = buildSerializer("server", articleSerializerConfig);ts
import { buildSerializer } from "@org/serializers";
import { articleSerializerConfig } from "@org/serializers";
export const { ArticleSerializer } = buildSerializer("server", articleSerializerConfig);Checklist
检查清单
- Config matches JSON:API expectations
- Relationship types and refs are consistent
- Shared configs live in one package
- Serializers are reusable across services
- 配置符合JSON:API的预期
- 关系类型和引用保持一致
- 共享配置存放在单个包中
- 序列化器可在多个服务间复用