<?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: Ibrahim Adeniyi</title>
    <description>The latest articles on DEV Community by Ibrahim Adeniyi (@dendekky).</description>
    <link>https://dev.to/dendekky</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%2F175403%2F5a5e4ee7-c12a-483a-b7a3-c42b27649e9a.png</url>
      <title>DEV Community: Ibrahim Adeniyi</title>
      <link>https://dev.to/dendekky</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dendekky"/>
    <language>en</language>
    <item>
      <title>How To Remove Items From RJSF Custom ArrayTemplate</title>
      <dc:creator>Ibrahim Adeniyi</dc:creator>
      <pubDate>Wed, 20 Nov 2024 10:08:17 +0000</pubDate>
      <link>https://dev.to/dendekky/how-to-remove-items-from-rjsf-custom-arraytemplate-1clo</link>
      <guid>https://dev.to/dendekky/how-to-remove-items-from-rjsf-custom-arraytemplate-1clo</guid>
      <description>&lt;p&gt;Using React JSON Schema Form can help save a lot of time and improve team collaboration. &lt;br&gt;
However, there will be many instances when the default templates are not good enough. This is when custom templates can be used to replace the defaults that come with this library. However, some of these functions and methods are not all documented in their documentation. &lt;br&gt;
Today we will look at removing items in an RJSF Custom Array Template. &lt;br&gt;
Our custom template for an array will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;ArrayFieldTemplate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ArrayFieldTemplateProps&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Title&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Title&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Row&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;justify&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;start&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;gutter&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Col&lt;/span&gt; &lt;span class="nx"&gt;span&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;22&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Col&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Col&lt;/span&gt;
            &lt;span class="nx"&gt;span&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
          &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;CloseOutlined&lt;/span&gt;
              &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onDropIndexClick&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
              &lt;span class="p"&gt;}}&lt;/span&gt;
            &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Col&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Row&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="p"&gt;))}&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;canAdd&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Button&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;primary&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onAddClick&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;block&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="nx"&gt;Add&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="p"&gt;)}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will see that we have a close button on each item with an onClick that calls the item's &lt;code&gt;onDropIndexClick&lt;/code&gt; method. This functions removes the item that's find at the index that's passed to it. It's an HOC that needs to be called first with the items' index, then called immediatley with the event.&lt;/p&gt;

&lt;p&gt;If you call it like &lt;code&gt;onClick={(event) =&amp;gt; {&lt;br&gt;
                element.onDropIndexClick(element.index);&lt;br&gt;
              }}&lt;/code&gt;, this function will not work as expected. &lt;/p&gt;

&lt;p&gt;Thanks for reading. &lt;/p&gt;

</description>
    </item>
    <item>
      <title>This condition will always return 'false' since the types X and Y have no overlap</title>
      <dc:creator>Ibrahim Adeniyi</dc:creator>
      <pubDate>Tue, 11 Jul 2023 19:13:01 +0000</pubDate>
      <link>https://dev.to/dendekky/this-condition-will-always-return-false-since-the-types-x-and-y-have-no-overlap-3395</link>
      <guid>https://dev.to/dendekky/this-condition-will-always-return-false-since-the-types-x-and-y-have-no-overlap-3395</guid>
      <description>&lt;p&gt;There are a few reasons why typescript will throw this error. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Typescript enum limitations&lt;/li&gt;
