|
|
|
@ -1,14 +1,71 @@ |
|
|
|
import fastify from 'fastify'; |
|
|
|
// see axios doc on how to use it
|
|
|
|
import axios from 'axios'; |
|
|
|
|
|
|
|
const app = fastify({ logger: true }); |
|
|
|
|
|
|
|
/** |
|
|
|
* EXERCICE I - CATS FACTS |
|
|
|
*/ |
|
|
|
|
|
|
|
const CAT_FACTS_BASE_URL = "https://cat-fact.herokuapp.com/facts/random?animal_type=cat&amount=" |
|
|
|
|
|
|
|
const fetch_facts = async (number_of_facts) => { |
|
|
|
try { |
|
|
|
return await axios.get(CAT_FACTS_BASE_URL + number_of_facts) |
|
|
|
.then( |
|
|
|
function (response) { |
|
|
|
return response.data.map(element => element.text) |
|
|
|
} |
|
|
|
) |
|
|
|
} catch (error) { |
|
|
|
return null |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* EXERCICE II - FOX IMAGE |
|
|
|
*/ |
|
|
|
|
|
|
|
const FOX_IMAGE_BASE_URL = "https://randomfox.ca/floof/" |
|
|
|
|
|
|
|
const fetch_fox = async () => { |
|
|
|
try { |
|
|
|
return await axios.get(FOX_IMAGE_BASE_URL) |
|
|
|
.then( |
|
|
|
function (response) { |
|
|
|
return response.data.image |
|
|
|
} |
|
|
|
) |
|
|
|
} catch (error) { |
|
|
|
return null |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* EXERCICE II - FOX IMAGE |
|
|
|
*/ |
|
|
|
|
|
|
|
const HOLIDAY_BASE_URL = "https://date.nager.at/api/v3/publicholidays/2022/" |
|
|
|
|
|
|
|
const fetch_holiday = async (country_code) => { |
|
|
|
try { |
|
|
|
return await axios.get(HOLIDAY_BASE_URL + country_code) |
|
|
|
.then( |
|
|
|
function (response) { |
|
|
|
return response.data |
|
|
|
} |
|
|
|
) |
|
|
|
|
|
|
|
} catch (error) { |
|
|
|
return null |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
app.post('/', async (req, res) => { |
|
|
|
return { |
|
|
|
message: `Welcome to Node Babel with ${ |
|
|
|
req.body?.testValue ?? 'no testValue' |
|
|
|
}`,
|
|
|
|
"catFacts": await fetch_facts(req.body?.numberOfFacts ?? 3), |
|
|
|
"foxPicture": await fetch_fox(), |
|
|
|
"holidays": await fetch_holiday(req.body?.countryCode ?? "FR") |
|
|
|
}; |
|
|
|
}); |
|
|
|
|
|
|
|
|