diff --git a/index.js b/index.js index 44fb5bf..dccc90f 100644 --- a/index.js +++ b/index.js @@ -5,72 +5,66 @@ import axios from 'axios'; const app = fastify({ logger: true }); let requestCat = async () => { - try { - - const cat = await axios.get(`https://cat-fact.herokuapp.com/facts/random?animal_type=cat&amount=3`); - const fact = cat.data.map((cat) => { - return cat.text; - }); - return fact; - - } catch (err) { - console.log("Error"); - return null; - } - -} - + try { + const cat = await axios.get( + `https://cat-fact.herokuapp.com/facts/random?animal_type=cat&amount=3` + ); + console.log(cat); + const fact = cat.data.map((cat) => { + return cat.text; + }); + return fact; + } catch (err) { + console.log('Error'); + return null; + } +}; let requestFox = async () => { + try { + const fox = await axios.post('https://randomfox.ca/floof/'); + return fox.data.image; + } catch (err) { + console.log('Error'); + return null; + } +}; - try { - const fox = await axios.get('https://randomfox.ca/floof/'); - return fox.data.image; - } catch (err) { - console.log("Error"); - return null; - } - -} - - -let requestHolidays = async (countryCode = 'FR') => { - - try { - const holidays = await axios.get(`https://date.nager.at/api/v2/PublicHolidays/2021/${countryCode}`); - return holidays.data; - } catch (err) { - console.log("Error"); - return null; - } -} - -let fetchApis = async (countryCode) =>{ - const catFacts = await requestCat(); - const foxPicture = await requestFox(); - const holidays = await requestHolidays(countryCode); +let requestHolidays = async () => { + try { + const holidays = await axios.get( + `https://date.nager.at/api/v2/PublicHolidays/2021/fr` + ); + return holidays.data; + } catch (err) { + console.log('Error'); + return null; + } +}; - return { - foxPicture: foxPicture, - catFacts: catFacts, - holidays: holidays - } +let fetchApis = async () => { + const catFacts = await requestCat(); + const foxPicture = await requestFox(); + const holidays = await requestHolidays(); -} + return { + foxPicture: foxPicture, + catFacts: catFacts, + holidays: holidays, + }; +}; app.post('/', async (req, res) => { - - return await fetchApis(req.body.countryCode); - + return await fetchApis(req.body); }); // Run the server! const start = async () => { - try { - await app.listen(5000); - } catch (err) { - app.log.error(err); - process.exit(1); - } + try { + await app.listen(5000); + } catch (err) { + app.log.error(err); + process.exit(1); + } }; -start(); +start(); \ No newline at end of file