feat(core-trpc): add typed React tRPC client + getQueryClient
This commit is contained in:
6
packages/core-trpc/src/client.ts
Normal file
6
packages/core-trpc/src/client.ts
Normal 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>();
|
||||
@@ -1 +1,3 @@
|
||||
export {};
|
||||
export { useTRPC, TRPCProvider } from "./client";
|
||||
export { getQueryClient } from "./query-client";
|
||||
export type { AppRouter } from "@repo/core-api";
|
||||
|
||||
22
packages/core-trpc/src/query-client.ts
Normal file
22
packages/core-trpc/src/query-client.ts
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user