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 ""; if (process.env.VERCEL_ENV === "production") return "https://www.openstatus.dev"; if (process.env.VERCEL_URL) return `https://${process.env.VERCEL_URL}`; // Vercel return "http://localhost:3000"; // Local dev }; const lambdas = ["stripeRouter", "emailRouter"]; export const endingLink = (opts?: { headers?: HTTPHeaders | (() => HTTPHeaders | Promise); }) => ((runtime) => { const sharedOpts = { headers: opts?.headers, // REMINDER: fails when trying to `getTotalActiveMonitors()` 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;