-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathweather.js
More file actions
32 lines (29 loc) · 987 Bytes
/
weather.js
File metadata and controls
32 lines (29 loc) · 987 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { get } from "wasi:config/store@0.2.0-draft";
export async function getWeather(city) {
const apiKey = await get("OPENWEATHER_API_KEY");
if (apiKey === undefined) {
throw "Error: OPENWEATHER_API_KEY is not set";
}
try {
const geoResponse = await fetch(
`https://api.openweathermap.org/geo/1.0/direct?q=${city}&limit=1&appid=${apiKey}`
);
if (!geoResponse.ok) {
throw "Error: Failed to fetch geo data";
}
const geoData = await geoResponse.json();
const lat = geoData[0].lat;
const lon = geoData[0].lon;
const response = await fetch(
`https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${apiKey}&units=metric`
);
if (!response.ok) {
throw "Error: Failed to fetch weather data";
}
const data = await response.json();
const weather = data.main.temp.toString();
return weather;
} catch (error) {
throw error.message || "Error fetching weather data";
}
}