level-up/frontend/trpc.ts
2025-04-12 21:51:01 +02:00

34 lines
1 KiB
TypeScript

import { createTRPCClient, httpLink, httpSubscriptionLink, loggerLink, splitLink } from "@trpc/client"
import superjson from "superjson"
import type { AppRouter } from "../backend"
import { EventSourcePolyfill } from "event-source-polyfill"
import { nanoid } from "nanoid"
const crewToken = window.localStorage.getItem("crew-token")
const sessionId = nanoid(64)
const headers = {
"auio-crew-token": crewToken ?? "",
"auio-session-id": sessionId,
}
export const trpcClient = createTRPCClient<AppRouter>({
links: [
loggerLink(),
splitLink({
condition: op => op.type === "subscription",
true: httpSubscriptionLink({
url: "/trpc",
transformer: superjson,
EventSource: EventSourcePolyfill,
eventSourceOptions: {
headers
}
}),
false: httpLink({
url: "/trpc",
transformer: superjson,
headers
}),
})
]
})