Straw-Etiam 5 years ago
parent
commit
917cb27823
  1. 66
      index.js
  2. 6667
      package-lock.json
  3. 4225
      yarn.lock

66
index.js

@ -5,13 +5,12 @@ import axios from 'axios';
const app = fastify({ logger: true }); const app = fastify({ logger: true });
app.get('/', async (req, res) => { app.get('/', async (req, res) => {
return {
message: `Welcome to Node Babel with ${
req.body?.testValue ?? 'no testValue'
}`,
};
return getAll();
}); });
//getting the catFact
// Run the server! // Run the server!
const start = async () => { const start = async () => {
try { try {
@ -21,4 +20,61 @@ const start = async () => {
process.exit(1); process.exit(1);
} }
}; };
// Get cat facts
const getCatFact = (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);
}).catch(err => {
resolve(null);
})
});
};
// Get fox image
const getFoxImage = () => {
return new Promise(resolve => {
axios.get(`https://randomfox.ca/floof/`).then(res => {
const data = res.data;
resolve(data.image);
}).catch(err => {
resolve(null);
})
});
};
// Get days off by country
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);
}).catch(err => {
resolve(null);
})
});
};
// Get all API
const getAll = () => {
const catFact = getCatFact(2);
const fox = getFoxImage();
const dayOff = getDayOff('fr');
return Promise.all([catFact, fox, dayOff]).then((values) => {
let obj = {};
obj['catFact'] = values[0];
obj['foxPicture'] = values[1];
obj['holidays'] = values[2];
return obj;
});
};
start(); start();

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

4225
yarn.lock
File diff suppressed because it is too large
View File

Loading…
Cancel
Save