1
+ <% if (addOnEnabled .tRPC ) { % >
2
+ import { QueryClient } from " @tanstack/react-query" ;
3
+ import superjson from " superjson" ;
4
+ import { createTRPCClient , httpBatchStreamLink } from " @trpc/client" ;
5
+ import { createTRPCOptionsProxy } from " @trpc/tanstack-react-query" ;
6
+
7
+ import { TRPCProvider } from " @/integrations/trpc/react" ;
8
+
9
+ import type { TRPCRouter } from " @/integrations/trpc/router" ;
10
+
11
+ function getUrl () {
12
+ const base = (() => {
13
+ if (typeof window !== " undefined" ) return " " ;
14
+ return ` http://localhost:${ process .env .PORT ?? 3000 }` ;
15
+ })();
16
+ return base + "/api/trpc";
17
+ }
18
+
19
+ export const trpcClient = createTRPCClient<TRPCRouter>({
20
+ links: [
21
+ httpBatchStreamLink({
22
+ transformer: superjson,
23
+ url: getUrl(),
24
+ }),
25
+ ],
26
+ });
27
+
28
+ const queryClient = new QueryClient({
29
+ defaultOptions: {
30
+ dehydrate: { serializeData: superjson.serialize },
31
+ hydrate: { deserializeData: superjson.deserialize },
32
+ },
33
+ });
34
+
35
+ const serverHelpers = createTRPCOptionsProxy({
36
+ client: trpcClient,
37
+ queryClient: queryClient,
38
+ });
39
+
40
+ export function getContext() {
41
+ return {
42
+ queryClient,
43
+ trpc: serverHelpers,
44
+ };
45
+ }
46
+
47
+ export function Provider({ children }: { children: React.ReactNode }) {
48
+ return (
49
+ <TRPCProvider trpcClient={trpcClient} queryClient={queryClient}>
50
+ {children}
51
+ </TRPCProvider>
52
+ );
53
+ }
54
+ <% } else { %>
55
+ import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
56
+
57
+ const queryClient = new QueryClient()
58
+
59
+ export function getContext() {
60
+ return {
61
+ queryClient,
62
+ }
63
+ }
64
+
65
+ export function Provider({ children }: { children: React.ReactNode }) {
66
+ return (
67
+ <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
68
+ )
69
+ }
70
+ <% } %>
0 commit comments