import { createTRPCReact, httpBatchLink } from "@trpc/react-query"; import type { inferRouterInputs, inferRouterOutputs } from "@trpc/server"; import type { AppRouter } from "@micprotocol/api"; export const trpc = createTRPCReact(); export function getTrpcClient() { return trpc.createClient({ links: [ httpBatchLink({ url: "/trpc", headers() { const headers: Record = {}; // Dev-only: bypass auth via x-user-id header when DEV_AUTH_BYPASS_ENABLED=true if (process.env.NODE_ENV !== "production") { const devUserId = typeof localStorage !== "undefined" ? localStorage.getItem("micprotocol_admin_user_id") : null; if (devUserId) headers["x-user-id"] = devUserId; const workspaceId = typeof localStorage !== "undefined" ? localStorage.getItem("micprotocol_admin_workspace_id") : null; if (workspaceId) headers["x-workspace-id"] = workspaceId; } if (typeof document !== "undefined") { const match = document.cookie .split("; ") .find((c) => c.startsWith("csrf-token=")); if (match) headers["x-csrf-token"] = match.split("=")[1]; } return headers; }, fetch(url, options) { return fetch(url, { ...options, credentials: "include" }); }, }), ], }); } export type RouterInputs = inferRouterInputs; export type RouterOutputs = inferRouterOutputs;