feat: add model and skill admin pages

This commit is contained in:
hailin
2026-07-14 14:08:04 +08:00
parent 9d4fae58bd
commit 92b7af118e
15 changed files with 669 additions and 9 deletions

View File

@@ -2,13 +2,38 @@ const API_BASE_URL = import.meta.env.VITE_API_BASE_URL ?? 'http://localhost:8000
let csrfToken: string | null = null
export class ApiError extends Error {
status: number
payload: unknown
constructor(status: number, message: string, payload: unknown) {
super(message)
this.name = 'ApiError'
this.status = status
this.payload = payload
}
}
async function readError(response: Response) {
let payload: unknown
try {
payload = await response.json()
} catch {
payload = null
}
const detail = payload && typeof payload === 'object' && 'detail' in payload
? String((payload as { detail: unknown }).detail)
: `API 请求失败(${response.status})`
return new ApiError(response.status, detail, payload)
}
async function getCsrfToken() {
if (!csrfToken) {
const response = await fetch(`${API_BASE_URL}/accounts/auth/csrf`, {
credentials: 'include'
})
if (!response.ok) {
throw new Error(`API request failed: ${response.status}`)
throw await readError(response)
}
const data = (await response.json()) as { csrfToken: string }
csrfToken = data.csrfToken
@@ -33,7 +58,7 @@ export async function apiRequest<T>(path: string, init?: RequestInit): Promise<T
})
if (!response.ok) {
throw new Error(`API request failed: ${response.status}`)
throw await readError(response)
}
if (response.status === 204) {