<?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: Mopharr</title>
    <description>The latest articles on DEV Community by Mopharr (@mopharr).</description>
    <link>https://dev.to/mopharr</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%2F539309%2F970b6bf5-4130-4393-bff3-fb9606b5d0d2.jpg</url>
      <title>DEV Community: Mopharr</title>
      <link>https://dev.to/mopharr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mopharr"/>
    <language>en</language>
    <item>
      <title>Typescript Could not find declaration file for module 'react'</title>
      <dc:creator>Mopharr</dc:creator>
      <pubDate>Fri, 09 Sep 2022 10:34:31 +0000</pubDate>
      <link>https://dev.to/mopharr/typescript-could-not-find-declaration-file-for-module-react-104b</link>
      <guid>https://dev.to/mopharr/typescript-could-not-find-declaration-file-for-module-react-104b</guid>
      <description>&lt;p&gt;Typescript has made it easy to locate errors, but most time QuickFix doesn't work. The error Could not find a declaration file for module 'Module Name' often happens when you start typescript. This is because typescript couldn't find the type deceleration for a react-related module. Don't panic it is easy to solve. Let's jump on it&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YajleUOE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6y94rrxbrpl3p1pt2y9s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YajleUOE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6y94rrxbrpl3p1pt2y9s.png" alt="Image description" width="880" height="128"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is what the error message looks like in the terminal and these are ways to solve it.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Install the @type package for the module&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Installing the type module with npm
npm install @type/Module-Name

// Installing the type module for react-dom with npm
npm install @type/react-dom

// Installing the type module with npm

npm install @type/react-reveal
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the other hand, the error below might pop up when npm could not find the type of the module you are trying to install. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--baGacOMJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dg3nymdd9y94sqm4u677.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--baGacOMJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dg3nymdd9y94sqm4u677.png" alt="Image description" width="744" height="197"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you fall into this error, that the module you are trying to install doesn't provide typing. So let's discuss another way of fixing this.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Using a .d.ts files&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;What are &lt;strong&gt;.d.ts files&lt;/strong&gt;? These are &lt;strong&gt;declaration files that contain only type information&lt;/strong&gt;. They are only used for checking types and typescript read them the same way it does with the regular .ts file which is mostly determined by the &lt;strong&gt;Include&lt;/strong&gt; and &lt;strong&gt;Exclude&lt;/strong&gt; setting in the &lt;strong&gt;tsconfig.json&lt;/strong&gt; file&lt;/p&gt;

