Skip to content

Commit

Permalink
fix: trpc client
Browse files Browse the repository at this point in the history
  • Loading branch information
rharkor committed Mar 20, 2024
1 parent bcd7d0c commit 2c53011
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 35 deletions.
32 changes: 31 additions & 1 deletion apps/app/src/lib/trpc/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@
import SuperJSON from "superjson"

import { type AppRouter } from "@/api/_app"
import { createTRPCReact } from "@trpc/react-query"
import { createTRPCReact, httpBatchLink, loggerLink } from "@trpc/react-query"

import { getUrl } from "./utils"

export const trpc = createTRPCReact<AppRouter>({})

export const trpcClient = trpc.createClient({
transformer: SuperJSON,
links: [
// adds pretty logs to your console in development and logs errors in production
loggerLink({
enabled: (opts) =>
(process.env.NODE_ENV === "development" && typeof window !== "undefined") ||
(opts.direction === "down" && opts.result instanceof Error),
}),
// splitLink({
// condition(op) {
// return op.type === "subscription"
// },
// true: wsLink({
// client: wsClient,
// }),
// false: httpBatchLink({
// url: getUrl(),
// }),
// }),
httpBatchLink({
url: getUrl(),
}),
],
})
35 changes: 3 additions & 32 deletions apps/app/src/lib/trpc/provider.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
"use client"
import React, { useState } from "react"
import { useRouter } from "next/navigation"
import SuperJSON from "superjson"

import { AppRouter } from "@/api/_app"
import { useDictionary } from "@/contexts/dictionary/utils"
import { MutationCache, QueryCache, QueryClient, QueryClientProvider } from "@tanstack/react-query"
import { httpBatchLink, loggerLink, TRPCClientErrorLike } from "@trpc/client"
import { TRPCClientErrorLike } from "@trpc/client"

import { handleMutationError, handleQueryError } from "../utils/client-utils"

import { trpc } from "./client"
import { getUrl } from "./utils"
import { trpc, trpcClient } from "./client"

const testNoDefaultErrorHandling = (query: unknown) =>
typeof query === "object" &&
Expand Down Expand Up @@ -43,34 +41,7 @@ export default function TrpcProvider({ children }: { children: React.ReactNode }
}),
})
)

const [trpcClient] = useState(() =>
trpc.createClient({
transformer: SuperJSON,
links: [
// adds pretty logs to your console in development and logs errors in production
loggerLink({
enabled: (opts) =>
(process.env.NODE_ENV === "development" && typeof window !== "undefined") ||
(opts.direction === "down" && opts.result instanceof Error),
}),
// splitLink({
// condition(op) {
// return op.type === "subscription"
// },
// true: wsLink({
// client: wsClient,
// }),
// false: httpBatchLink({
// url: getUrl(),
// }),
// }),
httpBatchLink({
url: getUrl(),
}),
],
})
)

return (
<trpc.Provider client={trpcClient} queryClient={queryClient}>
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

0 comments on commit 2c53011

Please sign in to comment.