So far, we have been passing parameters to get the weather for a city using its name and country code. By allowing users to enter zip codes, we must make our service more flexible to accept both types of inputs.
OpenWeatherMap's API accepts URI parameters, so we can refactor the existing getCurrentWeather function using a TypeScript union type and using a type guard, we can supply different parameters, while preserving type checking:
- Refactor the getCurrentWeather function in weather.service to handle both zip and city inputs:
app/src/weather/weather.service.ts getCurrentWeather( search: string | number, country?: string ): Observable<ICurrentWeather> { let uriParams = '' if (typeof search === 'string') ...