-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
33 lines (26 loc) · 1.25 KB
/
index.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
'use strict';
const micro = require('micro');
const axios = require('axios');
const qs = require('querystring');
const url = require('url');
const WEATHER_ENDPOINT = 'https://api.openweathermap.org/data/2.5/weather';
const AIR_QUALITY_ENDPOINT = "http://api.waqi.info/feed/";
const WEATHER_API_KEY = process.env.WEATHER_API_KEY;
const AIR_QUALITY_API_KEY = process.env.AIR_QUALITY_API_KEY;
module.exports = async (req, res) => {
let queryParams = qs.parse(url.parse(req.url).query);
if (!queryParams.country || !queryParams.city) {
return micro.send(res, 400, 'Country and/or City is missing!');
}
try {
let weatherResults = await axios.get(`${WEATHER_ENDPOINT}?q=${queryParams.city},${queryParams.country}&appid=${WEATHER_API_KEY}&units=metric`);
let airQualityResults = await axios.get(`${AIR_QUALITY_ENDPOINT}/${queryParams.city}/?token=${AIR_QUALITY_API_KEY}`);
let response = Object.assign(weatherResults.data.main, { condition: weatherResults.data.weather[0].main});
if (airQualityResults.data.data) {
response = Object.assign(response, { aqi: airQualityResults.data.data.aqi, iaqi: airQualityResults.data.data.iaqi });
}
res.end(JSON.stringify(response));
} catch(e) {
micro.send(res, 400, e.message || e);
}
};