Return

All tests / src/pages/api weather.ts

76.92% Statements 50/65
35.29% Branches 6/17
80% Functions 8/10
77.04% Lines 47/61

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208    1x 1x 1x 1x                                                         1x                                                 1x                                               1x                             1x 2x   2x 2x 2x 2x 2x   2x 2x     2x 2x 2x         2x 2x 2x 2x         2x 2x     2x 2x 2x         2x                                       2x   2x       2x 2x     2x         2x   2x     2x       2x       2x   2x       2x 2x   2x 2x 2x 2x 2x                  
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from 'next';
import console from '@/helpers/console';
import allowCors from '../../helpers/cors';
import { CACHE, fetchWithTimeout } from '@/helpers/http';
import { isValidCityName } from '../../helpers/city';
 
interface WeatherData {
    city: string;
    name?: string | null;
    precipitation?: string | null;
    humidity?: string | null;
    windSpeed?: string | null;
    grades?: string | null;
    imageUrl?: string;
}
 
type WeatherResponse = WeatherData[] | { error: string };
 
type GeocodingResponse = {
    results?: Array<{ latitude: number; longitude: number; name: string; country?: string }>;
};
 
type ForecastResponse = {
    current?: {
        temperature_2m: number;
        relative_humidity_2m: number;
        precipitation: number;
        wind_speed_10m: number;
        weather_code: number;
    };
};
 
// Minimal WMO weather-code → description map, used as the image alt / name.
const WEATHER_CODE_TEXT: Record<number, string> = {
    0: 'Clear sky',
    1: 'Mainly clear',
    2: 'Partly cloudy',
    3: 'Overcast',
    45: 'Fog',
    48: 'Depositing rime fog',
    51: 'Light drizzle',
    53: 'Moderate drizzle',
    55: 'Dense drizzle',
    61: 'Slight rain',
    63: 'Moderate rain',
    65: 'Heavy rain',
    71: 'Slight snow',
    73: 'Moderate snow',
    75: 'Heavy snow',
    80: 'Slight rain showers',
    81: 'Moderate rain showers',
    82: 'Violent rain showers',
    95: 'Thunderstorm',
    96: 'Thunderstorm with slight hail',
    99: 'Thunderstorm with heavy hail',
};
 
// WMO weather-code → local icon under public/weather/ (SDD-001: no remote image hosts involved).
const WEATHER_CODE_ICON: Record<number, string> = {
    0: 'sun',
    1: 'sun',
    2: 'partly-cloudy',
    3: 'cloud',
    45: 'fog',
    48: 'fog',
    51: 'drizzle',
    53: 'drizzle',
    55: 'drizzle',
    61: 'rain',
    63: 'rain',
    65: 'rain',
    71: 'snow',
    73: 'snow',
    75: 'snow',
    80: 'showers',
    81: 'showers',
    82: 'showers',
    95: 'storm',
    96: 'storm',
    99: 'storm',
};
 
const emptyWeather = (city: string): WeatherData => ({
    city,
    name: null,
    precipitation: null,
    humidity: null,
    windSpeed: null,
    grades: null,
    imageUrl: undefined,
});
 
/**
 * @description Get current weather for a city from Open-Meteo (free, no API key).
 * City names arrive as "limerick+ireland"; the first token is used to geocode,
 * then the resulting coordinates feed the forecast endpoint.
 */
const getWeatherData = async (city: string): Promise<WeatherData> => {
    const query = city.split('+')[0].trim();
 
    const geoUrl = new URL('https://geocoding-api.open-meteo.com/v1/search');
    geoUrl.searchParams.set('name', query);
    geoUrl.searchParams.set('count', '1');
    geoUrl.searchParams.set('language', 'en');
    geoUrl.searchParams.set('format', 'json');
 
    const geoRes = await fetchWithTimeout(geoUrl.toString());
    Iif (!geoRes.ok) {
        throw new Error(`Geocoding HTTP error! status: ${geoRes.status}`);
    }
    const geo = (await geoRes.json()) as GeocodingResponse;
    const place = geo?.results?.[0];
    Iif (!place) {
        console.warn(`No geocoding result for city: ${city}`);
        return emptyWeather(city);
    }
 
    const forecastUrl = new URL('https://api.open-meteo.com/v1/forecast');
    forecastUrl.searchParams.set('latitude', String(place.latitude));
    forecastUrl.searchParams.set('longitude', String(place.longitude));
    forecastUrl.searchParams.set(
        'current',
        'temperature_2m,relative_humidity_2m,precipitation,wind_speed_10m,weather_code'
    );
 
    const forecastRes = await fetchWithTimeout(forecastUrl.toString());
    Iif (!forecastRes.ok) {
        throw new Error(`Forecast HTTP error! status: ${forecastRes.status}`);
    }
    const forecast = (await forecastRes.json()) as ForecastResponse;
    const current = forecast?.current;
    Iif (!current) {
        console.warn(`No forecast data for city: ${city}`);
        return emptyWeather(city);
    }
 
    return {
        city,
        name: WEATHER_CODE_TEXT[current.weather_code] ?? null,
        grades: `${Math.round(current.temperature_2m)}`,
        precipitation: `${current.precipitation} mm`,
        humidity: `${current.relative_humidity_2m}%`,
        windSpeed: `${Math.round(current.wind_speed_10m)} km/h`,
        imageUrl: WEATHER_CODE_ICON[current.weather_code]
            ? `/weather/${WEATHER_CODE_ICON[current.weather_code]}.svg`
            : undefined,
    };
};
 
/**
 * @description Get weather data from Open-Meteo
 * @param req {NextApiRequest}
 * @param res {NextApiResponse}
 * @returns Promise<void>
 * @example http://localhost:3000/api/weather?cities=Madrid,Barcelona
 */
export default allowCors(async function handler(req: NextApiRequest, res: NextApiResponse<WeatherResponse>) {
    // Only allow GET requests
    Iif (req.method !== 'GET') {
        return res.status(405).json({ error: 'Method not allowed' });
    }
 
    const { query } = req;
    const { cities = '' } = query;
 
    // Validate cities parameter
    Iif (!cities || typeof cities !== 'string') {
        return res.status(400).json({ error: 'Cities parameter must be a non-empty string' });
    }
 
    // Validate cities format and length
    const citiesArray = String(cities)
        .split(',')
        .map((city) => city.trim())
        .filter(Boolean);
 
    Iif (citiesArray.length === 0) {
        return res.status(400).json({ error: 'At least one city must be provided' });
    }
 
    Iif (citiesArray.length > 5) {
        return res.status(400).json({ error: 'Maximum 5 cities allowed' });
    }
 
    const invalidCities = citiesArray.filter((city) => !isValidCityName(city));
 
    Iif (invalidCities.length > 0) {
        return res.status(400).json({ error: `Invalid city names: ${invalidCities.join(', ')}` });
    }
 
    try {
        await Promise.allSettled(citiesArray.map((city) => getWeatherData(city)))
            .then((raw) => {
                const results = raw
                    .filter((r): r is PromiseFulfilledResult<WeatherData> => r.status === 'fulfilled')
                    .map((result) => result.value);
                res.setHeader('Cache-Control', CACHE.weather);
                res.status(200).json(results);
            })
            .catch((err) => res.status(500).json({ error: err.message }));
    } catch (err: unknown) {
        Iif (err instanceof Error) {
            res.status(500).json({ error: err.message });
        }
    }
});