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({ 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 }), }) ] })