-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for decimal.js. Support for Prisma.Decimal. #158
Support for decimal.js. Support for Prisma.Decimal. #158
Conversation
Hi @michaeljonsampson! Thanks for opening this PR. When you opened the issue, I somehow thought that To keep bundle size down, SuperJSON needs to stay as lean as possible. I've adapted your changes into #159, which adds a test for Decimal.js and mentions how to integrate in the README. |
Just a heads up for anyone using superjson with tRPC Data Transformers, make sure to put the Decimal.js I'd recommend making a |
Hi @michaelhays ... do you have a minimal reproduction of your superjson transformer with decimal.js support? I'd love to use it one of our projects ... |
Sure, here are the relevant files for a Next.js project: src/utils/superjson.tsimport Decimal from 'decimal.js'
import superjson from 'superjson'
superjson.registerCustom<Decimal, string>(
{
isApplicable: (v): v is Decimal => Decimal.isDecimal(v),
serialize: (v) => v.toJSON(),
deserialize: (v) => new Decimal(v),
},
'decimal.js',
)
export default superjson src/utils/trpc.tsimport type { AppRouter } from '@/server/appRouter'
import { createTRPCNext } from '@trpc/next'
import superjson from './superjson'
export const trpc = createTRPCNext<AppRouter>({
config() {
return {
transformer: superjson,
links: [...],
}
},
}) src/server/trpc.tsimport { initTRPC, TRPCError } from '@trpc/server'
import superjson from '../utils/superjson'
import type { Context } from './context'
const t = initTRPC.context<Context>().create({ transformer: superjson })
export const publicProcedure = t.procedure |
Thank you for the code examples. The workaround for Decimal.js works with trpc, but how do I register custom when using it with the next-superjson-plugin (swc)? I'm unable to pass Decimal properties as props from a Server Component to a Client Component. |
You need to execute the |
Yes, I did try this. I added the registerCustom function on the server component level (at layout.tsx) and inside a client component (inside a useEffect of a ClientComponent and outside). Adding it to the client component seemed to do nothing, but adding it on the server component threw the same error. I'm not sure if there's a way to run a function before the build even begins, with either webpack or SWC, that might be a good place to add this. |
Did you find a solution? |
@pedro757 Nope, just reverted back to using |
I was able to make it work, setting the See the CodeServer Component// page.tsx
import { Temporal } from "temporal-polyfill";
import Button from "./client";
import SuperJSON from "superjson";
import PlainDate, { startOfMonth, weekNumber } from 'plain-date'
SuperJSON.registerCustom<Temporal.PlainDate, string>(
{
isApplicable: (v): v is Temporal.PlainDate =>
v instanceof Temporal.PlainDate ||
(typeof v === "string" &&
RegExp(
/^(19[0-9]{2}|2[0-9]{3})-(0[1-9]|1[012])-([123]0|[012][1-9]|31)$/,
).test(v)),
serialize: (v) => v.toJSON(),
deserialize: (v) => Temporal.PlainDate.from(v),
},
"PlainDate",
)
export default function Home() {
return (
<main className="flex min-h-screen flex-col items-center justify-between p-24">
<Button date={new Temporal.PlainDate(2023, 1, 1)} data-superjson />
</main>
);
} With that I receive my custom type in the client component |
Allows using Prisma Decimal type in getServerSideProps.
This fixes #152