Yield generated for 7d2a8e0b-6601-469a-9521-fc71ca67795c
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.
 
 

78 lines
1.7 KiB

import fastify from 'fastify';
// see axios doc on how to use it
import axios from 'axios';
const app = fastify({logger: true});
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);
console.log()
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] = await Promise.all([
fetchFowImage(),
fetch3CatFacts(),
fetchDaysOfPerCountry(countryCode)
])
return {
foxPicture,
catFacts,
holidays
};
});
// Only used for dev server, do not remove
app.head('/', () => ({ping: 'pong'}));
// Run the server!
const start = async () => {
try {
await app.listen(5000);
} catch (err) {
app.log.error(err);
process.exit(1);
}
};
start();