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 transparently use, e.g., standard Date/Map/Sets over the wire between the server and client. That is, you can return any of these types from your API-resolver and use them in the client without having to recreate the objects from JSON.
How to
1. Install
bash
bash
2. Add to your initTRPC
routers/router/_app.tsts
routers/router/_app.tsts
3. Add to httpLink(), wsLink(), etc
TypeScript will guide you to where you need to add
transformeras soon as you've added it on theinitTRPC-object
createTRPCClient():
src/app/_trpc/client.tsts
src/app/_trpc/client.tsts
Using devalue
Devalue works like superjson, but focus in performance and compact payloads, but at the cost of a less human readable body.
How to
1. Install
bash
bash
2. Add to utils/trpc.ts
Here we use parse and stringify as they mitigate XSS.
utils/trpc.tsts
utils/trpc.tsts
3. Add to your initTRPC
server/routers/_app.tsts
server/routers/_app.tsts
4. Add to httpLink(), wsLink(), etc
TypeScript will guide you to where you need to add
transformeras soon as you've added it on theinitTRPC-object
createTRPCClient():
src/app/_trpc/client.tsts
src/app/_trpc/client.tsts
Different transformers for upload and download
If a transformer should only be used for one direction or different transformers should be used for upload and download (e.g., for performance reasons), you can provide individual transformers for upload and download. Make sure you use the same combined transformer everywhere.
DataTransformer interface
ts
ts