メインコンテンツへスキップ
バージョン: 11.x

データトランスフォーマー

レスポンスデータと入力引数をシリアル化できます。トランスフォーマーはサーバーとクライアントの両方に追加する必要があります。

superjsonの使用

SuperJSONを使用すると、サーバーとクライアント間で標準のDate/Map/Setなどを透過的に使用できます。つまり、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
}),
],
});

アップロードとダウンロードのための異なるトランスフォーマー

トランスフォーマーを一方向のみに使用する場合、またはアップロードとダウンロードで異なるトランスフォーマーを使用する場合(パフォーマンス上の理由など)、アップロードとダウンロードに対して個別のトランスフォーマーを提供できます。同じ組み合わせのトランスフォーマーをすべてで使用してください。

方法

ここでは、アップロードにはsuperjsonを、ダウンロードにはdevalueを使用しています。devalueははるかに高速ですが、サーバーで使用するには安全ではありません。

1. インストール

bash
yarn add superjson devalue
bash
yarn add superjson devalue

2. utils/trpc.tsに追加

utils/trpc.ts
ts
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.ts
ts
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.ts
ts
import { initTRPC } from '@trpc/server';
import { transformer } from '../../utils/trpc';
export const t = initTRPC.create({
transformer,
});
export const appRouter = t.router({
// [...]
});
server/routers/_app.ts
ts
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;
}