@hasna/events 0.1.5 → 0.1.6

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.
Files changed (2) hide show
  1. package/dist/commander.js +20 -14
  2. package/package.json +1 -1
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.6",
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",