跳至主内容
版本:11.x

数据转换器

非官方测试版翻译

本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →

你可以对响应数据和输入参数进行序列化处理。转换器需要同时在服务器端和客户端添加。

使用 superjson

SuperJSON 允许我们在服务器和客户端之间透明地传输标准类型(如 DateMapSet)。这意味着你可以直接从 API 解析器返回这些类型,并在客户端直接使用它们,无需从 JSON 重新构建对象。

使用方法

1. 安装

bash
yarn add superjson
bash
yarn add superjson

2. 添加到 initTRPC

routers/router/_app.ts
ts
import { initTRPC } from '@trpc/server';
import superjson from 'superjson';
export const t = initTRPC.create({
transformer: superjson,
});
routers/router/_app.ts
ts
import { initTRPC } from '@trpc/server';
import superjson from 'superjson';
export const t = initTRPC.create({
transformer: superjson,
});

initTRPC 对象添加转换器后,TypeScript 会引导你在需要的位置添加 transformer

createTRPCClient()

src/app/_trpc/client.ts
ts
import { 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.ts
ts
import { 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. 安装

bash
yarn add superjson devalue
bash
yarn add superjson devalue

2. 添加到 utils/trpc.ts

这里使用 parsestringify 是因为它们能缓解 XSS 攻击

utils/trpc.ts
ts
import { parse, stringify } from 'devalue';
// [...]
export const transformer = {
deserialize: (object: any) => parse(object),
serialize: (object: any) => stringify(object),
};
utils/trpc.ts
ts
import { parse, stringify } from 'devalue';
// [...]
export const transformer = {
deserialize: (object: any) => parse(object),
serialize: (object: any) => stringify(object),
};

3. 添加到你的 initTRPC

server/routers/_app.ts
ts
import { initTRPC } from '@trpc/server';
import { transformer } from '../../utils/trpc';
export const t = initTRPC.create({
transformer,
});
server/routers/_app.ts
ts
import { initTRPC } from '@trpc/server';
import { transformer } from '../../utils/trpc';
export const t = initTRPC.create({
transformer,
});

initTRPC 对象添加转换器后,TypeScript 会引导你在需要的位置添加 transformer

createTRPCClient()

src/app/_trpc/client.ts
ts
import { 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.ts
ts
import { 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 接口

ts
export 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;
}
ts
export 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;
}