fix: 接口不再把页面当成运行面板数据
This commit is contained in:
+3
-1
@@ -5,7 +5,7 @@ from pathlib import Path
|
||||
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from fastapi.responses import FileResponse, JSONResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
|
||||
@@ -50,6 +50,8 @@ def create_app() -> FastAPI:
|
||||
|
||||
@application.get("/{path:path}")
|
||||
def pages(path: str) -> FileResponse:
|
||||
if path == "api" or path.startswith("api/"):
|
||||
raise HTTPException(status_code=404, detail="接口不存在")
|
||||
target = WEB_DIST / path
|
||||
if path and target.is_file():
|
||||
return FileResponse(target)
|
||||
|
||||
+9
-10
@@ -41,7 +41,7 @@ export async function api<T>(path: string, options: RequestInit = {}): Promise<T
|
||||
}
|
||||
const current = token();
|
||||
if (current) headers.set("Authorization", `Bearer ${current}`);
|
||||
const response = await fetch(path, { ...options, headers });
|
||||
const response = await fetch(path, { ...options, headers, cache: "no-store" });
|
||||
if (response.status === 401) {
|
||||
setToken("");
|
||||
sessionStorage.removeItem("decom-role");
|
||||
@@ -49,18 +49,17 @@ export async function api<T>(path: string, options: RequestInit = {}): Promise<T
|
||||
sessionStorage.removeItem("decom-id");
|
||||
sessionStorage.removeItem("decom-kind");
|
||||
}
|
||||
if (!response.ok) {
|
||||
let message = "请求失败";
|
||||
const text = await response.text();
|
||||
let payload: { detail?: string } | null = null;
|
||||
if (text) {
|
||||
try {
|
||||
const payload = await response.json();
|
||||
message = payload.detail || message;
|
||||
payload = JSON.parse(text) as { detail?: string };
|
||||
} catch {
|
||||
message = response.statusText || message;
|
||||
payload = null;
|
||||
}
|
||||
throw new ApiError(response.status, message);
|
||||
}
|
||||
if (response.headers.get("content-type")?.includes("application/json")) {
|
||||
return response.json() as Promise<T>;
|
||||
if (!response.ok || payload === null) {
|
||||
throw new ApiError(response.status, payload?.detail || "请求失败");
|
||||
}
|
||||
return response as unknown as T;
|
||||
return payload as T;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user