feat(core-trpc): add typed React tRPC client + getQueryClient

This commit is contained in:
2026-05-05 08:46:24 +02:00
parent a3b72f5a7f
commit 8d80361785
5 changed files with 33 additions and 7 deletions

View File

@@ -15,9 +15,4 @@ export const appRouter = router({
navigation: navigationRouter,
});
export type AppRouter = {
auth: AuthRouter;
blog: BlogRouter;
marketingPages: MarketingPagesRouter;
navigation: NavigationRouter;
};
export type AppRouter = typeof appRouter;

View File

@@ -16,6 +16,7 @@
"@trpc/client": "^11.0.0",
"@trpc/react-query": "^11.0.0",
"@trpc/server": "^11.0.0",
"@trpc/tanstack-react-query": "^11.1.0",
"@tanstack/react-query": "^5.66.0",
"react": "^19.0.0",
"superjson": "^2.2.1"

View File

@@ -0,0 +1,6 @@
"use client";
import { createTRPCContext } from "@trpc/tanstack-react-query";
import type { AppRouter } from "@repo/core-api";
export const { TRPCProvider, useTRPC } = createTRPCContext<AppRouter>();

View File

@@ -1 +1,3 @@
export {};
export { useTRPC, TRPCProvider } from "./client";
export { getQueryClient } from "./query-client";
export type { AppRouter } from "@repo/core-api";

View File

@@ -0,0 +1,22 @@
import { QueryClient } from "@tanstack/react-query";
let clientQueryClient: QueryClient | undefined;
const defaultOptions = {
queries: {
staleTime: 30 * 1000,
refetchOnWindowFocus: false,
},
};
export function getQueryClient(): QueryClient {
if (typeof window === "undefined") {
// Server: always create a new instance per request
return new QueryClient({ defaultOptions });
}
// Browser: singleton
if (!clientQueryClient) {
clientQueryClient = new QueryClient({ defaultOptions });
}
return clientQueryClient;
}