<?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: Tuhin Bar</title>
    <description>The latest articles on DEV Community by Tuhin Bar (@tuhinbar).</description>
    <link>https://dev.to/tuhinbar</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%2F2093967%2F6f7f92ed-55ff-4e0c-9996-bb3f913c2db6.jpg</url>
      <title>DEV Community: Tuhin Bar</title>
      <link>https://dev.to/tuhinbar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tuhinbar"/>
    <language>en</language>
    <item>
      <title>Making TypeSafe APIs are easy now!</title>
      <dc:creator>Tuhin Bar</dc:creator>
      <pubDate>Sun, 03 Nov 2024 07:41:16 +0000</pubDate>
      <link>https://dev.to/tuhinbar/making-typesafe-apis-are-easy-now-302b</link>
      <guid>https://dev.to/tuhinbar/making-typesafe-apis-are-easy-now-302b</guid>
      <description>&lt;p&gt;Setting up a TypeScript Express API project from scratch can be time-consuming. From creating the right directory structure to installing essential dependencies, every step requires careful attention to ensure everything is configured correctly. With Snugger CLI, you can generate a fully configured, type-safe Express API project with a single command, allowing you to focus more on building and less on setup.&lt;/p&gt;

&lt;p&gt;This blog will introduce &lt;a href="https://github.com/The-Finding-Labs/snugger" rel="noopener noreferrer"&gt;Snugger CLI&lt;/a&gt;, a tool designed to automate and simplify the setup of a TypeScript-based Express API project. We'll explore the problems developers face when manually setting up such projects and how Snugger CLI addresses them by streamlining the entire process.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;The Problem&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Setting up a TypeScript-based Express API project requires multiple steps, especially if you’re aiming for a fully functional, type-safe environment. Here’s what a typical setup might look like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Initialize TypeScript and NPM:&lt;/strong&gt; Run &lt;code&gt;tsc --init&lt;/code&gt; and &lt;code&gt;npm init -y&lt;/code&gt; to create basic tsconfig.json and package.json files.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Install Core Dependencies&lt;/strong&gt;: Manually install dependencies like express, mongodb, dotenv, cors, and type definitions for each of them, like @types/express, @types/cors, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Directory and File Structure&lt;/strong&gt;: Create the essential folder structure with directories for controllers, routes, and models. Each of these requires additional files to get started with a functional API.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Scripts and Configurations&lt;/strong&gt;: Add development and build scripts to package.json and configure tsconfig.json to be compatible with the project structure.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Manual Adjustments and Typings&lt;/strong&gt;: Ensure all type safety by configuring type declarations for every dependency and checking that everything is compatible.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For new or even experienced developers, this setup can take significant time and is prone to errors. Missing a single configuration step or installing the wrong dependency version can lead to runtime errors and other issues.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;The Solution: Snugger CLI&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Snugger CLI eliminates the tedious setup process and generates a ready-to-use, type-safe Express API project. Here’s what Snugger CLI does to make your development workflow easier:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Automated Project Setup&lt;/strong&gt;: With a single command, Snugger CLI initializes both TypeScript and NPM, so you don’t have to run tsc --init and npm init separately.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Core Dependencies Included&lt;/strong&gt;: It installs all essential packages, including express, mongodb, dotenv, and cors, along with their TypeScript definitions. This saves you the hassle of looking up and installing each one individually.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Pre-Configured Project Structure&lt;/strong&gt;: Snugger CLI generates a complete directory structure for you, with folders for controllers, routes, and models, each populated with starter files. This structure makes it easier to start coding without worrying about setting up files manually.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Type-Safe Configurations&lt;/strong&gt;: The tsconfig.json is optimized for backend development, ensuring compatibility with Express and other dependencies. It also adds dev and build scripts to package.json, making it simple to run or build your project without additional configuration.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Manual Installation Option&lt;/strong&gt;: Snugger CLI allows you to choose whether to install dependencies automatically or manually. This flexibility is ideal for those who might want to add specific packages during the setup.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;With Snugger CLI, all you need to do is specify your project name, and within seconds, you have a fully configured, type-safe Express API project ready to go. This streamlined setup enables you to focus on development rather than configuration, making it a must-have for anyone looking to start a new TypeScript API project.&lt;/p&gt;

&lt;p&gt;Setting up a new TypeScript-based API project no longer needs to be a time-consuming task. With Snugger CLI, you can generate a type-safe Express API project with essential dependencies and a clean directory structure in one go. By automating the repetitive steps, Snugger CLI enables you to save time and jump straight into coding.&lt;/p&gt;

