<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Victor Kurauchi</title>
    <description>The latest articles on DEV Community by Victor Kurauchi (@victorkurauchi).</description>
    <link>https://dev.to/victorkurauchi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F29662%2F778f6cd2-da19-4f9d-9e31-e6175b9b6997.jpeg</url>
      <title>DEV Community: Victor Kurauchi</title>
      <link>https://dev.to/victorkurauchi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/victorkurauchi"/>
    <language>en</language>
    <item>
      <title>Exploring Javascript libraries</title>
      <dc:creator>Victor Kurauchi</dc:creator>
      <pubDate>Mon, 04 Feb 2019 05:31:21 +0000</pubDate>
      <link>https://dev.to/victorkurauchi/exploring-javascript-libraries-4oag</link>
      <guid>https://dev.to/victorkurauchi/exploring-javascript-libraries-4oag</guid>
      <description>&lt;p&gt;This blog post will explore JavaScript, focusing in how we can create a JavaScript library that works either in client-side and server-side, what many people call today as Isomorphic Javascript.&lt;/p&gt;

&lt;p&gt;We’ll go through a few important parts in this article, which are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Build the library&lt;/li&gt;
&lt;li&gt;Create the build file with Webpack&lt;/li&gt;
&lt;li&gt;Use the library from a server side application&lt;/li&gt;
&lt;li&gt;Use the library from a client side application&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;&lt;a href="https://github.com/victorkurauchi/exploring-js-libraries" rel="noopener noreferrer"&gt;You can check this repo for guidance during our next steps.&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Build the library
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;So, what’s a library ?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In small words, it’s safe to say that a library is a set of code responsible for doing one thing, and doing it with excellence. This way you can reuse your library whenever and wherever you need to work with this specific situation.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;So, why use a library ?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Think about making HTTP requests, we don’t want to add more complex code in every project in which we need to make HTTP calls, so we could choose one from many existent HTTP libraries. In Javascript land we have axios and node-fetch that are quite popular.&lt;/p&gt;

&lt;p&gt;So instead of making our own functions every time to make HTTP calls in our projects, we just require the library and save some time and effort for what we really need to do.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;What are we going to create ?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A library that can fetch weather by city, showing the next 5 days' predictions (consuming a Public API &lt;a href="https://www.metaweather.com/api/" rel="noopener noreferrer"&gt;https://www.metaweather.com/api/&lt;/a&gt;). We definitely can have a more complex library to handle the infos as we need. For the sake of simplicity, now we're doing only the fetch and showing predictions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let’s start our library. First things first:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir weather &amp;amp;&amp;amp; cd weather
npm init -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;&lt;em&gt;Adding axios dependency&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;npm i axios&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Adding our code to grab weather information from the public API&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;As we saw before, we can benefit from libraries to focus on what we need to build. In this case we are using axios to make our HTTP calls and focus only on Weather information that is returned to us and handle this response.&lt;/p&gt;

&lt;p&gt;For now our library will only return the sources where the predictions came from and the weather data grouped by dates.&lt;/p&gt;

&lt;p&gt;Now if you create a test file you can see our library in action.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You now can run from your terminal &lt;code&gt;node weather-consumer.js&lt;/code&gt; and you'll see our library doing its job :)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2At8iGzs3t_H4Nsc7zRhn_JQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2At8iGzs3t_H4Nsc7zRhn_JQ.png" alt="Alt text of image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Quite simple, isn’t ?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So now we don’t need to add repeated code in our many different projects every time we need to handle weather. We can only require our new library and do the job with few lines of javascript code.&lt;/p&gt;




&lt;h3&gt;
  
  
  Create the build file with Webpack
&lt;/h3&gt;

&lt;p&gt;Now we want to use our library from another project. And we're going in parts, so first we'll make it work for server-side applications and then for client-side apps. And for this to happen Webpack can be a good friend!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;At its core, webpack is a static module bundler for modern JavaScript applications&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There's a lot more about what Webpack can offer, but for now I’ll assume we know the basics about what Webpack is and what Webpack can do for us. (But please feel free to check the docs and intro at &lt;a href="https://webpack.js.org/concepts/" rel="noopener noreferrer"&gt;https://webpack.js.org/concepts/&lt;/a&gt;.)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Installing and configuring Webpack&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save-dev webpack
npm install --save-dev webpack-cli
npm install --save-dev babel-loader @babel/core @babel/preset-env
npm install --save @babel/polyfill
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;&lt;em&gt;Babel and polyfills&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We are using babel and polyfills to be able to build our library using ES6 syntax and use the awesome features that come with it. So Babel will take care of transpiling our fresh code to ES5 syntax, making it to work in older browsers or environments.&lt;/p&gt;

