All academies
All projects

Weather App

easy

Build a weather dashboard that fetches current conditions and a five-day forecast from a public API. The focus is typing external data: writing response interfaces, validating unknown JSON at the boundary, and modeling loading, success, and error states as a discriminated union.

Next.jsTypeScriptTailwind CSSOpenWeather API

Architecture

  • UI layer: search input, current-conditions card, and forecast strip components driven entirely by typed props
  • State layer: a useWeather hook that holds a RequestState union (idle, loading, success, error) so the UI can never render half-loaded data
  • Service layer: a weather API client that fetches, validates, and maps raw responses into clean domain types
  • API layer: a Next.js route handler that proxies the third-party API and keeps the API key on the server
  • Type layer: separate raw-response types and domain types, with mapping functions between them

Folder structure

src/
  app/
    page.tsx
    api/
      weather/
        route.ts
  components/
    SearchBar.tsx
    CurrentConditions.tsx
    ForecastStrip.tsx
    ErrorBanner.tsx
  hooks/
    useWeather.ts
  lib/
    weatherClient.ts
    mapWeather.ts
  types/
    weather.ts

Domain type definitions

Start every project by defining its types — they become the contract for every component, service, and API call that follows.

export interface CurrentWeather {
  city: string;
  temperatureC: number;
  feelsLikeC: number;
  humidity: number;
  condition: string;
  icon: string;
}

export interface ForecastDay {
  date: string;
  minC: number;
  maxC: number;
  condition: string;
  icon: string;
}

export interface WeatherReport {
  current: CurrentWeather;
  forecast: ForecastDay[];
}

export type RequestState<T> =
  | { status: "idle" }
  | { status: "loading" }
  | { status: "success"; data: T }
  | { status: "error"; message: string };

Build steps

  1. 1Define the domain types — CurrentWeather, ForecastDay, WeatherReport, and the generic RequestState union.
  2. 2Create the API route handler that calls OpenWeather with the server-side key and returns JSON.
  3. 3Write raw-response interfaces for the OpenWeather payload in weatherClient.ts, typed from the actual API docs.
  4. 4Write mapWeather.ts to convert raw responses into WeatherReport, converting units and handling missing fields.
  5. 5Implement useWeather so it transitions through RequestState values and never exposes undefined data on success.
  6. 6Build SearchBar with a typed submit handler and debounce the city query.
  7. 7Render CurrentConditions and ForecastStrip only when status is success, letting narrowing prove data exists.
  8. 8Add ErrorBanner for the error state and a retry button that re-triggers the fetch.