-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
140 lines (116 loc) · 3.76 KB
/
app.js
File metadata and controls
140 lines (116 loc) · 3.76 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { getEnvironment } from "wasi:cli/environment@0.2.7";
const OPENWEATHER_ENDPOINT = "https://api.openweathermap.org/data/2.5/weather";
const TIMEOUT_SECS = 10000; // 10 seconds in milliseconds
class Unit {
static METRIC = "metric";
static IMPERIAL = "imperial";
}
class WeatherParams {
constructor(location, unit) {
this.location = location;
this.unit = unit;
}
}
class WeatherResponse {
constructor(data) {
this.location = data.location;
this.temperature = data.temperature;
this.feels_like_temperature = data.feels_like_temperature;
this.wind_speed = data.wind_speed;
this.wind_degrees = data.wind_degrees;
this.humidity = data.humidity;
this.unit = data.unit;
this.weather_conditions = data.weather_conditions;
}
toJSON() {
const result = {
location: this.location,
temperature: this.temperature,
feels_like_temperature: this.feels_like_temperature,
unit: this.unit,
weather_conditions: this.weather_conditions
};
if (this.wind_speed !== null && this.wind_speed !== undefined) {
result.wind_speed = this.wind_speed;
}
if (this.wind_degrees !== null && this.wind_degrees !== undefined) {
result.wind_degrees = this.wind_degrees;
}
if (this.humidity !== null && this.humidity !== undefined) {
result.humidity = this.humidity;
}
return result;
}
}
async function getWeather(apiKey, params) {
const unitQuery = params.unit;
const encodedLocation = encodeURIComponent(params.location);
const requestUrl = `${OPENWEATHER_ENDPOINT}?q=${encodedLocation}&appid=${apiKey}&units=${unitQuery}`;
try {
const response = await fetch(requestUrl, {
method: 'GET',
headers: {
'User-Agent': 'Mozilla/5.0 (compatible; noorle/1.0)'
}
});
if (!response.ok) {
throw new Error(`HTTP error: status code ${response.status}`);
}
const weatherData = await response.json();
const weatherConditions = [];
if (weatherData.weather && Array.isArray(weatherData.weather)) {
for (const w of weatherData.weather) {
if (w.description) {
weatherConditions.push(w.description);
}
}
}
const weatherResponse = new WeatherResponse({
location: weatherData.name || "",
temperature: weatherData.main?.temp,
feels_like_temperature: weatherData.main?.feels_like,
wind_speed: weatherData.wind?.speed || null,
wind_degrees: weatherData.wind?.deg || null,
humidity: weatherData.main?.humidity || null,
unit: params.unit,
weather_conditions: weatherConditions
});
if (weatherResponse.temperature === undefined || weatherResponse.feels_like_temperature === undefined) {
throw new Error("Error parsing temperature data");
}
return weatherResponse;
} catch (error) {
throw new Error(`Failed to fetch weather: ${error.message}`);
}
}
export async function checkWeather(location, unit) {
try {
const envVars = getEnvironment();
let apiKey = "";
for (const [key, value] of envVars) {
if (key === "OPENWEATHER_API_KEY") {
apiKey = value;
break;
}
}
if (!apiKey) {
return JSON.stringify({ error: "OPENWEATHER_API_KEY environment variable not set" });
}
const unitLower = unit.toLowerCase();
let unitEnum;
if (unitLower === "imperial") {
unitEnum = Unit.IMPERIAL;
} else {
unitEnum = Unit.METRIC;
}
const params = new WeatherParams(location, unitEnum);
try {
const weather = await getWeather(apiKey, params);
return JSON.stringify(weather.toJSON());
} catch (error) {
return JSON.stringify({ error: error.message });
}
} catch (error) {
return JSON.stringify({ error: `Unexpected error: ${error.message}` });
}
}