import type { HTTPBatchLinkOptions, HTTPHeaders, TRPCLink } from "@trpc/client"; import { httpBatchLink } from "@trpc/client"; import type { AppRouter } from "@openstatus/api"; import superjson from "superjson"; const getBaseUrl = () => { if (typeof window !== "undefined") return ""; // Note: dashboard has its own tRPC API routes if (process.env.VERCEL_URL) return "https://app.openstatus.dev"; // Vercel return "http://localhost:3000"; // Local dev and Docker (internal calls) }; const lambdas = ["stripeRouter", "emailRouter", "apiKeyRouter"]; export const endingLink = (opts?: { fetch?: typeof fetch; headers?: HTTPHeaders | (() => HTTPHeaders | Promise); }) => ((runtime) => { const sharedOpts = { headers: opts?.headers, fetch: opts?.fetch, transformer: superjson, // biome-ignore lint/suspicious/noExplicitAny: FIXME: remove any } satisfies Partial>; const edgeLink = httpBatchLink({ ...sharedOpts, url: `${getBaseUrl()}/api/trpc/edge`, })(runtime); const lambdaLink = httpBatchLink({ ...sharedOpts, url: `${getBaseUrl()}/api/trpc/lambda`, })(runtime); return (ctx) => { const path = ctx.op.path.split(".") as [string, ...string[]]; const endpoint = lambdas.includes(path[0]) ? "lambda" : "edge"; const newCtx = { ...ctx, op: { ...ctx.op, path: path.join(".") }, }; return endpoint === "edge" ? edgeLink(newCtx) : lambdaLink(newCtx); }; }) satisfies TRPCLink;