/** * SuperJSON 配置 - 用于 JSON 序列化/反序列化 * 处理 Next.js Server Components 和 Client Components 之间的数据传输 */ import { SuperJSON } from 'superjson'; export const superjson = SuperJSON; /** * 将数据序列化为 JSON 字符串(用于 Server -> Client 传输) */ export function serialize(data: T): string { return superjson.stringify(data); } /** * 从 JSON 字符串反序列化数据(用于 Client -> Server 传输) */ export function deserialize(json: string): T { return superjson.parse(json); } /** * Server Actions 返回值的序列化包装 */ export function createSerializedResponse(data: T) { return { data: superjson.serialize(data).json, }; } /** * 解析从 Server 传来的序列化数据 */ export function parseSerializedResponse(response: { data: unknown }): T { return superjson.deserialize({ json: response.data as any, }) as T; }