Browse Source

Intégration et retour des API cat facts, fox et holidays

main
Gianni GIUDICE 5 years ago
parent
commit
d073a17fa2
  1. 60
      index.js
  2. 3029
      package-lock.json

60
index.js

@ -4,12 +4,10 @@ import axios from 'axios';
const app = fastify({ logger: true });
const instance = axios.create();
app.get('/', async (req, res) => {
return {
message: `Welcome to Node Babel with ${
req.body?.testValue ?? 'no testValue'
}`,
};
return getData(3, 'FR');
});
// Run the server!
@ -21,4 +19,56 @@ const start = async () => {
process.exit(1);
}
};
// Get $amount random cat facts
const getCatFacts = (amount) => {
return new Promise(resolve => {
axios.get('https://cat-fact.herokuapp.com/facts/random?animal_type=cat&amount=' + amount).then(res => {
const data = res.data;
let facts = [];
for (let i = 0; i < data.length; i++) {
facts.push(data[i].text);
}
resolve(facts);
})
});
};
// Get random fox image
const getFoxImage = () => {
return new Promise(resolve => {
axios.get('https://randomfox.ca/floof/').then(res => {
const data = res.data;
resolve(data.image);
})
});
};
// Get $country days off
const getDayOff = (country) => {
return new Promise(resolve => {
axios.get('https://date.nager.at/api/v2/PublicHolidays/2021/' + country).then(res => {
const data = res.data;
resolve(data);
})
});
};
// Get data to return to the API
const getData = () => {
const cat = getCatFacts(3);
const fox = getFoxImage();
const day_off = getDayOff('FR');
return Promise.all([cat, fox, day_off]).then((values) => {
let obj = {};
obj['catFacts'] = values[0];
obj['foxPicture'] = values[1];
obj['holidays'] = values[2];
return obj;
});
};
start();

3029
package-lock.json
File diff suppressed because it is too large
View File

Loading…
Cancel
Save