@cnbcool/cnb-api-generate 1.1.0 → 1.1.1

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/client/index.ts +3 -118
  2. package/package.json +1 -1
package/client/index.ts CHANGED
@@ -248,114 +248,6 @@ function formatParams(
248
248
  return formatted;
249
249
  }
250
250
 
251
- /**
252
- * 将嵌套对象打平取主字段值
253
- * 用于表格输出时将嵌套字段(如 author: {username: "alice"})展示为 "alice"
254
- * @param val 任意值
255
- * @returns 打平后的字符串
256
- */
257
- function flattenValue(val: any): string {
258
- if (val === null || val === undefined) return '';
259
- if (typeof val !== 'object') return String(val);
260
- if (Array.isArray(val)) {
261
- return val.map(v => typeof v === 'object' ? flattenValue(v) : String(v)).join(',');
262
- }
263
- // 对象:取第一个 string 类型的字段值(如 author.login)
264
- for (const v of Object.values(val)) {
265
- if (typeof v === 'string') return v;
266
- if (typeof v === 'number') return String(v);
267
- }
268
- return JSON.stringify(val);
269
- }
270
-
271
- /**
272
- * 缩短时间戳:2026-04-05T15:50:35Z → 2026-04-05
273
- */
274
- function shortenTimestamp(str: string): string {
275
- if (/^\d{4}-\d{2}-\d{2}T/.test(str)) {
276
- return str.substring(0, 10);
277
- }
278
- return str;
279
- }
280
-
281
- /**
282
- * 格式化数组响应为紧凑表格
283
- * - 过滤低信号字段(url、avatar 等)和全空列
284
- * - 缩短时间戳为日期
285
- * - 嵌套对象打平取主字段值
286
- * - 格式:表头行 + 数据行,用 | 分隔
287
- */
288
- function formatArrayAsTable(response: any): string {
289
- const { status, page, total, totalPages, data } = response;
290
-
291
- if (!Array.isArray(data) || data.length === 0) {
292
- return JSON.stringify(response);
293
- }
294
-
295
- // 收集所有出现的 key
296
- const allKeys = new Set<string>();
297
- for (const item of data) {
298
- if (typeof item === 'object' && item !== null) {
299
- for (const key of Object.keys(item)) {
300
- allKeys.add(key);
301
- }
302
- }
303
- }
304
-
305
- if (allKeys.size === 0) {
306
- return JSON.stringify(response);
307
- }
308
-
309
- // 低信号字段
310
- const lowSignalPatterns = [
311
- /url$/i, /avatar/i, /html_url/i, /href/i, /link/i,
312
- /^_/, /updated_at/i, /^invisible$/i, /^state_reason$/i,
313
- /^started_at$/i, /^ended_at$/i, /^last_acted_at$/i,
314
- /^is_npc$/i, /^email$/i, /^nickname$/i,
315
- ];
316
-
317
- const filteredKeys = [...allKeys].filter(key =>
318
- !lowSignalPatterns.some(p => p.test(key))
319
- );
320
-
321
- const keysList = filteredKeys.length >= 3 ? filteredKeys : [...allKeys];
322
-
323
- // 构建列数据,同时过滤全空列
324
- const columns: { key: string; values: string[] }[] = [];
325
- for (const key of keysList) {
326
- const values: string[] = [];
327
- let hasNonEmpty = false;
328
- for (const item of data) {
329
- let str = flattenValue(item?.[key]);
330
- str = shortenTimestamp(str);
331
- str = str.replace(/\|/g, '/');
332
- if (str.length > 80) str = str.substring(0, 77) + '...';
333
- if (str !== '' && str !== 'false') hasNonEmpty = true;
334
- values.push(str);
335
- }
336
- if (hasNonEmpty) {
337
- columns.push({ key, values });
338
- }
339
- }
340
-
341
- if (columns.length === 0) {
342
- return JSON.stringify(response);
343
- }
344
-
345
- // 构建输出(紧凑格式,不对齐)
346
- let output = `status:${status}`;
347
- if (total != null) {
348
- output += ` page:${page}/${totalPages} total:${total}`;
349
- }
350
- output += '\n';
351
- output += columns.map(c => c.key).join('|') + '\n';
352
- for (let r = 0; r < data.length; r++) {
353
- output += columns.map(c => c.values[r]).join('|') + '\n';
354
- }
355
-
356
- return output.trimEnd();
357
- }
358
-
359
251
  /**
360
252
  * 精简响应对象(非 verbose 模式使用)
361
253
  * - 去掉 trace(仅错误时保留)
@@ -396,8 +288,7 @@ function isStandardResponse(response: any): boolean {
396
288
  * 格式化 CLI 输出
397
289
  * - verbose 模式:完整 JSON(含 trace、header 等全部字段)
398
290
  * - 非标准响应(converter 返回裸 data):直接 JSON 输出
399
- * - 数组响应:紧凑表格格式
400
- * - 单对象响应:精简 JSON(去掉 trace、header)
291
+ * - 标准响应:精简 JSON(去掉 trace、header)
401
292
  */
402
293
  function formatOutput(response: any, verbose: boolean): string {
403
294
  // --verbose 模式:输出完整原始信息
@@ -407,19 +298,13 @@ function formatOutput(response: any, verbose: boolean): string {
407
298
 
408
299
  // converter 可能返回裸 data(没有标准 {status, data} 结构),直接输出
409
300
  if (!isStandardResponse(response)) {
410
- return JSON.stringify(response);
301
+ return JSON.stringify(response, null, 2);
411
302
  }
412
303
 
413
304
  // 精简响应
414
305
  const compact = compactResponse(response);
415
306
 
416
- // 数组响应 表格格式
417
- if (compact && Array.isArray(compact.data)) {
418
- return formatArrayAsTable(compact);
419
- }
420
-
421
- // 单对象响应 → 精简 JSON
422
- return JSON.stringify(compact);
307
+ return JSON.stringify(compact, null, 2);
423
308
  }
424
309
 
425
310
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cnbcool/cnb-api-generate",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "main": "./built/index.js",
5
5
  "module": "./src/index.ts",
6
6
  "types": "./src/index.ts",