|
|
|
@ -4,11 +4,61 @@ import axios from 'axios'; |
|
|
|
|
|
|
|
const app = fastify({logger: true}); |
|
|
|
|
|
|
|
app.post('/', async (req, res) => { |
|
|
|
const CAT_API_ENDPOINT = "https://cat-fact.herokuapp.com"; |
|
|
|
const CAT_FACTS_COUNT = 3; |
|
|
|
|
|
|
|
async function fetch3CatFacts() { |
|
|
|
const uri = `${CAT_API_ENDPOINT}/facts/random?amount=${CAT_FACTS_COUNT}`; |
|
|
|
try { |
|
|
|
const response = await axios.get(uri); |
|
|
|
return response.data.map(rep => rep.text); |
|
|
|
} catch { |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
const FOX_API_ENDPOINT = 'https://randomfox.ca/floof'; |
|
|
|
|
|
|
|
async function fetchFowImage() { |
|
|
|
try { |
|
|
|
const response = await axios.get(FOX_API_ENDPOINT); |
|
|
|
return response.data.image; |
|
|
|
} catch { |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
const DAYS_OFF_API_ENDPOINT = "https://date.nager.at" |
|
|
|
|
|
|
|
async function fetchDaysOfPerCountry(country) { |
|
|
|
|
|
|
|
if (country) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
const year = new Date().getFullYear() |
|
|
|
const uri = `${DAYS_OFF_API_ENDPOINT}/api/v3/PublicHolidays/${year}/${country}` |
|
|
|
try { |
|
|
|
const response = await axios.get(uri); |
|
|
|
return response.data; |
|
|
|
} catch { |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
app.post('/', async (req, _res) => { |
|
|
|
|
|
|
|
const countryCode = req.body?.countryCode; |
|
|
|
const [foxPicture, catFacts, holidays] = Promise.all([ |
|
|
|
fetchFowImage(), |
|
|
|
fetch3CatFacts(), |
|
|
|
fetchDaysOfPerCountry(countryCode) |
|
|
|
]) |
|
|
|
|
|
|
|
return { |
|
|
|
message: `Welcome to Node Babel with ${ |
|
|
|
req.body?.testValue ?? 'no testValue' |
|
|
|
}`,
|
|
|
|
foxPicture, |
|
|
|
catFacts, |
|
|
|
holidays |
|
|
|
}; |
|
|
|
}); |
|
|
|
|
|
|
|
|