37 lines
1.0 KiB
TypeScript
37 lines
1.0 KiB
TypeScript
import type { MapOption } from "../types";
|
|
|
|
const API_BASE_URL = import.meta.env.PROD
|
|
? '' // Empty string for same-origin requests in production
|
|
: 'http://localhost:3001';
|
|
|
|
const apiCall = async (endpoint: string, options: RequestInit = {}) => {
|
|
const response = await fetch(`${API_BASE_URL}${endpoint}`, {
|
|
...options,
|
|
credentials: 'include', // Always include cookies
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
...options.headers,
|
|
},
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
}
|
|
|
|
return response.json();
|
|
};
|
|
|
|
export const authService = {
|
|
getCurrentUser: () => apiCall('/auth/user'),
|
|
logout: () => apiCall('/auth/logout', { method: 'POST' }),
|
|
};
|
|
|
|
export const voteService = {
|
|
submitVote: (vote: MapOption[]) =>
|
|
apiCall('/api/submit-vote', {
|
|
method: 'POST',
|
|
body: JSON.stringify({ vote }),
|
|
}),
|
|
getVoteStatus: () => apiCall('/api/vote-status'),
|
|
getResults: () => apiCall('/api/results'),
|
|
}; |