Merge main into upgrade-nextjs with resolved conflicts
This commit is contained in:
@@ -1,4 +1,47 @@
|
||||
import { normalizeApiUrl, getAuthToken, createApiHeaders } from './api-utils';
|
||||
/**
|
||||
* Normalizes a URL to ensure it has the correct /api prefix
|
||||
* This prevents double prefixing which causes API errors
|
||||
*/
|
||||
function normalizeApiUrl(url: string): string {
|
||||
// Remove any existing /api or api prefix
|
||||
const cleanPath = url.replace(/^\/?(api\/)+/, '');
|
||||
|
||||
// Add a single /api prefix
|
||||
return `/api/${cleanPath.replace(/^\//, '')}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the authentication token from cookies or localStorage
|
||||
*/
|
||||
function getAuthToken(): string | null {
|
||||
if (typeof document === 'undefined') return null; // Guard for SSR
|
||||
|
||||
return document.cookie
|
||||
.split('; ')
|
||||
.find(row => row.startsWith('Authorization='))
|
||||
?.split('=')[1] || localStorage.getItem('Authorization');
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates standard API request headers with authentication
|
||||
*
|
||||
* @param token Optional auth token (fetched automatically if not provided)
|
||||
* @param customHeaders Additional headers to include
|
||||
* @returns Headers object ready for fetch requests
|
||||
*/
|
||||
function createApiHeaders(token?: string | null, customHeaders: Record<string, string> = {}): Headers {
|
||||
const headers = new Headers({
|
||||
'Content-Type': 'application/json',
|
||||
...customHeaders
|
||||
});
|
||||
|
||||
const authToken = token || getAuthToken();
|
||||
if (authToken) {
|
||||
headers.set('Authorization', `Bearer ${authToken}`);
|
||||
}
|
||||
|
||||
return headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple client-side fetch function for making API calls with Authorization header.
|
||||
|
||||
Reference in New Issue
Block a user