File size: 2,897 Bytes
5d9430a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// AIMHSA API Helper
window.AIMHSA = window.AIMHSA || {};

// API helper class for making HTTP requests
class AIMHSAAPI {
    constructor() {
        this.baseUrl = this.getApiBaseUrl();
        this.defaultHeaders = {
            'Content-Type': 'application/json'
        };
    }
    
    getApiBaseUrl() {
        if (window.AIMHSA && window.AIMHSA.Config) {
            return window.AIMHSA.Config.getApiBaseUrl();
        }
        // Fallback to auto-detection
        return `${window.location.protocol}//${window.location.hostname}${window.location.port ? ':' + window.location.port : ''}`;
    }
    
    async request(path, options = {}) {
        const url = `${this.baseUrl}${path}`;
        const config = {
            method: 'GET',
            headers: { ...this.defaultHeaders },
            ...options
        };
        
        // Add body if provided and method supports it
        if (options.body && ['POST', 'PUT', 'PATCH'].includes(config.method)) {
            config.body = typeof options.body === 'string' ? options.body : JSON.stringify(options.body);
        }
        
        try {
            const response = await fetch(url, config);
            
            if (!response.ok) {
                throw new Error(`HTTP ${response.status}: ${response.statusText}`);
            }
            
            const contentType = response.headers.get('content-type');
            if (contentType && contentType.includes('application/json')) {
                return await response.json();
            }
            
            return await response.text();
        } catch (error) {
            console.error(`API request failed: ${config.method} ${url}`, error);
            throw error;
        }
    }
    
    // Convenience methods
    get(path, options = {}) {
        return this.request(path, { ...options, method: 'GET' });
    }
    
    post(path, body, options = {}) {
        return this.request(path, { ...options, method: 'POST', body });
    }
    
    put(path, body, options = {}) {
        return this.request(path, { ...options, method: 'PUT', body });
    }
    
    delete(path, options = {}) {
        return this.request(path, { ...options, method: 'DELETE' });
    }
    
    // Authentication helpers
    setAuthToken(token) {
        this.defaultHeaders['Authorization'] = `Bearer ${token}`;
    }
    
    clearAuthToken() {
        delete this.defaultHeaders['Authorization'];
    }
    
    // Professional-specific helper
    setProfessionalId(professionalId) {
        this.defaultHeaders['X-Professional-ID'] = professionalId;
    }
    
    clearProfessionalId() {
        delete this.defaultHeaders['X-Professional-ID'];
    }
}

// Initialize global API instance
window.AIMHSA.API = new AIMHSAAPI();

// Export for module systems if available
if (typeof module !== 'undefined' && module.exports) {
    module.exports = AIMHSAAPI;
}