listpage_cli 0.0.302 → 0.0.303
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/package.json
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"dependencies": {
|
|
13
13
|
"react": "^19.2.0",
|
|
14
14
|
"react-dom": "^19.2.0",
|
|
15
|
-
"listpage-next": "~0.0.
|
|
15
|
+
"listpage-next": "~0.0.303",
|
|
16
16
|
"react-router-dom": ">=6.0.0",
|
|
17
17
|
"@ant-design/v5-patch-for-react-19": "~1.0.3",
|
|
18
18
|
"ahooks": "^3.9.5",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"styled-components": "^6.1.19",
|
|
24
24
|
"mobx": "~6.15.0",
|
|
25
25
|
"@ant-design/icons": "~6.0.2",
|
|
26
|
-
"listpage-components": "~0.0.
|
|
26
|
+
"listpage-components": "~0.0.303",
|
|
27
27
|
"lucide-react": "~0.575.0"
|
|
28
28
|
"mobx-react-lite": "~4.1.1"
|
|
29
29
|
},
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
- `EndpointConfig<Req, Res>`:单个接口配置泛型。
|
|
13
13
|
- `EndpointMode`:`"json" | "upload" | "download" | "sse"`。
|
|
14
14
|
- `ApiEnvelope<T>`:后端 `{ code, message, data }` 统一模型。
|
|
15
|
+
- `DownloadResult`:下载模式统一返回类型 `{ blob: Blob; filename?: string }`。
|
|
15
16
|
- **配置工具**
|
|
16
17
|
- `defineEndpoint<Req, Res>(config: EndpointConfig<Req, Res>)`:声明一个接口。
|
|
17
18
|
- **客户端**
|
|
@@ -43,6 +44,7 @@ export function defineEndpoint<Req, Res>(
|
|
|
43
44
|
|
|
44
45
|
- 声明接口的 HTTP 方法、路径、是否 SSE、是否需要鉴权,以及数据模式(`mode`)。
|
|
45
46
|
- 当 `mode === "upload"` 时,请求体参数会被视为普通对象,并在内部自动转换为 `FormData` 进行 `multipart/form-data` 上传。
|
|
47
|
+
- 当 `mode === "download"` 时,建议将 `Res` 统一写为 `DownloadResult`,内部会返回 `{ blob, filename }`,**不再走 JSON 解析与业务 code 判定**。
|
|
46
48
|
- 绑定请求泛型 `Req` 与响应泛型 `Res`,后续通过 `ApiClient` 自动推导。
|
|
47
49
|
|
|
48
50
|
### ClientOptions
|
|
@@ -249,9 +249,62 @@ export async function uploadFile(params: ReqUploadFile): Promise<ResUploadFile>
|
|
|
249
249
|
|
|
250
250
|
---
|
|
251
251
|
|
|
252
|
-
## 9.
|
|
252
|
+
## 9. 文件下载示例(`mode: "download"`,统一使用 `DownloadResult`)
|
|
253
|
+
|
|
254
|
+
```ts
|
|
255
|
+
// src/api/report.ts
|
|
256
|
+
export interface ReqDownloadReport {
|
|
257
|
+
id: string;
|
|
258
|
+
}
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
```ts
|
|
262
|
+
// src/api/request-config.ts 片段
|
|
263
|
+
import { defineEndpoint, type DownloadResult } from "listpage-http";
|
|
264
|
+
import type { ReqDownloadReport } from "./report";
|
|
265
|
+
|
|
266
|
+
export const requestConfig = {
|
|
267
|
+
report: {
|
|
268
|
+
download: defineEndpoint<ReqDownloadReport, DownloadResult>({
|
|
269
|
+
method: "POST",
|
|
270
|
+
path: "/report/download",
|
|
271
|
+
mode: "download",
|
|
272
|
+
}),
|
|
273
|
+
},
|
|
274
|
+
} as const;
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
```ts
|
|
278
|
+
// service 调用:获取 blob 和文件名,交由业务决定如何处理(下载 / 预览等)
|
|
279
|
+
import api from "@/api";
|
|
280
|
+
import type { ReqDownloadReport } from "@/api/report";
|
|
281
|
+
|
|
282
|
+
export async function downloadReport(params: ReqDownloadReport) {
|
|
283
|
+
const { blob, filename } = await api.report.download(params);
|
|
284
|
+
|
|
285
|
+
const url = window.URL.createObjectURL(blob);
|
|
286
|
+
const link = document.createElement("a");
|
|
287
|
+
link.href = url;
|
|
288
|
+
link.download = filename || "download";
|
|
289
|
+
document.body.appendChild(link);
|
|
290
|
+
link.click();
|
|
291
|
+
document.body.removeChild(link);
|
|
292
|
+
window.URL.revokeObjectURL(url);
|
|
293
|
+
}
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
说明:
|
|
297
|
+
|
|
298
|
+
- 下载模式下(`mode: "download"`):
|
|
299
|
+
- `listpage-http` 不再解析 JSON,直接返回 `{ blob, filename }`。
|
|
300
|
+
- 文件名优先从响应头 `Content-Disposition` 中解析,失败则为 `undefined`,由业务自行决定默认值。
|
|
301
|
+
- 即使下载失败(如 404/500),仍会按统一错误模型抛出 `ApiError` 并触发 `onError`。
|
|
302
|
+
|
|
303
|
+
---
|
|
304
|
+
|
|
305
|
+
## 10. 新增接口 Checklist
|
|
253
306
|
|
|
254
307
|
1. 在对应模块类型文件(如 `src/api/order.ts`)中添加 `ReqOrderList` / `ResOrderList` 等类型。
|
|
255
308
|
2. 在 `src/api/request-config.ts` 中为该模块新增一个 `defineEndpoint`(例如 `order.list`)。
|
|
256
|
-
3. 在 service/hook 中通过 `api.order.list(params)` 调用。
|
|
309
|
+
3. 在 service/hook 中通过 `api.order.list(params)` 或 `api.order.list(undefined, options)` 调用。
|
|
257
310
|
4. 禁止在业务代码中直接使用 `fetch`/`axios`,必须通过统一的 `api` 客户端。
|