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