import { mutationSpec, parseWithAccount } from '../lib/api.ts' import { granolaRequest } from '../lib/client.ts' import { echo, mutationMode, preview } from '../lib/guard.ts' import { WEBHOOK_ID_PATTERN } from '../lib/validate.ts' import { findWebhookEndpoint } from '../lib/webhooks.ts' /** * Delete a webhook endpoint (`DELETE /v1/webhook-endpoints/{id}`); deliveries stop immediately. * Cannot be undone (create a new endpoint to resume; it gets a new signing secret). * WRITE: previews unless `confirm: true`; the preview shows the endpoint that would be deleted. * * @param input.account - Account name (secret `granola-`). Required. @param input.secretName - Secret override. * @param input.webhookId - `whe_…` id. Required. * @param input.dryRun - Preview only. @param input.confirm - `true` to delete. * @returns Preview `{ dryRun: true, current, request }`, or `{ executed: true, result: { id, deleted: true } }` * * @example * import deleteWebhook from 'kody:@charlesmatte/granola/webhooks/delete' * * const preview = await deleteWebhook({ account: 'work', webhookId: 'whe_2mKr8fQxLp7Ta3' }) */ export default async function deleteGranolaWebhook(input: unknown) { const p = parseWithAccount<{ webhookId: string; dryRun?: boolean; confirm?: boolean }>( input, { webhookId: { type: 'string', required: true, pattern: WEBHOOK_ID_PATTERN }, ...mutationSpec }, 'webhooks/delete', ) const mode = mutationMode(p) const path = `/v1/webhook-endpoints/${p.webhookId}` const target = `webhook ${p.webhookId}` if (!mode.execute) { const current = await findWebhookEndpoint(p.acct, p.webhookId).catch(() => null) return preview(p.acct, 'webhooks/delete', target, mode, { method: 'DELETE', path }, { current: current ?? 'not visible to this key (the delete would likely 404)' }) } const res = await granolaRequest(p.acct, { method: 'DELETE', path, retries: 1 }) return { dryRun: false, executed: true, echo: echo(p.acct, 'webhooks/delete', target, mode), result: res.data } }