From 093aebf81f3ceaf087d7bebb5da0b2ca9bb3ff91 Mon Sep 17 00:00:00 2001 From: Ido Bouskila <94786579+IdoBouskila@users.noreply.github.com> Date: Sat, 30 Nov 2024 23:21:53 +0200 Subject: [PATCH] feat(forecast): add weekly forecast procedure (#23) * refactor(server): simplify search response * feat(fetchers-types): add types for external fetchs * refactor(fetchers): rename type for search response * feat(forecast): add weekly forecast procedure * wip --- server/index.ts | 7 ++++ server/utils/fetchers/fetchers.ts | 39 +++++++++++++++++++++ server/utils/fetchers/types/day-forecast.ts | 2 +- 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/server/index.ts b/server/index.ts index 288964a..7b0e961 100644 --- a/server/index.ts +++ b/server/index.ts @@ -14,6 +14,13 @@ const appRouter = router({ return results; }), + getWeeklyForecast: publicProcedure + .input(z.string()) + .query(async ({ input }) => { + const results = await fetchSearch(input); + + return results; + }), getPopularLocations: publicProcedure .query(async () => { const results = await fetchPopularLocations(); diff --git a/server/utils/fetchers/fetchers.ts b/server/utils/fetchers/fetchers.ts index 0653745..cb16ee7 100644 --- a/server/utils/fetchers/fetchers.ts +++ b/server/utils/fetchers/fetchers.ts @@ -1,4 +1,5 @@ import fetchExternalData from './fetch-external-data'; +import DayForecastResponse from './types/day-forecast'; import CurrentWeatherResponse from './types/current-forecast'; import LocationSearchResponse from './types/search-locations'; @@ -16,6 +17,44 @@ export const fetchSearch = async (query: string) => { })); } +export const fetchForecast = async (query: string) => { + const searchParams = new URLSearchParams({ q: query, days: '10' }); + + const { current, forecast, location } = await fetchExternalData({ + endpoint: '/forecast.json', + searchParams: searchParams, + }); + + return { + location: { + lat: location.lat, + lng: location.lon, + name: location.name, + }, + current: { + uv: current.uv, + wind: current.wind_kph, + humidity: current.humidity, + visibility: current.vis_km, + icon_code: current.condition.code, + }, + forecast: forecast.forecastday.map(({ date, day }) => ({ + date: date, + maxTemp: day.maxtemp_c, + minTemp: day.mintemp_c, + condition: day.condition.text, + icon_code: day.condition.code, + })), + hourly: forecast.forecastday[0].hour.map((test) => ({ + time: test.time, + temp: test.temp_c, + wind: test.wind_kph, + humidity: test.humidity, + feels_like: test.feelslike_c, + })), + }; +} + export const fetchCurrentWeather = async (query: string) => { const searchParams = new URLSearchParams({ q: query }); diff --git a/server/utils/fetchers/types/day-forecast.ts b/server/utils/fetchers/types/day-forecast.ts index 6c37533..32f46ce 100644 --- a/server/utils/fetchers/types/day-forecast.ts +++ b/server/utils/fetchers/types/day-forecast.ts @@ -11,9 +11,9 @@ type DayForecast = { }; type DayForecastResponse = { - forecast: DayForecast; location: Location; current: CurrentWeatherResponse['current']; + forecast: Record<'forecastday', DayForecast[]>; }; export default DayForecastResponse;