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.
82 lines
1.7 KiB
82 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});
|
|
|
|
// CAT FACTS
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
// FOX API
|
|
|
|
const FOX_API_ENDPOINT = 'https://randomfox.ca/floof';
|
|
|
|
async function fetchRandomFoxImage() {
|
|
try {
|
|
const response = await axios.get(FOX_API_ENDPOINT);
|
|
return response.data.image;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// OFF DAYS
|
|
|
|
const DAYS_OFF_API_ENDPOINT = "https://date.nager.at"
|
|
|
|
async function fetchDaysOffPerCountry(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([
|
|
fetchRandomFoxImage(),
|
|
fetch3CatFacts(),
|
|
fetchDaysOffPerCountry(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();
|