UNPKG

x-weather

Version:

A collection of web components implementing portions of the OpenWeatherMap API.

2 lines (1 loc) 1.3 kB
import{convertTemperature as t}from"./utilities.js";import"./types.js";import{updateCurrentWeather as e}from"./updateCurrentWeather.js";var a=e=>{var{appid:a,host:n,latitude:i,longitude:r,placename:o}=e;if(a&&n&&i&&r&&o)return(t=>{var{appid:e,host:a,latitude:n,longitude:i,placename:r}=t,o="https://".concat(a,"/data/2.5/weather?lat=").concat(n,"&lon=").concat(i,"&appid=").concat(e);return fetch(o,{method:"GET"}).then(t=>{if(t.ok)return t.json()})})({appid:a,host:n,latitude:i,longitude:r,placename:o}).then(e=>{if(!e)throw new Error("failed to get result from the current weather service using: appid: ".concat(a,", host: ").concat(n,", latitude: ").concat(i,", longitude: ").concat(r,", placename: ").concat(o));var{coord:c,weather:s,base:p,main:u,visibility:d,wind:l,clouds:m,dt:h,sys:f,timezone:g,id:v,name:w,cod:j}=e;return JSON.stringify({sunrise:f.sunrise,sunset:f.sunset,timezone:g,iconAlt:s[0].description,iconSrc:s[0].icon,temperature:{kelvin:u.temp,celsius:t(u.temp,"kToC"),fahrenheit:t(u.temp,"kToF")},timestamp:h})})},n=t=>{var{appid:n,host:i}=t;return(t,r)=>{var{latitude:o,longitude:c,placename:s}=r().weather.preferences.location;return a({appid:n,host:i,latitude:o,longitude:c,placename:s}).then(a=>{a.length>0&&t(e({json:a}))})}};export default n;export{n as getCurrentWeather};