import type { TriggerDefinition } from "../types" import * as z from "zod/mini" const HTTP_PRESENTATION_CONFIG_SCHEMA = z.object({ scope: z.prefault(z.enum(["trigger", "automation", "project"]), "trigger"), }) export const httpRequestTriggerDefinition = { getPrimary: ({ appOrigin, automationId, config, hookSlot, projectId, scopePath, }) => ({ copyable: true, value: getHttpTriggerEndpoint({ appOrigin, automationId, hookSlot, projectId, scope: HTTP_PRESENTATION_CONFIG_SCHEMA.parse(config).scope, scopePath, }), }), icon: "webhook", name: "HTTP request", type: "http.request", } as const satisfies TriggerDefinition export type HttpTriggerScope = "trigger" | "automation" | "project" export interface HttpTriggerEndpointOptions { appOrigin: string automationId: string hookSlot: number projectId: string scope: HttpTriggerScope scopePath?: readonly number[] } /** * Returns the public URL for one deployed HTTP trigger. * * @param options - Public origin and trigger identity. */ export function getHttpTriggerEndpoint(options: HttpTriggerEndpointOptions) { return new URL( `/x/${getHttpTriggerEndpointKey(options)}/`, options.appOrigin, ).toString() } /** * Returns the durable endpoint identity for one configured HTTP scope. * * @param options - Trigger and owning resource identity. */ export function getHttpTriggerEndpointKey( options: Omit, ) { if (options.scope === "project") return options.projectId if (options.scope === "automation") return options.automationId return `${options.automationId}:${[ ...(options.scopePath ?? []), options.hookSlot, ].join(".")}` }