twenty-one/frontend/trpc.ts

25 lines
561 B
TypeScript

import { createTRPCProxyClient, httpLink, createWSClient, wsLink, splitLink } from "@trpc/client"
import type { AppRouter } from "../backend"
const wsUrl = new URL(location.href)
wsUrl.protocol = wsUrl.protocol.replace("http", "ws")
wsUrl.pathname = "/ws"
wsUrl.hash = ""
const wsClient = createWSClient({
url: wsUrl.href
})
export const trpcClient = createTRPCProxyClient<AppRouter>({
links: [
splitLink({
condition: o => o.type === "subscription",
true: wsLink({
client: wsClient
}),
false: httpLink({
url: "/trpc"
})
})
]
})