This commit is contained in:
phaichayon
2026-04-23 15:37:01 +07:00
parent 67960174d3
commit a330abf9b6
36 changed files with 4656 additions and 278 deletions

View File

@@ -1,12 +1,36 @@
const BASE_URL = '/api';
const BASE_URL = "/api";
export async function apiClient<T>(
endpoint: string,
options?: RequestInit,
): Promise<T> {
// Get token from global window object (set by Keycloak client)
const token =
typeof window !== "undefined" ? (window as any).__KEYCLOAK_TOKEN__ : null;
const headers: Record<string, string> = {
"Content-Type": "application/json",
...((options?.headers as Record<string, string>) || {}),
};
// Add Authorization header if token exists
if (token) {
headers["Authorization"] = `Bearer ${token}`;
}
export async function apiClient<T>(endpoint: string, options?: RequestInit): Promise<T> {
const res = await fetch(`${BASE_URL}${endpoint}`, {
headers: { 'Content-Type': 'application/json' },
...options
...options,
headers,
});
if (!res.ok) {
// Handle 401 - token expired
if (res.status === 401) {
// Trigger token refresh by dispatching event
if (typeof window !== "undefined") {
window.dispatchEvent(new CustomEvent("token-expired"));
}
}
throw new Error(`API error: ${res.status} ${res.statusText}`);
}