@hasna/events 0.1.5 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli/index.js CHANGED
@@ -564,10 +564,19 @@ function parseGlobalArgs(argv) {
564
564
  return { json, dir, rest };
565
565
  }
566
566
  function takeOption(args, name) {
567
+ const equalsPrefix = `${name}=`;
568
+ const equalsIndex = args.findIndex((arg) => arg.startsWith(equalsPrefix));
569
+ if (equalsIndex !== -1) {
570
+ const value2 = args[equalsIndex]?.slice(equalsPrefix.length);
571
+ args.splice(equalsIndex, 1);
572
+ return value2;
573
+ }
567
574
  const index = args.indexOf(name);
568
575
  if (index === -1)
569
576
  return;
570
577
  const value = args[index + 1];
578
+ if (value === undefined)
579
+ throw new Error(`${name} requires a value`);
571
580
  args.splice(index, 2);
572
581
  return value;
573
582
  }
@@ -580,7 +589,7 @@ function takeFlag(args, name) {
580
589
  }
581
590
  function takeMany(args, name) {
582
591
  const values = [];
583
- while (args.includes(name)) {
592
+ while (args.includes(name) || args.some((arg) => arg.startsWith(`${name}=`))) {
584
593
  const value = takeOption(args, name);
585
594
  if (value !== undefined)
586
595
  values.push(value);
@@ -710,6 +719,10 @@ async function runEventsCli(argv = process.argv.slice(2), options = {}) {
710
719
  printWebhooksHelp(options);
711
720
  return;
712
721
  }
722
+ if (tail.includes("--help") || tail.includes("-h")) {
723
+ printWebhooksHelp(options);
724
+ return;
725
+ }
713
726
  await handleWebhooks(client, command, tail, parsed, options);
714
727
  return;
715
728
  }
@@ -718,6 +731,10 @@ async function runEventsCli(argv = process.argv.slice(2), options = {}) {
718
731
  printEventsHelp(options);
719
732
  return;
720
733
  }
734
+ if (tail.includes("--help") || tail.includes("-h")) {
735
+ printEventsHelp(options);
736
+ return;
737
+ }
721
738
  await handleEvents(client, command, tail, parsed, options);
722
739
  return;
723
740
  }
@@ -735,6 +752,7 @@ async function handleWebhooks(client, command, tail, parsed, options) {
735
752
  const retryBackoffMs = numberOption(takeOption(args, "--retry-backoff-ms"));
736
753
  const disabled = takeFlag(args, "--disabled");
737
754
  const headerValues = takeMany(args, "--header");
755
+ const commandArgs = takeMany(args, "--arg");
738
756
  const redactions = takeMany(args, "--redact");
739
757
  const filters = parseFilter(args);
740
758
  const target = args[0];
@@ -755,7 +773,7 @@ async function handleWebhooks(client, command, tail, parsed, options) {
755
773
  if (transport === "webhook") {
756
774
  channel.webhook = { url: target, secret, headers: parseHeaders(headerValues), timeoutMs };
757
775
  } else if (transport === "command") {
758
- channel.command = { command: target, args: args.slice(1), timeoutMs };
776
+ channel.command = { command: target, args: [...args.slice(1), ...commandArgs], timeoutMs };
759
777
  } else {
760
778
  throw new Error(`Transport ${transport} is reserved for future use and cannot be added yet`);
761
779
  }
package/dist/commander.js CHANGED
@@ -594,9 +594,15 @@ function print(value, json, text) {
594
594
  else
595
595
  console.log(text);
596
596
  }
597
+ function hasJsonOption(options) {
598
+ return Boolean(options?.json || options?.opts?.().json || options?.optsWithGlobals?.().json || options?.parent?.opts?.().json || options?.parent?.optsWithGlobals?.().json);
599
+ }
600
+ function wantsJson(actionOptions, command) {
601
+ return hasJsonOption(actionOptions) || hasJsonOption(command);
602
+ }
597
603
  function registerWebhookCommands(program, options) {
598
604
  const webhooks = program.command(options.webhooksCommandName ?? "webhooks").description("Manage Hasna event webhook subscriptions");
599
- webhooks.command("add").description("Add or replace a webhook or command subscription").argument("<target>", "Webhook URL or command binary").requiredOption("--id <id>", "Subscription/channel identifier").option("--transport <kind>", "Transport kind: webhook or command", "webhook").option("--name <name>", "Display name").option("--type <pattern>", "Event type filter, e.g. todos.task.*").option("--source <pattern>", "Event source filter").option("--subject <pattern>", "Event subject filter").option("--severity <pattern>", "Event severity filter").option("--secret <secret>", "Webhook HMAC secret").option("--header <name=value...>", "Webhook header", collectValues, []).option("--arg <arg...>", "Command argument", collectValues, []).option("--timeout-ms <ms>", "Transport timeout in milliseconds", parseNumber).option("--retry-attempts <n>", "Maximum delivery attempts", parseNumber).option("--retry-backoff-ms <ms>", "Initial retry backoff in milliseconds", parseNumber).option("--redact <path...>", "Event field path to redact before delivery", collectValues, []).option("--disabled", "Create channel disabled", false).option("-j, --json", "Print JSON output", false).action(async (target, actionOptions) => {
605
+ webhooks.command("add").description("Add or replace a webhook or command subscription").argument("<target>", "Webhook URL or command binary").requiredOption("--id <id>", "Subscription/channel identifier").option("--transport <kind>", "Transport kind: webhook or command", "webhook").option("--name <name>", "Display name").option("--type <pattern>", "Event type filter, e.g. todos.task.*").option("--source <pattern>", "Event source filter").option("--subject <pattern>", "Event subject filter").option("--severity <pattern>", "Event severity filter").option("--secret <secret>", "Webhook HMAC secret").option("--header <name=value...>", "Webhook header", collectValues, []).option("--arg <arg...>", "Command argument", collectValues, []).option("--timeout-ms <ms>", "Transport timeout in milliseconds", parseNumber).option("--retry-attempts <n>", "Maximum delivery attempts", parseNumber).option("--retry-backoff-ms <ms>", "Initial retry backoff in milliseconds", parseNumber).option("--redact <path...>", "Event field path to redact before delivery", collectValues, []).option("--disabled", "Create channel disabled", false).option("-j, --json", "Print JSON output", false).action(async (target, actionOptions, command) => {
600
606
  const timestamp = new Date().toISOString();
601
607
  const channel = {
602
608
  id: actionOptions.id,
@@ -617,11 +623,11 @@ function registerWebhookCommands(program, options) {
617
623
  throw new Error(`Transport ${actionOptions.transport} is reserved for future use and cannot be added yet`);
618
624
  }
619
625
  const saved = await createClient(options).addChannel(channel);
620
- print(sanitizeChannelForOutput(saved), Boolean(actionOptions.json), `Added ${saved.transport} channel ${saved.id}`);
626
+ print(sanitizeChannelForOutput(saved), wantsJson(actionOptions, command), `Added ${saved.transport} channel ${saved.id}`);
621
627
  });
622
- webhooks.command("list").description("List configured subscriptions").option("-j, --json", "Print JSON output", false).action(async (actionOptions) => {
628
+ webhooks.command("list").description("List configured subscriptions").option("-j, --json", "Print JSON output", false).action(async (actionOptions, command) => {
623
629
  const channels = await createClient(options).listChannels();
624
- if (actionOptions.json) {
630
+ if (wantsJson(actionOptions, command)) {
625
631
  console.log(JSON.stringify(sanitizeChannelsForOutput(channels), null, 2));
626
632
  return;
627
633
  }
@@ -633,11 +639,11 @@ function registerWebhookCommands(program, options) {
633
639
  console.log(`${channel.id} ${channel.enabled ? "enabled" : "disabled"} ${channel.transport} ${channel.webhook?.url ?? channel.command?.command ?? channel.transport}`);
634
640
  }
635
641
  });
636
- webhooks.command("remove").description("Remove a subscription").argument("<id>", "Subscription/channel identifier").option("-j, --json", "Print JSON output", false).action(async (id, actionOptions) => {
642
+ webhooks.command("remove").description("Remove a subscription").argument("<id>", "Subscription/channel identifier").option("-j, --json", "Print JSON output", false).action(async (id, actionOptions, command) => {
637
643
  const removed = await createClient(options).removeChannel(id);
638
- print({ removed }, Boolean(actionOptions.json), removed ? `Removed ${id}` : `Channel not found: ${id}`);
644
+ print({ removed }, wantsJson(actionOptions, command), removed ? `Removed ${id}` : `Channel not found: ${id}`);
639
645
  });
640
- webhooks.command("test").description("Send a test event to one subscription").argument("<id>", "Subscription/channel identifier").option("--type <type>", "Event type", "events.test").option("--subject <subject>", "Event subject").option("--message <message>", "Event message", "Hasna events test delivery").option("--data <json>", "Event data JSON object").option("-j, --json", "Print JSON output", false).action(async (id, actionOptions) => {
646
+ webhooks.command("test").description("Send a test event to one subscription").argument("<id>", "Subscription/channel identifier").option("--type <type>", "Event type", "events.test").option("--subject <subject>", "Event subject").option("--message <message>", "Event message", "Hasna events test delivery").option("--data <json>", "Event data JSON object").option("-j, --json", "Print JSON output", false).action(async (id, actionOptions, command) => {
641
647
  const result = await createClient(options).testChannel(id, {
642
648
  source: options.source,
643
649
  type: actionOptions.type,
@@ -645,13 +651,13 @@ function registerWebhookCommands(program, options) {
645
651
  message: actionOptions.message,
646
652
  data: parseJsonObject(actionOptions.data, { test: true })
647
653
  });
648
- print(result, Boolean(actionOptions.json), `${result.status}: ${result.channelId}`);
654
+ print(result, wantsJson(actionOptions, command), `${result.status}: ${result.channelId}`);
649
655
  });
650
656
  return webhooks;
651
657
  }
652
658
  function registerEventCommands(program, options) {
653
659
  const events = program.command(options.eventsCommandName ?? "events").description("Emit, list, and replay Hasna events");
654
- events.command("emit").description("Emit an event from this app").argument("<type>", "Event type").option("--source <source>", "Event source override").option("--subject <subject>", "Event subject").option("--severity <severity>", "Event severity", "info").option("--message <message>", "Event message").option("--dedupe-key <key>", "Dedupe key").option("--data <json>", "Event data JSON object").option("--metadata <json>", "Event metadata JSON object").option("--no-deliver", "Record without delivering").option("--no-dedupe", "Allow duplicate id/dedupeKey events").option("-j, --json", "Print JSON output", false).action(async (type, actionOptions) => {
660
+ events.command("emit").description("Emit an event from this app").argument("<type>", "Event type").option("--source <source>", "Event source override").option("--subject <subject>", "Event subject").option("--severity <severity>", "Event severity", "info").option("--message <message>", "Event message").option("--dedupe-key <key>", "Dedupe key").option("--data <json>", "Event data JSON object").option("--metadata <json>", "Event metadata JSON object").option("--no-deliver", "Record without delivering").option("--no-dedupe", "Allow duplicate id/dedupeKey events").option("-j, --json", "Print JSON output", false).action(async (type, actionOptions, command) => {
655
661
  const result = await createClient(options).emit({
656
662
  source: actionOptions.source ?? options.source,
657
663
  type,
@@ -662,9 +668,9 @@ function registerEventCommands(program, options) {
662
668
  data: parseJsonObject(actionOptions.data, {}),
663
669
  metadata: parseJsonObject(actionOptions.metadata, {})
664
670
  }, { deliver: actionOptions.deliver, dedupe: actionOptions.dedupe });
665
- print(result, Boolean(actionOptions.json), `${result.deduped ? "Deduped" : "Emitted"} ${result.event.id} to ${result.deliveries.length} channel(s)`);
671
+ print(result, wantsJson(actionOptions, command), `${result.deduped ? "Deduped" : "Emitted"} ${result.event.id} to ${result.deliveries.length} channel(s)`);
666
672
  });
667
- events.command("list").description("List recorded events").option("--source <source>", "Filter by source").option("--type <type>", "Filter by type").option("--limit <n>", "Limit results", parseNumber).option("-j, --json", "Print JSON output", false).action(async (actionOptions) => {
673
+ events.command("list").description("List recorded events").option("--source <source>", "Filter by source").option("--type <type>", "Filter by type").option("--limit <n>", "Limit results", parseNumber).option("-j, --json", "Print JSON output", false).action(async (actionOptions, command) => {
668
674
  let rows = await createClient(options).listEvents();
669
675
  if (actionOptions.source)
670
676
  rows = rows.filter((event) => event.source === actionOptions.source);
@@ -672,7 +678,7 @@ function registerEventCommands(program, options) {
672
678
  rows = rows.filter((event) => event.type === actionOptions.type);
673
679
  if (actionOptions.limit)
674
680
  rows = rows.slice(-actionOptions.limit);
675
- if (actionOptions.json) {
681
+ if (wantsJson(actionOptions, command)) {
676
682
  console.log(JSON.stringify(rows, null, 2));
677
683
  return;
678
684
  }
@@ -683,14 +689,14 @@ function registerEventCommands(program, options) {
683
689
  for (const event of rows)
684
690
  console.log(`${event.time} ${event.id} ${event.source} ${event.type} ${event.severity}`);
685
691
  });
686
- events.command("replay").description("Replay recorded events").option("--id <id>", "Replay one event id").option("--source <source>", "Filter by source").option("--type <type>", "Filter by type").option("--dry-run", "Preview without delivery", false).option("-j, --json", "Print JSON output", false).action(async (actionOptions) => {
692
+ events.command("replay").description("Replay recorded events").option("--id <id>", "Replay one event id").option("--source <source>", "Filter by source").option("--type <type>", "Filter by type").option("--dry-run", "Preview without delivery", false).option("-j, --json", "Print JSON output", false).action(async (actionOptions, command) => {
687
693
  const result = await createClient(options).replay({
688
694
  eventId: actionOptions.id,
689
695
  source: actionOptions.source,
690
696
  type: actionOptions.type,
691
697
  dryRun: actionOptions.dryRun
692
698
  });
693
- print(result, Boolean(actionOptions.json), `Replayed ${result.events.length} event(s), ${result.deliveries.length} delivery result(s)`);
699
+ print(result, wantsJson(actionOptions, command), `Replayed ${result.events.length} event(s), ${result.deliveries.length} delivery result(s)`);
694
700
  });
695
701
  return events;
696
702
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hasna/events",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "description": "Shared event envelopes, local subscriptions, and webhook delivery for Hasna open-source apps",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",