Openstatus www.openstatus.dev
at 4c0f4c00a38753a5d0dfd7e7b7b7706dec6f1503 46 lines 1.6 kB view raw
1import type { HTTPBatchLinkOptions, HTTPHeaders, TRPCLink } from "@trpc/client"; 2import { httpBatchLink } from "@trpc/client"; 3 4import type { AppRouter } from "@openstatus/api"; 5import superjson from "superjson"; 6 7const getBaseUrl = () => { 8 if (typeof window !== "undefined") return ""; 9 if (process.env.VERCEL_ENV === "production") 10 return "https://www.openstatus.dev"; 11 if (process.env.VERCEL_URL) return `https://${process.env.VERCEL_URL}`; // Vercel 12 return "http://localhost:3000"; // Local dev 13}; 14 15const lambdas = ["stripeRouter", "emailRouter"]; 16 17export const endingLink = (opts?: { 18 headers?: HTTPHeaders | (() => HTTPHeaders | Promise<HTTPHeaders>); 19}) => 20 ((runtime) => { 21 const sharedOpts = { 22 headers: opts?.headers, // REMINDER: fails when trying to `getTotalActiveMonitors()` 23 transformer: superjson, 24 // biome-ignore lint/suspicious/noExplicitAny: FIXME: remove any 25 } satisfies Partial<HTTPBatchLinkOptions<any>>; 26 27 const edgeLink = httpBatchLink({ 28 ...sharedOpts, 29 url: `${getBaseUrl()}/api/trpc/edge`, 30 })(runtime); 31 const lambdaLink = httpBatchLink({ 32 ...sharedOpts, 33 url: `${getBaseUrl()}/api/trpc/lambda`, 34 })(runtime); 35 36 return (ctx) => { 37 const path = ctx.op.path.split(".") as [string, ...string[]]; 38 const endpoint = lambdas.includes(path[0]) ? "lambda" : "edge"; 39 40 const newCtx = { 41 ...ctx, 42 op: { ...ctx.op, path: path.join(".") }, 43 }; 44 return endpoint === "edge" ? edgeLink(newCtx) : lambdaLink(newCtx); 45 }; 46 }) satisfies TRPCLink<AppRouter>;