Browse Source

💥 API working

You now know about cats, foxes, and when to jerk off in any country in the world
main
Anatole-DC 4 years ago
parent
commit
4fb7ddbee9
  1. 65
      index.js
  2. 8535
      package-lock.json
  3. 5015
      yarn.lock

65
index.js

@ -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")
};
});

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

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

Loading…
Cancel
Save