Milad137
Milad137
TtRPC
Created by Martin on 10/3/2024 in #❓-help
Using superjson transformer makes data empty
superjson serializes the payload into two subkeys json and meta. if json key is not present in your payload, it can not deserialize it and returns undefined. just create a custom transformer to account for that.
import SuperJSON, { type SuperJSONResult } from "superjson";

export const transformer = {
input: {
/**
* This function runs **on the client** before sending the data to the server.
*/
serialize: SuperJSON.serialize,
/**
* This function runs **on the server** to transform the data before it is passed to the resolver
*/
deserialize: (obj: SuperJSONResult) => {
if ("json" in obj) {
return SuperJSON.deserialize(obj);
} else {
return SuperJSON.deserialize({ json: obj });
}
},
},
output: {
/**
* This function runs **on the server** before sending the data to the client.
*/
serialize: SuperJSON.serialize,
/**
* This function runs **only on the client** to transform the data sent from the server.
*/
deserialize: SuperJSON.deserialize,
},
};
import SuperJSON, { type SuperJSONResult } from "superjson";

export const transformer = {
input: {
/**
* This function runs **on the client** before sending the data to the server.
*/
serialize: SuperJSON.serialize,
/**
* This function runs **on the server** to transform the data before it is passed to the resolver
*/
deserialize: (obj: SuperJSONResult) => {
if ("json" in obj) {
return SuperJSON.deserialize(obj);
} else {
return SuperJSON.deserialize({ json: obj });
}
},
},
output: {
/**
* This function runs **on the server** before sending the data to the client.
*/
serialize: SuperJSON.serialize,
/**
* This function runs **only on the client** to transform the data sent from the server.
*/
deserialize: SuperJSON.deserialize,
},
};
assign this transformer in
import { initTRPC } from "@trpc/server";

initTRPC.create({
transformer,
});
import { initTRPC } from "@trpc/server";

initTRPC.create({
transformer,
});
3 replies