omorest / weatherweb-front Goto Github PK
View Code? Open in Web Editor NEWHome Page: https://theweathernow.vercel.app/
Home Page: https://theweathernow.vercel.app/
https://github.com/omorest/WeatherWeb/blob/f00f1221a785ae65a0836fddf0af761c4cd541fc/src/index.js#L13
@omorest Holaa bro! 🙌 he estado mirándote el código por lo que habíamos hablado, y justo en desde esta línea veo que haces lo que se suele llamar parsear los datos de la request. En vez de hacerlo aquí puedes crearte una función toWeatherInformation
o similar y meterle ahí toda las transformación que haces.
Esa función la llamas luego antes del return en el request.js tal que así:
export const getDataWeatherByLatLon = async (latitude, longitude) => {
const url = `https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&units=metric&appid=${process.env.KEY}`;
const res = await fetch(url);
const data = await res.json();
return toWeatherInformation(data);
}
Puedes crear un fichero dentro de un directorio weather y meterle ahí el parsers.js de esta manera puedes tener algo así como:
src/api/weather/requests.js ó endpoints.js
/parsers.js
/models.js // esto no pero por ejemplo si usas typescript siempre los tipos van en otro directorio aparte y así lo tienes todo organizado.
De esta manera en la función de initInformationWeather
te quedaría algo así:
const iniInformationWeather = () => {
async function success(position) {
const {latitude, longitude} = position.coords;
const dataWeather = await getDataWeatherByLatLon(latitude, longitude);
createInformationWeather(weatherInfoRelevant);
};
navigator.geolocation.getCurrentPosition(success);
}
La idea es que quitas de la función initInformationWeather la complejidad del parseo ya que su responsabilidad no es hacerlo, sino tal y como lo haces, coger la posición, llamar a la request y crear la información. Los parseos y cambios de modelo desde el back al front y viceversa se suelen hacer siempre en el punto más cercano a la salida o entrada de datos del front, en nuestro caso siempre serán las request. Por ello los parsers siempre se suelen poner ahí 😄
Espero haberte ayudado bro! 🙌
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.