Skip to content

Commit

Permalink
feat(fetchers-types): add types for external responses (#21)
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
  • Loading branch information
IdoBouskila authored Nov 30, 2024
1 parent 0da932a commit a85d9a4
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 18 deletions.
27 changes: 9 additions & 18 deletions server/utils/fetchers/fetchers.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
import fetchExternalData from './fetch-external-data';

type ExternalSearchResponse = Array<{
id: number;
lat: number;
lon: number;
name: string;
region: string;
country: string;
}>;
import LocationSearchResponse from './types/search-locations';

export const fetchSearch = async (query: string) => {
const searchParams = new URLSearchParams();
searchParams.set('q', query);
const searchParams = new URLSearchParams({ q: query });

const data = await fetchExternalData<ExternalSearchResponse>({
endpoint: '/search.json',
searchParams: searchParams,
});
const data = await fetchExternalData<LocationSearchResponse>({
endpoint: '/search.json',
searchParams: searchParams,
});

return data.map(({ name, region, country, ...rest }) => ({
...rest,
name: `${ name }, ${ region }, ${ country }`,
return data.map((location) => ({
id: location.id,
name: `${ location.name }, ${ location.region }, ${ location.country }`,
}));
}
41 changes: 41 additions & 0 deletions server/utils/fetchers/types/current-forecast.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import Location from './location';
import { Condition } from './shared';

type Current = {
last_updated_epoch: number;
last_updated: string;
temp_c: number;
temp_f: number;
is_day: number;
condition: Condition;
wind_mph: number;
wind_kph: number;
wind_degree: number;
wind_dir: string;
pressure_mb: number;
pressure_in: number;
precip_mm: number;
precip_in: number;
humidity: number;
cloud: number;
feelslike_c: number;
feelslike_f: number;
windchill_c: number;
windchill_f: number;
heatindex_c: number;
heatindex_f: number;
dewpoint_c: number;
dewpoint_f: number;
vis_km: number;
vis_miles: number;
uv: number;
gust_mph: number;
gust_kph: number;
};

type CurrentWeatherResponse = {
current: Current;
location: Location;
};

export default CurrentWeatherResponse;
92 changes: 92 additions & 0 deletions server/utils/fetchers/types/day-forecast.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import Location from './location';
import { Condition } from './shared';
import CurrentWeatherResponse from './current-forecast';

type DayForecast = {
day: Day;
hour: Hour[];
date: string;
astro: Astro;
date_epoch: number;
};

type DayForecastResponse = {
forecast: DayForecast;
location: Location;
current: CurrentWeatherResponse['current'];
};

export default DayForecastResponse;

type Day = Record<
| 'maxtemp_c'
| 'maxtemp_f'
| 'mintemp_c'
| 'mintemp_f'
| 'avgtemp_c'
| 'avgtemp_f'
| 'maxwind_mph'
| 'maxwind_kph'
| 'totalprecip_mm'
| 'totalprecip_in'
| 'totalsnow_cm'
| 'avgvis_km'
| 'avgvis_miles'
| 'avghumidity'
| 'daily_will_it_rain'
| 'daily_chance_of_rain'
| 'daily_will_it_snow'
| 'daily_chance_of_snow'
| 'uv',
number
> & {
condition: Condition;
};

type Hour = {
time_epoch: number;
time: string;
temp_c: number;
temp_f: number;
is_day: number;
condition: Condition;
wind_mph: number;
wind_kph: number;
wind_degree: number;
wind_dir: string;
pressure_mb: number;
pressure_in: number;
precip_mm: number;
precip_in: number;
snow_cm: number;
humidity: number;
cloud: number;
feelslike_c: number;
feelslike_f: number;
windchill_c: number;
windchill_f: number;
heatindex_c: number;
heatindex_f: number;
dewpoint_c: number;
dewpoint_f: number;
will_it_rain: number;
chance_of_rain: number;
will_it_snow: number;
chance_of_snow: number;
vis_km: number;
vis_miles: number;
gust_mph: number;
gust_kph: number;
uv: number;
};

type Astro = {
sunrise: string;
sunset: string;
moonrise: string;
moonset: string;
moon_phase: string;
moon_illumination: number;
is_moon_up: number;
is_sun_up: number;
};
12 changes: 12 additions & 0 deletions server/utils/fetchers/types/location.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
type Location = {
name: string;
region: string;
country: string;
lat: number;
lon: number;
tz_id: string;
localtime_epoch: number;
localtime: string;
};

export default Location;
10 changes: 10 additions & 0 deletions server/utils/fetchers/types/search-locations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type ExternalSearchResponse = {
id: number;
lat: number;
lon: number;
name: string;
region: string;
country: string;
}[];

export default ExternalSearchResponse;
5 changes: 5 additions & 0 deletions server/utils/fetchers/types/shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type Condition = {
text: string;
icon: string;
code: number;
};

0 comments on commit a85d9a4

Please sign in to comment.