@cnbcool/cnb-api-generate 2.9.3 → 2.10.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.
- package/client/core.ts +4 -1
- package/client/lib/format-output.ts +5 -0
- package/client/lib/format-params.ts +12 -2
- package/package.json +1 -1
package/client/core.ts
CHANGED
|
@@ -43,6 +43,8 @@ async function formatResponse(data: any, response: any) {
|
|
|
43
43
|
const pageSize = response.headers.get('x-cnb-page-size');
|
|
44
44
|
const total = response.headers.get('x-cnb-total');
|
|
45
45
|
|
|
46
|
+
const contentType = response.headers.get('content-type') || '';
|
|
47
|
+
|
|
46
48
|
const responseData: {
|
|
47
49
|
status: number;
|
|
48
50
|
trace?: string;
|
|
@@ -51,6 +53,7 @@ async function formatResponse(data: any, response: any) {
|
|
|
51
53
|
pageSize?: number;
|
|
52
54
|
total?: number;
|
|
53
55
|
totalPages?: number;
|
|
56
|
+
contentType?: string;
|
|
54
57
|
data: Record<string, any> | string | null;
|
|
55
58
|
} = {
|
|
56
59
|
status,
|
|
@@ -60,6 +63,7 @@ async function formatResponse(data: any, response: any) {
|
|
|
60
63
|
'x-cnb-page-size': pageSize,
|
|
61
64
|
'x-cnb-total': total,
|
|
62
65
|
},
|
|
66
|
+
contentType: contentType || undefined,
|
|
63
67
|
data: null,
|
|
64
68
|
};
|
|
65
69
|
|
|
@@ -74,7 +78,6 @@ async function formatResponse(data: any, response: any) {
|
|
|
74
78
|
responseData.totalPages = Math.ceil(totalNum / pageSizeNum);
|
|
75
79
|
}
|
|
76
80
|
|
|
77
|
-
const contentType = response.headers.get('content-type') || '';
|
|
78
81
|
const isJson = [
|
|
79
82
|
'application/vnd.cnb.api+json',
|
|
80
83
|
'application/json',
|
|
@@ -25,6 +25,11 @@ export function compactResponse(response: any): any {
|
|
|
25
25
|
compact.totalPages = response.totalPages;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
// 保留 contentType(下载文件时有用)
|
|
29
|
+
if (response.contentType) {
|
|
30
|
+
compact.contentType = response.contentType;
|
|
31
|
+
}
|
|
32
|
+
|
|
28
33
|
compact.data = response.data;
|
|
29
34
|
return compact;
|
|
30
35
|
}
|
|
@@ -26,6 +26,16 @@ export function getToolParamDefs(moduleName: string, toolName: string) {
|
|
|
26
26
|
* 收到的是字面的反斜杠 + n。这里用 slashes.removeSlashes 做一次反解析,
|
|
27
27
|
* 让 `--body "a\n b"` 与 `echo -e` 体验一致,正确还原为真实换行/制表符等。
|
|
28
28
|
*/
|
|
29
|
+
/**
|
|
30
|
+
* 过滤非法字符(如 \u0000 NUL),避免破坏环境变量和 HTTP 传输。
|
|
31
|
+
* NUL 是无意义噪音/损坏数据,直接移除而非替换为乱码标记。
|
|
32
|
+
*/
|
|
33
|
+
function filterIllegalChars(str: string): string {
|
|
34
|
+
// \u0000 (NUL) 是核心非法字符,会在环境变量/JSON/HTTP 中引发错误
|
|
35
|
+
// 同时过滤其他已知会导致问题的高危控制字符
|
|
36
|
+
return str.replace(/\x00/g, '');
|
|
37
|
+
}
|
|
38
|
+
|
|
29
39
|
function assignBodyValue(
|
|
30
40
|
target: Record<string, any>,
|
|
31
41
|
key: string,
|
|
@@ -35,7 +45,7 @@ function assignBodyValue(
|
|
|
35
45
|
if (Array.isArray(value)) {
|
|
36
46
|
target[key] = value;
|
|
37
47
|
} else if (propType === 'string') {
|
|
38
|
-
target[key] = typeof value === 'string' ? removeSlashes(value) : value;
|
|
48
|
+
target[key] = typeof value === 'string' ? filterIllegalChars(removeSlashes(value)) : value;
|
|
39
49
|
} else {
|
|
40
50
|
target[key] = tryParseJSON(value as string);
|
|
41
51
|
}
|
|
@@ -322,7 +332,7 @@ export function formatParams(
|
|
|
322
332
|
// 确保「从文件读取正文」的显式意图最终生效。
|
|
323
333
|
if (bodyFromFile !== undefined) {
|
|
324
334
|
if (!formatted.data) formatted.data = {};
|
|
325
|
-
formatted.data.body = bodyFromFile;
|
|
335
|
+
formatted.data.body = filterIllegalChars(bodyFromFile);
|
|
326
336
|
}
|
|
327
337
|
}
|
|
328
338
|
|