@cnbcool/cnb-api-generate 2.9.2 → 2.10.0
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 +6 -3
- package/client/lib/format-output.ts +5 -0
- 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',
|
|
@@ -100,8 +103,8 @@ async function formatResponse(data: any, response: any) {
|
|
|
100
103
|
let decoded = decodeURIComponent(lastSegment);
|
|
101
104
|
// 拒绝空字节(不同 OS/Node.js 版本行为不一致)
|
|
102
105
|
if (decoded.includes('\0')) throw new Error('Invalid filename: contains null byte');
|
|
103
|
-
//
|
|
104
|
-
fileName = decoded.replace(/[
|
|
106
|
+
// 最小化过滤:只去除控制字符和跨平台不允许的文件名字符
|
|
107
|
+
fileName = decoded.replace(/[<>:"/\\|?*\x00-\x1f]/g, '_');
|
|
105
108
|
}
|
|
106
109
|
} catch (err) {
|
|
107
110
|
console.warn('[cnb-api] Failed to parse filename from URL:', err instanceof Error ? err.message : err);
|
|
@@ -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
|
}
|