データトランスフォーマー
非公式ベータ版翻訳
このページは 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;}