&lt;p&gt;So create a .d.ts file in your project file inside your project file, it can have any name you want like the react-reveal.d.ts file (mostly use the module name), let's continue. Inside the file, you need to declare the module 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;declare module 'module-name'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure you change the module name to the module you are trying to fix the error. In most cases, this fixes the error because, with this, typescript automatically set the module type of &lt;strong&gt;any&lt;/strong&gt; and the code should work fine.&lt;br&gt;
In some cases, we might want to set the type ourselves, if you want to set the type you can just can this to your code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;declare module 'module-name' {
    export function myFunction(): string 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unfortunately, if the error isn't clear, we might have another option to fix it,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Adding the eslint-disable next line comment&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You can easily resolve the error by adding the &lt;strong&gt;eslint-disable next line comment&lt;/strong&gt; to ignore the error on the code and allow your typescript to run perfectly without error&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { myFunction } from 'module-name';

// Example
import { Bounce } from "react-reveal";

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

&lt;/div&gt;



&lt;p&gt;So, with any of these solution &lt;strong&gt;Typescript  Could not find declaration file for module 'react'&lt;/strong&gt; can be fix. &lt;/p&gt;

&lt;p&gt;If you like the article and it would be beneficial, please like and share for friends to they can benefit. &lt;/p&gt;

&lt;p&gt;Thank you &lt;/p&gt;

</description>
      <category>typescript</category>
      <category>react</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Fixing CROSS Origin Resource Sharing (CORS) from the Front end.</title>
      <dc:creator>Mopharr</dc:creator>
      <pubDate>Sat, 18 Dec 2021 02:11:41 +0000</pubDate>
      <link>https://dev.to/mopharr/fixing-cross-origin-resource-sharing-cors-from-the-front-end-5hcb</link>
      <guid>https://dev.to/mopharr/fixing-cross-origin-resource-sharing-cors-from-the-front-end-5hcb</guid>
      <description>&lt;p&gt;Most front-end developer tends to come across CORS issue and they try to fix it with a solution on Stack overflow as I did, some turns out wrong and some didn't explain fully for better understanding. &lt;/p&gt;

&lt;p&gt;In this article, I will talk about CORS and how it can be fixed from the Front End (client-side).&lt;/p&gt;

&lt;h2&gt;
  
  
  Know Your Enemy, What is CORS? 👹👹
&lt;/h2&gt;

&lt;p&gt;The common Cross Origin Resource Sharing (CORS) is a mechanism that allows restricted resources on a web page to be requested from another domain outside the domain from which the first resource was served.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why does it Exist?🤔
&lt;/h2&gt;

&lt;p&gt;To protect us! It’s our friend!💯🐕&lt;/p&gt;

&lt;p&gt;Most of the time a website will require resources hosted in the same place as the website is, for example, making API calls to the same Back end that is serving the website.🏡&lt;/p&gt;

&lt;p&gt;So this policy would be the first layer of protection to avoid other unknown people using our API.⚔️&lt;/p&gt;

&lt;h2&gt;
  
  
  Why do we end up having CORS issues?
&lt;/h2&gt;

&lt;p&gt;Because in some scenarios we need to request resources from one origin to a different one. For example, when our API is hosted in a different domain than our website (any 3rd party API), or when you need to use web fonts.&lt;/p&gt;

&lt;p&gt;CORS doesn’t allow this by default, so an error will appear that can be seen in the browser console.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw057pocs2kcib28vz3oe.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw057pocs2kcib28vz3oe.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is happening between our browser and the destination of our requests?
&lt;/h2&gt;

&lt;p&gt;Web browsers can use special headers to determine whether or not an XMLHttpRequest call should continue or fail.&lt;/p&gt;

&lt;p&gt;Let’s get into more technical detail… 👇👇👇👇👇👇👇&lt;/p&gt;

&lt;h2&gt;
  
  
  OPTIONS request 🧐
&lt;/h2&gt;

&lt;p&gt;Most people know the basic request methods (POST, GET, PUT) but not everyone is familiarized with the OPTIONS method. This request method is used to ask for information from a specific endpoint about what communication options it supports.&lt;/p&gt;

&lt;p&gt;This request is used to determine the exact CORS capabilities of the server, which is in turn used to determine whether or not the intended CORS protocol is understood. If the result of the OPTIONS call dictates that the request cannot be made, the actual request to the server will not be executed.&lt;/p&gt;

&lt;p&gt;The options contained in an &lt;strong&gt;OPTIONS&lt;/strong&gt; request are the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Access-Control-Request-Method&lt;/code&gt;: The intended method of the request (e.g. GET or POST)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Access-Control-Request-Headers&lt;/code&gt;: An indication of the custom headers that will be sent with the request&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Origin&lt;/code&gt;: The usual origin header that contains the script's current origin&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So our client is asking🤔:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;“_I would like to make a GET request with the Content-Type and Accept headers from http://localhost:3000 — is that possible?_”.&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;curl -i -X OPTIONS localhost:3001/api/ping 
'Access-Control-Request-Method: GET'
'Access-Control-Request-Headers: Content-Type, Accept'
'Origin: &amp;lt;http://localhost:3000&amp;gt;'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The server will include some &lt;code&gt;Access-Control-*&lt;/code&gt; headers within the response to indicate whether the request that follows will be allowed or not. These include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Access-Control-Allow-Origin&lt;/code&gt;: The origin that is allowed to make the request, or * if a request can be made from any origin.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Access-Control-Allow-Methods&lt;/code&gt;: A comma-separated list of HTTP methods that are allowed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Access-Control-Allow-Headers&lt;/code&gt;: A comma-separated list of the custom headers that are allowed to be sent.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Access-Control-Max-Age&lt;/code&gt;: The maximum duration that the response to the pref light request can be cached before another call is made.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The response would then be examined by the browser to decide whether to continue with the requester to abandon it.&lt;/p&gt;

&lt;p&gt;So our server is replying🤓:&lt;/p&gt;

&lt;p&gt;“&lt;em&gt;I accept requests from every domain (*) and I allow the following methods GET, HEAD, PUT, PATCH, POST, DELETE”&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HTTP/1.1 204 No Content
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET,HEAD,PUT,PATCH,POST,DELETE
Vary: Access-Control-Request-Headers
Access-Control-Allow-Headers: Content-Type, Accept
Content-Length: 0
Date: Fri, 05 Apr 2019 11:41:08 GMT
Connection: keep-alive
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So if the first request the client is trying to perform meets the requirements it sends the initial request and the process continues normally, and everyone is happy 🎊🎊🎉😁 . If it doesn’t you get a dramatic CORS error which means your initial request wasn’t even sent and you cry 😭.&lt;/p&gt;

&lt;h2&gt;
  
  
  So, how do we fix it? 🔧
&lt;/h2&gt;

&lt;p&gt;If a CORS issue appears during development, because we are using a local development server, this is not an “error”, it is just an inconvenience that we will learn to solve.&lt;/p&gt;

&lt;p&gt;🤔Maybe you are thinking about changing the “origin” header to avoid CORS, forget about it, ‘origin’ is a forbidden header which means you can’t update it.🙄&lt;/p&gt;

&lt;p&gt;🤔The next and most common solution is to ask the server to set the headers to *** (allow all)**.&lt;/p&gt;

&lt;p&gt;💩🤦‍♀️This is not a good practice, the final domain for the Front end should be whitelisted of course, but to simply allow all is a breach of security that shouldn’t be encouraged.🤦‍♂️💩&lt;br&gt;
While developing our domain is usually localhost and toward the outside world that domain is our IP, unless you whitelist it, which can be ineffective if the IP changes, it is never allowed by CORS restrictions unless you allow all domains. 🤷‍♂️🤷‍♀️&lt;/p&gt;

&lt;p&gt;🤓Now the most efficient and least painful way to bypass this restriction for development is to use a proxy. This way we do the request to a proxy server that allows our requests and this one does the request to the original API domain. This request will be performed by a proxy server, not a browser, so it will be able to perform the requests without restrictions. It will also send the original content back to our Front end in the response from the API.👌&lt;/p&gt;

&lt;p&gt;There are already built proxy servers, so you don’t have to create your own, just install one cors-proxy from npm and you are all set.&lt;/p&gt;
&lt;h2&gt;
  
  
  Setting up a CORS proxy 🛠
&lt;/h2&gt;

&lt;p&gt;In my case, I used this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.npmjs.com/package/local-cors-proxy" rel="noopener noreferrer"&gt;https://www.npmjs.com/package/local-cors-proxy&lt;/a&gt; but there are hundreds. You can install it locally in your project or globally on your computer, depending on if you want to add it to your project development dependencies and make it run while you are developing.&lt;/p&gt;

&lt;p&gt;(Usually automating stuff is a good idea, just saying🙄)&lt;/p&gt;

&lt;p&gt;In this case, the steps are easy to follow, but just in case I’m going to review them one by one:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Simple Example&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;API endpoint that we want to request that has CORS issues:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://www.yourdomain.ie/movies/list

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Install dependency by running the following command:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i local-cors-proxy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Start proxy for the specific domain if it’s giving you issues:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;lcp --proxyUrl https://www.yourdomain.ie
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In your client update your API endpoint to point it to your proxy:
From this:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;https://www.yourdomain.ie/movies/list&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;http://localhost:8010/proxy/movies/list&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;CORS Fixed 🎉🎉🎉🎉&lt;/p&gt;

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

&lt;p&gt;As I hope you can see now, there is no magic surrounding CORS🧙‍♂️, it’s something “the backend is supposed to fix”. But if you are working with a Public API, we have the issue and now you can fix CORS yourself. &lt;strong&gt;Thank You&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>node</category>
      <category>react</category>
    </item>
  </channel>
</rss>
