File size: 2,152 Bytes
24755ba
 
 
39d06f7
 
 
24755ba
 
 
 
 
 
 
 
 
 
 
39d06f7
 
24755ba
 
 
 
 
 
 
 
39d06f7
24755ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
 * Helper functions for API calls
 * Handles both server-side and client-side URL construction
 * 
 * IMPORTANT: We cannot import 'next/headers' at top level because it breaks client-side usage.
 * We use dynamic import only when needed on server-side.
 */

/**
 * Get the base URL for API calls
 * For server-side, we need absolute URLs
 * For client-side, we can use relative URLs
 */
export async function getApiBaseUrl(): Promise<string> {
  // Server-side: need absolute URL
  if (typeof window === 'undefined') {
    try {
      // Dynamically import headers only when server-side (avoids client-side import error)
      const { headers } = await import('next/headers');
      const headersList = await headers();
      const host = headersList.get('host');
      const protocol = headersList.get('x-forwarded-proto') || 'https';
      
      if (host) {
        return `${protocol}://${host}`;
      }
    } catch (error) {
      // headers() might not be available in all contexts (e.g., during build)
      // Fall through to environment variables
    }
    
    // Fallback: try environment variables
    const spaceUrl = 
      process.env.SPACE_URL || 
      process.env.NEXT_PUBLIC_APP_URL ||
      process.env.VERCEL_URL ||
      process.env.HF_SPACE_URL;
    
    if (spaceUrl) {
      // Ensure it has protocol
      if (spaceUrl.startsWith('http://') || spaceUrl.startsWith('https://')) {
        // Remove trailing slash
        return spaceUrl.endsWith('/') ? spaceUrl.slice(0, -1) : spaceUrl;
      }
      return `https://${spaceUrl}`;
    }
    
    // Last resort: use relative URL (Next.js should handle this for same-origin)
    return '';
  }
  
  // Client-side: use relative URL (empty string)
  return '';
}

/**
 * Build proxy URL for Hugging Face requests
 * Automatically handles server-side vs client-side
 * Note: This is async because getApiBaseUrl() is async (needed for server-side)
 */
export async function buildProxyUrl(targetUrl: string): Promise<string> {
  const apiBase = await getApiBaseUrl();
  const encodedUrl = encodeURIComponent(targetUrl);
  return `${apiBase}/api/hf-proxy?url=${encodedUrl}`;
}