Skip to content

Commit

Permalink
feat(forecast): add weekly forecast procedure (#23)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
IdoBouskila authored Nov 30, 2024
1 parent 9ffc559 commit 093aebf
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
7 changes: 7 additions & 0 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
39 changes: 39 additions & 0 deletions server/utils/fetchers/fetchers.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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<DayForecastResponse>({
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 });

Expand Down
2 changes: 1 addition & 1 deletion server/utils/fetchers/types/day-forecast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ type DayForecast = {
};

type DayForecastResponse = {
forecast: DayForecast;
location: Location;
current: CurrentWeatherResponse['current'];
forecast: Record<'forecastday', DayForecast[]>;
};

export default DayForecastResponse;
Expand Down

0 comments on commit 093aebf

Please sign in to comment.