Adding search to weather service

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:

  1. 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') ...

Get Angular 6 for Enterprise-Ready Web Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.