1
0

feat: 前端集成 Prettier 代码格式化

This commit is contained in:
2026-04-24 13:40:53 +08:00
parent 52007c9461
commit 365943e4c4
61 changed files with 1968 additions and 1698 deletions

View File

@@ -13,6 +13,7 @@ AI 网关管理前端,提供供应商配置和用量统计界面。
- **数据获取**: TanStack Query v5
- **样式**: SCSS Modules禁止使用纯 CSS
- **测试**: Vitest + React Testing Library + Playwright
- **代码格式化**: Prettier
## API 层
@@ -22,10 +23,10 @@ AI 网关管理前端,提供供应商配置和用量统计界面。
```typescript
// 发送请求时camelCase → snake_case
toApi({ providerId: "openai" }) // → { provider_id: "openai" }
toApi({ providerId: 'openai' }) // → { provider_id: "openai" }
// 接收响应时snake_case → camelCase
fromApi({ provider_id: "openai" }) // → { providerId: "openai" }
fromApi({ provider_id: 'openai' }) // → { providerId: "openai" }
```
### 统一请求函数
@@ -42,9 +43,9 @@ export async function request<T>(method: string, path: string, body?: unknown):
```typescript
class ApiError extends Error {
status: number; // HTTP 状态码
code?: string; // 业务错误码
message: string; // 错误消息
status: number // HTTP 状态码
code?: string // 业务错误码
message: string // 错误消息
}
```
@@ -56,13 +57,13 @@ class ApiError extends Error {
// src/hooks/useProviders.ts
export const providerKeys = {
all: ['providers'] as const,
};
}
// src/hooks/useModels.ts
export const modelKeys = {
all: ['models'] as const,
byProvider: (providerId: string) => [...modelKeys.all, { providerId }] as const,
};
}
```
### Mutation 使用
@@ -71,9 +72,9 @@ export const modelKeys = {
const mutation = useMutation({
mutationFn: createProvider,
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: providerKeys.all });
queryClient.invalidateQueries({ queryKey: providerKeys.all })
},
});
})
```
## 项目结构
@@ -142,9 +143,20 @@ bun run build
### 代码检查
```bash
bun run lint
bun run lint # ESLint 检查
bun run format:check # Prettier 格式检查
bun run check # 同时检查 lint 和格式
```
### 代码格式化
```bash
bun run format # 格式化所有文件
bun run fix # 修复 lint 问题并格式化
```
VS Code 保存时自动格式化(需安装 Prettier 扩展)。
## 测试
### 单元测试 + 组件测试
@@ -219,26 +231,30 @@ __tests__/
## 环境变量
| 变量 | 开发环境 | 生产环境 | 说明 |
|------|----------|----------|------|
| `VITE_API_BASE` | (空) | `/api` | API 基础路径,空则走 Vite proxy |
| 变量 | 开发环境 | 生产环境 | 说明 |
| --------------- | -------- | -------- | ------------------------------- |
| `VITE_API_BASE` | (空) | `/api` | API 基础路径,空则走 Vite proxy |
**E2E 测试特有**
- `NEX_BACKEND_PORT` - E2E 后端端口(默认 19026
- `NEX_E2E_TEMP_DIR` - E2E 临时目录
## 开发规范
- 所有样式使用 SCSS禁止使用纯 CSS 文件
- 组件级样式使用 SCSS Modules*.module.scss
- 组件级样式使用 SCSS Modules\*.module.scss
- 图标优先使用 TDesign 图标tdesign-icons-react
- TypeScript strict 模式,禁止 any 类型
- API 层自动处理 snake_case ↔ camelCase 字段转换
- 使用路径别名 `@/` 引用 src 目录
- 代码格式化使用 Prettier配置见 `.prettierrc`
- 编辑器配置见 `.editorconfig`(统一缩进、换行符、编码)
### 环境要求
- Bun 1.0 或更高版本
- VS Code 推荐安装 Prettier 和 ESLint 扩展(见 `.vscode/extensions.json`
### 添加新页面