-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetWeather.js
46 lines (41 loc) · 1.65 KB
/
getWeather.js
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
const axios = require("axios");
const moment = require("moment");
const KELVIN_TO_CELSIUS = 273.15;
const transformWeatherData = (data) => {
const {
weather, main, wind, clouds, rain, snow, sys, visibility, pop, timezone, name, coord, dt,
} = data;
return {
icon: weather[0].icon,
main: {
Weather: weather[0].description,
Temperature: `${(main.temp - KELVIN_TO_CELSIUS).toFixed(2)}°C`,
Humidity: `${main.humidity}%`,
Visibility: `${visibility}m`,
Pressure: `${main.pressure} hPa`,
"Wind Speed": `${wind.speed} m/s`,
"Wind Direction": wind?.deg ? `${wind.deg}deg` : undefined,
"Wind Gust": wind?.gust ? `${wind.gust}m/s` : undefined,
Cloudiness: clouds?.all ? `${clouds.all}%` : "No",
Precipitation: pop ? `${Math.floor(pop * 100)}%` : "No",
Rain: rain?.["3h"] ? `${rain["3h"]}mm` : "No",
Snow: snow?.["3h"] ? `${snow["3h"]}mm` : "No",
Sunrise: moment.unix(sys.sunrise).format("h:mm:ss A"),
Sunset: moment.unix(sys.sunset).format("h:mm:ss A"),
TimeZone: timezone,
City: name,
Latitude: coord.lat,
Longitude: coord.lon,
Time: moment.unix(dt).format("dddd, Do MMMM YYYY, h:mm:ss A"),
},
};
};
const getWeather = async (city) => {
try {
const response = await axios.get(`https://cli-mate.vercel.app/api/weather/${city}`);
return transformWeatherData(response.data);
} catch (error) {
throw error?.response?.data?.message || error.message;
}
};
module.exports = getWeather;