MicProtocol / apps /admin /src /lib /trpc.ts
Benaah's picture
Upload folder using huggingface_hub
fecff63 verified
Raw History Blame Contribute Delete
1.63 kB
import { createTRPCReact, httpBatchLink } from "@trpc/react-query";
import type { inferRouterInputs, inferRouterOutputs } from "@trpc/server";
import type { AppRouter } from "@micprotocol/api";
export const trpc = createTRPCReact<AppRouter>();
export function getTrpcClient() {
return trpc.createClient({
links: [
httpBatchLink({
url: "/trpc",
headers() {
const headers: Record<string, string> = {};
// 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<AppRouter>;
export type RouterOutputs = inferRouterOutputs<AppRouter>;