Skip to content

Commit

Permalink
Decimal Parser in trpc
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego Romero committed Nov 20, 2023
1 parent 7732824 commit 623ddd7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions apps/expo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@trpc/client": "^10.1.0",
"@trpc/react-query": "^10.1.0",
"@trpc/server": "^10.1.0",
"decimal.js": "^10.4.3",
"expo": "^48.0.6",
"expo-auth-session": "^4.0.3",
"expo-random": "^13.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/trpc.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { initTRPC, TRPCError } from "@trpc/server";
import { type Context } from "./context";
import superjson from "superjson";
import { transformer } from "../transformer";

const t = initTRPC.context<Context>().create({
transformer: superjson,
transformer: transformer,
errorFormatter({ shape }) {
return shape;
},
Expand Down
15 changes: 13 additions & 2 deletions packages/api/transformer.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
import superjson from "superjson";
export const transformer = superjson;
import { 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 const transformer = SuperJSON;
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 623ddd7

Please sign in to comment.