fix: 补上清单里还没对上的登录拒绝、会话回退和代理清理
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
type Denied = () => void;
|
||||
|
||||
let onDenied: Denied | null = null;
|
||||
|
||||
export function setSessionDenied(handler: Denied) {
|
||||
onDenied = handler;
|
||||
}
|
||||
|
||||
function requestUrl(input: RequestInfo | URL) {
|
||||
if (typeof input === "string") return input;
|
||||
if (input instanceof URL) return input.href;
|
||||
return input.url;
|
||||
}
|
||||
|
||||
export async function apiFetch(input: RequestInfo | URL, init?: RequestInit) {
|
||||
const response = await fetch(input, init);
|
||||
if (requestUrl(input).includes("/api/session")) return response;
|
||||
if (response.status === 401) {
|
||||
onDenied?.();
|
||||
return response;
|
||||
}
|
||||
if (response.status === 403) {
|
||||
const body = await response.clone().json().catch(() => null);
|
||||
const detail = body && typeof body === "object" ? (body as { detail?: unknown }).detail : "";
|
||||
if (detail === "管理员不能访问该接口") onDenied?.();
|
||||
}
|
||||
return response;
|
||||
}
|
||||
Reference in New Issue
Block a user