diff --git a/index.js b/index.js index f33eefe..d2bace0 100644 --- a/index.js +++ b/index.js @@ -5,30 +5,57 @@ import axios from "axios"; const app = fastify({logger: true}); const getCatFacts = async () => { - const url ='https://cat-fact.herokuapp.com/facts/random?amount=3'; + try { + const url =`https://cat-fact.herokuapp.com/facts/random?amount=3`; const {data} = await axios.get(url); data.forEach( d => console.log(d.text)); + return data.map((item) => item.text); + } catch (err) { + console.log(err); + return null; + } }; const getFoxImage = async () => { - const url ='https://randomfox.ca/floof/'; + try { + const url =`https://randomfox.ca/floof/`; const {data} = await axios.get(url); console.log(data.image); + return data.image; + } catch (err) { + console.log(err); + return null; + } }; -const getCountryHour = async () => { - const url ='https://date.nager.at/api/v2/publicholidays/2021/CU'; +const getCountryDays = async (countrycode) => { + try { + const url =`https://date.nager.at/api/v2/PublicHolidays/2021/${countrycode}`; const {data} = await axios.get(url); data.forEach( d => console.log(d.date)); + return data.map((item) => item.date); + } catch (err) { + console.log(err); + return null; + } }; -app.get('/', async (req, res) => { - return { - message: `Welcome to Node Babel with ${ - req.body?.testValue ?? 'no testValue' - }`, - }; +app.post('/' , async (req, res) => { + let countrycode = req.body?.countryCode; + console.log(req.body); + + console.log(countrycode); + const [catFacts, foxImage, holidays] = await Promise.all([ + getCatFacts(), + getFoxImage(), + getCountryDays(countrycode), + ]); + return { + foxImage, + catFacts, + holidays, + }; }); // Run the server! @@ -41,6 +68,3 @@ const start = async () => { } }; start(); -getCatFacts(); -getFoxImage(); -getCountryHour();