|
|
@ -1,15 +1,58 @@ |
|
|
import fastify from 'fastify'; |
|
|
import fastify from 'fastify'; |
|
|
// see axios doc on how to use it
|
|
|
|
|
|
import axios from 'axios'; |
|
|
import axios from 'axios'; |
|
|
|
|
|
|
|
|
const app = fastify({ logger: true }); |
|
|
const app = fastify({ logger: true }); |
|
|
|
|
|
|
|
|
app.get('/', async (req, res) => { |
|
|
|
|
|
|
|
|
const getcatfacts = () => { |
|
|
|
|
|
return new Promise((resolve) => { |
|
|
|
|
|
axios.get("https://cat-fact.herokuapp.com/facts/random?animal_type=cat&amount=3").then((response) => { |
|
|
|
|
|
resolve(response.data.map(res=>res.text)) |
|
|
|
|
|
}).catch((error) => { |
|
|
|
|
|
resolve(null) |
|
|
|
|
|
console.log(error) |
|
|
|
|
|
}) |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const getfoximage = () => { |
|
|
|
|
|
|
|
|
|
|
|
return new Promise((resolve) => { |
|
|
|
|
|
axios.get("https://randomfox.ca/floof").then((response) => { |
|
|
|
|
|
resolve(response.data.image) |
|
|
|
|
|
}).catch((error) => { |
|
|
|
|
|
resolve(null) |
|
|
|
|
|
console.log(error) |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
)} |
|
|
|
|
|
|
|
|
|
|
|
const getholiday = (country) => { |
|
|
|
|
|
|
|
|
|
|
|
return new Promise((resolve) => { |
|
|
|
|
|
axios.get(`https://date.nager.at/api/v2/PublicHolidays/2021/${country}`).then((response) => { |
|
|
|
|
|
resolve(response.data) |
|
|
|
|
|
}).catch((error) => { |
|
|
|
|
|
resolve(null) |
|
|
|
|
|
console.log(error) |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
)} |
|
|
|
|
|
|
|
|
|
|
|
const fetchapi = async(country) => { |
|
|
|
|
|
const catFacts = await getcatfacts(); |
|
|
|
|
|
const foxPicture = await getfoximage(); |
|
|
|
|
|
const holidays = await getholiday(country); |
|
|
|
|
|
return Promise.all([catFacts, foxPicture, holidays]).then(() => { |
|
|
return { |
|
|
return { |
|
|
message: `Welcome to Node Babel with ${ |
|
|
|
|
|
req.body?.testValue ?? 'no testValue' |
|
|
|
|
|
}`,
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
foxPicture,catFacts,holidays |
|
|
|
|
|
} |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.post('/', async (req, res) => { |
|
|
|
|
|
return fetchapi(req.body.countryCode) |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
// Run the server!
|
|
|
// Run the server!
|
|
|
|