feat: weather widget scalable (#574)

* refactor: Make weather widget scalable

* fix: formatting

* fix: map key again

* fix: null assertions
This commit is contained in:
SeDemal
2024-06-05 21:26:59 +02:00
committed by GitHub
parent 2623708c6d
commit cfd8b328b4
6 changed files with 183 additions and 115 deletions

View File

@@ -3,13 +3,22 @@ import { validation } from "@homarr/validation";
import { createTRPCRouter, publicProcedure } from "../../trpc";
export const weatherRouter = createTRPCRouter({
atLocation: publicProcedure
.input(validation.widget.weather.atLocationInput)
.output(validation.widget.weather.atLocationOutput)
.query(async ({ input }) => {
const res = await fetch(
`https://api.open-meteo.com/v1/forecast?latitude=${input.latitude}&longitude=${input.longitude}&daily=weathercode,temperature_2m_max,temperature_2m_min&current_weather=true&timezone=auto`,
);
return res.json();
}),
atLocation: publicProcedure.input(validation.widget.weather.atLocationInput).query(async ({ input }) => {
const res = await fetch(
`https://api.open-meteo.com/v1/forecast?latitude=${input.latitude}&longitude=${input.longitude}&daily=weathercode,temperature_2m_max,temperature_2m_min&current_weather=true&timezone=auto`,
);
const json: unknown = await res.json();
const weather = await validation.widget.weather.atLocationOutput.parseAsync(json);
return {
current: weather.current_weather,
daily: weather.daily.time.map((value, index) => {
return {
time: value,
weatherCode: weather.daily.weathercode[index] ?? 404,
maxTemp: weather.daily.temperature_2m_max[index],
minTemp: weather.daily.temperature_2m_min[index],
};
}) ?? [{ time: 0, weatherCode: 404 }],
};
}),
});