File size: 2,970 Bytes
de6a95b
 
6dd9bad
 
2ab1980
 
 
 
 
 
cfeac02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6dd9bad
 
 
 
 
 
 
 
 
 
 
cfeac02
 
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
68
export const API_URL = import.meta.env.VITE_API_URL || 'http://localhost:3001';

export const ah = (token: string, orgId?: string | null): Record<string, string> => {
    const headers: Record<string, string> = {
        'Authorization': `Bearer ${token}`,
        'Content-Type': 'application/json'
    };
    if (orgId) headers['x-organization-id'] = orgId;
    return headers;
};

export const api = {
    async request(path: string, options: any = {}, token: string | null = null, orgId: string | null = null) {
        const url = path.startsWith('http') ? path : `${API_URL}${path}`;
        const headers = {
            ...ah(token || '', orgId),
            ...(options.headers || {})
        };

        const res = await fetch(url, { ...options, headers });
        
        if (res.status === 401) {
            // Global 401 handling: Clear session and redirect to login
            sessionStorage.clear();
            window.location.href = '/login';
            throw new Error('Unauthorized');
        }

        if (!res.ok) {
            const error = await res.json().catch(() => ({ error: 'Unknown error' }));
            throw new Error(error.error || `Request failed with status ${res.status}`);
        }

        return res.json();
    },

    get(path: string, token: string | null, orgId: string | null = null) {
        return this.request(path, { method: 'GET' }, token, orgId);
    },

    post(path: string, body: any, token: string | null, orgId: string | null = null) {
        return this.request(path, { method: 'POST', body: JSON.stringify(body) }, token, orgId);
    },

    put(path: string, body: any, token: string | null, orgId: string | null = null) {
        return this.request(path, { method: 'PUT', body: JSON.stringify(body) }, token, orgId);
    },

    patch(path: string, body: any, token: string | null, orgId: string | null = null) {
        return this.request(path, { method: 'PATCH', body: JSON.stringify(body) }, token, orgId);
    },

    delete(path: string, token: string | null, orgId: string | null = null) {
        return this.request(path, { method: 'DELETE' }, token, orgId);
    },

    async upload(path: string, formData: FormData, token: string | null, orgId: string | null = null) {
        // Omit Content-Type so the browser sets multipart boundary automatically
        const url = path.startsWith('http') ? path : `${API_URL}${path}`;
        const headers: Record<string, string> = { 'Authorization': `Bearer ${token || ''}` };
        if (orgId) headers['x-organization-id'] = orgId;
        const res = await fetch(url, { method: 'POST', headers, body: formData });
        if (res.status === 401) { sessionStorage.clear(); window.location.href = '/login'; throw new Error('Unauthorized'); }
        if (!res.ok) { const e = await res.json().catch(() => ({ error: 'Unknown error' })); throw new Error(e.error || `Upload failed with status ${res.status}`); }
        return res.json();
    }
};