본문 바로가기
버전: 11.x

데이터 변환기

비공식 베타 번역

이 페이지는 PageTurner AI로 번역되었습니다(베타). 프로젝트 공식 승인을 받지 않았습니다. 오류를 발견하셨나요? 문제 신고 →

응답 데이터와 입력 인자를 직렬화할 수 있습니다. 변환기는 서버와 클라이언트 양쪽에 추가해야 합니다.

superjson 사용하기

SuperJSON를 사용하면 서버와 클라이언트 간 통신에서 Date/Map/Set 같은 표준 객체를 투명하게 사용할 수 있습니다. 즉, API 리졸버에서 이러한 타입을 반환하고 JSON에서 객체를 재생성하지 않고도 클라이언트에서 사용할 수 있습니다.

방법

1. 설치

bash
yarn add superjson
bash
yarn add superjson

2. initTRPC에 추가

server/routers/_app.ts
ts
import { initTRPC } from '@trpc/server';
import superjson from 'superjson';
 
export const t = initTRPC.create({
transformer: superjson,
});
server/routers/_app.ts
ts
import { initTRPC } from '@trpc/server';
import superjson from 'superjson';
 
export const t = initTRPC.create({
transformer: superjson,
});

TypeScript는 initTRPC 객체에 추가하는 즉시 transformer를 추가해야 할 위치를 안내해 줍니다.

createTRPCClient():

src/app/_trpc/client.ts
ts
import { createTRPCClient, httpLink } from '@trpc/client';
import type { AppRouter } from './server';
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, httpLink } from '@trpc/client';
import type { AppRouter } from './server';
import superjson from 'superjson';
 
export const client = createTRPCClient<AppRouter>({
links: [
httpLink({
url: 'http://localhost:3000',
transformer: superjson,
}),
],
});

devalue 사용하기

Devalue는 superjson과 유사하게 동작하며 성능과 작은 페이로드 크기에 최적화되어 있지만, 대신 가독성이 낮은 본문(body)을 가지게 됩니다.

방법

1. 설치

bash
yarn add devalue
bash
yarn add devalue

2. utils/trpc.ts에 추가

여기서는 parsestringify를 사용합니다. 이들은 XSS 방지 기능을 제공합니다.

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

TypeScript는 initTRPC 객체에 추가하는 즉시 transformer를 추가해야 할 위치를 안내해 줍니다.

createTRPCClient():

src/app/_trpc/client.ts
ts
import { createTRPCClient, httpLink } 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, httpLink } 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;
}