Browse Source

finish the three call to api

main
Neel-Leo Coffin 5 years ago
parent
commit
77620ce2f2
  1. 52
      index.js

52
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!

Loading…
Cancel
Save