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