Browse Source

Add files via upload

main
NicolasLepinette 5 years ago
committed by GitHub
commit
ce25a45024
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 21
      TP 1 Beauss 2/Readme.MD
  2. 54
      TP 1 Beauss 2/index.js
  3. 3029
      TP 1 Beauss 2/package-lock.json
  4. 37
      TP 1 Beauss 2/package.json
  5. 2647
      TP 1 Beauss 2/yarn.lock

21
TP 1 Beauss 2/Readme.MD

@ -0,0 +1,21 @@
# Ecmascript tp
This practice is a fastify api agregator.
Please reference to the lab for what to do.
## Install
```shell
$ yarn install
# OR
$ npm install
```
## Start the server
```shell
$ yarn start
# OR
$ npm run start
```

54
TP 1 Beauss 2/index.js

@ -0,0 +1,54 @@
import fastify from 'fastify';
import axios from 'axios';
const app = fastify({ logger: true });
app.get('/', async (req, res) => {
return getAll();
});
const getCatFacts = () =>{
return new Promise(resolve => {axios.get('https://cat-fact.herokuapp.com/facts/random?animal_type=cat&amount=3').then(res => {
let facts = [];
let i = 0;
while (i < res.data.length) {
facts.push(res.data[i].text);
i++;
}
resolve(facts);
}).catch(fail => resolve(null));});
};
const getFoxDay = () =>{
return new Promise(resolve => {
axios.get('https://randomfox.ca/floof/').then(res => {resolve(res.data.image)}).catch(fail => resolve(null));
});
};
const getDayOffCountry = (countryCode ) => {
return new Promise(resolve => {
axios.get('https://date.nager.at/api/v2/publicholidays/2021/'+countryCode).then(res => {resolve(res.data)}).catch(fail => resolve(null));
});
};
const getAll = () => {
return Promise.all([getCatFacts(),getFoxDay(),getDayOffCountry('FR')]).then(res => {
let data = {};
data['foxPicture'] = res[1];
data['catFacts'] = res[0];
data['holidays'] = res[2];
return data;
});
};
const start = async () => {
try {
await app.listen(5000);
} catch (err) {
app.log.error(err);
process.exit(1);
}
};
start();

3029
TP 1 Beauss 2/package-lock.json
File diff suppressed because it is too large
View File

37
TP 1 Beauss 2/package.json

@ -0,0 +1,37 @@
{
"name": "2020-2021-angular-ecma-tp",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"@babel/core": "^7.12.16",
"@babel/node": "^7.12.16",
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.12.13",
"@babel/plugin-proposal-optional-chaining": "^7.12.16",
"@babel/plugin-transform-destructuring": "^7.12.13",
"@babel/preset-env": "^7.12.16",
"@babel/preset-stage-3": "^7.8.3",
"axios": "^0.21.1",
"fastify": "^3.12.0",
"nodemon": "^2.0.7"
},
"devDependencies": {
"husky": "5.0.9",
"prettier": "2.2.1",
"pretty-quick": "3.1.0"
},
"scripts": {
"start": "nodemon exec babel-node index.js",
"format:write": "prettier --write \"**/*.{js,vue,json,ts,tsx,md,yml,html}\"",
"format:check": "prettier --list-different \"**/*.{js,vue,json,ts,tsx,md,yml,html}\""
},
"engines": {
"node": ">=14"
},
"type": "module",
"husky": {
"hooks": {
"pre-commit": "pretty-quick --staged"
}
}
}

2647
TP 1 Beauss 2/yarn.lock
File diff suppressed because it is too large
View File

Loading…
Cancel
Save