&lt;li&gt;Messing up logical operators.&lt;/li&gt;
&lt;li&gt;Strictly comparing values of different types. 
In the last two cases, you can review your code logic and fix it. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Working with enums, you might inadvertently create a situation where you run into this error. Let's take a look at the typing below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kr"&gt;enum&lt;/span&gt; &lt;span class="nx"&gt;DashboardType&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;Shape&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Shape&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;SingleShape&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;SingleShape&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;MultipleShapes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;MultipleShapes&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we have a type of dashboard that could be a single shape or multiple shapes dashboard, or either of the two. &lt;br&gt;
In the case where the dashboard is a Shape dashboard, let's assume you have a component that uses this check to render or handle some logic.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dashboard&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;DashboardType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Shape&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the logic, if you have a check like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dashboard&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;DashboardType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Shape&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// some operations&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dashboard&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;DashboardType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;SingleShape&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="c1"&gt;// some rendering/operation&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dashboard&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;DashboardType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;MultipleShapes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="c1"&gt;// some rendering/operation&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;then typescript will throw an error like this &lt;code&gt;This condition will always return 'false' since the types dashboard and DashboardType have no overlap&lt;/code&gt;. &lt;br&gt;
You can use type coercion to fix the error like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dashboard&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;DashboardType&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;DashboardType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;SingleShape&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the other hand, you can just improve your conditional logic and the enum type if you can afford too. &lt;br&gt;
For example, the type DashboardType could become&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;SingleShape&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;SingleShape&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;MultipleShapes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;MultipleShapes&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, to handle a shape component(regardless of whether it's single or multiple), you can have the logic like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dashboard&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;DashboardType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;SingleShape&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;dashboard&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;DashboardType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;MultipleShapes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Happy coding!.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>enums</category>
      <category>webdev</category>
    </item>
    <item>
      <title>GraphQL Codegen Web Wrapper{TypeScript}.</title>
      <dc:creator>Ibrahim Adeniyi</dc:creator>
      <pubDate>Thu, 23 Feb 2023 19:11:38 +0000</pubDate>
      <link>https://dev.to/dendekky/graphql-codegen-web-wrappertypescript-49h6</link>
      <guid>https://dev.to/dendekky/graphql-codegen-web-wrappertypescript-49h6</guid>
      <description>&lt;p&gt;Creating graphql result types for a web application can be done by adding an extra step to your application's development process. &lt;br&gt;
That step has been extensively covered by the guys at &lt;a href="https://the-guild.dev/graphql/codegen" rel="noopener noreferrer"&gt;The Guild&lt;/a&gt;. &lt;br&gt;
However, I've created a &lt;a href="https://dendekky-graphql-codegen.vercel.app/" rel="noopener noreferrer"&gt;codegen web wrapper&lt;/a&gt; that serves a slightly different purpose which includes but might not be limited to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Skipping an extra step of adding the codegen to your development process but still wanting to know what result types the graphql api returns.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Getting all the return types from a graphql endpoint without setting up anything extraneous.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The wrapper guide and source code can be found on &lt;a href="https://dendekky-graphql-codegen.vercel.app/" rel="noopener noreferrer"&gt;github&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let me know what you think. &lt;br&gt;
If you have any questions regarding this, I am available in the comment section. Also, feel free to correct or add anything I might have missed. Do not hesitate to send me a message by email or on &lt;a href="https://twitter.com/dendekky" rel="noopener noreferrer"&gt;twitter&lt;/a&gt;&lt;/p&gt;

</description>
      <category>graphql</category>
      <category>nextjs</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Accessing LocalStorage in NextJS</title>
      <dc:creator>Ibrahim Adeniyi</dc:creator>
      <pubDate>Mon, 05 Oct 2020 20:07:16 +0000</pubDate>
      <link>https://dev.to/dendekky/accessing-localstorage-in-nextjs-39he</link>
      <guid>https://dev.to/dendekky/accessing-localstorage-in-nextjs-39he</guid>
      <description>&lt;p&gt;I recently migrated a Content Management System from Create React App to NextJS in order to score some SEO points. &lt;br&gt;
One of the challenges I faced was seeing these errors at compile time. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;window is undefined&lt;/code&gt; or &lt;code&gt;document is undefined&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Window&lt;/code&gt;, and &lt;code&gt;document&lt;/code&gt; are not available on the server. This is why you'll run into these types of errors if you are trying to access &lt;code&gt;window&lt;/code&gt; properties or &lt;code&gt;document&lt;/code&gt;. &lt;br&gt;
In my case, I was persisting my authentication token to localStorage on the previous application. &lt;/p&gt;

&lt;p&gt;To avoid running into these undefined errors at compile and build time, you can run a simple check.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;undefined&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

&lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This basically tells your piece of code only to run when it's in the client environment where it can access &lt;code&gt;window&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Keep hacking.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>nextjs</category>
    </item>
    <item>
      <title>Accessing LocalStorage in NextJS</title>
      <dc:creator>Ibrahim Adeniyi</dc:creator>
      <pubDate>Mon, 05 Oct 2020 20:02:14 +0000</pubDate>
      <link>https://dev.to/dendekky/handling-authentication-in-nextjs-5gd0</link>
      <guid>https://dev.to/dendekky/handling-authentication-in-nextjs-5gd0</guid>
      <description>&lt;p&gt;I recently migrated a Content Management System from Create React App to NextJS in order to score some SEO points. &lt;br&gt;
One of the challenges I faced was seeing these errors at compile time. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;window is undefined&lt;/code&gt; or &lt;code&gt;document is undefined&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Window&lt;/code&gt;, and &lt;code&gt;document&lt;/code&gt; are not available on the server. This is why you'll run into these types of errors if you are trying to access &lt;code&gt;window&lt;/code&gt; properties or &lt;code&gt;document&lt;/code&gt;. &lt;br&gt;
In my case, I was persisting my authentication token to &lt;code&gt;localStorage&lt;/code&gt; on the previous application.&lt;br&gt;
To avoid running into these undefined errors at compile and build time, you can run a simple check.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (typeof window !== "undefined") {

localStorage.setItem(key, value)

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This basically tells your piece of code only to run when it's in the client environment where it can access &lt;code&gt;window&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Keep hacking.&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>react</category>
      <category>authentication</category>
    </item>
    <item>
      <title>How To Build A Command-Line Tool With NodeJS - A step-by-step guide</title>
      <dc:creator>Ibrahim Adeniyi</dc:creator>
      <pubDate>Sun, 12 Jan 2020 11:45:04 +0000</pubDate>
      <link>https://dev.to/dendekky/how-to-build-a-command-line-tool-with-nodejs-a-step-by-step-guide-386k</link>
      <guid>https://dev.to/dendekky/how-to-build-a-command-line-tool-with-nodejs-a-step-by-step-guide-386k</guid>
      <description>&lt;p&gt;This post will guide developers on building CLI tools with node.js. You will also learn how to publish the tool to NPM. Node.js allows us to build command-line tools using JavaScript. There is a rich node.js package ecosystem as shown by the npm registry. &lt;br&gt;
Building CLI tools that people can use is a good way to increase your coding and problem-solving skills. In this post, we'll explore how I created a &lt;a href="https://www.npmjs.com/package/runweb" rel="noopener noreferrer"&gt;cli tool&lt;/a&gt; that checks if a website is up or down. You can find the source code &lt;a href="https://github.com/Dendekky/runweb-cli-tool" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  STEPS
&lt;/h1&gt;
&lt;h2&gt;
  
  
  Have A Plan
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Run the app on nodejs&lt;/li&gt;
&lt;li&gt;Get my arguments from the terminal&lt;/li&gt;
&lt;li&gt;Check the website status from &lt;a href="https://isitup.org" rel="noopener noreferrer"&gt;isitup&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Return the response to the terminal&lt;/li&gt;
&lt;li&gt;Create an option to launch the website if it is up.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Create A Node App
&lt;/h2&gt;

&lt;p&gt;Let's create a folder for our project and navigate to the root of the project directory on our terminal.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;mkdir cli-project &amp;amp;&amp;amp; cd cli-project&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Initialize a node project&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm init -y&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;This creates a node app with a package.json structure like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "cli-project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1"
  },

  "keywords": [],
  "author": "",
  "license": "ISC",
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create an index.js file&lt;/p&gt;

&lt;p&gt;&lt;code&gt;touch index.js&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Open this file and print "Hello, here is my first CLI tool" to the console&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\\ index.js

console.log("Hello, here is my first CLI tool")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Navigate back to the terminal now and run &lt;code&gt;node index&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;$ node index
Hello, here is my first CLI tool
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that your node app runs, it is time to turn it into a shell command.&lt;br&gt;
In order to invoke our index.js file directly without the node command, put this &lt;code&gt;#!/usr/bin/env node&lt;/code&gt; at the top of our index.js file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\\ index.js
#!/usr/bin/env node

console.log("Hello, here is my first CLI tool")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we are going to add a bin property to our package.json file. However, our project is going to run on a single file so we won't use the bin property to specify command names. We will be using the name property for that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "cli-project",
  "version": "1.0.0",
  // ...
  "bin": "./index.js",
  // ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you run &lt;code&gt;cli-project&lt;/code&gt; in the project's directory now it should return&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cli-project
Hello, here is my first CLI tool
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will make two changes now. We don't want our CLI name to be &lt;code&gt;cli-project&lt;/code&gt;. So we will change the value of the package.json &lt;code&gt;name&lt;/code&gt; property to &lt;code&gt;webcheck&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;{
  "name": "webcheck",
  // ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our shell command is still local. It is time to make it global. Run&lt;br&gt;
&lt;code&gt;npm link&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Navigate away from the root of our project directory and run &lt;code&gt;webcheck&lt;/code&gt; from any directory. You should see this as the result.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ webcheck
Hello, here is my first CLI tool
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;CONGRATULATIONS!!! You just created your first shell command with a node app. This can be pushed to NPM for users to download and run but since we are only halfway done with project, I would recommend waiting until our node app is done before publishing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Parse Arguments From The Terminal
&lt;/h2&gt;

&lt;p&gt;To parse our arguments from the terminal, we will be using a built-in node module, &lt;em&gt;argv&lt;/em&gt;. According to the official nodejs docs, &lt;a href="https://nodejs.org/docs/latest/api/process.html#process_process_argv" rel="noopener noreferrer"&gt;the process.argv property returns an array containing the command line arguments passed when the Node.js process was launched. The first element will be process.execPath. The second element will be the path to the JavaScript file being executed. The remaining elements will be any additional command line arguments&lt;/a&gt;. So, any argument we pass to the terminal will be the third element of the array. Edit your index.js file to look like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\\ index.js
#!/usr/bin/env node

console.log(processs.argv);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run your app on the terminal. The output should resemble this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ webcheck
[
  'C:\\Program Files\\nodejs\\node.exe',
  'C:\\Users\\adeniyi\\Desktop\\Projects\\cli-project\\index'
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now add an additional argument to your command and your output should resemble this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ webcheck file
[
  'C:\\Program Files\\nodejs\\node.exe',
  'C:\\Users\\adeniyi\\Desktop\\Projects\\cli-project\\index',
  'file'
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: The more arguments you append, the larger the array becomes. For our purpose, we will limit our argument to a string and parse it into our project as the third element of the array. &lt;br&gt;
It is time to parse this argument into our app and fetch information from &lt;a href="http://isitup.remotelan.net/api/api.html" rel="noopener noreferrer"&gt;isitup api&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Open your index.js file and put this code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   #!/usr/bin/env node
   const fetch = require("node-fetch");

   // console.log(process.argv);
   const website = process.argv[2]; 

   function CheckWeb(name) {
        const info =fetch(`https://isitup.org/${name}.json`)
        .then(response =&amp;gt; response.json());

        info.then(function(result) {
            if (result.response_code == 200) {
                console.log('website is up and running')
            } else {
               console.log('website is down')
            }
        }

   CheckWeb(website);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We required the &lt;code&gt;node-fetch&lt;/code&gt; package to help us get our data from the &lt;a href="http://isitup.remotelan.net/api/api.html" rel="noopener noreferrer"&gt;isitup api&lt;/a&gt; because node doesn't support native javascript fetch. Run &lt;code&gt;npm install node-fetch&lt;/code&gt;.&lt;br&gt;
Our CheckWeb function takes in a name argument and fetches the appropriate response from the API. We are now passing our command line argument into the function. Let's head over to the terminal and see some code running.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ webcheck duckduckgo.com
website is up and running
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yay!!!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ webcheck google.com
website is down
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wait what?! &lt;br&gt;
Let's try to figure out what went wrong here. My favorite debugging tool to the rescue(console).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   #!/usr/bin/env node
   //... 

   function CheckWeb(name) {
        const info =fetch(`https://isitup.org/${name}.json`)
        .then(response =&amp;gt; response.json());

        info.then(function(result) {
            console.log(result)
        }

   CheckWeb(website);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the app from the terminal again&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ webcheck google.com
{
  domain: "google.com",
  port: 80,
  status_code: 1,
  response_ip: "216.58.210.206",
  response_code: 301,
  response_time: 0.008
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, &lt;a href="https://en.wikipedia.org/wiki/List_of_HTTP_status_codes" rel="noopener noreferrer"&gt;the 301 redirect is considered a best practice for upgrading users from HTTP to HTTPS&lt;/a&gt;. We need our app to know this and tell us google is up. There are two ways we can go about this; a chain of if else statements navigating the appropiate response codes or looking fore &lt;code&gt;null&lt;/code&gt; response codes&lt;br&gt;
This&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   #!/usr/bin/env node
   const fetch = require("node-fetch");

   // console.log(process.argv);
   const website = process.argv[2]; 

   function CheckWeb(name) {
        const info =fetch(`https://isitup.org/${name}.json`)
        .then(response =&amp;gt; response.json());

        info.then(function(result) {
            if (result.response_code == null) {
                console.log('website is down')
            } else {
               console.log('website is up and running')
            }
        }

   CheckWeb(website);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run your app&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ webcheck google.com
website is up and running
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OR This&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   #!/usr/bin/env node
   const fetch = require("node-fetch");

   // console.log(process.argv);
   const website = process.argv[2]; 

   function CheckWeb(name) {
        const info =fetch(`https://isitup.org/${name}.json`)
        .then(response =&amp;gt; response.json());

        info.then(function(result) {
            if (result.response_code == 200) {
                console.log('\x1b[32m%s\x1b[0m', 'website is up and running');
            } else if (result.response_code == 301) {
                console.log('\x1b[34m%s\x1b[0m', 'website has been moved permanently but is up');
            } else if (result.response_code == 302){
                console.log('\x1b[34m%s\x1b[0m', 'temporary redirect, website is up');
            } else if (result.response_code == 403) {
                console.log('\x1b[33m%s\x1b[0m', 'information not found');
            }
            else {
                console.log('\x1b[31m%s\x1b[0m', 'website is down')
            }
        });

   CheckWeb(website);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This &lt;code&gt;'\x1b[31m%s\x1b[0m'&lt;/code&gt; snippet and others like it you see in the console statement determine the color of our response message.&lt;br&gt;
Run your app&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ webcheck google.com
website has been moved permanently but is up
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can publish the first version of our cli tool to NPM now. You need to create a &lt;code&gt;.npmignore&lt;/code&gt; file. Copy this into the file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//.npmignore

node_modules/

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This ensures that you don't publish node modules with the package. Now, run&lt;br&gt;
&lt;code&gt;npm publish&lt;/code&gt;&lt;br&gt;
If you have not logged in to npm from your terminal before, do so first&lt;br&gt;
&lt;code&gt;npm login&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Cheers mate. Users can now head to NPM to search for and download your cli tool.&lt;/p&gt;
&lt;h2&gt;
  
  
  Launching The Website From The Terminal
&lt;/h2&gt;

&lt;p&gt;For this, we will be requiring &lt;code&gt;open&lt;/code&gt; a package that will help us open our url. Then we will write a function we can call to launch website.&lt;br&gt;
&lt;code&gt;npm install open&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Edit your index.js file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/usr/bin/env node
const fetch = require("node-fetch");
const open = require("open");

const website = process.argv[2]; 

function CheckWeb(name) {
        const info =fetch(`https://isitup.org/${name}.json`)
        .then(response =&amp;gt; response.json());

        info.then(function(result) {
            function openWebSite () {
                setTimeout(function()
                { open(`https://${result.domain}`); }, 1000);
            };

            if (result.response_code == 200) {
                console.log('\x1b[32m%s\x1b[0m', 'website is up and running');
                openWebSite();
            } else if (result.response_code == 301) {
                console.log('\x1b[32m%s\x1b[0m', 'website has been moved permanently but is up');
                openWebSite();
            } else if (result.response_code == 302){
                console.log('\x1b[34m%s\x1b[0m', 'temporary redirect, website is up');
                openWebSite();
            } else if (result.response_code == 403) {
                console.log('\x1b[33m%s\x1b[0m', 'information not found');
                openWebSite();
            }
            else {
                console.log('\x1b[31m%s\x1b[0m', 'website is down')
            }
        });
    }
}

CheckWeb(website); 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function openWebsite launches the checked website in your default browser automatically from the terminal. However, we want our users to have the power to open the website or not. &lt;br&gt;
We are going to install two packages &lt;code&gt;arg&lt;/code&gt; and &lt;code&gt;inquirer&lt;/code&gt;. We shall be parsing the command line argument into options with &lt;code&gt;arg&lt;/code&gt; and &lt;code&gt;inquirer&lt;/code&gt; to prompt users for values. &lt;br&gt;
&lt;code&gt;npm install arg inquirer&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We are going to structure our index.js file like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/usr/bin/env node
const fetch = require("node-fetch");
const open = require('open');
const arg = require('arg');
const inquirer = require('inquirer');

function ParseCliArgsIntoOptions() {
    const args = arg(
      {
        '--website': Boolean,
        '--yes': Boolean,
        '-w': '--website',
        '-y': '--yes',
      },
      {
        argv: process.argv.slice(2),
      }
    );
    return {
      website: args['--website'] || false,
    };
}

async function PromptForOptions(options) {
    const questions = [];

    if (!options.website) {
      questions.push({
        type: 'confirm',
        name: 'website',
        message: 'Open the website on your browser?',
        default: false,
      });
    }

    const answers =  await inquirer.prompt(questions);
    return {
      ...options,
      website: options.website || answers.website,
    };
}

async function LaunchWebsite(result) {
    let options = ParseCliArgsIntoOptions();
    options =  await PromptForOptions(options);
    if (options.website == true) {
        open(`https://${result.domain}`); 
    }
}


const website = process.argv[2]; 

function CheckWeb(name) {
// ....
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What we have done is create a LaunchWebsite function that takes in two other functions &lt;code&gt;ParseCliArgsIntoOptions()&lt;/code&gt; which provides you a boolean yes/no option to the question prompted by the &lt;code&gt;PromptForOptions()&lt;/code&gt; function. If the chosen options is &lt;code&gt;true&lt;/code&gt; i.e 'yes', the website is opened.&lt;br&gt;
We will now inject the LaunchWebsite function into our Checkweb function and pass the result of our &lt;code&gt;fetch&lt;/code&gt; operation down to it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/usr/bin/env node
const fetch = require("node-fetch");
const open = require('open');
const arg = require('arg');
const inquirer = require('inquirer');

function ParseCliArgsIntoOptions() {
//...
}
async function PromptForOptions(options) {
//...
}
async function LaunchWebsite(result) {
//...
}

function CheckWeb(name) {
        const info =fetch(`https://isitup.org/${name}.json`)
        .then(response =&amp;gt; response.json());

        info.then(function(result) {
            if (result.response_code == 200) {
                console.log('\x1b[32m%s\x1b[0m', 'website is up and running');
                LaunchWebsite(result)
            } else if (result.response_code == 301) {
                console.log('\x1b[32m%s\x1b[0m', 'website has been moved permanently but is up');
                LaunchWebsite(result)
                console.log('\x1b[34m%s\x1b[0m', 'website has been moved permanently but is up');
                LaunchWebsite(result)
            } else if (result.response_code == 302){
                console.log('\x1b[34m%s\x1b[0m', 'temporary redirect, website is up');
                LaunchWebsite(result)
            } else if (result.response_code == 403) {
                console.log('\x1b[33m%s\x1b[0m', 'information not found');
                LaunchWebsite(result)
            }
            else {
                console.log('\x1b[31m%s\x1b[0m', 'website is down')
            }
        });
    }
}
CheckWeb(website);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you run your shell command on the terminal now, this should happen&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ webcheck google.com
website has been moved permanently but is up
? Open the website on your browser? (y/N)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Great! The journey is almost over. &lt;br&gt;
Let us round off by handling errors for people who might forget to add the website extension. The website might be up but this will definitely return it as down.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ webcheck google
website is down
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are many ways to approach this. You could write create an array of all the possible extensions (over 400) and write a regex function that searches for any of the array arguments in our website string. A bit unnecessary if you ask me. Or you could just search for the substring '.' in our argument as done below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/usr/bin/env node
const fetch = require("node-fetch");
const open = require('open');
const arg = require('arg');
const inquirer = require('inquirer');

function ParseCliArgsIntoOptions() {
//...
}
async function PromptForOptions(options) {
//...
}
async function LaunchWebsite(result) {
//...
}

function CheckWeb(name) {
      if (name.indexOf('.') &amp;gt; -1) {
        const info =fetch(`https://isitup.org/${name}.json`)
        .then(response =&amp;gt; response.json());

        info.then(function(result) {
           //...
        });
    } else {
        console.log('\x1b[31m%s\x1b[0m', 'please append your url extension e.g(mouse.com)')
    }
}
CheckWeb(website);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At the terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ webcheck google
please append your url extension e.g(mouse.com)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's publish our updated tool again. You have to update the version number. Run&lt;br&gt;
&lt;code&gt;npm version 1.1.0&lt;/code&gt;&lt;br&gt;
Then push to NPM&lt;br&gt;
&lt;code&gt;npm publish&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Our CLI tool is up and running on NPM. &lt;br&gt;
If you have any questions regarding this, I am available in the comment section. Also, feel free to correct or add anything I might have missed. Do not hesitate to send me a message by email or on &lt;a href="https://twitter.com/dendekky" rel="noopener noreferrer"&gt;twitter&lt;/a&gt;. &lt;br&gt;
Once again, you can find the source code &lt;a href="https://github.com/Dendekky/runweb-cli-tool" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;br&gt;
Thanks!&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>cli</category>
      <category>npm</category>
    </item>
  </channel>
</rss>
