Yield generated for 56d75d66-6a33-4702-bf15-e4788df0f780
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

76 lines
1.4 KiB

import fastify from 'fastify';
// see axios doc on how to use it
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;
}
}
let requestFox = async () => {
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);
return {
foxPicture: foxPicture,
catFacts: catFacts,
holidays: holidays
}
}
app.post('/', async (req, res) => {
return await fetchApis(req.body.countryCode);
});
// Run the server!
const start = async () => {
try {
await app.listen(5000);
} catch (err) {
app.log.error(err);
process.exit(1);
}
};
start();