svelte-ts/src/routes/temps/+page.server.ts

62 líneas
2,6 KiB
TypeScript
Original Vista normal Històric

2024-03-11 19:23:50 +01:00
export const prerender = false;
2023-02-06 13:04:36 +01:00
const headers = { 'X-API-KEY': '1FvqqqUpoeNgNpxZQxJX6FBDLbwKscx5hgXhZxUb', 'Content-type': 'application/json' }
2023-02-06 21:25:31 +01:00
const geocodingApiBase: string = "https://geocoding-api.open-meteo.com/v1/search";
const openMeteoApiBase: string = "https://api.open-meteo.com/v1/forecast";
2023-02-03 15:06:55 +01:00
let location: string = "L'Hospitalet de Llobregat";
let municipis = await fetch('https://api.meteo.cat/referencia/v1/municipis', { method: 'GET', headers: new Headers(headers), credentials: 'include' }).then( (data) => data.json() );
2023-02-06 21:25:31 +01:00
export function load(): object {
2023-02-06 13:04:36 +01:00
let objMunicipi = getCityCode( location );
if (objMunicipi === location) {
2023-02-06 21:25:31 +01:00
return { location, weatherData: getWeather(location).then( (data) => data.message ), openMeteoData: getFromOpenMeteo(location) }
2023-02-06 13:04:36 +01:00
}
let codiMunicipi: string = objMunicipi.codi;
let locFullName: string = objMunicipi.nom;
2023-02-06 21:25:31 +01:00
return { location: locFullName, weatherData: getWeather(codiMunicipi), openMeteoData: getFromOpenMeteo(location), consums: checkConsum() }
2023-02-03 15:06:55 +01:00
};
2023-02-06 13:04:36 +01:00
function changeLocation(newLoc: string): void {
2023-02-03 15:06:55 +01:00
location = newLoc;
}
2023-02-06 13:04:36 +01:00
async function fetchApi(url: string) {
let response: Response = await fetch(url, { method: 'GET', headers: new Headers(headers), credentials: 'include' })
return response;
}
2023-02-03 15:06:55 +01:00
export const actions = {
changeLoc: async ({ request }) => {
const data: FormData = await request.formData();
changeLocation(data.get('newloc'));
}
}
2023-02-06 21:25:31 +01:00
async function checkConsum(): Promise<any> {
let response: Response = await fetchApi('https://api.meteo.cat/quotes/v1/consum-actual');
return response.json();
}
2023-02-06 13:04:36 +01:00
async function getWeather(codiMunicipi: string): Promise<any> {
if (codiMunicipi !== '') {
let response = await fetchApi(`https://api.meteo.cat/pronostic/v1/municipal/${codiMunicipi}`)
return response.json();
}
return '';
2023-02-03 15:06:55 +01:00
}
2023-02-06 21:25:31 +01:00
async function getFromOpenMeteo(ubicacio: string): Promise<any> {
let geoCodeResponse: Response = await fetch(`${geocodingApiBase}?name=${ubicacio}&count=1`);
// let coordinates: { latitude: Promise<number>, longitude: Promise<number> } = { latitude: geoCodeResponse.json().then( (data) => data[0].latitude ), longitude: geoCodeResponse.json().then( (data) => data[0].longitude )};
// let weatherData: Response = await fetch(`${openMeteoApiBase}?latitude=${coordinates.latitude}&longitude=${coordinates.longitude}&hourly=temperature_850hPa&models=best_match`);
return geoCodeResponse.json();
}
2023-02-06 13:04:36 +01:00
function getCityCode(locName: string): object | string {
let foundMunicipis: object[] = municipis.filter( municipi => municipi["nom"].toLowerCase().startsWith(locName.toLowerCase()) )
if ( foundMunicipis.length > 0 ) {
return foundMunicipis[0];
}
return locName;
2023-02-03 15:06:55 +01:00
}