import * as chalk from 'chalk'; import {command, help, namespace, option, param} from 'oo-cli'; import {die} from '../../lib/die'; import {formatError} from '../../lib/formatError'; import {Rivendell} from '../../lib/Rivendell'; import Webhook = Rivendell.Webhook; import InstallationResolutionType = Rivendell.InstallationResolutionType; @namespace('directory') export class ListFunctionsCommand { @param @help('The App ID (e.g. my_app)') public appId!: string; @param @help('The Tracker ID of the installed account') public trackerId!: string; @option('a') @help('The availability zone that will be targeted (default: us)') private availability: string = ''; @command('list-functions') @help('List the functions exposed by an app installation') public async listFunctions() { try { const appInstallation = await Rivendell.fetchAppInstallation(this.appId, this.trackerId, this.availability); const webhooks = await Rivendell.searchWebhooks({ appInstallId: appInstallation.id, includeInternal: true, includeDisabled: true }, this.availability); const functionWebhooks = webhooks.filter((w) => { return !w.attributes.dataSyncId; }); this.render(functionWebhooks); } catch (e: any) { die(formatError(e)); } } private render(webhooks: Webhook[]) { if (webhooks.length === 0) { console.log(chalk.yellow(`${this.appId} install for ${this.trackerId} does not have functions.`)); } else { const longest = Math.max(...webhooks.map((webhook) => webhook.url.length)); webhooks.forEach((webhook) => { const attrs = webhook.attributes!; const caveats = []; const resolutionInfo = this.installationResolutionText( attrs.installationResolutionType, attrs.installationResolutionKey ); if (resolutionInfo) { caveats.push(resolutionInfo); } if (attrs.internal) { caveats.push('internal'); } if (!attrs.enabled) { caveats.push('disabled'); } if (caveats.length > 0) { console.log(webhook.url.padEnd(longest + 2), `(${caveats.join(', ')})`); } else { console.log(webhook.url); } }); } } private installationResolutionText(type: InstallationResolutionType, key: string) { switch (type) { case InstallationResolutionType.HEADER: return `required HTTP Header with name '${key}' and the value set to public tracking id`; case InstallationResolutionType.QUERY_PARAM: return `required URL query parameter with name '${key}' and the value set to public tracking id`; case InstallationResolutionType.JSON_BODY_FIELD: return `required JSON body field that matches JSONPath '${key}' and the value set to public tracking id and HTTP Header set to 'Content-Type: application/json'`; default: return null; } } }