@zapier/zapier-sdk-cli 0.4.0 ā 0.4.2
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/CHANGELOG.md +8 -0
- package/dist/cli.js +961 -284
- package/dist/src/cli.d.ts +2 -0
- package/dist/src/cli.js +28 -0
- package/dist/src/commands/bundle-code/cli.d.ts +2 -0
- package/dist/src/commands/bundle-code/cli.js +77 -0
- package/dist/src/commands/bundle-code/index.d.ts +5 -0
- package/dist/src/commands/bundle-code/index.js +62 -0
- package/dist/src/commands/bundle-code/schemas.d.ts +24 -0
- package/dist/src/commands/bundle-code/schemas.js +19 -0
- package/dist/src/commands/configPath.d.ts +2 -0
- package/dist/src/commands/configPath.js +9 -0
- package/dist/src/commands/generate-types/cli.d.ts +2 -0
- package/dist/src/commands/generate-types/cli.js +73 -0
- package/dist/src/commands/generate-types/index.d.ts +8 -0
- package/dist/src/commands/generate-types/index.js +273 -0
- package/dist/src/commands/generate-types/schemas.d.ts +18 -0
- package/dist/src/commands/generate-types/schemas.js +11 -0
- package/dist/src/commands/index.d.ts +6 -0
- package/dist/src/commands/index.js +6 -0
- package/dist/src/commands/login.d.ts +2 -0
- package/dist/src/commands/login.js +25 -0
- package/dist/src/commands/logout.d.ts +2 -0
- package/dist/src/commands/logout.js +16 -0
- package/dist/src/commands/mcp.d.ts +2 -0
- package/dist/src/commands/mcp.js +11 -0
- package/dist/src/index.d.ts +0 -0
- package/dist/src/index.js +3 -0
- package/dist/src/utils/api/client.d.ts +15 -0
- package/dist/src/utils/api/client.js +27 -0
- package/dist/src/utils/auth/login.d.ts +2 -0
- package/dist/src/utils/auth/login.js +134 -0
- package/dist/src/utils/cli-generator-utils.d.ts +13 -0
- package/dist/src/utils/cli-generator-utils.js +116 -0
- package/dist/src/utils/cli-generator.d.ts +3 -0
- package/dist/src/utils/cli-generator.js +443 -0
- package/dist/src/utils/constants.d.ts +5 -0
- package/dist/src/utils/constants.js +6 -0
- package/dist/src/utils/getCallablePromise.d.ts +6 -0
- package/dist/src/utils/getCallablePromise.js +14 -0
- package/dist/src/utils/log.d.ts +7 -0
- package/dist/src/utils/log.js +16 -0
- package/dist/src/utils/parameter-resolver.d.ts +14 -0
- package/dist/src/utils/parameter-resolver.js +387 -0
- package/dist/src/utils/schema-formatter.d.ts +2 -0
- package/dist/src/utils/schema-formatter.js +71 -0
- package/dist/src/utils/serializeAsync.d.ts +2 -0
- package/dist/src/utils/serializeAsync.js +16 -0
- package/dist/src/utils/spinner.d.ts +1 -0
- package/dist/src/utils/spinner.js +13 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +5 -3
- package/src/cli.test.ts +15 -0
- package/src/cli.ts +9 -3
- package/src/commands/bundle-code/cli.ts +103 -0
- package/src/commands/bundle-code/index.ts +91 -0
- package/src/commands/bundle-code/schemas.ts +24 -0
- package/src/commands/generate-types/cli.ts +110 -0
- package/src/commands/generate-types/index.ts +365 -0
- package/src/commands/generate-types/schemas.ts +23 -0
- package/src/commands/index.ts +3 -1
- package/src/commands/mcp.ts +14 -0
- package/src/utils/cli-generator-utils.ts +157 -0
- package/src/utils/cli-generator.ts +148 -91
- package/src/utils/parameter-resolver.ts +217 -85
- package/src/utils/schema-formatter.ts +1 -1
- package/tsconfig.json +3 -5
- package/src/commands/whoami.ts +0 -25
- package/src/utils/pager.ts +0 -202
- package/test/cli.test.ts +0 -46
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
import { Command } from "commander";
|
|
2
2
|
import { z } from "zod";
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
ZapierSdk,
|
|
5
|
+
hasResolver,
|
|
6
|
+
isPositional,
|
|
7
|
+
formatErrorMessage,
|
|
8
|
+
ZapierError,
|
|
9
|
+
} from "@zapier/zapier-sdk";
|
|
4
10
|
import { SchemaParameterResolver } from "./parameter-resolver";
|
|
5
|
-
import { createPager } from "./pager";
|
|
6
11
|
import { formatItemsFromSchema } from "./schema-formatter";
|
|
7
12
|
import chalk from "chalk";
|
|
8
13
|
import util from "util";
|
|
14
|
+
import inquirer from "inquirer";
|
|
9
15
|
|
|
10
16
|
// ============================================================================
|
|
11
17
|
// JSON Formatting
|
|
@@ -193,12 +199,10 @@ function createCommandConfig(
|
|
|
193
199
|
// Check if this is a list command for pagination
|
|
194
200
|
const isListCommand = cliCommandName.startsWith("list-");
|
|
195
201
|
const hasPaginationParams = parameters.some(
|
|
196
|
-
(p) => p.name === "
|
|
202
|
+
(p) => p.name === "maxItems" || p.name === "pageSize",
|
|
197
203
|
);
|
|
198
|
-
const
|
|
199
|
-
"
|
|
200
|
-
const shouldUsePaging =
|
|
201
|
-
isListCommand && hasPaginationParams && !hasUserSpecifiedLimit;
|
|
204
|
+
const hasUserSpecifiedMaxItems =
|
|
205
|
+
"maxItems" in options && options.maxItems !== undefined;
|
|
202
206
|
const shouldUseJson = options.json;
|
|
203
207
|
|
|
204
208
|
// Convert CLI args to SDK method parameters
|
|
@@ -216,42 +220,50 @@ function createCommandConfig(
|
|
|
216
220
|
sdk,
|
|
217
221
|
);
|
|
218
222
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
223
|
+
// Special handling for commands that write to files
|
|
224
|
+
const hasOutputFile = resolvedParams.output;
|
|
225
|
+
if (hasOutputFile) {
|
|
226
|
+
// Call the SDK method for file output commands
|
|
227
|
+
await (sdk as any)[sdkMethodName](resolvedParams);
|
|
228
|
+
console.log(
|
|
229
|
+
chalk.green(`ā
${cliCommandName} completed successfully!`),
|
|
230
|
+
);
|
|
231
|
+
console.log(chalk.gray(`Output written to: ${resolvedParams.output}`));
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// Handle paginated list commands with async iteration
|
|
236
|
+
if (
|
|
237
|
+
isListCommand &&
|
|
238
|
+
hasPaginationParams &&
|
|
239
|
+
!shouldUseJson &&
|
|
240
|
+
!hasUserSpecifiedMaxItems
|
|
241
|
+
) {
|
|
242
|
+
// Get the async iterator directly from the SDK method call
|
|
243
|
+
const sdkIterator = (sdk as any)[sdkMethodName](resolvedParams);
|
|
244
|
+
await handlePaginatedListWithAsyncIteration(
|
|
245
|
+
sdkMethodName,
|
|
246
|
+
sdkIterator,
|
|
247
|
+
schema,
|
|
248
|
+
);
|
|
222
249
|
} else {
|
|
223
|
-
// Call the SDK method
|
|
224
|
-
const result
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
if (!hasOutputFile && (shouldUseJson || !isListCommand)) {
|
|
231
|
-
// Use raw JSON if --json flag is specified, otherwise use pretty formatting
|
|
232
|
-
if (shouldUseJson) {
|
|
233
|
-
console.log(JSON.stringify(result, null, 2));
|
|
234
|
-
} else {
|
|
235
|
-
formatJsonOutput(result);
|
|
236
|
-
}
|
|
237
|
-
} else if (!hasOutputFile) {
|
|
238
|
-
// Format list results nicely (non-paginated)
|
|
250
|
+
// Call the SDK method and handle non-paginated results
|
|
251
|
+
const result = await (sdk as any)[sdkMethodName](resolvedParams);
|
|
252
|
+
const items = result?.data ? result.data : result;
|
|
253
|
+
|
|
254
|
+
if (shouldUseJson) {
|
|
255
|
+
console.log(JSON.stringify(items, null, 2));
|
|
256
|
+
} else if (isListCommand) {
|
|
239
257
|
formatNonPaginatedResults(
|
|
240
|
-
|
|
241
|
-
resolvedParams.
|
|
242
|
-
|
|
258
|
+
items,
|
|
259
|
+
resolvedParams.maxItems,
|
|
260
|
+
hasUserSpecifiedMaxItems,
|
|
243
261
|
shouldUseJson,
|
|
244
262
|
schema,
|
|
245
263
|
sdkMethodName,
|
|
246
264
|
);
|
|
247
|
-
} else
|
|
248
|
-
|
|
249
|
-
console.log(
|
|
250
|
-
chalk.green(`ā
${cliCommandName} completed successfully!`),
|
|
251
|
-
);
|
|
252
|
-
console.log(
|
|
253
|
-
chalk.gray(`Output written to: ${resolvedParams.output}`),
|
|
254
|
-
);
|
|
265
|
+
} else {
|
|
266
|
+
formatJsonOutput(items);
|
|
255
267
|
}
|
|
256
268
|
}
|
|
257
269
|
} catch (error) {
|
|
@@ -270,11 +282,15 @@ function createCommandConfig(
|
|
|
270
282
|
} catch {
|
|
271
283
|
console.error(chalk.red("Error:"), error.message);
|
|
272
284
|
}
|
|
285
|
+
} else if (error instanceof ZapierError) {
|
|
286
|
+
// Handle SDK errors with clean, user-friendly messages using our formatter
|
|
287
|
+
const formattedMessage = formatErrorMessage(error);
|
|
288
|
+
console.error(chalk.red("ā Error:"), formattedMessage);
|
|
273
289
|
} else {
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
error instanceof Error ? error.message : "Unknown error"
|
|
277
|
-
);
|
|
290
|
+
// Handle other errors
|
|
291
|
+
const errorMessage =
|
|
292
|
+
error instanceof Error ? error.message : "Unknown error";
|
|
293
|
+
console.error(chalk.red("ā Error:"), errorMessage);
|
|
278
294
|
}
|
|
279
295
|
process.exit(1);
|
|
280
296
|
}
|
|
@@ -323,6 +339,10 @@ function addCommand(
|
|
|
323
339
|
|
|
324
340
|
if (param.type === "boolean") {
|
|
325
341
|
command.option(flags.join(", "), param.description);
|
|
342
|
+
} else if (param.type === "array") {
|
|
343
|
+
// For arrays, use variadic syntax to collect multiple values
|
|
344
|
+
const flagSignature = flags.join(", ") + ` <values...>`;
|
|
345
|
+
command.option(flagSignature, param.description, param.default);
|
|
326
346
|
} else {
|
|
327
347
|
const flagSignature = flags.join(", ") + ` <${param.type}>`;
|
|
328
348
|
command.option(flagSignature, param.description, param.default);
|
|
@@ -406,69 +426,106 @@ function convertValue(value: any, type: CliParameter["type"]): any {
|
|
|
406
426
|
// Pagination Handlers
|
|
407
427
|
// ============================================================================
|
|
408
428
|
|
|
409
|
-
async function
|
|
429
|
+
async function handlePaginatedListWithAsyncIteration(
|
|
410
430
|
sdkMethodName: string,
|
|
411
|
-
|
|
412
|
-
sdk: ZapierSdk,
|
|
431
|
+
sdkResult: any,
|
|
413
432
|
schema: z.ZodSchema,
|
|
414
433
|
): Promise<void> {
|
|
415
|
-
const limit = baseParams.limit || 20;
|
|
416
434
|
const itemName = getItemNameFromMethod(sdkMethodName);
|
|
435
|
+
let totalShown = 0;
|
|
436
|
+
let pageCount = 0;
|
|
417
437
|
|
|
418
|
-
console.log(chalk.blue(`š
|
|
438
|
+
console.log(chalk.blue(`š ${getListTitleFromMethod(sdkMethodName)}\n`));
|
|
419
439
|
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
440
|
+
try {
|
|
441
|
+
// Use async iteration to go through pages
|
|
442
|
+
for await (const page of sdkResult) {
|
|
443
|
+
const items = page.data || page;
|
|
444
|
+
pageCount++;
|
|
424
445
|
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
) => {
|
|
430
|
-
// Only clear screen if we have items to show
|
|
431
|
-
if (items.length > 0) {
|
|
432
|
-
console.clear();
|
|
433
|
-
}
|
|
434
|
-
console.log(chalk.blue(`š ${getListTitleFromMethod(sdkMethodName)}\n`));
|
|
446
|
+
if (!Array.isArray(items)) {
|
|
447
|
+
console.log(chalk.yellow(`No ${itemName} found.`));
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
435
450
|
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
451
|
+
if (items.length === 0 && pageCount === 1) {
|
|
452
|
+
console.log(chalk.yellow(`No ${itemName} found.`));
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
440
455
|
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
//
|
|
446
|
-
|
|
456
|
+
if (items.length === 0) {
|
|
457
|
+
break; // No more items
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
// Clear screen for subsequent pages (not the first)
|
|
461
|
+
if (pageCount > 1) {
|
|
462
|
+
console.clear();
|
|
463
|
+
console.log(
|
|
464
|
+
chalk.blue(`š ${getListTitleFromMethod(sdkMethodName)}\n`),
|
|
465
|
+
);
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
// Format and display items using schema
|
|
469
|
+
if (schema) {
|
|
470
|
+
formatItemsFromSchema(schema as z.ZodType, items);
|
|
471
|
+
} else {
|
|
472
|
+
formatItemsGeneric(items);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
totalShown += items.length;
|
|
476
|
+
console.log(
|
|
477
|
+
chalk.green(
|
|
478
|
+
`\nā
Showing ${totalShown} ${itemName} (page ${pageCount})`,
|
|
479
|
+
),
|
|
480
|
+
);
|
|
481
|
+
|
|
482
|
+
// Only prompt if there's a nextCursor (more pages available)
|
|
483
|
+
if (page.nextCursor) {
|
|
484
|
+
const { continueReading } = await inquirer.prompt([
|
|
485
|
+
{
|
|
486
|
+
type: "confirm",
|
|
487
|
+
name: "continueReading",
|
|
488
|
+
message: `Load next page?`,
|
|
489
|
+
default: true,
|
|
490
|
+
},
|
|
491
|
+
]);
|
|
492
|
+
|
|
493
|
+
if (!continueReading) {
|
|
494
|
+
break;
|
|
495
|
+
}
|
|
496
|
+
} else {
|
|
497
|
+
// No more pages available, exit gracefully
|
|
498
|
+
break;
|
|
499
|
+
}
|
|
447
500
|
}
|
|
448
501
|
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
502
|
+
console.log(chalk.gray(`\nš Finished browsing ${itemName}`));
|
|
503
|
+
} catch (error) {
|
|
504
|
+
// If the result is not async iterable, fall back to showing the first page
|
|
505
|
+
const items = sdkResult?.data || sdkResult;
|
|
506
|
+
if (Array.isArray(items)) {
|
|
507
|
+
if (items.length === 0) {
|
|
508
|
+
console.log(chalk.yellow(`No ${itemName} found.`));
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
456
511
|
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
512
|
+
if (schema) {
|
|
513
|
+
formatItemsFromSchema(schema as z.ZodType, items);
|
|
514
|
+
} else {
|
|
515
|
+
formatItemsGeneric(items);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
console.log(chalk.green(`\nā
Showing ${items.length} ${itemName}`));
|
|
519
|
+
} else {
|
|
520
|
+
throw error;
|
|
521
|
+
}
|
|
522
|
+
}
|
|
466
523
|
}
|
|
467
524
|
|
|
468
525
|
function formatNonPaginatedResults(
|
|
469
526
|
result: any[],
|
|
470
|
-
|
|
471
|
-
|
|
527
|
+
requestedMaxItems?: number,
|
|
528
|
+
userSpecifiedMaxItems?: boolean,
|
|
472
529
|
useRawJson?: boolean,
|
|
473
530
|
schema?: z.ZodSchema,
|
|
474
531
|
methodName?: string,
|
|
@@ -505,10 +562,10 @@ function formatNonPaginatedResults(
|
|
|
505
562
|
}
|
|
506
563
|
|
|
507
564
|
// Show appropriate status message
|
|
508
|
-
if (
|
|
565
|
+
if (userSpecifiedMaxItems && requestedMaxItems) {
|
|
509
566
|
console.log(
|
|
510
567
|
chalk.gray(
|
|
511
|
-
`\nš Showing up to ${
|
|
568
|
+
`\nš Showing up to ${requestedMaxItems} ${itemName} (--max-items ${requestedMaxItems})`,
|
|
512
569
|
),
|
|
513
570
|
);
|
|
514
571
|
} else {
|
|
@@ -519,7 +576,7 @@ function formatNonPaginatedResults(
|
|
|
519
576
|
function formatItemsGeneric(items: any[]): void {
|
|
520
577
|
// Fallback formatting for items without schema metadata
|
|
521
578
|
items.forEach((item, index) => {
|
|
522
|
-
const name = item.name || item.key || item.id || "Item";
|
|
579
|
+
const name = item.title || item.name || item.key || item.id || "Item";
|
|
523
580
|
console.log(`${chalk.gray(`${index + 1}.`)} ${chalk.cyan(name)}`);
|
|
524
581
|
if (item.description) {
|
|
525
582
|
console.log(` ${chalk.dim(item.description)}`);
|