local-weather/components/WeatherNow.tsx
2024-05-04 23:04:30 +02:00

82 lines
1.5 KiB
TypeScript

import { error } from "console";
interface Forecast {
coord: {
lon: number;
lat: number;
};
weather: {
id: number;
main: string;
description: string;
icon: string;
}[];
base: string;
main: {
temp: number;
feels_like: number;
temp_min: number;
temp_max: number;
pressure: number;
humidity: number;
sea_level: number;
grnd_level: number;
};
visibility: number;
wind: {
speed: number;
deg: number;
gust: number;
};
rain: {
onehour: number;
};
clouds: {
all: number;
};
dt: number;
sys: {
type: number;
id: number;
country: string;
sunrise: number;
sunset: number;
};
timezone: number;
id: number;
name: string;
cod: number;
}
interface LocationType {
latitude: string;
longtitude: string;
}
async function getForecast(location: LocationType): Promise<Forecast> {
const { latitude, longtitude } = location;
const appId = "546911d860cb81f16585f7973b394b70";
const res = await fetch(
`https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longtitude}&appid=${appId}`
);
if (!res.ok) {
throw new Error(`This is not great ${error}`);
}
return res.json();
}
export default async function WeatherNow(props: { location: LocationType }) {
const kelvin = 273.15;
const location = props.location;
const weather = await getForecast(location);
const temp = weather.main.temp - kelvin;
return (
<main>
<h1>Forecast</h1>
<p>{temp}</p>
<p></p>
</main>
);
}