refactor: 抽离管理后台基础设施模块
This commit is contained in:
1 parent
cfa183015b
commit
d811478d0f
3 files changed
+26
-21
No files matched your search
@@ -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<Response> {
|
||||
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,
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user