ChrQR
a21c44185f
The application structure and naming should take in to account the weathernow component will be the container
32 lines
1.6 KiB
TypeScript
32 lines
1.6 KiB
TypeScript
'use server'
|
|
import { Forecast, HourlyForecast, LocationType, coordType } from "@/types/types";
|
|
|
|
//takes address and returns coords in obj as {lat: number, lng: number}
|
|
export async function getLocation(searchLocation: string): Promise<coordType>{
|
|
const placesKey = process.env.PLACES_API;
|
|
const url = `https://maps.googleapis.com/maps/api/geocode/json?address=${searchLocation}&key=${placesKey}`;
|
|
const res = await fetch(url);
|
|
if (!res.ok) {
|
|
throw new Error(`There was an error fetching the data`);
|
|
}
|
|
const data: LocationType = await res.json();
|
|
return {
|
|
name: data.results[0].formatted_address,
|
|
geo: data.results[0].geometry.location
|
|
}
|
|
}
|
|
|
|
|
|
|
|
export async function getHourlyForecast(geoLocation: coordType): Promise<HourlyForecast> {
|
|
const { lat, lng } = geoLocation.geo;
|
|
const tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
const url = `https://api.open-meteo.com/v1/forecast?latitude=${lat}&longitude=${lng}¤t=temperature_2m,apparent_temperature,is_day,precipitation,rain,showers,snowfall,weather_code,cloud_cover,wind_speed_10m,wind_direction_10m&hourly=temperature_2m,apparent_temperature,precipitation_probability,precipitation,weather_code,wind_speed_10m,is_day&daily=weather_code,temperature_2m_max,temperature_2m_min,apparent_temperature_max,apparent_temperature_min,sunrise,sunset,daylight_duration,sunshine_duration,uv_index_max,precipitation_sum,precipitation_hours,wind_speed_10m_max&timezone=${tz}`
|
|
const res = await fetch(url);
|
|
if (!res.ok) {
|
|
throw new Error(`Failed to fetch the weather data`);
|
|
}
|
|
const data: HourlyForecast = await res.json();
|
|
return data;
|
|
}
|