feat: 移除仓库内 nginx 并拆分前端运行时
This commit is contained in:
@@ -7,9 +7,12 @@ import { useStudyStore } from "../store/study";
|
||||
import { TEXT } from "../locales";
|
||||
import { extendAccessToken, forceLogout, lockSession, markNetworkActive } from "../session/sessionManager";
|
||||
import { useSessionStore } from "../store/session";
|
||||
import { resolveApiBaseUrl } from "./baseUrl";
|
||||
|
||||
const apiBaseUrl = resolveApiBaseUrl(import.meta.env.VITE_API_BASE_URL);
|
||||
|
||||
const instance: AxiosInstance = axios.create({
|
||||
baseURL: "/",
|
||||
baseURL: apiBaseUrl,
|
||||
timeout: 15000,
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { resolveApiBaseUrl } from "./baseUrl";
|
||||
|
||||
describe("resolveApiBaseUrl", () => {
|
||||
it("defaults to same-origin root when no env override is set", () => {
|
||||
expect(resolveApiBaseUrl(undefined)).toBe("/");
|
||||
expect(resolveApiBaseUrl("")).toBe("/");
|
||||
expect(resolveApiBaseUrl(" ")).toBe("/");
|
||||
});
|
||||
|
||||
it("preserves explicit root", () => {
|
||||
expect(resolveApiBaseUrl("/")).toBe("/");
|
||||
});
|
||||
|
||||
it("trims whitespace and trailing slashes from absolute base urls", () => {
|
||||
expect(resolveApiBaseUrl(" http://localhost:8000/ ")).toBe("http://localhost:8000");
|
||||
expect(resolveApiBaseUrl("https://api.example.com///")).toBe("https://api.example.com");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
const trimTrailingSlashes = (value: string): string => value.replace(/\/+$/, "");
|
||||
|
||||
export const resolveApiBaseUrl = (rawValue: string | undefined): string => {
|
||||
const normalized = rawValue?.trim() ?? "";
|
||||
if (!normalized) {
|
||||
return "/";
|
||||
}
|
||||
if (normalized === "/") {
|
||||
return "/";
|
||||
}
|
||||
return trimTrailingSlashes(normalized);
|
||||
};
|
||||
Reference in New Issue
Block a user