location-weather-info
Version:
Retrieve current weather information based on user's location.
40 lines (35 loc) • 1.07 kB
JavaScript
const axios = require("axios");
class LocationWeatherInfo {
constructor(key) {
this.apiKey = key;
}
async getWeatherByCoords(lat, lon) {
try {
const url = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${this.apiKey}`;
const response = await axios.get(url);
if(response.data){
return { success: true, data: response.data };
}
else{
return { success: false, error: "No data is found" };
}
} catch (error) {
return { success: false, error: error };
}
}
async getWeatherByLocation(locationName) {
try {
const url = `https://api.openweathermap.org/data/2.5/weather?q=${locationName}&appid=${this.apiKey}`;
const response = await axios.get(url);
if(response.data){
return { success: true, data: response.data };
}
else{
return { success: false, error: "No data is found" };
}
} catch (error) {
return { success: false, data: error };
}
}
}
module.exports = LocationWeatherInfo;