-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrpc.ts
33 lines (30 loc) · 998 Bytes
/
trpc.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { createTRPCNext } from "@trpc/next";
import { httpBatchLink, splitLink } from "@trpc/client";
import { AppRouter } from "./server/router";
import { httpSseLink } from "trpc-sse-link";
const getBaseUrl = () => {
if (typeof window !== "undefined") return ""; // browser should use relative url
if (process.env["VERCEL_URL"]) return `https://${process.env["VERCEL_URL"]}`; // SSR should use vercel url
return `http://localhost:${process.env.PORT ?? 3000}`; // dev SSR should use localhost
};
export const trpcClient = createTRPCNext<AppRouter>({
config() {
return {
links: [
// Send things using `useSubscription` via SSE
splitLink({
condition(op) {
return op.type === "subscription";
},
true: httpSseLink({
baseUrl: `${getBaseUrl()}/api/trpc`,
}),
false: httpBatchLink({
url: `${getBaseUrl()}/api/trpc`,
}),
}),
],
};
},
ssr: false,
});