23 lines
671 B
TypeScript
23 lines
671 B
TypeScript
import { ApiClient } from "@personal-brand/api-client";
|
|
|
|
export const apiBaseUrl = import.meta.env.VITE_API_BASE_URL ?? "/api";
|
|
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,
|
|
});
|
|
}
|