Skip to content

Commit 24ec5dd

Browse files
committed
refactor: improve API response handling and type assertions in ItemsTable component
- Simplified the handling of API responses in the ItemsTable component by using type assertions for better clarity and safety. - Updated the way items and count are derived from the API response to ensure robustness against unexpected data structures. - Removed unused imports to streamline the codebase.
1 parent 945e9d3 commit 24ec5dd

File tree

5 files changed

+53
-22
lines changed

5 files changed

+53
-22
lines changed

admin/src/routes/_layout/items.tsx

Lines changed: 10 additions & 3 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, type ItemsResponse, ItemsService } from "@/client"
14+
import { type ItemPublic, 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,8 +60,15 @@ function ItemsTable() {
6060
})
6161

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

6673
if (isLoading) {
6774
return <PendingItems />

backend/app/core/config.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
from pydantic import (
88
AnyUrl,
9-
BaseModel,
109
BeforeValidator,
1110
EmailStr,
1211
HttpUrl,
@@ -123,29 +122,37 @@ def SQLALCHEMY_DATABASE_URI(self) -> PostgresDsn:
123122

124123
password = urllib.parse.quote_plus(self.SUPABASE_DB_PASSWORD or "")
125124

126-
return PostgresDsn(str(URL.build(
127-
scheme="postgresql+psycopg",
128-
user=self.SUPABASE_DB_USER or "",
129-
password=password,
130-
host=self.SUPABASE_DB_HOST,
131-
port=port,
132-
path=f"/{self.SUPABASE_DB_NAME or ''}",
133-
)))
125+
return PostgresDsn(
126+
str(
127+
URL.build(
128+
scheme="postgresql+psycopg",
129+
user=self.SUPABASE_DB_USER or "",
130+
password=password,
131+
host=self.SUPABASE_DB_HOST,
132+
port=port,
133+
path=f"/{self.SUPABASE_DB_NAME or ''}",
134+
)
135+
)
136+
)
134137
else:
135138
# Use standard PostgreSQL connection
136139
# URL encode the password to handle special characters
137140
import urllib.parse
138141

139142
password = urllib.parse.quote_plus(self.POSTGRES_PASSWORD)
140143

141-
return PostgresDsn(str(URL.build(
142-
scheme="postgresql+psycopg",
143-
user=self.POSTGRES_USER,
144-
password=password,
145-
host=self.POSTGRES_SERVER,
146-
port=self.POSTGRES_PORT,
147-
path=f"/{self.POSTGRES_DB}",
148-
)))
144+
return PostgresDsn(
145+
str(
146+
URL.build(
147+
scheme="postgresql+psycopg",
148+
user=self.POSTGRES_USER,
149+
password=password,
150+
host=self.POSTGRES_SERVER,
151+
port=self.POSTGRES_PORT,
152+
path=f"/{self.POSTGRES_DB}",
153+
)
154+
)
155+
)
149156

150157
SMTP_TLS: bool = True
151158
SMTP_SSL: bool = False

backend/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ dev-dependencies = [
3333
"ruff<1.0.0,>=0.2.2",
3434
"pre-commit<4.0.0,>=3.6.2",
3535
"types-passlib<2.0.0.0,>=1.7.7.20240106",
36+
"types-requests<3.0.0.0,>=2.32.0",
3637
"coverage<8.0.0,>=7.4.3",
3738
]
3839

backend/uv.lock

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/openapi-ts.config.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { defineConfig } from "@hey-api/openapi-ts";
22
import { config } from "dotenv";
3+
import path from "path";
34

45
config({ path: ".env" });
56

6-
const openapiFile = process.env.OPENAPI_OUTPUT_FILE;
7+
// 提供默认值以防环境变量未被设置
8+
const openapiFile = process.env.OPENAPI_OUTPUT_FILE || path.resolve(process.cwd(), "openapi.json");
79

810
export default defineConfig({
911
client: "@hey-api/client-axios",
10-
input: openapiFile as string,
12+
input: openapiFile,
1113
output: {
1214
format: "prettier",
1315
lint: "eslint",

0 commit comments

Comments
 (0)