@zapier/zapier-sdk-cli 0.9.0 → 0.11.0

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.
@@ -1,11 +1,15 @@
1
1
  import chalk from "chalk";
2
2
  import type { z } from "zod";
3
+ import util from "util";
3
4
  // These functions are internal to SDK, implementing basic formatting fallback
4
5
  // TODO: Consider exposing these utilities or implementing proper CLI formatting
5
6
 
6
7
  interface FormattedItem {
7
8
  title: string;
8
- subtitle?: string;
9
+ id?: string;
10
+ key?: string;
11
+ description?: string;
12
+ data?: unknown; // Optional: if provided, use formatJsonOutput instead of details
9
13
  details: Array<{
10
14
  text: string;
11
15
  style: "normal" | "dim" | "accent" | "warning" | "success";
@@ -25,17 +29,34 @@ function getOutputSchema(schema: unknown): unknown {
25
29
  return (schema as { _def?: { outputSchema?: unknown } })?._def?.outputSchema;
26
30
  }
27
31
 
32
+ // ============================================================================
33
+ // JSON Formatting
34
+ // ============================================================================
35
+
36
+ export function formatJsonOutput(data: unknown): void {
37
+ // Don't print anything for undefined results (commands that just perform actions)
38
+ if (data === undefined) {
39
+ return;
40
+ }
41
+
42
+ // Use util.inspect for colored output
43
+ console.log(
44
+ util.inspect(data, { colors: true, depth: null, breakLength: 80 }),
45
+ );
46
+ }
47
+
28
48
  // ============================================================================
29
49
  // Generic Schema-Driven Formatter
30
50
  // ============================================================================
31
51
 
32
52
  export function formatItemsFromSchema(
33
- inputSchema: z.ZodType,
53
+ functionInfo: { inputSchema: z.ZodType; outputSchema?: z.ZodType },
34
54
  items: unknown[],
35
55
  startingNumber: number = 0,
36
56
  ): void {
37
- // Get the output schema and its format metadata
38
- const outputSchema = getOutputSchema(inputSchema);
57
+ // Get the output schema from function info or fall back to input schema output schema
58
+ const outputSchema =
59
+ functionInfo.outputSchema || getOutputSchema(functionInfo.inputSchema);
39
60
  if (!outputSchema) {
40
61
  // Fallback to generic formatting if no output schema
41
62
  formatItemsGeneric(items, startingNumber);
@@ -51,25 +72,35 @@ export function formatItemsFromSchema(
51
72
 
52
73
  // Format each item using the schema metadata
53
74
  items.forEach((item, index) => {
54
- formatSingleItem(item, startingNumber + index, formatMeta);
75
+ const formatted = formatMeta.format(item);
76
+ formatSingleItem(formatted, startingNumber + index);
55
77
  });
56
78
  }
57
79
 
58
- function formatSingleItem(
59
- item: unknown,
60
- itemNumber: number,
61
- formatMeta: FormatMetadata,
62
- ): void {
63
- // Get the formatted item from the format function
64
- const formatted = formatMeta.format(item);
65
-
66
- // Build the main title line
80
+ function formatSingleItem(formatted: FormattedItem, itemNumber: number): void {
81
+ // Build the main title line with optional subtitle
67
82
  let titleLine = `${chalk.gray(`${itemNumber + 1}.`)} ${chalk.cyan(formatted.title)}`;
68
- if (formatted.subtitle) {
69
- titleLine += ` ${chalk.gray(formatted.subtitle)}`;
83
+
84
+ // Generate subtitle from id or key
85
+ if (formatted.id) {
86
+ titleLine += ` ${chalk.gray(`(ID: ${formatted.id})`)}`;
87
+ } else if (formatted.key) {
88
+ titleLine += ` ${chalk.gray(`(${formatted.key})`)}`;
70
89
  }
71
90
  console.log(titleLine);
72
91
 
92
+ // Show description if available
93
+ if (formatted.description) {
94
+ console.log(` ${chalk.dim(formatted.description)}`);
95
+ }
96
+
97
+ // If data is provided, use JSON formatting instead of details
98
+ if (formatted.data !== undefined) {
99
+ formatJsonOutput(formatted.data);
100
+ console.log(); // Empty line between items
101
+ return;
102
+ }
103
+
73
104
  // Format detail lines
74
105
  for (const detail of formatted.details) {
75
106
  const styledText = applyStyle(detail.text, detail.style);
@@ -95,27 +126,31 @@ function applyStyle(value: string, style: string): string {
95
126
  }
96
127
  }
97
128
 
129
+ function convertGenericItemToFormattedItem(item: unknown): FormattedItem {
130
+ const itemObj = item as {
131
+ title?: string;
132
+ name?: string;
133
+ key?: string;
134
+ id?: string;
135
+ description?: string;
136
+ };
137
+
138
+ return {
139
+ title: itemObj.title || itemObj.name || itemObj.key || itemObj.id || "Item",
140
+ id: itemObj.id,
141
+ key: itemObj.key,
142
+ description: itemObj.description,
143
+ details: [],
144
+ };
145
+ }
146
+
98
147
  function formatItemsGeneric(
99
148
  items: unknown[],
100
149
  startingNumber: number = 0,
101
150
  ): void {
102
- // Fallback formatting for items without schema metadata
151
+ // Convert generic items to FormattedItem and use formatSingleItem
103
152
  items.forEach((item, index) => {
104
- const itemObj = item as {
105
- title?: string;
106
- name?: string;
107
- key?: string;
108
- id?: string;
109
- description?: string;
110
- };
111
- const name =
112
- itemObj.title || itemObj.name || itemObj.key || itemObj.id || "Item";
113
- console.log(
114
- `${chalk.gray(`${startingNumber + index + 1}.`)} ${chalk.cyan(name)}`,
115
- );
116
- if (itemObj.description) {
117
- console.log(` ${chalk.dim(itemObj.description)}`);
118
- }
119
- console.log();
153
+ const formatted = convertGenericItemToFormattedItem(item);
154
+ formatSingleItem(formatted, startingNumber + index);
120
155
  });
121
156
  }