数据转换器
非官方测试版翻译
本页面由 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. 在 createTRPCProxyClient() 或 createTRPCNext() 中添加
tsimport { createTRPCProxyClient } from '@trpc/client';import type { AppRouter } from '~/server/routers/_app';import superjson from 'superjson';export const client = createTRPCProxyClient<AppRouter>({transformer: superjson, // <--// [...]});
tsimport { createTRPCProxyClient } from '@trpc/client';import type { AppRouter } from '~/server/routers/_app';import superjson from 'superjson';export const client = createTRPCProxyClient<AppRouter>({transformer: superjson, // <--// [...]});
utils/trpc.tstsimport { createTRPCNext } from '@trpc/next';import type { AppRouter } from '~/server/routers/_app';import superjson from 'superjson';// [...]export const trpc = createTRPCNext<AppRouter>({config(config) {return {transformer: superjson, // <--};},// [...]});
utils/trpc.tstsimport { createTRPCNext } from '@trpc/next';import type { AppRouter } from '~/server/routers/_app';import superjson from 'superjson';// [...]export const trpc = createTRPCNext<AppRouter>({config(config) {return {transformer: superjson, // <--};},// [...]});
为上传和下载配置不同转换器
如果转换器只需单向使用,或需要为上传/下载分别配置不同转换器(例如出于性能考虑),可以为上传和下载分别指定独立转换器。请确保在所有位置使用相同的组合转换器。
使用方法
此处使用 superjson 处理上传数据,采用 devalue 处理下载数据——因 devalue 速度更快但存在服务端安全风险。
1. 安装
bashyarn add superjson devalue
bashyarn add superjson devalue
2. 添加到 utils/trpc.ts
utils/trpc.tstsimport { uneval } from 'devalue';import superjson from 'superjson';// [...]export const transformer = {input: superjson,output: {serialize: (object) => uneval(object),// This `eval` only ever happens on the **client**.deserialize: (object) => (0, eval)(`(${object})`),},};
utils/trpc.tstsimport { uneval } from 'devalue';import superjson from 'superjson';// [...]export const transformer = {input: superjson,output: {serialize: (object) => uneval(object),// This `eval` only ever happens on the **client**.deserialize: (object) => (0, eval)(`(${object})`),},};
3. 添加到您的 AppRouter
server/routers/_app.tstsimport { initTRPC } from '@trpc/server';import { transformer } from '../../utils/trpc';export const t = initTRPC.create({transformer,});export const appRouter = t.router({// [...]});
server/routers/_app.tstsimport { initTRPC } from '@trpc/server';import { transformer } from '../../utils/trpc';export const t = initTRPC.create({transformer,});export const appRouter = t.router({// [...]});
4. 在 createTRPCProxyClient() 中添加
client.tstsimport { createTRPCProxyClient } from '@trpc/client';import { transformer } from '../utils/trpc';export const client = createTRPCProxyClient<AppRouter>({transformer, // <--// [...]});
client.tstsimport { createTRPCProxyClient } from '@trpc/client';import { transformer } from '../utils/trpc';export const client = createTRPCProxyClient<AppRouter>({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;}