数据转换器
非官方测试版翻译
本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →
你可以对响应数据和输入参数进行序列化处理。转换器需要同时在服务器端和客户端添加。
使用 superjson
SuperJSON 允许我们在服务器和客户端之间透明地传输标准类型(如 Date、Map、Set)。这意味着你可以直接从 API 解析器返回这些类型,并在客户端直接使用它们,无需从 JSON 重新构建对象。
使用方法
1. 安装
bashyarn add superjson
bashyarn add superjson
2. 添加到 initTRPC
routers/router/_app.tstsimport { initTRPC } from '@trpc/server';import superjson from 'superjson';export const t = initTRPC.create({transformer: superjson,});
routers/router/_app.tstsimport { initTRPC } from '@trpc/server';import superjson from 'superjson';export const t = initTRPC.create({transformer: superjson,});
3. 添加到 httpLink()、wsLink() 等
在
initTRPC对象添加转换器后,TypeScript 会引导你在需要的位置添加transformer
createTRPCClient():
src/app/_trpc/client.tstsimport { createTRPCClient } from '@trpc/client';import type { AppRouter } from '~/server/routers/_app';import superjson from 'superjson';export const client = createTRPCClient<AppRouter>({links: [httpLink({url: 'http://localhost:3000',transformer: superjson,}),],});
src/app/_trpc/client.tstsimport { createTRPCClient } from '@trpc/client';import type { AppRouter } from '~/server/routers/_app';import superjson from 'superjson';export const client = createTRPCClient<AppRouter>({links: [httpLink({url: 'http://localhost:3000',transformer: superjson,}),],});
使用 devalue
Devalue 的功能类似 superjson,但专注于提升性能和缩小负载体积,代价是降低了正文的人类可读性。
使用方法
1. 安装
bashyarn add superjson devalue
bashyarn add superjson devalue
2. 添加到 utils/trpc.ts
这里使用 parse 和 stringify 是因为它们能缓解 XSS 攻击。
utils/trpc.tstsimport { parse, stringify } from 'devalue';// [...]export const transformer = {deserialize: (object: any) => parse(object),serialize: (object: any) => stringify(object),};
utils/trpc.tstsimport { parse, stringify } from 'devalue';// [...]export const transformer = {deserialize: (object: any) => parse(object),serialize: (object: any) => stringify(object),};
3. 添加到你的 initTRPC
server/routers/_app.tstsimport { initTRPC } from '@trpc/server';import { transformer } from '../../utils/trpc';export const t = initTRPC.create({transformer,});
server/routers/_app.tstsimport { initTRPC } from '@trpc/server';import { transformer } from '../../utils/trpc';export const t = initTRPC.create({transformer,});
4. 添加到 httpLink()、wsLink() 等
在
initTRPC对象添加转换器后,TypeScript 会引导你在需要的位置添加transformer
createTRPCClient():
src/app/_trpc/client.tstsimport { createTRPCClient } from '@trpc/client';import type { AppRouter } from '~/server/routers/_app';import { transformer } from '../../utils/trpc';export const client = createTRPCClient<AppRouter>({links: [httpLink({url: 'http://localhost:3000',transformer,}),],});
src/app/_trpc/client.tstsimport { createTRPCClient } from '@trpc/client';import type { AppRouter } from '~/server/routers/_app';import { transformer } from '../../utils/trpc';export const client = createTRPCClient<AppRouter>({links: [httpLink({url: 'http://localhost:3000',transformer,}),],});
为上传和下载配置不同转换器
如果转换器只需单向使用,或需要为上传/下载分别配置不同转换器(例如出于性能考虑),可以为上传和下载分别指定独立转换器。请确保在所有位置使用相同的组合转换器。
DataTransformer 接口
tsexport interface DataTransformer {serialize(object: any): any;deserialize(object: any): any;}interface InputDataTransformer extends DataTransformer {/*** This function runs **on the client** before sending the data to the server.*/serialize(object: any): any;/*** This function runs **on the server** to transform the data before it is passed to the resolver*/deserialize(object: any): any;}interface OutputDataTransformer extends DataTransformer {/*** This function runs **on the server** before sending the data to the client.*/serialize(object: any): any;/*** This function runs **only on the client** to transform the data sent from the server.*/deserialize(object: any): any;}export interface CombinedDataTransformer {/*** Specify how the data sent from the client to the server should be transformed.*/input: InputDataTransformer;/*** Specify how the data sent from the server to the client should be transformed.*/output: OutputDataTransformer;}
tsexport interface DataTransformer {serialize(object: any): any;deserialize(object: any): any;}interface InputDataTransformer extends DataTransformer {/*** This function runs **on the client** before sending the data to the server.*/serialize(object: any): any;/*** This function runs **on the server** to transform the data before it is passed to the resolver*/deserialize(object: any): any;}interface OutputDataTransformer extends DataTransformer {/*** This function runs **on the server** before sending the data to the client.*/serialize(object: any): any;/*** This function runs **only on the client** to transform the data sent from the server.*/deserialize(object: any): any;}export interface CombinedDataTransformer {/*** Specify how the data sent from the client to the server should be transformed.*/input: InputDataTransformer;/*** Specify how the data sent from the server to the client should be transformed.*/output: OutputDataTransformer;}