|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import type { AppRouter } from "@/server/api/root"; |
|
|
import { |
|
|
createWSClient, |
|
|
experimental_formDataLink, |
|
|
httpBatchLink, |
|
|
splitLink, |
|
|
wsLink, |
|
|
} from "@trpc/client"; |
|
|
import { createTRPCNext } from "@trpc/next"; |
|
|
import type { inferRouterInputs, inferRouterOutputs } from "@trpc/server"; |
|
|
import superjson from "superjson"; |
|
|
|
|
|
const getBaseUrl = () => { |
|
|
if (typeof window !== "undefined") return ""; |
|
|
return `http://localhost:${process.env.PORT ?? 3000}`; |
|
|
}; |
|
|
|
|
|
const getWsUrl = () => { |
|
|
if (typeof window === "undefined") return null; |
|
|
|
|
|
const protocol = window.location.protocol === "https:" ? "wss:" : "ws:"; |
|
|
const host = window.location.host; |
|
|
|
|
|
return `${protocol}${host}/drawer-logs`; |
|
|
}; |
|
|
|
|
|
const wsClient = |
|
|
typeof window !== "undefined" |
|
|
? createWSClient({ |
|
|
url: getWsUrl() || "", |
|
|
}) |
|
|
: null; |
|
|
|
|
|
|
|
|
export const api = createTRPCNext<AppRouter>({ |
|
|
config() { |
|
|
return { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
transformer: superjson, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
links: [ |
|
|
splitLink({ |
|
|
condition: (op) => op.type === "subscription", |
|
|
true: wsLink({ |
|
|
client: wsClient!, |
|
|
}), |
|
|
false: splitLink({ |
|
|
condition: (op) => op.input instanceof FormData, |
|
|
true: experimental_formDataLink({ |
|
|
url: `${getBaseUrl()}/api/trpc`, |
|
|
}), |
|
|
false: httpBatchLink({ |
|
|
url: `${getBaseUrl()}/api/trpc`, |
|
|
}), |
|
|
}), |
|
|
}), |
|
|
], |
|
|
}; |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ssr: false, |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export type RouterInputs = inferRouterInputs<AppRouter>; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export type RouterOutputs = inferRouterOutputs<AppRouter>; |
|
|
|