From 214c42606e31c6352f0e079f6df834ad7dd5eaae Mon Sep 17 00:00:00 2001 From: Kushagra Raj Tiwari Date: Sat, 19 Oct 2024 16:36:59 +0530 Subject: [PATCH 1/5] Adding local setup guide. Signed-off-by: Kushagra Raj Tiwari --- README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/README.md b/README.md index 515c662..729df74 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,35 @@ - Click on the **Compare & pull request** button. - Submit the pull request. +## 🚀 Local Setup Guide + +1. **Add ENVIRONMENT variables** + ```bash + OPENWEATHERMAP_TOKEN=your_open_weather_api_key + WEATHER_API_BASE_URL=https://api.openweathermap.org/data/2.5/weather + WEATHER_DATA_UNIT=metric + ``` + Keep your `.env` file inside `netlify/functions` directory. +2. **Install required packages** + Run: + ```bash + npm i + ``` +3. **Intall Netlify CLI locally** + This site is hosted on Netlify. Netlify CLI is needed to run it locally. + ```bash + npm install netlify-cli --global + ``` + Check version + ```bash + ntl --version + ``` +4. **Run project in local** + ```bash + netlify dev + ``` + + It's that easy! We are eagerly waiting for your pull request. 😊 ## 📝 How to Get Concluded From 02448b6d921e9e992c96d7691bce2dacda3c8b39 Mon Sep 17 00:00:00 2001 From: Kushagra Raj Tiwari Date: Sat, 19 Oct 2024 16:37:24 +0530 Subject: [PATCH 2/5] Ignoring files for git. Signed-off-by: Kushagra Raj Tiwari --- .gitignore | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..035839d --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +# Local Netlify folder +.netlify +.vscode +node_modules/ +.env \ No newline at end of file From e608174d73ef97767161f7b6447068fa6f52eab6 Mon Sep 17 00:00:00 2001 From: Kushagra Raj Tiwari Date: Sat, 19 Oct 2024 16:39:10 +0530 Subject: [PATCH 3/5] Adding Netlify function to fetch Weather Data. Signed-off-by: Kushagra Raj Tiwari --- netlify/functions/getWeatherData.js | 39 +++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 netlify/functions/getWeatherData.js diff --git a/netlify/functions/getWeatherData.js b/netlify/functions/getWeatherData.js new file mode 100644 index 0000000..e832262 --- /dev/null +++ b/netlify/functions/getWeatherData.js @@ -0,0 +1,39 @@ +import dotenv from "dotenv"; +dotenv.config({ path: "./netlify/functions/.env" }); + +export const handler = async (event) => { + let status = 500; + try { + let { city } = event.queryStringParameters; + const baseUrl = process.env.WEATHER_API_BASE_URL; + const appKey = process.env.OPENWEATHERMAP_TOKEN; + const units = process.env.WEATHER_DATA_UNIT; + const url = `${baseUrl}?q=${city}&appid=${appKey}&units=${units}`; + let response = await fetch(url); + if (response.ok) { + status = response.status; + let data = await response.json(); + return { + statusCode: status, + body: JSON.stringify({ + error: null, + data: data, + }), + }; + } else { + status = response.status; + throw new Error( + `Invalid response code from open weather API: ${response.status}` + ); + } + } catch (error) { + console.log(error.message); + return { + statusCode: status, + body: JSON.stringify({ + error: error.message, + data: null, + }), + }; + } +}; \ No newline at end of file From 9c97f3f1c13f491f0d55925a690dfe6c48dcb038 Mon Sep 17 00:00:00 2001 From: Kushagra Raj Tiwari Date: Sat, 19 Oct 2024 16:44:55 +0530 Subject: [PATCH 4/5] Adding dotenv package. Signed-off-by: Kushagra Raj Tiwari --- package-lock.json | 24 ++++++++++++++++++++++++ package.json | 6 ++++++ 2 files changed, 30 insertions(+) create mode 100644 package-lock.json create mode 100644 package.json diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..d1098e4 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,24 @@ +{ + "name": "WeatherNow", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "dependencies": { + "dotenv": "^16.4.5" + } + }, + "node_modules/dotenv": { + "version": "16.4.5", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.5.tgz", + "integrity": "sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==", + "license": "BSD-2-Clause", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..4bf8897 --- /dev/null +++ b/package.json @@ -0,0 +1,6 @@ +{ + "type": "module", + "dependencies": { + "dotenv": "^16.4.5" + } +} \ No newline at end of file From 31562f5c05a1aef10ffadf43806f2e601f8665d8 Mon Sep 17 00:00:00 2001 From: Kushagra Raj Tiwari Date: Sat, 19 Oct 2024 16:46:00 +0530 Subject: [PATCH 5/5] Using Netlify function to fetch Weather Data. Signed-off-by: Kushagra Raj Tiwari --- js/script.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/js/script.js b/js/script.js index 619af6d..81c6fad 100644 --- a/js/script.js +++ b/js/script.js @@ -38,11 +38,9 @@ window.getWeather = function () { // Check if the input is not empty if (input) { // Create a URL for the weather API with the input as a query parameter - const token = OPENWEATHERMAP_TOKEN; - var url = "https://api.openweathermap.org/data/2.5/weather?q=" + input + "&units=metric&appid=" + token; - // Fetch the data from the URL using the fetch API - fetch(url) + // Fetch the data from netlify function using the fetch API + fetch(`/.netlify/functions/getWeatherData?city=${input}`) .then(function (response) { // Check if the response is ok if (response.ok) { @@ -50,10 +48,15 @@ window.getWeather = function () { return response.json(); } else { // Throw an error if the response is not ok - throw new Error("Something went wrong"); + throw new Error(`Invalid response code from open weather API: ${response.status}`); } }) - .then(function (data) { + .then(function (responseData) { + let data = responseData["data"] + if(!data){ + console.log(responseData["error"]) + throw new Error(responseData["error"]) + } // Extract the relevant data from the JSON object var city = data.name; // The city name var country = data.sys.country; // The country code