This commit is contained in:
Iliyan Angelov
2025-12-10 01:36:00 +02:00
parent 2f6dca736a
commit 6a9e823402
84 changed files with 5293 additions and 1836 deletions

View File

@@ -30,14 +30,35 @@ export interface PolicyListItem {
}
class PolicyServiceAPI {
private baseUrl = `${API_BASE_URL}/api/policies`;
private getBaseUrl(): string {
// Safely get base URL, handling both server and client environments
try {
const base = API_BASE_URL || '';
if (base) {
return `${base}/api/policies`;
}
// Fallback for SSR or when API_BASE_URL is not available
if (typeof window !== 'undefined') {
// Client-side: use relative URL (proxied by nginx)
return '/api/policies';
}
// Server-side: use environment variable or fallback
return `${process.env.NEXT_PUBLIC_SITE_URL || 'https://gnxsoft.com'}/api/policies`;
} catch (error) {
// Ultimate fallback
if (typeof window !== 'undefined') {
return '/api/policies';
}
return 'https://gnxsoft.com/api/policies';
}
}
/**
* Get all policies
*/
async getPolicies(): Promise<PolicyListItem[]> {
try {
const response = await fetch(`${this.baseUrl}/`, {
const response = await fetch(`${this.getBaseUrl()}/`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
@@ -60,20 +81,36 @@ class PolicyServiceAPI {
*/
async getPolicyByType(type: 'privacy' | 'terms' | 'support'): Promise<Policy> {
try {
const response = await fetch(`${this.baseUrl}/${type}/`, {
const baseUrl = this.getBaseUrl();
const url = `${baseUrl}/${type}/`;
const response = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
// Add cache control for client-side requests
cache: 'no-store',
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
const errorText = await response.text().catch(() => 'Unknown error');
throw new Error(`HTTP error! status: ${response.status}, message: ${errorText}`);
}
const data = await response.json();
// Validate response structure
if (!data || typeof data !== 'object') {
throw new Error('Invalid response format from API');
}
return data;
} catch (error) {
// Log error for debugging (only on client side)
if (typeof window !== 'undefined') {
console.error(`Error fetching policy type "${type}":`, error);
}
throw error;
}
}
@@ -83,7 +120,7 @@ class PolicyServiceAPI {
*/
async getPolicyById(id: number): Promise<Policy> {
try {
const response = await fetch(`${this.baseUrl}/${id}/`, {
const response = await fetch(`${this.getBaseUrl()}/${id}/`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',