32 lines
1.1 KiB
TypeScript
32 lines
1.1 KiB
TypeScript
/**
|
|
* Helper function to construct proper URLs for server components.
|
|
* In Next.js server components, relative URLs don't work in fetch() calls.
|
|
*/
|
|
export function getServerApiUrl(endpoint: string): string {
|
|
// Base API URL - use a hardcoded value for server components
|
|
// This must be an absolute URL with protocol and domain
|
|
const baseUrl = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3000/api';
|
|
|
|
// Ensure endpoint doesn't start with a slash if baseUrl ends with one
|
|
const normalizedEndpoint = endpoint.startsWith('/') ? endpoint.substring(1) : endpoint;
|
|
|
|
// Ensure the baseUrl ends with a slash if it doesn't already
|
|
const normalizedBaseUrl = baseUrl.endsWith('/') ? baseUrl : `${baseUrl}/`;
|
|
|
|
// Combine them to get a complete URL
|
|
return `${normalizedBaseUrl}${normalizedEndpoint}`;
|
|
}
|
|
|
|
/**
|
|
* Helper to detect if code is running on server or client
|
|
*/
|
|
export function isServer(): boolean {
|
|
return typeof window === 'undefined';
|
|
}
|
|
|
|
/**
|
|
* Helper to detect if code is running in development mode
|
|
*/
|
|
export function isDevelopment(): boolean {
|
|
return process.env.NODE_ENV === 'development';
|
|
}
|