&lt;p&gt;Babel can definitely get more complex depending on what is your use and the result you want to achieve, but for our purposes those packages will be enough.&lt;/p&gt;

&lt;p&gt;We need to add our npm scripts to run on dev and production environment:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Also we need to add webpack.config.js, so when we run &lt;code&gt;npm run build&lt;/code&gt; webpack will read the config from it to build our final script.&lt;/p&gt;

&lt;p&gt;Note that for now we're not focussing on optimising our build file, but it's good to have in mind that it should be optimised and we can do that.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You can now run &lt;code&gt;npm run build&lt;/code&gt; and will see that we have a dist/weather.js&lt;/p&gt;

&lt;p&gt;Please &lt;a href="https://github.com/victorkurauchi/exploring-js-libraries/blob/master/weather/package.json" rel="noopener noreferrer"&gt;check the file here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;What's happening with our build script is that we call the webpack cli and then the cli goes through our configuration file webpack.config.js, read the config we have set before and create the bundle for us.&lt;/p&gt;

&lt;p&gt;Our &lt;code&gt;webpack.config.js&lt;/code&gt; is using &lt;code&gt;babel-loader&lt;/code&gt; to transpile our code to ES5, this way our bundle file will contain JavaScript code that is compatible with current and older browsers or environments.&lt;/p&gt;

&lt;p&gt;Also, on line 5 note that we have &lt;code&gt;target: node&lt;/code&gt;, which means:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Because JavaScript can be written for both server and browser, webpack offers multiple deployment targets that you can set in your &lt;a href="https://webpack.js.org/configuration" rel="noopener noreferrer"&gt;webpack configuration&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;One important point to pay attention: We need to add &lt;code&gt;require('@babel/polyfill');&lt;/code&gt; as the first line of our library code, to prevent &lt;code&gt;regeneratorErrors&lt;/code&gt; when using our built file. And make sure you already have run &lt;code&gt;npm i --save @babel/polyfill&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;So let's check that in action…&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Use the library from a server side application
&lt;/h3&gt;

&lt;p&gt;We're going to use &lt;a href="https://www.fastify.io/" rel="noopener noreferrer"&gt;fastify&lt;/a&gt;, a small nodejs framework to create this example.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Our test case&lt;/strong&gt;&lt;br&gt;
Our server side application will use the weather library, let's say this application handles a catalog of places to travel.&lt;/p&gt;

&lt;p&gt;We want to show a list of places, then we can get the place detail and return the weather conditions for the next days for that place. For this scenario we're going to create 2 endpoints in our server side application.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;endpoint to list our travel destinations&lt;/li&gt;
&lt;li&gt;endpoint to retrieve details from a destination&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So let's get started.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd .
mkdir server-weather-app &amp;amp;&amp;amp; cd server-weather-app
npm init -y
npm install fastify

vim server.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now our base app is created and we need to use a feature from &lt;code&gt;npm&lt;/code&gt; called &lt;a href="https://docs.npmjs.com/cli/link.html" rel="noopener noreferrer"&gt;npm link&lt;/a&gt; that will make it easier to work with our package locally.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd ..
cd weather
npm link

cd ..
cd server-weather-app
npm link weather
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The code to handle our server will be like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;You can see it in action with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;node server&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;go to &lt;a href="http://localhost:3000/destinations" rel="noopener noreferrer"&gt;http://localhost:3000/destinations&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;go to &lt;a href="http://localhost:3000/destinations/sydney" rel="noopener noreferrer"&gt;http://localhost:3000/destinations/sydney&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By accessing those 2 endpoints you can check the work we did until here. The first one lists some destinations from a static json file (destinations.json).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;destinations.json&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  {
    "id": 1,
    "name": "sydney",
    "price": 700
  },
  {
    "id": 2,
    "name": "london",
    "price": 600
  },
  {
    "id": 3,
    "name": "paris",
    "price": 800
  }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The last one will return the destination you choose with the weather information about it. Something like that:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2A0goxi0AH57IJlJrm0rG1HA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2A0goxi0AH57IJlJrm0rG1HA.png" alt="Alt text of image"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;&lt;em&gt;So I think that's it for our server side part! Hopefully all the way until here will be useful for you. Maybe for next libraries that you need to create, or next projects you need to refactor and think about using some library. Or just to have some reference about it.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Originally posted on &lt;a href="https://theiconic.tech/exploring-javascript-libraries-9b91b635d842" rel="noopener noreferrer"&gt;Medium&lt;/a&gt;.&lt;br&gt;
*The blog post on Medium contemplates the second part as well, which is using the library from a client-side application. Maybe here it would be good to have the second part in a different post.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>web</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
