feat(mobile): create and copy webhook automations (#15087)
This commit is contained in:
1 parent
b070e52d3e
commit
aee889b0e0
6 files changed
+247
-14
No files matched your search
@@ -0,0 +1,36 @@
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
|
||||
import { webhookAddress } from "./webhookAddress.ts";
|
||||
|
||||
const path = "/api/hooks/scheduled-task%3Ahook/token";
|
||||
const endpoint = (url: string | null) => ({ path, url, hasSecret: false });
|
||||
|
||||
describe("webhookAddress", () => {
|
||||
it("uses the T3 Connect URL when the server has one", () => {
|
||||
expect(webhookAddress(endpoint("https://relay.t3.codes/v1/hooks/k/t/x"), null)).toEqual({
|
||||
address: "https://relay.t3.codes/v1/hooks/k/t/x",
|
||||
copyable: true,
|
||||
note: null,
|
||||
});
|
||||
});
|
||||
|
||||
it("builds a direct URL on the environment's address without T3 Connect", () => {
|
||||
const result = webhookAddress(endpoint(null), "https://mac.tail1234.ts.net/");
|
||||
expect(result.address).toBe(`https://mac.tail1234.ts.net${path}`);
|
||||
expect(result.copyable).toBe(true);
|
||||
expect(result.note).toContain("Tailscale");
|
||||
});
|
||||
|
||||
it("says only this computer can call a loopback address", () => {
|
||||
const result = webhookAddress(endpoint(null), "http://127.0.0.1:3773/");
|
||||
expect(result.copyable).toBe(true);
|
||||
expect(result.note).toContain("Only this computer");
|
||||
});
|
||||
|
||||
it("falls back to the path when the address is unknown", () => {
|
||||
expect(webhookAddress(endpoint(null), null)).toMatchObject({
|
||||
address: path,
|
||||
copyable: false,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,40 @@
|
||||
import type { ScheduledTaskWebhookEndpoint } from "@t3tools/contracts";
|
||||
import { isLocalLoopbackHost } from "@t3tools/shared/hostClassification";
|
||||
|
||||
/**
|
||||
* Where a sender can call a webhook task, as a client shows it. With T3
|
||||
* Connect the server returns a public URL; without it the path is resolved on
|
||||
* the address this client reaches the environment at.
|
||||
*/
|
||||
export interface WebhookAddress {
|
||||
/** The URL to give a sender, or the bare path when no address is known. */
|
||||
readonly address: string;
|
||||
/** Whether `address` is a full URL a sender can call. */
|
||||
readonly copyable: boolean;
|
||||
/** One line on who can reach `address`; null for a T3 Connect URL. */
|
||||
readonly note: string | null;
|
||||
}
|
||||
|
||||
export function webhookAddress(
|
||||
endpoint: ScheduledTaskWebhookEndpoint,
|
||||
httpBaseUrl: string | null,
|
||||
): WebhookAddress {
|
||||
if (endpoint.url !== null) {
|
||||
return { address: endpoint.url, copyable: true, note: null };
|
||||
}
|
||||
if (httpBaseUrl === null) {
|
||||
return {
|
||||
address: endpoint.path,
|
||||
copyable: false,
|
||||
note: "Link this environment to T3 Connect for a public URL.",
|
||||
};
|
||||
}
|
||||
const url = new URL(endpoint.path, httpBaseUrl);
|
||||
return {
|
||||
address: url.href,
|
||||
copyable: true,
|
||||
note: isLocalLoopbackHost(url.hostname)
|
||||
? "Only this computer can call this address. Link T3 Connect for a public URL."
|
||||
: "Works wherever this environment's address is reachable, for example over Tailscale or your own proxy. Link T3 Connect for a public URL.",
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user