Data Transformers

You are able to serialize the response data & input args. The transformers need to be added both to the server and the client.

Using superjson#

SuperJSON allows us to able to transparently use e.g. standard Date/Map/Sets over the wire between the server and client. That means you can return any of these types in your API-resolver and use them in the client without recreating the objects from JSON.

Working Example#

How to#

0. Install#

yarn add superjson

1. Add to createTRPCCLient()#

import superjson from 'superjson';
// [...]
export const client = createTRPCClient<AppRouter>({
// [...]
transformer: superjson,
});

2. Add to API handler#

import superjson from 'superjson';
// [...]
export default trpcNext.createNextApiHandler({
// [...]
transformer: superjson,
});

DataTransformer interface#

type DataTransformer = {
serialize(object: any): any;
deserialize(object: any): any;
};