|
|
@ -4,12 +4,69 @@ import axios from 'axios'; |
|
|
|
|
|
|
|
|
const app = fastify({ logger: true }); |
|
|
const app = fastify({ logger: true }); |
|
|
|
|
|
|
|
|
app.get('/', async (req, res) => { |
|
|
|
|
|
|
|
|
async function getFoxImage() { |
|
|
|
|
|
try { |
|
|
|
|
|
const fox = await axios.get('https://randomfox.ca/floof/'); |
|
|
|
|
|
return fox.data.image; |
|
|
|
|
|
} catch (e) { |
|
|
|
|
|
console.error(`Error : ${e}`); |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function getFactCat(amount = 0) { |
|
|
|
|
|
try { |
|
|
|
|
|
let fact; |
|
|
|
|
|
|
|
|
|
|
|
if (fact <= 0) { |
|
|
|
|
|
fact = await axios.get( |
|
|
|
|
|
`https://cat-fact.herokuapp.com/facts/random?animal_type=cat&amount=1`, |
|
|
|
|
|
); |
|
|
|
|
|
} else { |
|
|
|
|
|
fact = await axios.get( |
|
|
|
|
|
`https://cat-fact.herokuapp.com/facts/random?animal_type=cat&amount=${amount}`, |
|
|
|
|
|
); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const filteredFacts = fact.data.map((fact) => { |
|
|
|
|
|
return fact.text; |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
return filteredFacts; |
|
|
|
|
|
} catch (e) { |
|
|
|
|
|
console.error(`Error : ${e}`); |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function getHolidays(countryCode = 'FR') { |
|
|
|
|
|
try { |
|
|
|
|
|
const currentDate = new Date(Date.now()); |
|
|
|
|
|
const currentYear = currentDate.getFullYear(); |
|
|
|
|
|
const holidays = await axios.get( |
|
|
|
|
|
`https://date.nager.at/api/v2/PublicHolidays/2021/${countryCode}`, |
|
|
|
|
|
); |
|
|
|
|
|
return holidays.data; |
|
|
|
|
|
} catch (e) { |
|
|
|
|
|
console.error(`Error : ${e}`); |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function fetchApis(countryCode) { |
|
|
|
|
|
const foxImage = await getFoxImage(); |
|
|
|
|
|
const catFacts = await getFactCat(3); |
|
|
|
|
|
const holidays = await getHolidays(countryCode); |
|
|
|
|
|
|
|
|
return { |
|
|
return { |
|
|
message: `Welcome to Node Babel with ${ |
|
|
|
|
|
req.body?.testValue ?? 'no testValue' |
|
|
|
|
|
}`,
|
|
|
|
|
|
|
|
|
foxPicture: foxImage, |
|
|
|
|
|
catFacts: catFacts, |
|
|
|
|
|
holidays: holidays, |
|
|
}; |
|
|
}; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
app.post('/', async (req, res) => { |
|
|
|
|
|
return await fetchApis(req.body.countryCode); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
// Run the server!
|
|
|
// Run the server!
|
|
|
|