fix: support authenticated web deployments under a reverse proxy path

This commit is contained in:
AttilaNet
2026-11-10 20:01:44 +08:00
parent 0230a933b0
commit 99aff423d8
12 changed files with 529 additions and 31 deletions
+5 -2
View File
@@ -1,3 +1,5 @@
import {apiPath, webPath} from './paths.js';
export class ApiError extends Error {
constructor(message, status, detail) {
super(message);
@@ -16,17 +18,18 @@ function errorMessage(data, status) {
}
export async function request(path, {method = 'GET', body, signal, blob = false} = {}) {
const url = apiPath(path);
const headers = {Accept: blob ? '*/*' : 'application/json'};
if (body !== undefined && !(body instanceof FormData)) headers['Content-Type'] = 'application/json';
let response;
try {
response = await fetch(path, {method, credentials: 'same-origin', cache: 'no-store', headers, signal,
response = await fetch(url, {method, credentials: 'same-origin', cache: 'no-store', redirect: 'error', headers, signal,
body: body === undefined ? undefined : body instanceof FormData ? body : JSON.stringify(body)});
} catch (error) {
if (error.name === 'AbortError') throw error;
throw new ApiError('无法连接 Web 服务,请检查服务是否仍在运行。', 0);
}
if (response.status === 401 && !path.startsWith('/api/auth/')) window.dispatchEvent(new CustomEvent('auth-required'));
if (response.status === 401 && !url.startsWith(webPath('/api/auth/'))) window.dispatchEvent(new CustomEvent('auth-required'));
if (!response.ok) {
const data = await response.json().catch(() => null);
throw new ApiError(errorMessage(data, response.status), response.status, data);