diff --git a/packages/cli/src/cli/commands/sites/webhooks.ts b/packages/cli/src/cli/commands/sites/webhooks.ts new file mode 100644 index 00000000..e3f5a1a2 --- /dev/null +++ b/packages/cli/src/cli/commands/sites/webhooks.ts @@ -0,0 +1,38 @@ +import inquirer from "inquirer"; +import ora from "ora"; +import AddOnApiHelper from "../../../lib/addonApiHelper"; +import { errorHandler } from "../../exceptions"; + +async function configurePreferredWebhookEvents(siteId: string) { + // Fetch available events + const availableEventsSpinner = ora("Fetching available events...").start(); + const availableEvents = + await AddOnApiHelper.fetchAvailableWebhookEvents(siteId); + availableEventsSpinner.succeed("Fetched available events"); + + // Prompt user to select events + const { selectedEvents } = await inquirer.prompt([ + { + type: "checkbox", + name: "selectedEvents", + message: + "Select events to receive notifications for. Select none to receive notifications for all events.", + choices: availableEvents, + }, + ]); + + if (selectedEvents.length === 0) { + console.info( + "No events selected. Your webhook will receive notifications for all events.", + ); + } + + // Update events for the site + const updateEventsSpinner = ora("Updating preferred events...").start(); + await AddOnApiHelper.updateSiteConfig(siteId, { + preferredEvents: selectedEvents, + }); + updateEventsSpinner.succeed("Updated preferred events"); +} + +export default errorHandler(configurePreferredWebhookEvents); diff --git a/packages/cli/src/cli/index.ts b/packages/cli/src/cli/index.ts index e5b5a416..dfaab14b 100755 --- a/packages/cli/src/cli/index.ts +++ b/packages/cli/src/cli/index.ts @@ -34,6 +34,7 @@ import { SITE_EXAMPLES, updateSiteConfig, } from "./commands/sites/site"; +import configurePreferredWebhookEvents from "./commands/sites/webhooks"; import { createToken, listTokens, @@ -552,6 +553,19 @@ yargs(hideBin(process.argv)) id: args.id as string, limit: args.limit as number, }), + ) + .command( + "preferred-events ", + "Set preferred webhook events for a given site. Your webhook will only receive notifications for events that you specify.", + (yargs) => { + yargs.strictCommands().positional("", { + describe: "ID of the site for which you want to configure.", + demandOption: true, + type: "string", + }); + }, + async (args) => + configurePreferredWebhookEvents(String(args.id)), ); }, ) diff --git a/packages/cli/src/lib/addonApiHelper.ts b/packages/cli/src/lib/addonApiHelper.ts index 65e60d62..eb839db1 100644 --- a/packages/cli/src/lib/addonApiHelper.ts +++ b/packages/cli/src/lib/addonApiHelper.ts @@ -430,15 +430,17 @@ class AddOnApiHelper { url, webhookUrl, webhookSecret, + preferredEvents, }: { url?: string; webhookUrl?: string; webhookSecret?: string; + preferredEvents?: string[]; }, ): Promise { const idToken = await this.getIdToken(); - const configuredWebhook = webhookUrl || webhookSecret; + const configuredWebhook = webhookUrl || webhookSecret || preferredEvents; await axios.patch( `${(await getApiConfig()).SITE_ENDPOINT}/${id}`, @@ -448,6 +450,7 @@ class AddOnApiHelper { webhookConfig: { ...(webhookUrl && { webhookUrl: webhookUrl }), ...(webhookSecret && { webhookSecret: webhookSecret }), + ...(preferredEvents && { preferredEvents }), }, }), }, @@ -486,6 +489,21 @@ class AddOnApiHelper { return resp.data as WebhookDeliveryLog[]; } + + static async fetchAvailableWebhookEvents(siteId: string) { + const idToken = await this.getIdToken(); + + const resp = await axios.get( + `${(await getApiConfig()).SITE_ENDPOINT}/${siteId}/availableWebhookEvents`, + { + headers: { + Authorization: `Bearer ${idToken}`, + }, + }, + ); + + return resp.data as string[]; + } } export default AddOnApiHelper;