UNPKG

@swell/cli

Version:

Swell's command line interface/utility

53 lines (52 loc) 1.83 kB
import { titleize } from 'inflection'; /** * Parse comma-separated events string to array * Input: "payment.succeeded,payment.failed" * Output: ["payment.succeeded", "payment.failed"] */ const parseEvents = (eventsInput) => { if (!eventsInput) { return []; } return eventsInput .split(',') .map((e) => e.trim()) .filter(Boolean); }; /** * Validate event format (model.action pattern) * Valid: "product.created", "payment.succeeded" * Invalid: "product", "created", "", ".created", "product." */ const validateEventFormat = (events, onError) => { const invalid = []; for (const event of events) { const parts = event.split('.'); if (parts.length !== 2 || !parts[0] || !parts[1]) { invalid.push(event); } } if (invalid.length > 0) { onError(`Invalid event format: ${invalid.join(', ')}\n\nEvents must follow the 'model.action' pattern (e.g., product.created, payment.succeeded).\n\nExample: swell create webhook my-hook -u https://example.com -e order.created,order.updated -y`, { exit: 1 }); } }; /** * Validate URL format (basic check for protocol) */ const validateUrl = (url, onError) => { try { const parsed = new URL(url); if (!['http:', 'https:'].includes(parsed.protocol)) { throw new Error('Invalid protocol'); } } catch { onError(`Invalid URL format: ${url}\n\nURL must be a valid HTTP or HTTPS endpoint.\n\nExample: swell create webhook my-hook -u https://example.com/webhook -e order.created -y`, { exit: 1 }); } }; /** * Convert name to webhook label * "payment-handler" => "Payment Handler" */ const toWebhookLabel = (value) => titleize(value).replaceAll('-', ' '); export { parseEvents, toWebhookLabel, validateEventFormat, validateUrl, };