Open History to see the last commit
1import type { ScheduledTaskWebhookEndpoint } from "@t3tools/contracts";
2import { isLocalLoopbackHost } from "@t3tools/shared/hostClassification";
3
4/**
5 * Where a sender can call a webhook task, as a client shows it. With T3
6 * Connect the server returns a public URL; without it the path is resolved on
7 * the address this client reaches the environment at.
8 */
9export interface WebhookAddress {
10 /** The URL to give a sender, or the bare path when no address is known. */
11 readonly address: string;
12 /** Whether `address` is a full URL a sender can call. */
13 readonly copyable: boolean;
14 /** One line on who can reach `address`; null for a T3 Connect URL. */
15 readonly note: string | null;
16}
17
18export function webhookAddress(
19 endpoint: ScheduledTaskWebhookEndpoint,
20 httpBaseUrl: string | null,
21): WebhookAddress {
22 if (endpoint.url !== null) {
23 return { address: endpoint.url, copyable: true, note: null };
24 }
25 if (httpBaseUrl === null) {
26 return {
27 address: endpoint.path,
28 copyable: false,
29 note: "Link this environment to T3 Connect for a public URL.",
30 };
31 }
32 const url = new URL(endpoint.path, httpBaseUrl);
33 return {
34 address: url.href,
35 copyable: true,
36 note: isLocalLoopbackHost(url.hostname)
37 ? "Only this computer can call this address. Link T3 Connect for a public URL."
38 : "Works wherever this environment's address is reachable, for example over Tailscale or your own proxy. Link T3 Connect for a public URL.",
39 };
40}