73 lines
1.9 KiB
JavaScript
73 lines
1.9 KiB
JavaScript
// API Client for making requests to the backend
|
|
class APIClient {
|
|
constructor() {
|
|
this.baseURL = '/api';
|
|
}
|
|
|
|
async makeRequest(endpoint, options = {}) {
|
|
const config = {
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
...options.headers
|
|
},
|
|
...options
|
|
};
|
|
|
|
try {
|
|
const response = await fetch(`${this.baseURL}${endpoint}`, config);
|
|
const data = await response.json();
|
|
|
|
if (!response.ok) {
|
|
throw new Error(data.error || data.message || `HTTP ${response.status}`);
|
|
}
|
|
|
|
return data;
|
|
} catch (error) {
|
|
console.error('API request failed:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async get(endpoint) {
|
|
return this.makeRequest(endpoint, {
|
|
method: 'GET'
|
|
});
|
|
}
|
|
|
|
async post(endpoint, data) {
|
|
return this.makeRequest(endpoint, {
|
|
method: 'POST',
|
|
body: JSON.stringify(data)
|
|
});
|
|
}
|
|
|
|
// Health check
|
|
async checkHealth() {
|
|
return this.get('/health');
|
|
}
|
|
|
|
// Test Represent API connection
|
|
async testRepresent() {
|
|
return this.get('/test-represent');
|
|
}
|
|
|
|
// Get representatives by postal code
|
|
async getRepresentativesByPostalCode(postalCode) {
|
|
const cleanPostalCode = postalCode.replace(/\s/g, '').toUpperCase();
|
|
return this.get(`/representatives/by-postal/${cleanPostalCode}`);
|
|
}
|
|
|
|
// Refresh representatives for postal code
|
|
async refreshRepresentatives(postalCode) {
|
|
const cleanPostalCode = postalCode.replace(/\s/g, '').toUpperCase();
|
|
return this.post(`/representatives/refresh-postal/${cleanPostalCode}`);
|
|
}
|
|
|
|
// Send email to representative
|
|
async sendEmail(emailData) {
|
|
return this.post('/emails/send', emailData);
|
|
}
|
|
}
|
|
|
|
// Create global instance
|
|
window.apiClient = new APIClient(); |