&lt;p&gt;If this tool has helped simplify your project setup, consider giving the &lt;a href="https://github.com/The-Finding-Labs/snugger" rel="noopener noreferrer"&gt;repository&lt;/a&gt; a ⭐️ on GitHub. Your support encourages the development of more tools like this and helps make setup easier for others as well!&lt;/p&gt;

&lt;p&gt;Check out here for installation and usage --&amp;gt; &lt;a href="https://www.npmjs.com/package/snugger" rel="noopener noreferrer"&gt;Snugger&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Github --&amp;gt; &lt;a href="https://github.com/The-Finding-Labs/snugger" rel="noopener noreferrer"&gt;Snugger&lt;/a&gt;&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>cli</category>
      <category>backend</category>
      <category>express</category>
    </item>
    <item>
      <title>My first CLI with Go</title>
      <dc:creator>Tuhin Bar</dc:creator>
      <pubDate>Wed, 25 Sep 2024 13:05:57 +0000</pubDate>
      <link>https://dev.to/tuhinbar/my-first-cli-with-go-4eig</link>
      <guid>https://dev.to/tuhinbar/my-first-cli-with-go-4eig</guid>
      <description>&lt;p&gt;I always felt a bit anxious to write about my experiences. Well, I think I can deal with it now. It was an early morning in August when I decided to write a CLI tool just to "&lt;strong&gt;practice&lt;/strong&gt;" Go as I had learned the basics a few days earlier. I do not watch many tutorials nowadays but I watched &lt;a href="https://youtu.be/yyUHQIec83I?si=nn6i0E-EHefPmxDG" rel="noopener noreferrer"&gt;Nana's Video&lt;/a&gt; on Golang and it was pretty awesome. So, let's jump right in.&lt;/p&gt;

&lt;h3&gt;
  
  
  The reason
&lt;/h3&gt;

&lt;p&gt;I wanted to experiment with Go to learn the &lt;code&gt;os&lt;/code&gt; module. The first thought that came to my mind was to create a &lt;a href="https://turbo.build/repo/docs" rel="noopener noreferrer"&gt;turborepo&lt;/a&gt; with Golang as a server. Yes, I know that we cannot host it in Vercel, but the idea was to create a fast and secure server with Golang. We can use an API proxy in Vercel to rewrite the requests from the client side. However, you will be required to host your Go server separately in some places like &lt;a href="https://railway.app/" rel="noopener noreferrer"&gt;Railway&lt;/a&gt; or &lt;a href="https://render.com/" rel="noopener noreferrer"&gt;Render&lt;/a&gt;. This doesn't follow the rule of a mono repo architecture, it is just maintaining all the code in a single place and building a strong server. It was named &lt;strong&gt;turbo-g&lt;/strong&gt; (turbo + go). &lt;/p&gt;

&lt;p&gt;Well, after creating a &lt;a href="https://github.com/TuhinBar/turbo-g-template" rel="noopener noreferrer"&gt;template project&lt;/a&gt;, I realized that Turbo has some issues with hot reload and many other things. So, eventually, I dropped the idea and moved on to create something else. &lt;/p&gt;

&lt;p&gt;When creating this template project, I saw that I had to set up the server manually and I used &lt;a href="https://gofiber.io/" rel="noopener noreferrer"&gt;gofiber&lt;/a&gt; as it has express-like functionalities, and as a MERN stack developer, I found it very easy to use. Once I thought to make a server-project generator in Express as well, but they already have one, it is called &lt;a href="https://www.npmjs.com/package/express-generator" rel="noopener noreferrer"&gt;express-generator&lt;/a&gt;. Then I thought there might be the same thing for Fiber as well. But when I searched in GitHub with the &lt;code&gt;topic:go-backend-template&lt;/code&gt;, I only found pre-made templates that you can clone and use for your projects. That's when I decided to give it a shot.&lt;/p&gt;

&lt;h3&gt;
  
  
  The process
&lt;/h3&gt;

&lt;p&gt;Making a CLI with go is fairly simple, as go codes are compiled into a binary executable. But, I didn't know how to create files and folders using the &lt;strong&gt;os module&lt;/strong&gt;. In JavaScript(NodeJs), we can use the &lt;code&gt;fs&lt;/code&gt; module to manage files. All thanks to the &lt;a href="https://pkg.go.dev/os" rel="noopener noreferrer"&gt;docs&lt;/a&gt; for helping me out. I already knew what a basic backend project looks like in Express and Fiber as well. My approach was very simple. I will create a CLI tool that will use a few flags like &lt;code&gt;--init&lt;/code&gt; and &lt;code&gt;--name&lt;/code&gt; for the project name and it will generate a project and &lt;code&gt;go.mod&lt;/code&gt; with fiber as a dependency. &lt;/p&gt;

