From d811478d0fb46e90eca7087dc720f23a858b9dc8 Mon Sep 17 00:00:00 2001 From: que01 Date: Sat, 19 Sep 2026 18:44:47 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=8A=BD=E7=A6=BB=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E5=90=8E=E5=8F=B0=E5=9F=BA=E7=A1=80=E8=AE=BE=E6=96=BD?= =?UTF-8?q?=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/admin/src/entry.ts | 7 ++----- apps/admin/src/lib/api.ts | 22 ++++++++++++++++++++++ apps/admin/src/main.tsx | 18 ++---------------- 3 files changed, 26 insertions(+), 21 deletions(-) create mode 100644 apps/admin/src/lib/api.ts diff --git a/apps/admin/src/entry.ts b/apps/admin/src/entry.ts index d85f9f5..020ea3a 100644 --- a/apps/admin/src/entry.ts +++ b/apps/admin/src/entry.ts @@ -1,5 +1,5 @@ -import { ApiClient } from "@personal-brand/api-client"; import { bootstrapAdmin } from "./main.js"; +import { createApiClient } from "./lib/api.js"; const root = document.querySelector("#app"); if (root) { @@ -11,10 +11,7 @@ if (root) { const username = (root.querySelector("#username") as HTMLInputElement).value; const password = (root.querySelector("#password") as HTMLInputElement).value; try { - const client = new ApiClient({ - baseUrl: import.meta.env.VITE_API_BASE_URL ?? "http://localhost:3001", - fetcher: (input, init) => fetch(input, init), - }); + const client = createApiClient(); const result = await client.loginAdmin(username, password); localStorage.setItem("adminToken", result.token); location.reload(); diff --git a/apps/admin/src/lib/api.ts b/apps/admin/src/lib/api.ts new file mode 100644 index 0000000..c19edb1 --- /dev/null +++ b/apps/admin/src/lib/api.ts @@ -0,0 +1,22 @@ +import { ApiClient } from "@personal-brand/api-client"; + +export const apiBaseUrl = import.meta.env.VITE_API_BASE_URL ?? "http://localhost:3001"; +export async function authenticatedFetch( + input: RequestInfo | URL, + init?: RequestInit, +): Promise { + const response = await fetch(input, init); + if (response.status === 401) { + localStorage.removeItem("adminToken"); + window.dispatchEvent(new Event("admin-auth-expired")); + } + return response; +} +export function createApiClient(): ApiClient { + const token = localStorage.getItem("adminToken"); + return new ApiClient({ + baseUrl: apiBaseUrl, + ...(token ? { token } : {}), + fetcher: authenticatedFetch, + }); +} diff --git a/apps/admin/src/main.tsx b/apps/admin/src/main.tsx index 0e3e67a..be17375 100644 --- a/apps/admin/src/main.tsx +++ b/apps/admin/src/main.tsx @@ -25,7 +25,7 @@ import { Modal, Badge, } from "antd"; -import { ApiClient } from "@personal-brand/api-client"; +import { createApiClient } from "./lib/api.js"; import { BrowserRouter, Navigate, Route, Routes, useLocation, useNavigate } from "react-router-dom"; import type { BrandProfile, @@ -35,22 +35,8 @@ import type { } from "@personal-brand/api-client"; import "antd/dist/reset.css"; -const apiBaseUrl = import.meta.env.VITE_API_BASE_URL ?? "http://localhost:3001"; const queryClient = new QueryClient(); -async function authenticatedFetch(input: RequestInfo | URL, init?: RequestInit): Promise { - const response = await fetch(input, init); - if (response.status === 401) { - localStorage.removeItem("adminToken"); - window.dispatchEvent(new Event("admin-auth-expired")); - } - return response; -} -const api = () => - new ApiClient({ - baseUrl: apiBaseUrl, - token: localStorage.getItem("adminToken") ?? undefined, - fetcher: authenticatedFetch, - }); +const api = createApiClient; function Login({ onLogin }: { onLogin: () => void }) { const [form] = Form.useForm();