Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(forecast): add weekly forecast procedure #23

Merged
merged 6 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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