Skip to content

Commit 6833795

Browse files
author
DebaA17
committed
changes made
1 parent f9eddf4 commit 6833795

File tree

1 file changed

+43
-5
lines changed

1 file changed

+43
-5
lines changed

netlify/functions/weather.js

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,49 @@ exports.handler = async function(event) {
1919
let data = '';
2020
res.on('data', chunk => data += chunk);
2121
res.on('end', () => {
22-
resolve({
23-
statusCode: 200,
24-
headers: { 'Access-Control-Allow-Origin': '*' },
25-
body: data
26-
});
22+
try {
23+
const apiData = JSON.parse(data);
24+
25+
if (apiData.error) {
26+
resolve({
27+
statusCode: 400,
28+
headers: { 'Access-Control-Allow-Origin': '*' },
29+
body: JSON.stringify({ error: apiData.error.message })
30+
});
31+
return;
32+
}
33+
34+
// Transform response to match frontend expectations
35+
const transformedData = {
36+
name: String(apiData.location.name).substring(0, 100),
37+
sys: { country: String(apiData.location.country).substring(0, 50) },
38+
main: {
39+
temp: Number(apiData.current.temp_c) || 0,
40+
feels_like: Number(apiData.current.feelslike_c) || 0,
41+
humidity: Number(apiData.current.humidity) || 0,
42+
pressure: Number(apiData.current.pressure_mb) || 0
43+
},
44+
weather: [{
45+
description: String(apiData.current.condition.text).substring(0, 100),
46+
icon: String(apiData.current.condition.icon).substring(0, 200)
47+
}],
48+
wind: {
49+
speed: Number(apiData.current.wind_kph) / 3.6 || 0
50+
}
51+
};
52+
53+
resolve({
54+
statusCode: 200,
55+
headers: { 'Access-Control-Allow-Origin': '*' },
56+
body: JSON.stringify(transformedData)
57+
});
58+
} catch (error) {
59+
resolve({
60+
statusCode: 500,
61+
headers: { 'Access-Control-Allow-Origin': '*' },
62+
body: JSON.stringify({ error: 'Failed to parse weather data.' })
63+
});
64+
}
2765
});
2866
}).on('error', (e) => {
2967
resolve({

0 commit comments

Comments
 (0)