/** <<<<<<< Updated upstream <<<<<<< Updated upstream * API utilities for consistent request handling */ /** * Normalizes a URL to ensure it passes through the Next.js API proxy * This ensures all client-side requests go through the Next.js rewrites. * * @param url The endpoint URL to normalize * @returns A normalized URL with proper /api prefix */ export function normalizeApiUrl(url: string): string { if (url.startsWith('/api/')) { return url; // Already has /api/ prefix } else { // Add /api prefix, handling any leading slashes const cleanUrl = url.startsWith('/') ? url : `/${url}`; return `/api${cleanUrl}`; } } /** * Constructs a server-side API URL for backend requests * Used in Server Components and API routes to directly access the backend API * * @param endpoint The API endpoint path * @returns A complete URL to the backend API */ export function getServerApiUrl(endpoint: string): string { const apiUrl = process.env.SERVER_API_URL || 'https://internal-api.inboxi.ng/api'; const cleanEndpoint = endpoint.startsWith('/') ? endpoint.substring(1) : endpoint; return apiUrl.endsWith('/') ? `${apiUrl}${cleanEndpoint}` : `${apiUrl}/${cleanEndpoint}`; ======= ======= >>>>>>> Stashed changes * API utilities for client and server-side requests */ /** * Normalizes the API URL to ensure it uses the proper prefix * For client-side, ensures all requests go through the Next.js API proxy */ export function normalizeApiUrl(url: string): string { // If URL already starts with http or https, return as is if (url.startsWith('http://') || url.startsWith('https://')) { return url; } // If URL already starts with /api, use as is if (url.startsWith('/api/')) { return url; } // Otherwise, ensure it has the /api prefix return `/api${url.startsWith('/') ? '' : '/'}${url}`; } /** * Get the server API URL for server-side requests */ export function getServerApiUrl(endpoint: string): string { // Get the base API URL from environment const baseUrl = process.env.SERVER_API_URL || process.env.NEXT_PUBLIC_API_URL || 'https://internal-api.inboxi.ng/api'; // Ensure it doesn't have trailing slash const normalizedBaseUrl = baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl; // Ensure endpoint has leading slash const normalizedEndpoint = endpoint.startsWith('/') ? endpoint : `/${endpoint}`; return `${normalizedBaseUrl}${normalizedEndpoint}`; <<<<<<< Updated upstream >>>>>>> Stashed changes ======= >>>>>>> Stashed changes } /** * Get the authentication token from cookies or localStorage <<<<<<< Updated upstream <<<<<<< Updated upstream */ export function getAuthToken(): string | null { if (typeof document === 'undefined') return null; // Guard for SSR ======= ======= >>>>>>> Stashed changes * Only available in client-side code */ export function getAuthToken(): string | null { if (typeof document === 'undefined') return null; <<<<<<< Updated upstream >>>>>>> Stashed changes ======= >>>>>>> Stashed changes return document.cookie .split('; ') .find(row => row.startsWith('Authorization=')) <<<<<<< Updated upstream <<<<<<< Updated upstream ?.split('=')[1] || localStorage.getItem('Authorization'); } /** * Check if the user is logged in */ export function isAuthenticated(): boolean { return !!getAuthToken(); } /** * 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 */ export function createApiHeaders(token?: string | null, customHeaders: Record = {}): Headers { const headers = new Headers({ 'Content-Type': 'application/json', ...customHeaders }); const authToken = token || getAuthToken(); if (authToken) { headers.set('Authorization', `Bearer ${authToken}`); } ======= ======= >>>>>>> Stashed changes ?.split('=')[1] || (typeof localStorage !== 'undefined' ? localStorage.getItem('Authorization') : null); } /** * Create headers with authentication for API requests */ export function createApiHeaders(token: string | null = null, additionalHeaders: Record = {}): Headers { const headers = new Headers({ 'Content-Type': 'application/json', ...additionalHeaders }); // Use provided token or try to get it from storage const authToken = token || getAuthToken(); if (authToken) { headers.append('Authorization', `Bearer ${authToken}`); } <<<<<<< Updated upstream >>>>>>> Stashed changes ======= >>>>>>> Stashed changes return headers; }