UNPKG

@daniel-szulc/react-weather-widget

Version:

A simple weather widget created using React.js ☀. This Component loading forecast data from various weather providers ⛈.

183 lines (182 loc) 4.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; require("core-js/modules/es.symbol.description.js"); require("core-js/modules/es.promise.js"); var _request = _interopRequireDefault(require("../request.js")); var _unitsConverter = require("../unitsConverter.js"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function getWeatherType(name, iconCode, id) { switch (name) { case 'Thunderstorm': { if (200 <= id <= 202 || 230 <= id <= 232) return 'ThunderstormRain'; return name; } case 'Drizzle': { switch (id) { case 300: case 310: { return 'LightShowerRain'; } default: { return 'ShowerRain'; } } } case 'Rain': { switch (id) { case 500: { return 'LightRain'; } case 501: { return 'ModerateRain'; } case 511: { return 'FreezingRain'; } case 520: { return 'LightShowerRain'; } case 521: case 522: case 531: { return 'ShowerRain'; } default: { return name; } } } case 'Snow': { switch (id) { case 600: { return 'LightSnow'; } case 602: case 611: { return 'HeavySnow'; } case 612: { return 'LightShowerSnow'; } case 613: { return 'ShowerSnow'; } case 615: case 616: { return 'SnowAndRain'; } case 520: { return 'ShowerRain'; } case 620: { return 'LightShowerSnow'; } case 621: case 622: { return 'ShowerSnow'; } default: { return name; } } } case 'Atmosphere': { return 'Fog'; } case 'Clear': { return name; } case 'Clouds': { switch (id) { case 801: { return 'Cloudy'; } default: { return name; } } } default: { if (id >= 700 && id < 800) { return 'Fog'; } return name; } } } function isNight(sunrise, sunset, dt) { return dt < sunrise || dt > sunset; } function castData(data, opts) { return { location: data.name, isNight: isNight(data.sys.sunrise, data.sys.sunset, data.dt), temperature: Math.round((0, _unitsConverter.unitConvert)(data.main.temp, opts.tempUnit)), humidity: data.main.humidity, weather_desc: data.weather[0].description, weather_type: getWeatherType(data.weather[0].main, data.weather[0].icon, data.weather[0].id), feels_like: Math.round((0, _unitsConverter.unitConvert)(data.main.feels_like, opts.tempUnit)), wind: Math.round((0, _unitsConverter.unitConvert)(data.wind.speed, opts.windSpeedUnit) * 10) / 10, units: { temp: (0, _unitsConverter.getTempUnit)(opts.tempUnit), wind: (0, _unitsConverter.getSpeedUnit)(opts.windSpeedUnit) } }; } function openWeather(opts) { if (opts) { const body = { params: { q: opts.location, lon: opts.lon, lat: opts.lat, appid: opts.apiKey, lang: opts.lang, units: 'standard' } }; const url = 'https://api.openweathermap.org/data/2.5/weather'; const requestOptions = { url, body, followRedirect: true, headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' } }; return new Promise(async result => { (0, _request.default)(requestOptions).then(res => castData(res, opts)).then(data => result(data)); }); } } var _default = exports.default = openWeather;