Files
ember-market-frontend/lib/server-service.ts
2025-03-24 00:30:01 +00:00

58 lines
1.7 KiB
TypeScript

import { cookies } from 'next/headers';
import { redirect } from 'next/navigation';
import { getServerApiUrl } from './api-utils';
/**
* Server-side fetch wrapper with authentication.
* Used in Server Components to make authenticated API requests to the backend.
* This uses the SERVER_API_URL environment variable and is different from client-side fetching.
*/
export async function fetchServer<T = unknown>(
endpoint: string,
options: RequestInit = {}
): Promise<T> {
// Get auth token from cookies
const cookieStore = await cookies();
const authToken = cookieStore.get('Authorization')?.value;
// Redirect to login if not authenticated
if (!authToken) redirect('/login');
try {
// Get the complete backend URL using the utility function
const url = getServerApiUrl(endpoint);
console.log(`Making server request to: ${url}`);
// Make the request with proper auth headers
const res = await fetch(url, {
...options,
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${authToken}`,
...options.headers,
},
cache: 'no-store', // Always fetch fresh data
});
// Handle auth failures
if (res.status === 401) redirect('/login');
// Handle other errors
if (!res.ok) {
const errorData = await res.json().catch(() => ({}));
const errorMessage = errorData.message || errorData.error || `Request failed: ${res.status} ${res.statusText}`;
throw new Error(errorMessage);
}
// Handle 204 No Content responses
if (res.status === 204) {
return {} as T;
}
return await res.json();
} catch (error) {
console.error(`Server request to ${endpoint} failed:`, error);
throw error;
}
}