diff --git a/backend/.gitignore b/backend/.gitignore index 4d29575..8692cf6 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -13,6 +13,7 @@ # misc .DS_Store +.env .env.local .env.development.local .env.test.local diff --git a/backend/app.js b/backend/app.js index 74dadf6..385675a 100644 --- a/backend/app.js +++ b/backend/app.js @@ -1,4 +1,5 @@ /* Modules */ +require('dotenv').config(); const express = require('express'); const helmet = require('helmet'); const cors = require('cors'); diff --git a/backend/assets/config/config.js b/backend/assets/config/config.js index 48ec1d3..381683b 100644 --- a/backend/assets/config/config.js +++ b/backend/assets/config/config.js @@ -1,5 +1,6 @@ const config = { - PORT: process.env.PORT || 8080 + PORT: process.env.PORT || 8080, + WEATHER_API_KEY: process.env.OpenWeatherMap_API_KEY }; module.exports = config; \ No newline at end of file diff --git a/backend/assets/functions/functionObject.js b/backend/assets/functions/functionObject.js index e30bf60..1d1f85f 100644 --- a/backend/assets/functions/functionObject.js +++ b/backend/assets/functions/functionObject.js @@ -3,6 +3,7 @@ const { convertArabicToRomanOutput, convertRomanToArabicOutput } = require('./ma const { convertDistanceOutput } = require('./main/convertDistance'); const { convertTemperatureOutput } = require('./main/convertTemperature'); const { armstrongNumberOutput } = require('./main/armstrongNumber'); +const { weatherRequestOutput } = require('./main/weatherRequest'); const functionObject = { randomNumber : randomNumberOutput, @@ -10,7 +11,8 @@ const functionObject = { convertRomanToArabic: convertRomanToArabicOutput, convertDistance : convertDistanceOutput, convertTemperature : convertTemperatureOutput, - armstrongNumber : armstrongNumberOutput + armstrongNumber : armstrongNumberOutput, + weatherRequest : weatherRequestOutput }; // Choisi la fonction à exécuter diff --git a/backend/assets/functions/main/armstrongNumber.js b/backend/assets/functions/main/armstrongNumber.js index b224f68..6a97008 100644 --- a/backend/assets/functions/main/armstrongNumber.js +++ b/backend/assets/functions/main/armstrongNumber.js @@ -38,7 +38,7 @@ exports.armstrongNumberOutput = (res, argsObject) => { // Si ce n'est pas un nombre number = parseInt(number); - if (isNaN(number) && number >= 0) { + if (isNaN(number) || number <= 0) { return sendResponse(res, { result: "Veuillez rentré un nombre valide.", httpStatus: 400 }); } diff --git a/backend/assets/functions/main/weatherRequest.js b/backend/assets/functions/main/weatherRequest.js new file mode 100644 index 0000000..877af1b --- /dev/null +++ b/backend/assets/functions/main/weatherRequest.js @@ -0,0 +1,19 @@ +const axios = require('axios'); +const sendResponse = require('../../utils/sendResponse'); +const { requiredFields } = require('../../config/errors'); +const { WEATHER_API_KEY } = require('../../config/config'); + +/* OUTPUTS */ +exports.weatherRequestOutput = async (res, argsObject) => { + let { cityName } = argsObject; + + // S'il n'y a pas les champs obligatoire + if (!(cityName)) { + return sendResponse(res, requiredFields); + } + + // Récupère les données météo grâce à l'API : openweathermap.org. + axios.get(`https://api.openweathermap.org/data/2.5/weather?q=${cityName}&lang=fr&units=metric&appid=${WEATHER_API_KEY}`) + .then((response) => sendResponse(res, { result: response.data }, true)) + .catch(() => sendResponse(res, { result: "La ville n'existe pas (dans l'API de openweathermap.org)." })); +} \ No newline at end of file diff --git a/backend/package-lock.json b/backend/package-lock.json index 636e402..9345010 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -58,6 +58,14 @@ "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=" }, + "axios": { + "version": "0.19.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.19.2.tgz", + "integrity": "sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==", + "requires": { + "follow-redirects": "1.5.10" + } + }, "balanced-match": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", @@ -342,6 +350,12 @@ "is-obj": "^1.0.0" } }, + "dotenv": { + "version": "8.2.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz", + "integrity": "sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==", + "dev": true + }, "duplexer3": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/duplexer3/-/duplexer3-0.1.4.tgz", @@ -459,6 +473,24 @@ "unpipe": "~1.0.0" } }, + "follow-redirects": { + "version": "1.5.10", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.10.tgz", + "integrity": "sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==", + "requires": { + "debug": "=3.1.0" + }, + "dependencies": { + "debug": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", + "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", + "requires": { + "ms": "2.0.0" + } + } + } + }, "forwarded": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.1.2.tgz", diff --git a/backend/package.json b/backend/package.json index 601c728..52c5ca7 100644 --- a/backend/package.json +++ b/backend/package.json @@ -10,11 +10,13 @@ "author": "Divlo", "license": "MIT", "dependencies": { + "axios": "^0.19.2", "cors": "^2.8.5", "express": "^4.17.1", "helmet": "^3.21.3" }, "devDependencies": { + "dotenv": "^8.2.0", "morgan": "^1.9.1", "nodemon": "^2.0.2" }