mirror of
https://github.com/MacRimi/ProxMenux.git
synced 2025-11-17 19:16:25 +00:00
Update api-config.ts
This commit is contained in:
@@ -60,6 +60,18 @@ export function getApiUrl(endpoint: string): string {
|
||||
return `${baseUrl}${normalizedEndpoint}`
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the JWT token from localStorage
|
||||
*
|
||||
* @returns JWT token or null if not authenticated
|
||||
*/
|
||||
export function getAuthToken(): string | null {
|
||||
if (typeof window === "undefined") {
|
||||
return null
|
||||
}
|
||||
return localStorage.getItem("proxmenux-auth-token")
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches data from an API endpoint with error handling
|
||||
*
|
||||
@@ -70,12 +82,20 @@ export function getApiUrl(endpoint: string): string {
|
||||
export async function fetchApi<T>(endpoint: string, options?: RequestInit): Promise<T> {
|
||||
const url = getApiUrl(endpoint)
|
||||
|
||||
const token = getAuthToken()
|
||||
|
||||
const headers: Record<string, string> = {
|
||||
"Content-Type": "application/json",
|
||||
...(options?.headers as Record<string, string>),
|
||||
}
|
||||
|
||||
if (token) {
|
||||
headers["Authorization"] = `Bearer ${token}`
|
||||
}
|
||||
|
||||
const response = await fetch(url, {
|
||||
...options,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
...options?.headers,
|
||||
},
|
||||
headers,
|
||||
cache: "no-store",
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user