export type units = "metric" | "imperial"; export const fetchWeather = async ( city: string, apiKey: string, units: units ) => { // Get location data const location = await fetch( `http://api.openweathermap.org/geo/1.0/direct?q=${city}&appid=${apiKey}` ).then((response) => { if (response.status === 401) { console.error( "You are not authorized to access this resource. Please check your API key." ); } else if (response.ok) { return response.json(); } else { console.error("Error fetching location data: ", response.statusText); } }); const lat = location[0].lat; const lon = location[0].lon; // Get weather data const weather = await fetch( `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${apiKey}&units=${units}` ).then((response) => response.json()); localStorage.setItem("weather", JSON.stringify(weather)); };