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

57 lines
1.8 KiB
TypeScript

import { WiFiHotspot } from '../types';
const API_Base_URL = '/api/markers';
export const apiService = {
getHotspots: async (): Promise<WiFiHotspot[]> => {
console.log(`[API Call] GET ${API_Base_URL}`);
try {
const response = await fetch(API_Base_URL);
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);
}
}
};