From 4ae92989f7c5202ae333e163b61e667685c0231d Mon Sep 17 00:00:00 2001 From: shootylife Date: Mon, 15 Feb 2021 17:05:47 +0100 Subject: [PATCH] :sparkles: add API --- index.js | 46 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index a282a8e..a58d9a3 100644 --- a/index.js +++ b/index.js @@ -5,10 +5,14 @@ import axios from 'axios'; const app = fastify({ logger: true }); app.get('/', async (req, res) => { + // const countryCode = req.body.countryCode + const cat = await getCatFacts() + const fox = await getFox() + const holidays = await getHolidays("FR") return { - message: `Welcome to Node Babel with ${ - req.body?.testValue ?? 'no testValue' - }`, + foxPicture : fox, + catFacts : cat, + holidays : holidays }; }); @@ -22,3 +26,39 @@ const start = async () => { } }; start(); + +async function getCatFacts() { + try { + const response = await axios ({ + url: "https://cat-fact.herokuapp.com/facts/random?amount=3", + method: "GET" + }) + return response.data.map(obj => obj.text) + } catch { + return null + } +} + +async function getFox() { + try { + const response = await axios ({ + url: "https://randomfox.ca/floof", + method: "GET" + }) + return response.data.image + } catch { + return null + } +} + +async function getHolidays(countryCode) { + try { + const response = await axios ({ + url: "https://date.nager.at/api/v2/PublicHolidays/2021/" + countryCode, + method: "GET" + }) + return response.data + } catch { + return null + } +}