3.2.2 • Published 1 year ago

trpc-transformer v3.2.2

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

✨ tRPC-transformer

NPM version License Downloads Sponsor the author

A simple tRPC transformer combining superjson with Decimal.js.

Installation

yarn add trpc-transformer

or

npm i trpc-transformer

Usage

  1. Add it to your initTRPC:
import { initTRPC } from '@trpc/server';
import transformer from 'trpc-transformer';

export const t = initTRPC.create({ transformer });
  1. ...and to your tRPC client:
import transformer from 'trpc-transformer';

const client = trpc.createClient({
  links: [
    httpBatchLink({
      // ...
      transformer,
    }),
  ],
});

Benefits

Assuming you have an appRouter.ts like this on the server-side:

import { initTRPC } from '@trpc/server';
import transformer from 'trpc-transformer';
import prisma from '~/lib/server/prisma';

const t = initTRPC.create({ transformer });

export const appRouter = t.router({
  users: t.procedure.query(() =>
    prisma.user.findMany({
      select: {
        id: true,
        name: true,
        createdAt: true,
        accounts: {
          select: { iban: true, balance: true },
        },
      },
    })
  )
});

export type AppRouter = typeof appRouter;

...then, you'll have your data correctly serialized/deserialized on the client-side:

import Decimal from 'decimal.js';
import { trpc } from '~/lib/client/trpc';

const usersQuery = trpc.users.useQuery();

console.log('Account createdAt is Date:', usersQuery.data?.[0].createdAt instanceof Date); // 👈 true
console.log('Account balance is Decimal.js:', usersQuery.data?.[0].accounts[0].balance instanceof Decimal); // 👈 true

!NOTE The above example assumes a Next.js project with a lib/client/trpc.ts file like this:

import { createTRPCReact } from '@trpc/react-query';
import type { AppRouter } from 'swapp.ro.server';
export const trpc = createTRPCReact<AppRouter>();

...and a layout.tsx file like this:

'use client';

import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { httpBatchLink } from '@trpc/client';
import { useState } from 'react';
import transformer from 'trpc-transformer';
import { trpc } from '~/lib/client/trpc';

export default function DynamicLayout({ children }: Readonly<{ children: React.ReactNode }>) {
  const [queryClient] = useState(() => new QueryClient());
  const [trpcClient] = useState(() =>
    trpc.createClient({
      links: [
        httpBatchLink({
          url: 'your api url',
          transformer,
        }),
      ],
    })
  );

  return (
    <trpc.Provider client={trpcClient} queryClient={queryClient}>
      <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
    </trpc.Provider>
  );
}

Learn more

See trpc.io/docs/data-transformers and github.com/blitz-js/superjson.

License

The ISC License.

3.2.2

1 year ago

3.2.1

1 year ago

3.2.0

1 year ago

3.1.1

1 year ago

3.1.0

1 year ago

3.0.0

2 years ago

2.4.1

2 years ago

2.3.2

2 years ago

2.4.0

2 years ago

2.3.4

2 years ago

2.3.3

2 years ago

2.3.6

2 years ago

2.3.5

2 years ago

2.3.8

2 years ago

2.3.7

2 years ago

2.3.9

2 years ago

2.3.0

2 years ago

2.2.3

2 years ago

2.3.1

2 years ago

2.2.1

2 years ago

2.2.0

2 years ago

2.1.4

3 years ago

2.2.2

2 years ago

2.1.6

2 years ago

2.1.5

3 years ago

2.1.8

2 years ago

2.1.7

2 years ago

2.1.9

2 years ago

2.1.10

2 years ago

2.1.2

3 years ago

2.1.1

3 years ago

2.1.3

3 years ago

2.0.3

3 years ago

2.0.2

3 years ago

2.0.4

3 years ago

2.1.0

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.0.1

3 years ago