Rename api module

This commit is contained in:
Francesco Esposito 2019-08-22 17:41:54 +02:00
parent c20d67455e
commit 47dd4b4a74
1 changed files with 25 additions and 11 deletions

View File

@ -1,10 +1,11 @@
import axios from 'axios';
import config from './config';
import logger from './logger';
import citySchema from './db';
export const getCurrentWeatherById = (cityId) => {
return axios.get(`${config.owm.baseurl}/weather`, {
export const getCurrentWeatherById = (cityId) => (
axios.get(`${config.owm.baseurl}/weather`, {
params: {
appid: config.owm.token,
id: cityId,
@ -29,23 +30,22 @@ export const getCurrentWeatherById = (cityId) => {
const { status } = error.response;
logger.error(`getCurrentWeatherById (cityId=${cityId}) - Status:${status}`);
throw new Error(status);
});
};
})
);
export const getForecastWeatherById = (cityId) => {
return axios.get(`${config.owm.baseurl}/forecast`, {
export const getForecastWeatherById = (cityId) => (
axios.get(`${config.owm.baseurl}/forecast`, {
params: {
appid: config.owm.token,
id: cityId,
units: config.owm.units,
lang: config.owm.lang,
}
},
})
.then((res) => {
const { data } = res;
let date = new Date(Date.now());
date = date.toISOString().substr(0, 10);
const forecastList = data.list
@ -62,7 +62,7 @@ export const getForecastWeatherById = (cityId) => {
const forecastWeather = {
city: `${data.city.name}, ${data.city.country}`,
rain: forecastList.some((e) => e.flag_rain === 'Y') ? 'Y' : 'N',
rain: forecastList.some((e) => e.rain_flag === 'Y') ? 'Y' : 'N',
list: forecastList,
};
@ -72,5 +72,19 @@ export const getForecastWeatherById = (cityId) => {
const { status } = error.response;
logger.error(`getCurrentWeatherById (cityId=${cityId}) - Status:${status}`);
throw new Error(status);
});
};
})
);
export const getCityIdByName = (cityName) => (
new Promise((resolve, reject) => {
citySchema.find({ name: new RegExp(`^${cityName}`, 'i') }, {
_id: 0, name: 1, country: 1, id: 1,
}, (err, cities) => {
if (err) {
reject(err);
}
resolve(cities);
}).sort({ name: 1 }).limit(5);
})
);