Skip to content

Commit eb738b2

Browse files
committed
feat: expand item types and improve API response handling
- Added new interfaces and types for ItemsPublic, ItemResponse, and ItemsResponse to enhance type safety and clarity in API interactions. - Updated the index file to export the new types for broader accessibility. - Refactored the ItemsTable component to utilize the new response structure, improving data handling and reducing type assertions.
1 parent 395b881 commit eb738b2

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

admin/src/client/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ export { OpenAPI, type OpenAPIConfig } from "./core/OpenAPI"
55
export * from "./sdk.gen"
66
export * from "./types.gen"
77
export { isApiResponse, extractApiResponseError } from "./utils"
8-
export { type ItemPublic } from "./types"
8+
export { type ItemPublic, type ItemsPublic, type ItemResponse, type ItemsResponse } from "./types"

admin/src/client/types.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,30 @@ export interface ItemPublic {
99
id: string;
1010
title: string;
1111
description?: string | null;
12-
}
12+
}
13+
14+
/**
15+
* 项目集合的公共接口定义
16+
*/
17+
export interface ItemsPublic {
18+
data: Array<ItemPublic>;
19+
count: number;
20+
}
21+
22+
/**
23+
* 从API响应中提取Item数据的类型
24+
*/
25+
export type ItemResponse = {
26+
data: ItemPublic;
27+
meta?: Record<string, unknown> | null;
28+
error?: string | null;
29+
};
30+
31+
/**
32+
* 从API响应中提取Items集合数据的类型
33+
*/
34+
export type ItemsResponse = {
35+
data: ItemsPublic;
36+
meta?: Record<string, unknown> | null;
37+
error?: string | null;
38+
};

admin/src/routes/_layout/items.tsx

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { createFileRoute, useNavigate } from "@tanstack/react-router"
1111
import { FiSearch } from "react-icons/fi"
1212
import { z } from "zod"
1313

14-
import { type ItemPublic, ItemsService } from "@/client"
14+
import { type ItemPublic, type ItemsResponse, ItemsService } from "@/client"
1515
import { ItemActionsMenu } from "@/components/Common/ItemActionsMenu"
1616
import AddItem from "@/components/Items/AddItem"
1717
import PendingItems from "@/components/Pending/PendingItems"
@@ -60,17 +60,8 @@ function ItemsTable() {
6060
})
6161

6262
// 安全地处理API响应
63-
const apiData = apiResponse?.data || ({} as any)
64-
65-
// 使用类型断言处理响应数据
66-
const items: ItemPublic[] = Array.isArray(apiData.data)
67-
? apiData.data
68-
: Array.isArray(apiData)
69-
? apiData
70-
: []
71-
72-
// 使用类型断言处理计数
73-
const count = typeof apiData.count === "number" ? apiData.count : items.length
63+
const items: ItemPublic[] = apiResponse?.data?.data?.data || [];
64+
const count = apiResponse?.data?.data?.count || 0;
7465

7566
if (isLoading) {
7667
return <PendingItems />

0 commit comments

Comments
 (0)