-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fetchers-types): add types for external responses (#21)
* refactor(server): simplify search response * feat(fetchers-types): add types for external fetchs * refactor(fetchers): rename type for search response
- Loading branch information
1 parent
0da932a
commit a85d9a4
Showing
6 changed files
with
169 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }`, | ||
})); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export type Condition = { | ||
text: string; | ||
icon: string; | ||
code: number; | ||
}; |