Browse Source

🎨 Format code

main
Galimede 5 years ago
parent
commit
dba0301260
  1. 57
      index.js

57
index.js

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

Loading…
Cancel
Save