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

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

非公式ベータ版翻訳

このページは PageTurner AI で翻訳されました(ベータ版)。プロジェクト公式の承認はありません。 エラーを見つけましたか? 問題を報告 →

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

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,
});

3. httpLink()wsLink()などへの追加

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への追加

ここではXSSを軽減するためにparsestringifyを使用します。

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,
});

4. httpLink()wsLink()などへの追加

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