@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.
@@ -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-group 流程时只需要 repo) */
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-group 单条附件的描述(请求体里的元素) */
82
- interface AssetItem {
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-group 接口响应(与 swagger 一致;只列出我们用到的字段) */
101
- interface AssetUploadEntry {
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 函数承担,统一为 (firstArg, body)) */
109
+ /** 上传 API 函数签名(issue 与 pull 分别由对应的 swagger 函数承担) */
121
110
  type UploadApiFunction = (
122
111
  firstArg: UploadPathParams | string,
123
- body: IssueAssetGroupBody | PullUploadBody,
124
- ) => Promise<{ status?: number; data?: IssueAssetGroupResponseData | PullUploadResponseData }>;
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-group 一次性接口):
158
- * 1. POST /{repo}/-/issues/asset-groups 携带 file_assets 或 image_assets
159
- * 服务端创建 asset_group 并返回 upload_url + asset_link
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 给调用方拼到评论或 issue 描述里
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 只需 repo;pull 也只需 repo)
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 issueAssetLink: string | undefined;
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-group,一次拿 asset_group_id + upload_url + asset_link
216
- // 函数签名:(repo: string, { file_assets | image_assets }, ...)
217
- const assetItem: AssetItem = { name: fileName, size: fileSize, content_type: contentType };
218
- const requestBody: IssueAssetGroupBody = isImageKind
219
- ? { image_assets: [assetItem] }
220
- : { file_assets: [assetItem] };
221
-
222
- const apiResponse = await toolFunction(pathParams.repo, requestBody);
223
- const data = apiResponse?.data as IssueAssetGroupResponseData | undefined;
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
- // 服务端可能返回 201/200,从对应数组里取第一条 upload 描述
226
- const entries = isImageKind ? data?.image_upload_urls : data?.file_upload_urls;
227
- const entry = entries?.[0];
228
- uploadUrl = entry?.upload_url;
229
- issueAssetLink = entry?.asset_link;
230
- issueAssetGroupId = data?.asset_group_id;
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: issueAssetLink,
286
- asset_group_id: issueAssetGroupId,
287
- name: fileName,
275
+ asset_link: issueResponseData?.asset_link,
276
+ name: issueResponseData?.name || fileName,
277
+ path: issueResponseData?.path,
288
278
  size: fileSize,
289
279
  },
290
280
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cnbcool/cnb-api-generate",
3
- "version": "2.6.0",
3
+ "version": "2.6.1",
4
4
  "main": "./built/index.js",
5
5
  "module": "./src/index.ts",
6
6
  "types": "./src/index.ts",