@cnbcool/cnb-api-generate 2.6.0 → 2.6.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/utils/upload.ts +36 -46
- package/package.json +1 -1
package/client/utils/upload.ts
CHANGED
|
@@ -72,43 +72,32 @@ function mimeLookup(filePath: string): string {
|
|
|
72
72
|
// 上传文件大小上限:100MB
|
|
73
73
|
const MAX_FILE_SIZE = 100 * 1024 * 1024;
|
|
74
74
|
|
|
75
|
-
/** 上传 API 的 path 参数(issue 走 asset-
|
|
75
|
+
/** 上传 API 的 path 参数(issue 走 comment-asset-upload-url 流程需要 repo + number) */
|
|
76
76
|
interface UploadPathParams {
|
|
77
77
|
repo: string;
|
|
78
78
|
number?: string;
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
-
/** Issue asset-
|
|
82
|
-
interface
|
|
81
|
+
/** Issue comment-asset-upload-url 接口请求体(单文件,与 swagger PostIssueAssetUploadURLForm 对齐) */
|
|
82
|
+
interface IssueAssetUploadURLBody {
|
|
83
83
|
name: string;
|
|
84
84
|
size: number;
|
|
85
85
|
content_type: string;
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
-
/** Issue asset-group 创建请求体 */
|
|
89
|
-
interface IssueAssetGroupBody {
|
|
90
|
-
file_assets?: AssetItem[];
|
|
91
|
-
image_assets?: AssetItem[];
|
|
92
|
-
}
|
|
93
|
-
|
|
94
88
|
/** Pull 上传请求体 */
|
|
95
89
|
interface PullUploadBody {
|
|
96
90
|
name: string;
|
|
97
91
|
size: number;
|
|
98
92
|
}
|
|
99
93
|
|
|
100
|
-
/** Issue asset-
|
|
101
|
-
interface
|
|
94
|
+
/** Issue comment-asset-upload-url 接口响应(与 swagger api.IssueAssetUploadURL 对齐) */
|
|
95
|
+
interface IssueAssetUploadURLResponseData {
|
|
102
96
|
name?: string;
|
|
103
97
|
path?: string;
|
|
104
98
|
asset_link?: string;
|
|
105
99
|
upload_url?: string;
|
|
106
100
|
}
|
|
107
|
-
interface IssueAssetGroupResponseData {
|
|
108
|
-
asset_group_id?: string;
|
|
109
|
-
file_upload_urls?: AssetUploadEntry[];
|
|
110
|
-
image_upload_urls?: AssetUploadEntry[];
|
|
111
|
-
}
|
|
112
101
|
|
|
113
102
|
/** Pull 上传 API 响应 */
|
|
114
103
|
interface PullUploadResponseData {
|
|
@@ -117,11 +106,11 @@ interface PullUploadResponseData {
|
|
|
117
106
|
token?: string;
|
|
118
107
|
}
|
|
119
108
|
|
|
120
|
-
/** 上传 API 函数签名(issue 与 pull 分别由对应的 swagger
|
|
109
|
+
/** 上传 API 函数签名(issue 与 pull 分别由对应的 swagger 函数承担) */
|
|
121
110
|
type UploadApiFunction = (
|
|
122
111
|
firstArg: UploadPathParams | string,
|
|
123
|
-
body:
|
|
124
|
-
) => Promise<{ status?: number; data?:
|
|
112
|
+
body: IssueAssetUploadURLBody | PullUploadBody,
|
|
113
|
+
) => Promise<{ status?: number; data?: IssueAssetUploadURLResponseData | PullUploadResponseData }>;
|
|
125
114
|
|
|
126
115
|
/** 上传失败时的 data */
|
|
127
116
|
interface UploadErrorData {
|
|
@@ -132,8 +121,8 @@ interface UploadErrorData {
|
|
|
132
121
|
/** Issue 上传成功时的 data */
|
|
133
122
|
interface IssueUploadData {
|
|
134
123
|
asset_link?: string;
|
|
135
|
-
asset_group_id?: string;
|
|
136
124
|
name: string;
|
|
125
|
+
path?: string;
|
|
137
126
|
size: number;
|
|
138
127
|
}
|
|
139
128
|
|
|
@@ -154,11 +143,12 @@ interface UploadResult {
|
|
|
154
143
|
/**
|
|
155
144
|
* 处理完整上传流程
|
|
156
145
|
*
|
|
157
|
-
* Issue 路径(走 asset-
|
|
158
|
-
* 1. POST /{repo}/-/issues/asset-
|
|
159
|
-
*
|
|
146
|
+
* Issue 路径(走 comment-asset-upload-url 接口,按 file/image 分别调用):
|
|
147
|
+
* 1. POST /{repo}/-/issues/{number}/comment-file-asset-upload-url
|
|
148
|
+
* 或 /{repo}/-/issues/{number}/comment-image-asset-upload-url
|
|
149
|
+
* 携带 { name, size, content_type } → 服务端返回 upload_url + asset_link
|
|
160
150
|
* 2. PUT 文件流到 upload_url
|
|
161
|
-
* 3. 返回 asset_link
|
|
151
|
+
* 3. 返回 asset_link 给调用方拼到评论 body 里
|
|
162
152
|
*
|
|
163
153
|
* Pull 路径(保留原有的 upload-files / upload-imgs 一步上传):
|
|
164
154
|
* 1. POST /{repo}/upload-files(或 imgs) 拿 upload_url
|
|
@@ -167,7 +157,7 @@ interface UploadResult {
|
|
|
167
157
|
* @param shortcut 解析后的快捷命令;shortcut.kind 决定 issue 上传走 file 还是 image 通道
|
|
168
158
|
* @param filePath 本地文件路径
|
|
169
159
|
* @param toolFunction 原始上传 API 函数
|
|
170
|
-
* @param pathParams API 调用的 path 参数(issue
|
|
160
|
+
* @param pathParams API 调用的 path 参数(issue 需要 repo+number;pull 只需 repo)
|
|
171
161
|
*/
|
|
172
162
|
export async function handleUpload(
|
|
173
163
|
shortcut: ResolvedShortcut,
|
|
@@ -204,30 +194,30 @@ export async function handleUpload(
|
|
|
204
194
|
|
|
205
195
|
// 2. 调用上传 API 获取 upload_url
|
|
206
196
|
const isIssueUpload = shortcut.module === 'issues';
|
|
207
|
-
const isImageKind = shortcut.kind === 'image';
|
|
208
197
|
|
|
209
198
|
let uploadUrl: string | undefined;
|
|
210
|
-
let
|
|
211
|
-
let issueAssetGroupId: string | undefined;
|
|
199
|
+
let issueResponseData: IssueAssetUploadURLResponseData | undefined;
|
|
212
200
|
let pullResponseData: PullUploadResponseData | undefined;
|
|
213
201
|
|
|
214
202
|
if (isIssueUpload) {
|
|
215
|
-
// Issue: 调 post-asset-
|
|
216
|
-
//
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
203
|
+
// Issue: 调 post-issue-comment-{file|image}-asset-upload-url,
|
|
204
|
+
// 由 cag.config.js 的 quickCommands 通过 kind=file/image 选择 realTool。
|
|
205
|
+
// 函数签名:({ repo, number }, { name, size, content_type })
|
|
206
|
+
if (!pathParams.number) {
|
|
207
|
+
return { status: 400, data: { error: '缺少 issue number(请确认 CNB_ISSUE_IID 是否已设置)' } };
|
|
208
|
+
}
|
|
209
|
+
const requestBody: IssueAssetUploadURLBody = {
|
|
210
|
+
name: fileName,
|
|
211
|
+
size: fileSize,
|
|
212
|
+
content_type: contentType,
|
|
213
|
+
};
|
|
224
214
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
215
|
+
const apiResponse = await toolFunction(
|
|
216
|
+
{ repo: pathParams.repo, number: pathParams.number },
|
|
217
|
+
requestBody,
|
|
218
|
+
);
|
|
219
|
+
issueResponseData = apiResponse?.data as IssueAssetUploadURLResponseData | undefined;
|
|
220
|
+
uploadUrl = issueResponseData?.upload_url;
|
|
231
221
|
|
|
232
222
|
if (!uploadUrl) {
|
|
233
223
|
// 拿 URL 失败:把原始响应作为错误返回,便于排查
|
|
@@ -282,9 +272,9 @@ export async function handleUpload(
|
|
|
282
272
|
return {
|
|
283
273
|
status: 200,
|
|
284
274
|
data: {
|
|
285
|
-
asset_link:
|
|
286
|
-
|
|
287
|
-
|
|
275
|
+
asset_link: issueResponseData?.asset_link,
|
|
276
|
+
name: issueResponseData?.name || fileName,
|
|
277
|
+
path: issueResponseData?.path,
|
|
288
278
|
size: fileSize,
|
|
289
279
|
},
|
|
290
280
|
};
|