数据转换器
您可以序列化响应数据和输入参数。转换器需要同时添加到服务器和客户端。
使用 superjson
SuperJSON 允许我们透明地使用,例如,标准的 Date
/Map
/Set
在服务器和客户端之间通过网络传输。也就是说,您可以从 API 解析器返回这些类型中的任何一个,并在客户端中使用它们,而无需从 JSON 重新创建对象。
如何
1. 安装
bash
yarn add superjson
bash
yarn add superjson
2. 添加到您的 initTRPC
routers/router/_app.tsts
import { initTRPC } from '@trpc/server';import superjson from 'superjson';export const t = initTRPC.create({transformer: superjson,});
routers/router/_app.tsts
import { initTRPC } from '@trpc/server';import superjson from 'superjson';export const t = initTRPC.create({transformer: superjson,});
3. 添加到 httpLink()
、wsLink()
等
一旦您在
initTRPC
对象上添加了transformer
,TypeScript 将指导您需要在何处添加它。
createTRPCClient()
:
src/app/_trpc/client.tsts
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.tsts
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}),],});
上传和下载的不同转换器
如果转换器只应用于一个方向,或者上传和下载应该使用不同的转换器(例如,出于性能原因),您可以为上传和下载提供单独的转换器。确保您在所有地方使用相同的组合转换器。
如何
这里 superjson 用于上传,而 devalue 用于下载数据,因为 devalue 速度更快,但在服务器上使用不安全。
1. 安装
bash
yarn add superjson devalue
bash
yarn add superjson devalue
2. 添加到 utils/trpc.ts
utils/trpc.tsts
import { 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) => eval(`(${object})`),},};
utils/trpc.tsts
import { 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) => eval(`(${object})`),},};
3. 添加到您的 AppRouter
server/routers/_app.tsts
import { initTRPC } from '@trpc/server';import { transformer } from '../../utils/trpc';export const t = initTRPC.create({transformer,});export const appRouter = t.router({// [...]});
server/routers/_app.tsts
import { initTRPC } from '@trpc/server';import { transformer } from '../../utils/trpc';export const t = initTRPC.create({transformer,});export const appRouter = t.router({// [...]});
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;}