데이터 변환기
비공식 베타 번역
이 페이지는 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. httpLink(), wsLink() 등에 추가
TypeScript는
initTRPC객체에 추가하는 즉시transformer를 추가해야 할 위치를 안내해 줍니다.
createTRPCClient():
src/app/_trpc/client.tstsimport { 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.tstsimport { 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. 설치
bashyarn add superjson devalue
bashyarn add superjson devalue
2. utils/trpc.ts에 추가
여기서는 parse와 stringify를 사용합니다. 이들은 XSS 방지 기능을 제공합니다.
utils/trpc.tstsimport { parse, stringify } from 'devalue';// [...]export const transformer = {deserialize: (object: any) => parse(object),serialize: (object: any) => stringify(object),};
utils/trpc.tstsimport { parse, stringify } from 'devalue';// [...]export const transformer = {deserialize: (object: any) => parse(object),serialize: (object: any) => stringify(object),};
3. initTRPC에 추가
server/routers/_app.tstsimport { initTRPC } from '@trpc/server';import { transformer } from '../../utils/trpc';export const t = initTRPC.create({transformer,});
server/routers/_app.tstsimport { initTRPC } from '@trpc/server';import { transformer } from '../../utils/trpc';export const t = initTRPC.create({transformer,});
4. httpLink(), wsLink() 등에 추가
TypeScript는
initTRPC객체에 추가하는 즉시transformer를 추가해야 할 위치를 안내해 줍니다.
createTRPCClient():
src/app/_trpc/client.tstsimport { 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.tstsimport { 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 인터페이스
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;}