taler-typescript-core

Wallet core logic and WebUIs for various components
Log | Files | Refs | Submodules | README | LICENSE

commit 060c357ce2205596e7d40774fa751465712f00f6
parent c1018ebb8a653ad48ddc284424675e8325cfad88
Author: Florian Dold <dold@taler.net>
Date:   Thu,  6 Aug 2026 16:48:10 +0200

wallet-cli: offer the api command as "advanced api"

Diffstat:
Mpackages/taler-wallet-cli/src/index.ts | 101+++++++++++++++++++++++++++++++++++++++++++++++--------------------------------
1 file changed, 60 insertions(+), 41 deletions(-)

diff --git a/packages/taler-wallet-cli/src/index.ts b/packages/taler-wallet-cli/src/index.ts @@ -718,49 +718,64 @@ walletCli ); }); -walletCli - .subcommand("api", "api", { help: "Call the wallet-core API directly." }) - .requiredArgument("operation", clk.STRING) - .requiredArgument("request", clk.STRING) - .flag("expectSuccess", ["--expect-success"], { - help: "Exit with non-zero status code when request fails instead of returning error JSON.", - }) - .action(async (args) => { - // Exit only after withWallet returned, so that the wallet - // is shut down cleanly. - await runCliAction(() => - withWallet(args, {}, async (wallet) => { - let requestJson; - logger.info(`handling 'api' request (${args.api.operation})`); - const jsonContent = args.api.request.startsWith("@") - ? readFile(args.api.request.substring(1)) - : args.api.request; - try { - requestJson = JSON.parse(jsonContent); - } catch (e) { - throw new CliUsageError("invalid JSON in request"); - } - try { - const resp = await wallet.makeCoreApiRequest( - args.api.operation, - requestJson, - ); - console.log(JSON.stringify(resp, undefined, 2)); - if (resp.type === "error") { - if (args.api.expectSuccess) { - return EXIT_API_ERROR; +function addApiCommand( + parent: any, + argKey: string, + name: string, + mark?: clk.CommandMark, +): void { + parent + .subcommand(argKey, name, { + help: "Call the wallet-core API directly.", + mark, + }) + .requiredArgument("operation", clk.STRING) + .requiredArgument("request", clk.STRING) + .flag("expectSuccess", ["--expect-success"], { + help: "Exit with non-zero status code when request fails instead of returning error JSON.", + }) + .action(async (args: any) => { + const a = args[argKey]; + // Exit only after withWallet returned, so that the wallet + // is shut down cleanly. + await runCliAction(() => + withWallet(args, {}, async (wallet) => { + let requestJson; + logger.info(`handling 'api' request (${a.operation})`); + const jsonContent = a.request.startsWith("@") + ? readFile(a.request.substring(1)) + : a.request; + try { + requestJson = JSON.parse(jsonContent); + } catch (e) { + throw new CliUsageError("invalid JSON in request"); + } + try { + const resp = await wallet.makeCoreApiRequest( + a.operation, + requestJson, + ); + console.log(JSON.stringify(resp, undefined, 2)); + if (resp.type === "error") { + if (a.expectSuccess) { + return EXIT_API_ERROR; + } + logger.warn("api request resulted in error response"); } - logger.warn("api request resulted in error response"); + } catch (e) { + logger.error(`Got exception while handling API request ${e}`); + return EXIT_EXCEPTION; } - } catch (e) { - logger.error(`Got exception while handling API request ${e}`); - return EXIT_EXCEPTION; - } - logger.info("finished handling API request"); - return 0; - }), - ); - }); + logger.info("finished handling API request"); + return 0; + }), + ); + }); +} + +// Kept at the top level for the scripts and the test harness that call it +// there, but "advanced api" is where it belongs. +addApiCommand(walletCli, "api", "api", "hidden"); /** * Resolve what the user passed in place of a transaction identifier. @@ -2890,6 +2905,10 @@ const advancedCli = walletCli.subcommand("advancedArgs", "advanced", { help: "Subcommands for advanced operations (only use if you know what you're doing!).", }); +// The discoverable spelling of the top-level "api" command. The +// argument key must differ from it. +addApiCommand(advancedCli, "advancedApi", "api"); + advancedCli .subcommand("convertDb", "convert-db", { help: "Convert a wallet database between storage backends.",