erm what the sigma

This commit is contained in:
NotII
2025-03-24 13:43:42 +00:00
parent 6fafe69f2c
commit edcd0c1e06
11 changed files with 562 additions and 16 deletions

View File

@@ -1,4 +1,5 @@
/**
<<<<<<< Updated upstream
* API utilities for consistent request handling
*/
@@ -33,17 +34,63 @@ export function getServerApiUrl(endpoint: string): string {
return apiUrl.endsWith('/')
? `${apiUrl}${cleanEndpoint}`
: `${apiUrl}/${cleanEndpoint}`;
=======
* 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}`;
>>>>>>> Stashed changes
}
/**
* Get the authentication token from cookies or localStorage
<<<<<<< Updated upstream
*/
export function getAuthToken(): string | null {
if (typeof document === 'undefined') return null; // Guard for SSR
=======
* Only available in client-side code
*/
export function getAuthToken(): string | null {
if (typeof document === 'undefined') return null;
>>>>>>> Stashed changes
return document.cookie
.split('; ')
.find(row => row.startsWith('Authorization='))
<<<<<<< Updated upstream
?.split('=')[1] || localStorage.getItem('Authorization');
}
@@ -72,5 +119,27 @@ export function createApiHeaders(token?: string | null, customHeaders: Record<st
headers.set('Authorization', `Bearer ${authToken}`);
}
=======
?.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<string, string> = {}): 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}`);
}
>>>>>>> Stashed changes
return headers;
}

View File

@@ -21,12 +21,6 @@ export async function clientFetch<T = any>(url: string, options: RequestInit = {
if (!res.ok) {
const errorData = await res.json().catch(() => ({}));
const errorMessage = errorData.message || errorData.error || `Request failed: ${res.status} ${res.statusText}`;
console.error('API Error:', {
status: res.status,
url: fullUrl,
response: errorData,
method: options.method || 'GET'
});
throw new Error(errorMessage);
}