(cmd: Command, spec: CommandSpec, meta: OperationMeta)
| 130 | type OperationMeta = (typeof operationMeta)[keyof typeof operationMeta] |
| 131 | |
| 132 | function registerApiCommand(cmd: Command, spec: CommandSpec, meta: OperationMeta) { |
| 133 | const seg = spec.path[spec.path.length - 1]! |
| 134 | cmd.summary(meta.summary).description(meta.description) |
| 135 | for (const p of meta.pathParams) |
| 136 | if (seg === toKebab(p.name)) cmd.argument(`<${p.name}>`, `${p.name} (path param)`) |
| 137 | else cmd.option(`--${p.name} <value>`, `${p.name} (path param)`) |
| 138 | |
| 139 | for (const p of meta.bodyParams) cmd.option(`--${p.name} <value>`, `body.${p.name}`) |
| 140 | cmd.option('--body <json>', 'raw JSON body (overrides individual body opts)') |
| 141 | cmd.action(async function (this: Command, ...positionalArgs: unknown[]) { |
| 142 | const merged = this.optsWithGlobals() as Record<string, unknown> |
| 143 | const baseUrl = (merged.baseUrl as string) ?? resolveBaseUrl() |
| 144 | const optsFromCmd = merged |
| 145 | const pathParamValues: Record<string, string> = {} |
| 146 | meta.pathParams.forEach((p, i) => { |
| 147 | const val = positionalArgs[i] |
| 148 | if (val !== undefined) pathParamValues[p.name] = String(val) |
| 149 | }) |
| 150 | const cmdOpts = { ...optsFromCmd, ...pathParamValues } |
| 151 | const apiKey = await ensureApiKey() |
| 152 | const client = createClient({ baseUrl, apiKey }) |
| 153 | const opts = buildOpts(meta, cmdOpts) |
| 154 | try { |
| 155 | const result = await runCommand({ client, spec, meta, opts }) |
| 156 | console.log(JSON.stringify(result, null, 2)) |
| 157 | } catch (err) { |
| 158 | const msg = err instanceof Error ? err.message : String(err) |
| 159 | const body = |
| 160 | err && typeof err === 'object' && 'body' in err |
| 161 | ? (err as { body: unknown }).body |
| 162 | : undefined |
| 163 | console.error('Error:', msg) |
| 164 | if (body) console.error('Details:', JSON.stringify(body, null, 2)) |
| 165 | process.exit(1) |
| 166 | } |
| 167 | }) |
| 168 | } |
| 169 | |
| 170 | for (const spec of commandSpecs) { |
| 171 | const meta = operationMeta[spec.operationId as keyof typeof operationMeta] |
no test coverage detected