|
|
@ -4,14 +4,6 @@ import axios from 'axios'; |
|
|
|
|
|
|
|
|
const app = fastify({ logger: true }); |
|
|
const app = fastify({ logger: true }); |
|
|
|
|
|
|
|
|
app.get('/', async (req, res) => { |
|
|
|
|
|
return { |
|
|
|
|
|
message: `Welcome to Node Babel with ${ |
|
|
|
|
|
req.body?.testValue ?? 'no testValue' |
|
|
|
|
|
}`,
|
|
|
|
|
|
}; |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// Run the server!
|
|
|
// Run the server!
|
|
|
const start = async () => { |
|
|
const start = async () => { |
|
|
try { |
|
|
try { |
|
|
@ -21,4 +13,62 @@ const start = async () => { |
|
|
process.exit(1); |
|
|
process.exit(1); |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
start(); |
|
|
start(); |
|
|
|
|
|
|
|
|
|
|
|
// Post in '/' routes
|
|
|
|
|
|
|
|
|
|
|
|
app.post('/', async (req) => { |
|
|
|
|
|
const country = req.body.countryCode; |
|
|
|
|
|
const catFacts = await getCatFacts(); |
|
|
|
|
|
const foxPic = await getFox(); |
|
|
|
|
|
const holidays = await getDays(country); |
|
|
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
|
foxPic: foxPic, |
|
|
|
|
|
catFacts: catFacts.map((cat) => cat.text), |
|
|
|
|
|
holidays: holidays, |
|
|
|
|
|
}; |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// Retrieve cat facts
|
|
|
|
|
|
|
|
|
|
|
|
async function getCatFacts() { |
|
|
|
|
|
try { |
|
|
|
|
|
const response = await axios({ |
|
|
|
|
|
url: 'https://cat-fact.herokuapp.com/facts/random?amount=3', |
|
|
|
|
|
method: 'GET', |
|
|
|
|
|
}); |
|
|
|
|
|
return response.data; |
|
|
|
|
|
} catch (e) { |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Retrieve fox image
|
|
|
|
|
|
|
|
|
|
|
|
async function getFox() { |
|
|
|
|
|
try { |
|
|
|
|
|
const response = await axios({ |
|
|
|
|
|
url: 'https://randomfox.ca/floof/', |
|
|
|
|
|
method: 'GET', |
|
|
|
|
|
}); |
|
|
|
|
|
return response.data.image; |
|
|
|
|
|
} catch (e) { |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Retrieve holidays based on contrycode input
|
|
|
|
|
|
|
|
|
|
|
|
async function getDays(country) { |
|
|
|
|
|
try { |
|
|
|
|
|
const response = await axios({ |
|
|
|
|
|
url: `https://date.nager.at/api/v2/PublicHolidays/2021/${country}`, |
|
|
|
|
|
method: 'GET', |
|
|
|
|
|
}); |
|
|
|
|
|
return response.data; |
|
|
|
|
|
} catch (e) { |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
} |