|
|
@ -2,13 +2,46 @@ import fastify from 'fastify'; |
|
|
// see axios doc on how to use it
|
|
|
// 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}); |
|
|
|
|
|
|
|
|
|
|
|
const getCatFacts = async (catFactsAmount) => { |
|
|
|
|
|
try { |
|
|
|
|
|
return (await axios.get(`https://cat-fact.herokuapp.com/facts/random?amount=${catFactsAmount}`)).data |
|
|
|
|
|
.map(fact => fact.text) |
|
|
|
|
|
} catch (e) { |
|
|
|
|
|
return null |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const getFoxImage = async () => { |
|
|
|
|
|
try { |
|
|
|
|
|
return (await axios.get('https://randomfox.ca/floof/')).data.image |
|
|
|
|
|
} catch (e) { |
|
|
|
|
|
return null |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const getHolidays = async (countryCode) => { |
|
|
|
|
|
try { |
|
|
|
|
|
return (await axios.get(`https://date.nager.at/api/v2/PublicHolidays/2021/${countryCode}`)).data |
|
|
|
|
|
} catch (e) { |
|
|
|
|
|
return null |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
app.get('/', async (req, res) => { |
|
|
app.get('/', async (req, res) => { |
|
|
return { |
|
|
return { |
|
|
message: `Welcome to Node Babel with ${ |
|
|
|
|
|
req.body?.testValue ?? 'no testValue' |
|
|
|
|
|
}`,
|
|
|
|
|
|
|
|
|
'foxPicture': await getFoxImage(), |
|
|
|
|
|
'catFacts': await getCatFacts(3), |
|
|
|
|
|
'holidays': await getHolidays(req.params.countryCode) |
|
|
|
|
|
}; |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
app.post('/', async (req, res) => { |
|
|
|
|
|
return { |
|
|
|
|
|
'foxPicture': await getFoxImage(), |
|
|
|
|
|
'catFacts': await getCatFacts(3), |
|
|
|
|
|
'holidays': await getHolidays(req.body.countryCode) |
|
|
}; |
|
|
}; |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
@ -21,4 +54,5 @@ const start = async () => { |
|
|
process.exit(1); |
|
|
process.exit(1); |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
start(); |
|
|
start(); |