-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenWeather.ts
More file actions
52 lines (42 loc) · 1.27 KB
/
openWeather.ts
File metadata and controls
52 lines (42 loc) · 1.27 KB
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
import type {
CurrentWeatherResponse,
ForecastResponse,
GeocodeResult,
} from '@/types/openWeather';
import { apiCall } from '@/utils/http';
import { buildUrl } from '@/utils/url';
const WEATHER_BASE_URL = 'https://api.openweathermap.org/data/2.5';
const GEOCODING_BASE_URL = 'https://api.openweathermap.org/geo/1.0';
const OPEN_WEATHER_KEY = process.env.EXPO_PUBLIC_OPEN_WEATHER_KEY;
if (!OPEN_WEATHER_KEY) {
throw new Error('Missing EXPO_PUBLIC_OPEN_WEATHER_KEY');
}
const API_KEY: string = OPEN_WEATHER_KEY;
type Coords = { lat: number; lon: number };
type Units = 'metric' | 'imperial';
export function fetchCurrentWeather({ lat, lon }: Coords, units: Units = 'metric') {
const url = buildUrl(WEATHER_BASE_URL, '/weather', {
lat,
lon,
appid: API_KEY,
units,
});
return apiCall<CurrentWeatherResponse>(url);
}
export function fetchForecast({ lat, lon }: Coords, units: Units = 'metric') {
const url = buildUrl(WEATHER_BASE_URL, '/forecast', {
lat,
lon,
appid: API_KEY,
units,
});
return apiCall<ForecastResponse>(url);
}
export function fetchCitySuggestions(query: string, limit = 5) {
const url = buildUrl(GEOCODING_BASE_URL, '/direct', {
q: query,
limit,
appid: API_KEY,
});
return apiCall<GeocodeResult[]>(url);
}