Openstatus www.openstatus.dev
at faccb97887890dd3a67ee3910959f4abcd169d2a 45 lines 1.5 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 const vc = process.env.VERCEL_URL; 10 if (vc) return `https://${vc}`; 11 return "http://localhost:3000"; 12}; 13 14const lambdas = ["stripeRouter", "emailRouter"]; 15 16export const endingLink = (opts?: { 17 headers?: HTTPHeaders | (() => HTTPHeaders | Promise<HTTPHeaders>); 18}) => 19 ((runtime) => { 20 const sharedOpts = { 21 headers: opts?.headers, // REMINDER: fails when trying to `getTotalActiveMonitors()` 22 transformer: superjson, 23 // biome-ignore lint/suspicious/noExplicitAny: FIXME: remove any 24 } satisfies Partial<HTTPBatchLinkOptions<any>>; 25 26 const edgeLink = httpBatchLink({ 27 ...sharedOpts, 28 url: `${getBaseUrl()}/api/trpc/edge`, 29 })(runtime); 30 const lambdaLink = httpBatchLink({ 31 ...sharedOpts, 32 url: `${getBaseUrl()}/api/trpc/lambda`, 33 })(runtime); 34 35 return (ctx) => { 36 const path = ctx.op.path.split(".") as [string, ...string[]]; 37 const endpoint = lambdas.includes(path[0]) ? "lambda" : "edge"; 38 39 const newCtx = { 40 ...ctx, 41 op: { ...ctx.op, path: path.join(".") }, 42 }; 43 return endpoint === "edge" ? edgeLink(newCtx) : lambdaLink(newCtx); 44 }; 45 }) satisfies TRPCLink<AppRouter>;