Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/openstatusHQ/openstatus. ๐ซ Status page with uptime monitoring & API monitoring as code ๐ซ openstatus.dev
bun drizzle-orm monitoring monitoring-as-code nextjs observability on-call open-source shadcn-ui status-page statuspage synthetic-monitoring tinybird turso uptime uptime-checker uptime-monitor
Something went wrong. Try again.
2.5 kB ยท 65 lines
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566import type { AppRouter } from "@openstatus/api";import type { HTTPBatchLinkOptions, HTTPHeaders, TRPCLink } from "@trpc/client";import { httpBatchLink } from "@trpc/client";import type { TRPCError } from "@trpc/server";import superjson from "superjson";
/** * Shared onError handler for tRPC route handlers. */export function createOnError(label: string) { return ({ error }: { error: TRPCError }) => { console.log(`Error in tRPC handler (${label})`); console.error(error); };}
/** * Filter out requests that don't come from our tRPC clients. * Our server and client links always set `x-trpc-source`. * This is a convention filter for bots/crawlers, not a security boundary โ * the header is trivially spoofable. Auth is enforced by protectedProcedure. */export function guardTRPCSource(req: Request): Response | null { const source = req.headers.get("x-trpc-source"); if (source !== "server" && source !== "client") { return new Response(null, { status: 401 }); } return null;}
/** * Vercel populates VERCEL_URL with a bare host (e.g. "my-app.vercel.app"). If * a developer wrote a full URL into `.env` by mistake, this strips the scheme * so `https://${host}` doesn't yield `https://https://โฆ` and crash fetch with * `getaddrinfo EAI_AGAIN https`. Works whether or not the prefix is present. */function stripScheme(url: string): string { if (url.startsWith("https://")) return url.slice("https://".length); if (url.startsWith("http://")) return url.slice("http://".length); return url;}
const getBaseUrl = () => { if (typeof window !== "undefined") return ""; // Note: status-page has its own tRPC API routes if (process.env.VERCEL_URL) return `https://${stripScheme(process.env.VERCEL_URL)}`; return "http://localhost:3000"; // Local dev and Docker (internal calls)};
// The whole tRPC surface is served from a single Node.js endpoint โ there is// no longer an Edge/Node split, so all calls go to one link.export const endingLink = (opts?: { fetch?: typeof fetch; headers?: HTTPHeaders | (() => HTTPHeaders | Promise<HTTPHeaders>); }): TRPCLink<AppRouter> => (runtime) => httpBatchLink({ headers: opts?.headers, fetch: opts?.fetch, transformer: superjson, url: `${getBaseUrl()}/api/trpc/lambda`, // oxlint-disable-next-line typescript/no-explicit-any -- FIXME: remove any } satisfies Partial<HTTPBatchLinkOptions<any>>)(runtime);