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

48 lines
1.4 KiB
TypeScript

import { WiFiHotspot } from '../types';
const API_Base_URL = 'http://localhost:3000/api/markers';
export const apiService = {
getHotspots: async (): Promise<WiFiHotspot[]> => {
try {
const response = await fetch(API_Base_URL);
if (!response.ok) throw new Error('Failed to fetch markers');
const data = await response.json();
return data;
} catch (e) {
console.error("Failed to load hotspots from API", e);
return [];
}
},
saveHotspot: async (hotspot: WiFiHotspot): Promise<void> => {
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');
// 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> => {
try {
const response = await fetch(`${API_Base_URL}/${id}`, {
method: 'DELETE',
});
if (!response.ok) throw new Error('Failed to delete marker');
window.dispatchEvent(new Event('storage_updated'));
} catch (e) {
console.error("Failed to delete hotspot", e);
}
}
};