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.
64 lines
1.3 KiB
64 lines
1.3 KiB
import fastify from 'fastify';
|
|
import axios from 'axios';
|
|
|
|
const app = fastify({ logger: true });
|
|
|
|
async function catApi() {
|
|
try {
|
|
const response = await axios({
|
|
url: 'https://cat-fact.herokuapp.com/facts/random?amount=3',
|
|
method: 'GET',
|
|
});
|
|
return response.data;
|
|
} catch (err) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
async function holidayApi(countryCode) {
|
|
try {
|
|
const response = await axios({
|
|
url: `https://date.nager.at/api/v2/PublicHolidays/2021/${countryCode}`,
|
|
method: 'GET',
|
|
});
|
|
return response.data;
|
|
} catch (err) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
async function foxApi() {
|
|
try {
|
|
const response = await axios({
|
|
url: 'https://randomfox.ca/floof/',
|
|
method: 'GET',
|
|
});
|
|
return response.data.image;
|
|
} catch (err) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
app.post('/', async (req, res) => {
|
|
const catsFact = await catApi();
|
|
const foxPicture = await foxApi();
|
|
const countryCode = req.body.countryCode;
|
|
const holidays = await holidayApi(countryCode);
|
|
|
|
return {
|
|
foxPicture: foxPicture,
|
|
catFacts: catsFact.map((cat) => cat.text),
|
|
holidays: holidays,
|
|
};
|
|
});
|
|
|
|
// Run the server!
|
|
const start = async () => {
|
|
try {
|
|
await app.listen(7800);
|
|
} catch (err) {
|
|
app.log.error(err);
|
|
process.exit(1);
|
|
}
|
|
};
|
|
start();
|