File size: 1,048 Bytes
a6480fb
 
 
 
 
 
 
 
6e0747c
 
 
 
 
 
 
 
a6480fb
 
6e0747c
a6480fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { supabase } from './supabase';

/**
 * Backend API base URL'ini döner.
 * - Tarayıcıda: '' (relative path → Vercel proxy üzerinden /api/...)
 * - Sunucuda (SSR): NEXT_PUBLIC_API_URL veya localhost fallback
 */
export function getApiUrl(): string {
  if (process.env.NEXT_PUBLIC_API_URL) {
    return process.env.NEXT_PUBLIC_API_URL;
  }
  // Tarayıcıda relative path kullan ('') -> Vercel proxy rewrite (/api/...) üzerinden çalışır
  if (typeof window !== 'undefined') {
    return '';
  }
  return 'http://127.0.0.1:8000';
}


/**
 * Auth token'lı fetch wrapper.
 * Supabase session varsa Authorization header ekler.
 */
export async function apiFetch(
  path: string,
  options: RequestInit = {}
): Promise<Response> {
  const {
    data: { session },
  } = await supabase.auth.getSession();
  const token = session?.access_token;

  const url = `${getApiUrl()}${path}`;

  return fetch(url, {
    ...options,
    headers: {
      ...options.headers,
      ...(token && { Authorization: `Bearer ${token}` }),
    },
  });
}