Files
WifiShare/services/api.ts
2026-02-06 16:15:47 +09:00

58 lines
1.9 KiB
TypeScript

import { WiFiHotspot } from '../types';
const API_Base_URL = '/api/markers';
export const apiService = {
getHotspots: async (): Promise<WiFiHotspot[]> => {
const fullUrl = `${window.location.origin}${API_Base_URL}`;
console.log(`[API Call] GET ${fullUrl} (Origin: ${window.location.origin})`);
try {
const response = await fetch(API_Base_URL, { cache: 'no-store' }); // 캐시 방지 추가
if (!response.ok) throw new Error('Failed to fetch markers');
const data = await response.json();
console.log(`[API Response] Received ${data.length} hotspots`, data);
return data;
} catch (e) {
console.error("Failed to load hotspots from API", e);
return [];
}
},
saveHotspot: async (hotspot: WiFiHotspot): Promise<void> => {
console.log(`[API Call] POST ${API_Base_URL}`, hotspot);
try {
const response = await fetch(API_Base_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(hotspot),
});
if (!response.ok) throw new Error('Failed to save marker');
const data = await response.json();
console.log(`[API Response] Save success:`, data);
// Trigger a storage event to refresh UI if needed (keeping compatibility)
window.dispatchEvent(new Event('storage_updated'));
} catch (e) {
console.error("Failed to save hotspot", e);
}
},
deleteHotspot: async (id: string): Promise<void> => {
const url = `${API_Base_URL}/${id}`;
console.log(`[API Call] DELETE ${url}`);
try {
const response = await fetch(url, {
method: 'DELETE',
});
if (!response.ok) throw new Error('Failed to delete marker');
const data = await response.json();
console.log(`[API Response] Delete success:`, data);
window.dispatchEvent(new Event('storage_updated'));
} catch (e) {
console.error("Failed to delete hotspot", e);
}
}
};