Skip to content
Source
Sign inSign up

webhookAddress.ts

32 lines1.0 KB

1import type { ScheduledTaskWebhookEndpoint } from "@t3tools/contracts";
2import { isLocalLoopbackHost } from "@t3tools/shared/hostClassification";
3​
4export interface WebhookAddress {
5 readonly address: string;
6 readonly copyable: boolean;
7 readonly note: string | null;
8}
9​
10export function webhookAddress(
11 endpoint: ScheduledTaskWebhookEndpoint,
12 httpBaseUrl: string | null,
13): WebhookAddress {
14 if (endpoint.url !== null) {
15 return { address: endpoint.url, copyable: true, note: null };
16 }
17 if (httpBaseUrl === null) {
18 return {
19 address: endpoint.path,
20 copyable: false,
21 note: "Link this environment to T3 Connect for a public URL.",
22 };
23 }
24 const url = new URL(endpoint.path, httpBaseUrl);
25 return {
26 address: url.href,
27 copyable: true,
28 note: isLocalLoopbackHost(url.hostname)
29 ? "Only this computer can call this address. Link T3 Connect for a public URL."
30 : "Works wherever this environment's address is reachable, for example over Tailscale or your own proxy. Link T3 Connect for a public URL.",
31 };