getweather-app 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Example/run.js +23 -0
- package/Index.js +39 -0
- package/README.md +113 -0
- package/lib/Weather-check.js +32 -0
- package/lib/data-parser.js +37 -0
- package/package.json +27 -0
package/Example/run.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import getWeatherInfo from '../Index.js';
|
|
2
|
+
|
|
3
|
+
async function testLocation(location) {
|
|
4
|
+
console.log(`\n--- Testing Location: ${location} ---`);
|
|
5
|
+
|
|
6
|
+
try {
|
|
7
|
+
const data = await getWeatherInfo(location);
|
|
8
|
+
|
|
9
|
+
console.log(`Success: Found weather for ${location}.`);
|
|
10
|
+
console.log('Result:', JSON.stringify(data, null, 2));
|
|
11
|
+
} catch (error) {
|
|
12
|
+
console.error(`Failure for ${location}: ${error.message}`);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async function runSimpleTests() {
|
|
17
|
+
|
|
18
|
+
await testLocation('Marrakech');
|
|
19
|
+
await testLocation('France');
|
|
20
|
+
await testLocation('ZZZ_Invalid_City_Name');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
runSimpleTests();
|
package/Index.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import {
|
|
2
|
+
fetchWeatherByCity
|
|
3
|
+
} from './lib/Weather-check.js';
|
|
4
|
+
import {
|
|
5
|
+
parseWeatherData
|
|
6
|
+
} from './lib/data-parser.js';
|
|
7
|
+
import 'dotenv/config';
|
|
8
|
+
|
|
9
|
+
const API_KEY = process.env.OPENWEATHER_API_KEY;
|
|
10
|
+
|
|
11
|
+
async function getWeatherInfo(input) {
|
|
12
|
+
if (!input || typeof input !== 'string' || input.trim() === '') {
|
|
13
|
+
throw new Error("Input must be a non-empty string (City or Country name).");
|
|
14
|
+
}
|
|
15
|
+
if (!API_KEY) {
|
|
16
|
+
throw new Error("API Key is missing. Check your .env file.");
|
|
17
|
+
}
|
|
18
|
+
const location = input.trim();
|
|
19
|
+
|
|
20
|
+
// Try City
|
|
21
|
+
try {
|
|
22
|
+
const rawData = await fetchWeatherByCity(location, API_KEY);
|
|
23
|
+
|
|
24
|
+
console.log("after fetch")
|
|
25
|
+
|
|
26
|
+
const cleanData = parseWeatherData(rawData);
|
|
27
|
+
|
|
28
|
+
console.log(`Successfully retrieved weather for city: ${cleanData.location.city}`);
|
|
29
|
+
return cleanData;
|
|
30
|
+
|
|
31
|
+
} catch (error) {
|
|
32
|
+
if (error.message.toLowerCase().includes('network error')) {
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
35
|
+
throw new Error(`Location not found: Could not find city or country "${location}".`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export default getWeatherInfo;
|
package/README.md
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# getweather-app
|
|
2
|
+
|
|
3
|
+
A simple and lightweight Node.js library for fetching weather information by **city** or **country**.
|
|
4
|
+
Environment variables (including your `API_KEY`) are loaded via **dotenv**, keeping your credentials secure.
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- :white_sun_rain_cloud: Fetch weather data by **city** or **country**
|
|
11
|
+
- :closed_lock_with_key: Uses `dotenv` for environment variable management
|
|
12
|
+
- :zap: Minimal, easy-to-use API
|
|
13
|
+
- :package: Zero configuration required
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
Run the following command in your project:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install getweather-app
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Create a `.env` file in your project root and add:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
OPENWEATHER_API_KEY=your_api_key_here
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
|
|
35
|
+
Import the library and call it with either a city or country:
|
|
36
|
+
|
|
37
|
+
```javascript
|
|
38
|
+
import getWeatherInfo from "getweather-app";
|
|
39
|
+
|
|
40
|
+
async function run() {
|
|
41
|
+
const result = await getWeatherInfo("Berlin");
|
|
42
|
+
console.log(result);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
run();
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
You may also pass a country name:
|
|
49
|
+
|
|
50
|
+
```javascript
|
|
51
|
+
await getWeatherInfo("Germany");
|
|
52
|
+
await getWeatherInfo("London");
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
## API
|
|
58
|
+
|
|
59
|
+
### `getWeatherInfo(input: string): Promise<any>`
|
|
60
|
+
|
|
61
|
+
Fetches weather data for the given input.
|
|
62
|
+
|
|
63
|
+
#### Parameters
|
|
64
|
+
|
|
65
|
+
| Name | Type | Description |
|
|
66
|
+
| ----- | ------ | -------------------- |
|
|
67
|
+
| input | string | City or country name |
|
|
68
|
+
|
|
69
|
+
#### Returns
|
|
70
|
+
|
|
71
|
+
A `Promise` resolving with the weather data returned by the upstream API.
|
|
72
|
+
|
|
73
|
+
---
|
|
74
|
+
|
|
75
|
+
## Environment Variables
|
|
76
|
+
|
|
77
|
+
Ensure your project loads dotenv before using the library:
|
|
78
|
+
|
|
79
|
+
```javascript
|
|
80
|
+
import "dotenv/config";
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
or
|
|
84
|
+
|
|
85
|
+
```javascript
|
|
86
|
+
require("dotenv").config();
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
---
|
|
90
|
+
|
|
91
|
+
## Example Output
|
|
92
|
+
|
|
93
|
+
```json
|
|
94
|
+
{
|
|
95
|
+
"type": "city",
|
|
96
|
+
"location": {
|
|
97
|
+
"city": "Marrakesh",
|
|
98
|
+
"countryCode": "MA"
|
|
99
|
+
},
|
|
100
|
+
"weather": {
|
|
101
|
+
"description": "clear sky",
|
|
102
|
+
"iconCode": "01n"
|
|
103
|
+
},
|
|
104
|
+
"temperature": {
|
|
105
|
+
"current": 16.04,
|
|
106
|
+
"feelsLike": 14.64,
|
|
107
|
+
"min": 16.04,
|
|
108
|
+
"max": 16.04,
|
|
109
|
+
"unit": "C"
|
|
110
|
+
},
|
|
111
|
+
"humidity": 36
|
|
112
|
+
}
|
|
113
|
+
```
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
const BASE_URL = 'https://api.openweathermap.org/data/2.5/';
|
|
6
|
+
|
|
7
|
+
function handleApiError(error) {
|
|
8
|
+
if (error.response) {
|
|
9
|
+
const message = error.response.data.message || 'Unknown API issue';
|
|
10
|
+
throw new Error(`API Error: ${message}`);
|
|
11
|
+
}
|
|
12
|
+
throw new Error(`Network Error: Failed to connect to the weather service.`);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export async function fetchWeatherByCity(locationName, apiKey) {
|
|
16
|
+
|
|
17
|
+
try {
|
|
18
|
+
const url = `${BASE_URL}weather`;
|
|
19
|
+
|
|
20
|
+
const response = await axios.get(url, {
|
|
21
|
+
params: {
|
|
22
|
+
q: locationName,
|
|
23
|
+
appid: apiKey,
|
|
24
|
+
units: 'metric'
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
return response.data;
|
|
29
|
+
} catch (error) {
|
|
30
|
+
handleApiError(error);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export function parseWeatherData(rawData) {
|
|
2
|
+
if (!rawData || !rawData.main || !rawData.weather) {
|
|
3
|
+
throw new Error("Invalid or incomplete data received for city weather.");
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
const {
|
|
7
|
+
temp,
|
|
8
|
+
feels_like,
|
|
9
|
+
temp_min,
|
|
10
|
+
temp_max,
|
|
11
|
+
humidity
|
|
12
|
+
} = rawData.main;
|
|
13
|
+
const weatherDescription = rawData.weather[0].description;
|
|
14
|
+
const weatherIcon = rawData.weather[0].icon;
|
|
15
|
+
|
|
16
|
+
const cleanData = {
|
|
17
|
+
type: 'city',
|
|
18
|
+
location: {
|
|
19
|
+
city: rawData.name,
|
|
20
|
+
countryCode: rawData.sys.country,
|
|
21
|
+
},
|
|
22
|
+
weather: {
|
|
23
|
+
description: weatherDescription,
|
|
24
|
+
iconCode: weatherIcon,
|
|
25
|
+
},
|
|
26
|
+
temperature: {
|
|
27
|
+
current: temp,
|
|
28
|
+
feelsLike: feels_like,
|
|
29
|
+
min: temp_min,
|
|
30
|
+
max: temp_max,
|
|
31
|
+
unit: 'C'
|
|
32
|
+
},
|
|
33
|
+
humidity: humidity,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
return cleanData;
|
|
37
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "getweather-app",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "Index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
8
|
+
"project": "node Example/run.js"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/Ichigoo/getWeather-app.git"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [],
|
|
15
|
+
"author": "",
|
|
16
|
+
"license": "ISC",
|
|
17
|
+
"type": "module",
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/Ichigoo/getWeather-app/issues"
|
|
20
|
+
},
|
|
21
|
+
"homepage": "https://github.com/Ichigoo/getWeather-app#readme",
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"axios": "^1.13.2",
|
|
24
|
+
"dotenv": "^17.2.3",
|
|
25
|
+
"express": "^5.1.0"
|
|
26
|
+
}
|
|
27
|
+
}
|