Alex
Alex
Explore posts from servers
TtRPC
Created by Alex on 6/27/2023 in #❓-help
Running TRPC on Vercel with custom serialization fails
This did resolve my issue. thanks for sharring this example.
8 replies
TtRPC
Created by Alex on 6/27/2023 in #❓-help
Running TRPC on Vercel with custom serialization fails
Hi Julius, I will try this. Thanks for sharing.👍
8 replies
TtRPC
Created by Alex on 6/27/2023 in #❓-help
Running TRPC on Vercel with custom serialization fails
It is injected in my singleton client (Using T3 approach) like so:
/**
* A set of typesafe react-query hooks for your tRPC API
*/
export const api = createTRPCNext<AppRouter>({
config() {
initializeSuperJson()
return {
/**
* Transformer used for data de-serialization from the server
* @see https://trpc.io/docs/data-transformers
**/
transformer: superjson,

/**
* Links used to determine request flow from client to server
* @see https://trpc.io/docs/links
* */
links: [
loggerLink({
enabled: (opts) =>
process.env.NODE_ENV === 'development' ||
(opts.direction === 'down' && opts.result instanceof Error),
}),
httpBatchLink({
url: `${getBaseUrl()}/api/trpc`,
}),
],
}
},
/**
* A set of typesafe react-query hooks for your tRPC API
*/
export const api = createTRPCNext<AppRouter>({
config() {
initializeSuperJson()
return {
/**
* Transformer used for data de-serialization from the server
* @see https://trpc.io/docs/data-transformers
**/
transformer: superjson,

/**
* Links used to determine request flow from client to server
* @see https://trpc.io/docs/links
* */
links: [
loggerLink({
enabled: (opts) =>
process.env.NODE_ENV === 'development' ||
(opts.direction === 'down' && opts.result instanceof Error),
}),
httpBatchLink({
url: `${getBaseUrl()}/api/trpc`,
}),
],
}
},
When I run it locally everything works as expected... But when deployed on vercel the custome serialization is not executed. I have added some logging and I can see the injection of custom serialization happening the first time I use a trpc route api.xxxx But the custom serilization does not take place. Any idea where I could start digging?
8 replies
TtRPC
Created by Alex on 6/27/2023 in #❓-help
Running TRPC on Vercel with custom serialization fails
I have implemented a custom serialization for it like so:
import { Money } from 'ts-money'
import { z } from 'zod'

const moneySchema = z.object({
amount: z.number(),
currency: z.union([z.literal('EUR'), z.literal('USD'), z.literal('GBP')]),
})
export const initializeSuperJson = () => {
superjson.registerCustom(
{
isApplicable: (m): m is Money => {
return moneySchema.safeParse(m).success
},
serialize: (m: Money) => {
return { amount: m.amount, currency: m.currency }
},
deserialize: (json: { amount: number; currency: string }) => {
return new Money(json.amount, json.currency)
},
},
'Money',
)
}
import { Money } from 'ts-money'
import { z } from 'zod'

const moneySchema = z.object({
amount: z.number(),
currency: z.union([z.literal('EUR'), z.literal('USD'), z.literal('GBP')]),
})
export const initializeSuperJson = () => {
superjson.registerCustom(
{
isApplicable: (m): m is Money => {
return moneySchema.safeParse(m).success
},
serialize: (m: Money) => {
return { amount: m.amount, currency: m.currency }
},
deserialize: (json: { amount: number; currency: string }) => {
return new Money(json.amount, json.currency)
},
},
'Money',
)
}
8 replies