ability-cli 0.3.4 → 0.3.5

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/index.js +22 -9
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -135,14 +135,15 @@ function printAbilityDetail(a) {
135
135
  console.log(JSON.stringify(a.inputSchema, null, 2));
136
136
  }
137
137
  }
138
- function handleApiResponse(res, jsonMode, formatter) {
139
- const hasStandardEnvelope = res && typeof res === "object" && Object.prototype.hasOwnProperty.call(res, "code");
140
- if (hasStandardEnvelope && res.code !== 200) {
141
- const msg = res.msg ?? res.message ?? JSON.stringify(res);
142
- printError(`\u63A5\u53E3\u8FD4\u56DE\u9519\u8BEF: [${res.code}] ${msg}`);
143
- process.exit(4);
138
+ function extractApiPayload(res) {
139
+ if (res && typeof res === "object" && "data" in res) {
140
+ const data = res.data;
141
+ if (data !== void 0) return data;
144
142
  }
145
- const payload = hasStandardEnvelope ? res.data : res;
143
+ return res;
144
+ }
145
+ function handleApiResponse(res, jsonMode, formatter) {
146
+ const payload = extractApiPayload(res);
146
147
  if (jsonMode) {
147
148
  printJson(payload);
148
149
  } else {
@@ -261,7 +262,7 @@ async function apiGet(ctx, path2, params) {
261
262
  console.error(`[verbose] Headers: ${JSON.stringify(headers, null, 2)}`);
262
263
  }
263
264
  const res = await fetch(url, { headers });
264
- const body = await res.json();
265
+ const body = await readJsonResponse(res, ctx);
265
266
  if (ctx.verbose) console.error(`[verbose] Response: ${JSON.stringify(body, null, 2)}`);
266
267
  return body;
267
268
  }
@@ -279,10 +280,22 @@ async function apiPost(ctx, path2, data) {
279
280
  headers,
280
281
  body: JSON.stringify(data)
281
282
  });
282
- const body = await res.json();
283
+ const body = await readJsonResponse(res, ctx);
283
284
  if (ctx.verbose) console.error(`[verbose] Response: ${JSON.stringify(body, null, 2)}`);
284
285
  return body;
285
286
  }
287
+ async function readJsonResponse(res, ctx) {
288
+ const text = await res.text();
289
+ if (!res.ok) {
290
+ throw new Error(`HTTP ${res.status} ${res.statusText}: ${text.slice(0, 500)}`);
291
+ }
292
+ if (!text.trim()) return {};
293
+ try {
294
+ return JSON.parse(text);
295
+ } catch {
296
+ throw new Error(`\u54CD\u5E94\u4E0D\u662F\u5408\u6CD5 JSON (HTTP ${res.status}): ${text.slice(0, 200)}`);
297
+ }
298
+ }
286
299
 
287
300
  // src/commands/raw.ts
288
301
  import fs3 from "fs";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ability-cli",
3
- "version": "0.3.4",
3
+ "version": "0.3.5",
4
4
  "description": "原子能力平台 CLI 工具",
5
5
  "repository": {
6
6
  "type": "git",