Some checks failed
CI Check / check (push) Has been cancelled
- 创建统一UI三层架构系统(基础层、颜色层、组合层) - 拆分Admin页面为独立Tab组件(MonitorTab、UsersTab、DepartmentsTab等) - 提取公共逻辑到Hooks(useToast、useDataFetch、useUserOperations等) - 提取工具函数(validators、formatters、api等) - 创建统一弹窗组件(Modal)和useModal Hook - 添加错误边界和统一状态组件 - 完善类型定义和可访问性支持 - 更新文档(优化记录、优化计划、更新日志)
95 lines
2.5 KiB
TypeScript
95 lines
2.5 KiB
TypeScript
/**
|
||
* API 工具函数
|
||
* 统一处理 API 请求和错误响应
|
||
*/
|
||
|
||
import type { ApiErrorResponse, ApiSuccessResponse } from '../_components/types'
|
||
|
||
/**
|
||
* 安全地解析 API 错误响应
|
||
* @param response - fetch Response 对象
|
||
* @returns Promise<ApiErrorResponse>
|
||
*/
|
||
export async function parseErrorResponse(response: Response): Promise<ApiErrorResponse> {
|
||
try {
|
||
const data = await response.json()
|
||
return (data as ApiErrorResponse) || { error: '请求失败' }
|
||
} catch {
|
||
// 如果响应不是 JSON,返回默认错误
|
||
return { error: `HTTP ${response.status}: ${response.statusText || '请求失败'}` }
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 安全地解析 API 成功响应
|
||
* @param response - fetch Response 对象
|
||
* @returns Promise<T>
|
||
*/
|
||
export async function parseSuccessResponse<T = unknown>(response: Response): Promise<T> {
|
||
const data = await response.json()
|
||
return data as T
|
||
}
|
||
|
||
/**
|
||
* 统一的 API 请求函数,自动处理错误
|
||
* @param url - API 端点
|
||
* @param options - fetch 选项
|
||
* @returns Promise<{ success: true; data: T } | { success: false; error: string }>
|
||
*/
|
||
export async function apiRequest<T = unknown>(
|
||
url: string,
|
||
options?: RequestInit
|
||
): Promise<{ success: true; data: T } | { success: false; error: string }> {
|
||
try {
|
||
const response = await fetch(url, options)
|
||
|
||
if (!response.ok) {
|
||
const errorData = await parseErrorResponse(response)
|
||
return {
|
||
success: false,
|
||
error: errorData.error || `HTTP ${response.status}: ${response.statusText || '请求失败'}`,
|
||
}
|
||
}
|
||
|
||
const data = await parseSuccessResponse<T>(response)
|
||
return {
|
||
success: true,
|
||
data,
|
||
}
|
||
} catch (error) {
|
||
console.error('API request error:', error)
|
||
const errorMessage = error instanceof Error ? error.message : '网络请求失败,请稍后重试'
|
||
return {
|
||
success: false,
|
||
error: errorMessage,
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取错误消息的辅助函数
|
||
* @param error - 错误对象或错误响应
|
||
* @param defaultMessage - 默认错误消息
|
||
* @returns 错误消息字符串
|
||
*/
|
||
export function getErrorMessage(
|
||
error: unknown,
|
||
defaultMessage: string = '操作失败'
|
||
): string {
|
||
if (typeof error === 'string') {
|
||
return error
|
||
}
|
||
|
||
if (error instanceof Error) {
|
||
return error.message || defaultMessage
|
||
}
|
||
|
||
if (error && typeof error === 'object' && 'error' in error) {
|
||
const apiError = error as ApiErrorResponse
|
||
return apiError.error || defaultMessage
|
||
}
|
||
|
||
return defaultMessage
|
||
}
|
||
|