&lt;p&gt;Then I got stuck in the thing called &lt;code&gt;templates&lt;/code&gt;. &lt;strong&gt;Templates are a skeleton of a go file&lt;/strong&gt;. A template holds the content of the file. It was interesting to learn about templates. Then it was easy to take the project name from the flag and create all file folders using &lt;code&gt;os.MkdirAll&lt;/code&gt;and parse the templates with the &lt;code&gt;text/template&lt;/code&gt; module and get the content to create the files. All the details of these modules can be found in the Golang docs I mentioned above. Then it was done! 🎉 But..&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;dirs&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"cmd"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"internal/handlers"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"internal/middleware"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"internal/models"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"internal/routes"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"internal/services"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"config"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dir&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;dirs&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MkdirAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filepath&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;projectPath&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dir&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ModePerm&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Errorf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"❗Failed to create directory %s: %w"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&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;h3&gt;
  
  
  The problems
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;I found out soon that I wasn't using &lt;code&gt;github.com/&amp;lt;username&amp;gt;/&amp;lt;repo&amp;gt;&lt;/code&gt; as the module name which was a problem I encountered when I was learning about the go modules for the first time. Then I learned about the module path from &lt;a href="https://go.dev/ref/mod#module-path" rel="noopener noreferrer"&gt;here&lt;/a&gt;. So, what I did to solve the problem is very simple. I just added a &lt;code&gt;-gh&lt;/code&gt; flag for users to add their GitHub Username to crate the module assuming the project name is the GitHub repo.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Another problem that still exists that is I did not provide any code in the middleware template file and the config template file. Users can use MongoDB or PostgreSQL as a database so it was unnecessary to provide any single or both configs in the template. Well, to fix this I created an &lt;a href="https://github.com/TuhinBar/optical/issues/3" rel="noopener noreferrer"&gt;&lt;strong&gt;issue&lt;/strong&gt;&lt;/a&gt; and anyone reading this blog can contribute to solving this problem. I already figured out a way but this comes down to the third problem.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The CLI can take a user input called &lt;code&gt;-db&lt;/code&gt; to get the user's choice of databases like MongoDB or PostgreSQL to make a dummy connection in the Config template. But, as the code grows to solve these kinds of problems, the CLI will be unusable. It already has 2 required flags and one optional which are &lt;code&gt;--init&lt;/code&gt;, &lt;code&gt;-gh&lt;/code&gt;, and &lt;code&gt;-name&lt;/code&gt;, and if we add a &lt;code&gt;-db&lt;/code&gt; flag it will be a very long command. To solve this problem I am going to modify the CLI as an interactive one like &lt;a href="https://nextjs.org/docs/app/api-reference/cli/create-next-app" rel="noopener noreferrer"&gt;&lt;code&gt;create-next-app&lt;/code&gt;&lt;/a&gt;. After this change, we can add many options for customization. I am already working on this. So, if you want to contribute, you are welcome. You can find the issue &lt;a href="https://github.com/TuhinBar/optical/issues/1" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The last problem for now is a hot reload module. Whenever we create a fiber app we don't have any change listener by default. If fiber has this already and I do not know about it then forgive me for that I am relatively new to this. If you know anything about this, let me know in the comments. To solve this problem I figured out to include &lt;a href="https://github.com/air-verse/air" rel="noopener noreferrer"&gt;&lt;strong&gt;&lt;code&gt;air&lt;/code&gt;&lt;/strong&gt;&lt;/a&gt; in the project.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The end
&lt;/h3&gt;

&lt;p&gt;This is the story of &lt;a href="https://github.com/TuhinBar/optical" rel="noopener noreferrer"&gt;&lt;strong&gt;Optical&lt;/strong&gt;&lt;/a&gt; . However, the blog intends to let you guys know about the little project and also it is an invitation to contribute to the project if you want.&lt;/p&gt;

&lt;p&gt;The journey has just started and it has been very interesting so far. I learned a lot of things and am willing to learn more ahead. This was my first blog here, so if I made any mistake or overlooked something, please ignore that and forgive me. it will be better next time.&lt;/p&gt;

&lt;p&gt;Thanks, everyone for reading the blog, give a ⭐ to &lt;a href="https://github.com/TuhinBar/optical" rel="noopener noreferrer"&gt;&lt;strong&gt;Optical&lt;/strong&gt;&lt;/a&gt; if you find it interesting. Also, share if you know anyone willing to contribute. Also, if you find any issues or have any ideas for improvement, you can create an issue &lt;a href="https://github.com/TuhinBar/optical/issues/new/choose" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks again. &lt;/p&gt;

</description>
      <category>go</category>
      <category>cli</category>
    </item>
  </channel>
</rss>
