feat: add model and skill admin pages
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user