From 77620ce2f25d2c6d70fa2353e434153f9f634398 Mon Sep 17 00:00:00 2001 From: Neel-Leo Coffin Date: Tue, 16 Feb 2021 13:25:32 +0100 Subject: [PATCH] :sparkles: finish the three call to api --- index.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index a282a8e..56840a7 100644 --- a/index.js +++ b/index.js @@ -4,12 +4,56 @@ import axios from 'axios'; const app = fastify({ logger: true }); -app.get('/', async (req, res) => { +async function getCats() { + try { + const facts = await axios.get(`https://cat-fact.herokuapp.com/facts/random?animal_type=cat&amount=3`); + let fact = facts.data.map((data) => { + return data.text; + }); + return fact; + } catch (error) { + console.error(`no cats here budy : ${error}`); + return null; + } +} + +async function getFox() { + try { + const foxy = await axios.get(`https://randomfox.ca/floof/`); + return foxy.data.image; + } catch (error) { + console.error(`what does the fox says ?? : ${error}`); + return null; + } +} + +async function getCountryOfMyChoice(countryCode) { + try { + const date = new Date(Date.now()); + const year = date.getFullYear(); + const dayOff = await axios.get(`https://date.nager.at/api/v2/PublicHolidays/2021/${countryCode}`); + return dayOff.data; + } catch (error) { + console.error(`no holidays for you... : ${error}`); + return null; + } +} + + +async function gettingApi(countryCode) { + const catFacts = await getCats(); + const foxImage = await getFox(); + const dayOff = await getCountryOfMyChoice(countryCode); + return { - message: `Welcome to Node Babel with ${ - req.body?.testValue ?? 'no testValue' - }`, + catFacts: catFacts, + foxImage: foxImage, + dayOff: dayOff }; +} + +app.post('/', async (req, res) => { + return await gettingApi(req.body.countryCode); }); // Run the server!