Skip to content

Commit

Permalink
feat: add support for setting preferred webhook events
Browse files Browse the repository at this point in the history
  • Loading branch information
a11rew committed Jun 24, 2024
1 parent 5096041 commit 6021e3c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
38 changes: 38 additions & 0 deletions packages/cli/src/cli/commands/sites/webhooks.ts
Original file line number Diff line number Diff line change
@@ -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);
14 changes: 14 additions & 0 deletions packages/cli/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
SITE_EXAMPLES,
updateSiteConfig,
} from "./commands/sites/site";
import configurePreferredWebhookEvents from "./commands/sites/webhooks";
import {
createToken,
listTokens,
Expand Down Expand Up @@ -552,6 +553,19 @@ yargs(hideBin(process.argv))
id: args.id as string,
limit: args.limit as number,
}),
)
.command(
"preferred-events <id>",
"Set preferred webhook events for a given site. Your webhook will only receive notifications for events that you specify.",
(yargs) => {
yargs.strictCommands().positional("<id>", {
describe: "ID of the site for which you want to configure.",
demandOption: true,
type: "string",
});
},
async (args) =>
configurePreferredWebhookEvents(String(args.id)),
);
},
)
Expand Down
20 changes: 19 additions & 1 deletion packages/cli/src/lib/addonApiHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,15 +430,17 @@ class AddOnApiHelper {
url,
webhookUrl,
webhookSecret,
preferredEvents,
}: {
url?: string;
webhookUrl?: string;
webhookSecret?: string;
preferredEvents?: string[];
},
): Promise<void> {
const idToken = await this.getIdToken();

const configuredWebhook = webhookUrl || webhookSecret;
const configuredWebhook = webhookUrl || webhookSecret || preferredEvents;

await axios.patch(
`${(await getApiConfig()).SITE_ENDPOINT}/${id}`,
Expand All @@ -448,6 +450,7 @@ class AddOnApiHelper {
webhookConfig: {
...(webhookUrl && { webhookUrl: webhookUrl }),
...(webhookSecret && { webhookSecret: webhookSecret }),
...(preferredEvents && { preferredEvents }),
},
}),
},
Expand Down Expand Up @@ -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}`,
},

Check warning

Code scanning / CodeQL

File data in outbound network request Medium

Outbound network request depends on
file data
.
},
);

return resp.data as string[];
}
}

export default AddOnApiHelper;

0 comments on commit 6021e3c

Please sign in to comment.