<?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: Hamidreza Mahdavipanah</title>
    <description>The latest articles on DEV Community by Hamidreza Mahdavipanah (@mahdavipanah).</description>
    <link>https://dev.to/mahdavipanah</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F89010%2F542893e5-1414-4e30-a5e3-3da18519dc23.jpg</url>
      <title>DEV Community: Hamidreza Mahdavipanah</title>
      <link>https://dev.to/mahdavipanah</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mahdavipanah"/>
    <language>en</language>
    <item>
      <title>Teaching Agents to Explore</title>
      <dc:creator>Hamidreza Mahdavipanah</dc:creator>
      <pubDate>Tue, 10 Nov 2026 17:53:32 +0000</pubDate>
      <link>https://dev.to/mahdavipanah/teaching-agents-to-explore-4p1b</link>
      <guid>https://dev.to/mahdavipanah/teaching-agents-to-explore-4p1b</guid>
      <description>&lt;h1&gt;
  
  
  Teaching Agents to Explore
&lt;/h1&gt;

&lt;p&gt;When I build with coding agents, the agent writes most of the code and most of the tests. The tests pass. But that green checkmark means less than it used to.&lt;/p&gt;

&lt;p&gt;The same agent wrote both, from one picture of how the feature should work. So the tests mostly check that picture. Nobody asks whether the picture is right.&lt;/p&gt;

&lt;p&gt;So after an agent builds something, I ask it to explore what it built. That is usually when it starts questioning its own assumptions.&lt;/p&gt;

&lt;p&gt;I wanted to know whether this habit really works, so I ran an experiment on two buggy APIs. The short answer: a coding agent can explore well, and a written mission helps most where bugs hide in state and in data you have to follow across the system. Exploring finds bugs that reading and guessing can't, with evidence for every claim.&lt;/p&gt;

&lt;p&gt;I turned what I learned into a skill any coding agent can use. The skill, the experiments and every result are in a &lt;a href="https://github.com/mahdavipanah/exploratory-testing-skill" rel="noopener noreferrer"&gt;public repo&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use the skill
&lt;/h2&gt;

&lt;p&gt;The skill works with any agent that supports the open &lt;a href="https://agentskills.io" rel="noopener noreferrer"&gt;Agent Skills&lt;/a&gt; format, including Claude Code, Codex, Cursor and Gemini CLI.&lt;/p&gt;

&lt;p&gt;In Claude Code, install it with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/plugin install exploratory-testing --marketplace mahdavipanah/exploratory-testing-skill

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

&lt;/div&gt;



&lt;p&gt;For other agents, use the &lt;a href="https://skills.sh" rel="noopener noreferrer"&gt;skills CLI&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx skills add mahdavipanah/exploratory-testing-skill

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

&lt;/div&gt;



&lt;p&gt;Then start your software and ask the agent to explore it:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Explore the orders API you just built. It runs on localhost:3000, and the docs are in docs/api.md.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The &lt;a href="https://github.com/mahdavipanah/exploratory-testing-skill/tree/main/skills/exploratory-testing" rel="noopener noreferrer"&gt;skill&lt;/a&gt; makes the agent work like a tester. It will:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Check that the environment is what it seems.&lt;/li&gt;
&lt;li&gt;Map the system: its entities, its states, and everything it can vary.&lt;/li&gt;
&lt;li&gt;Write down the rules that must never break, like "money is never created or lost", then rank the risks.&lt;/li&gt;
&lt;li&gt;Write three to five short missions, called charters, and use its whole budget on them.&lt;/li&gt;
&lt;li&gt;Follow the data after every change, try every action in every state, and test both sides of every boundary.&lt;/li&gt;
&lt;li&gt;Back every claim with the command and its output.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You get a report that keeps bugs, behaviour that needs your decision, and open questions apart. Every bug comes with the request and the response that prove it.&lt;/p&gt;

&lt;p&gt;For an API, a shell and curl are enough. For a web UI, give the agent a browser it can drive, such as &lt;a href="https://playwright.dev/docs/test-agents" rel="noopener noreferrer"&gt;Playwright&lt;/a&gt;, Google's &lt;a href="https://developer.chrome.com/blog/chrome-devtools-mcp" rel="noopener noreferrer"&gt;Chrome DevTools MCP&lt;/a&gt; or Vercel's &lt;a href="https://github.com/vercel-labs/agent-browser" rel="noopener noreferrer"&gt;agent-browser&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is exploratory testing?
&lt;/h2&gt;

&lt;p&gt;James Bach defined exploratory testing as &lt;a href="https://www.cs.uleth.ca/~benkoczi/3720/pres/exploratory_testing_explainedBach03.pdf" rel="noopener noreferrer"&gt;simultaneous learning, test design, and test execution&lt;/a&gt;. You try something, look at what happened, and let that decide what you try next.&lt;/p&gt;

&lt;p&gt;Elisabeth Hendrickson's book &lt;em&gt;Explore It!&lt;/em&gt; explains why you need both tests and exploring. Tests check that the software does what we intended. Exploring asks a different question: what other risks are there? An agent's own tests only ask the first one.&lt;/p&gt;

&lt;p&gt;Exploring isn't random clicking. Hendrickson gives each session a charter, with a &lt;a href="https://www.oreilly.com/library/view/explore-it/9781941222584/f_0019.html" rel="noopener noreferrer"&gt;simple template&lt;/a&gt;: &lt;em&gt;Explore (target) with (resources) to discover (information)&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Jonathan and James Bach's &lt;a href="https://www.ida.liu.se/~TDDD04/labs/2019/exploratory_testing/stqe-sbtm.pdf" rel="noopener noreferrer"&gt;session-based test management&lt;/a&gt; puts each charter in a time box, with notes. James Whittaker's &lt;a href="https://www.informit.com/articles/article.aspx?p=1430493&amp;amp;seqNum=2" rel="noopener noreferrer"&gt;tours&lt;/a&gt; add themes, such as following one piece of data through every path. Together they turn curiosity into a method you can hand to an agent.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why agents need a method
&lt;/h2&gt;

&lt;p&gt;Agents are good at the mechanics. Simon Willison &lt;a href="https://simonwillison.net/guides/agentic-engineering-patterns/agentic-manual-testing/" rel="noopener noreferrer"&gt;points out&lt;/a&gt; that a coding agent's defining ability is running the code it writes.&lt;/p&gt;

&lt;p&gt;Agents also fix exploratory testing's oldest &lt;a href="https://en.wikipedia.org/wiki/Exploratory_testing" rel="noopener noreferrer"&gt;weakness&lt;/a&gt;: it is hard to show what was tested, and hard to repeat it. An agent logs every command and every response.&lt;/p&gt;

&lt;p&gt;But left alone, agents explore badly. Maaret Pyhäjärvi &lt;a href="https://dev.to/maaretp/exploratory-testing-reimagined-2e43"&gt;tested&lt;/a&gt; this on a to-do app with 73 known problems. An unguided AI prompt found 5% of them, autonomous Playwright agents 16%, and a skilled human tester 62%.&lt;/p&gt;

&lt;p&gt;The researchers behind &lt;a href="https://arxiv.org/abs/2601.04500" rel="noopener noreferrer"&gt;GUITester&lt;/a&gt; name one reason: agents care more about finishing the task than about reporting what looks wrong. Hendrickson tells of a job candidate who listed a few test ideas and declared he was done. He didn't get the job. One of my agents did the same thing.&lt;/p&gt;

&lt;h2&gt;
  
  
  The experiment
&lt;/h2&gt;

&lt;p&gt;I gave Claude Opus 5.5, running in Claude Code, two practice APIs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://github.com/mwinteringham/restful-booker" rel="noopener noreferrer"&gt;Restful-booker&lt;/a&gt;, Mark Winteringham's deliberately buggy hotel-booking API. It is famous, so the model may remember its bugs.&lt;/li&gt;
&lt;li&gt;claimdesk, a small expense-claims API no model has seen. A separate agent built it and planted 15 bugs, and their list stayed sealed until the scoring was done.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every agent got the API docs, curl, 80 requests, and one rule: report only what you saw and reproduced. Only the mission changed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A&lt;/strong&gt; got one line: "Find bugs in this API."&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;B&lt;/strong&gt; got four charters I wrote.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;C&lt;/strong&gt; wrote its own charters.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;D&lt;/strong&gt; got the skill.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;M&lt;/strong&gt; got only the docs, no API, and had to predict the bugs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each one ran five times, in separate sessions, in random order. On claimdesk, a separate agent matched every finding to the bug list without knowing which agent wrote it. A second scorer agreed with 96 of 100 of its decisions.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Agent&lt;/th&gt;
&lt;th&gt;Restful-booker: known problems found&lt;/th&gt;
&lt;th&gt;claimdesk: planted bugs found&lt;/th&gt;
&lt;th&gt;Wrong findings, both APIs&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;A, no mission&lt;/td&gt;
&lt;td&gt;35%&lt;/td&gt;
&lt;td&gt;68%&lt;/td&gt;
&lt;td&gt;0 of 113&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;B, charters written for it&lt;/td&gt;
&lt;td&gt;46%&lt;/td&gt;
&lt;td&gt;71%&lt;/td&gt;
&lt;td&gt;0 of 135&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;C, its own charters&lt;/td&gt;
&lt;td&gt;39%&lt;/td&gt;
&lt;td&gt;63%&lt;/td&gt;
&lt;td&gt;0 of 105&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;D, the skill&lt;/td&gt;
&lt;td&gt;48%&lt;/td&gt;
&lt;td&gt;72%&lt;/td&gt;
&lt;td&gt;1 of 133&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;M, docs only, predicting&lt;/td&gt;
&lt;td&gt;50%&lt;/td&gt;
&lt;td&gt;71%&lt;/td&gt;
&lt;td&gt;219 of 380&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;On Restful-booker, a mission helped: B and D found clearly more than A. On claimdesk, all four exploring agents found about two-thirds of the bugs. That API is small and its docs state every rule, so even "find bugs" covered most of it.&lt;/p&gt;

&lt;p&gt;Writing its own charters worked most of the time. But one run of C wrote five good charters, used only 37 of its 80 requests, and stopped. That's Hendrickson's job candidate.&lt;/p&gt;

&lt;p&gt;The surprise was M. Without touching the API, it found as many known bugs as the explorers. On Restful-booker it remembered them. On claimdesk, the bugs sat exactly where the docs made promises.&lt;/p&gt;

&lt;p&gt;But M got there by guessing. On claimdesk, three out of four of its guesses were wrong. The exploring agents reported one wrong finding in 486.&lt;/p&gt;

&lt;h2&gt;
  
  
  What only exploring finds
&lt;/h2&gt;

&lt;p&gt;On both APIs, some bugs were found only by exploring. No prediction named any of these:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Restful-booker answers one documented response format with status 200 and an error message as the body.&lt;/li&gt;
&lt;li&gt;It labels its XML responses as HTML.&lt;/li&gt;
&lt;li&gt;It turns 30 February into 2 March without a word.&lt;/li&gt;
&lt;li&gt;One update that sets the dates to null breaks a booking for good: every later read returns status 200 with a JavaScript error as the body.&lt;/li&gt;
&lt;li&gt;A date sent with a timezone offset is saved as a different day.&lt;/li&gt;
&lt;li&gt;claimdesk's default page size is 10, but its docs say 20. Every exploring run caught it; no prediction did.&lt;/li&gt;
&lt;li&gt;A rejected claim can't be resubmitted, although the docs say it can.&lt;/li&gt;
&lt;li&gt;Ordinary amounts lose a cent: 19.99 is saved as 19.98.&lt;/li&gt;
&lt;li&gt;An unknown sort value crashes the server.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can't find these by reading. They live in what the system actually returns: its defaults, its arithmetic, its headers, and the state that earlier requests leave behind. Hendrickson's advice applies to agents too: when the map and the territory disagree, trust the territory.&lt;/p&gt;

&lt;p&gt;Reading the code doesn't replace exploring either. I had Claude review Restful-booker's source first, and it found 30 problems. The exploring agents then found 9 more, one of them on a line the review had read.&lt;/p&gt;

&lt;p&gt;Two problems from the review were never found by exploring. Reading and exploring find different bugs, so use both.&lt;/p&gt;

&lt;h2&gt;
  
  
  What a mission adds
&lt;/h2&gt;

&lt;p&gt;A mission helped most with bugs hidden in state and in data you have to follow. In claimdesk, removing an item from a claim updated the claim itself, but not its total in the list or the amount finance paid later.&lt;/p&gt;

&lt;p&gt;B's charter said to follow one claim through every view, and B found this bug in all five runs. The skill says to follow the data after every change, and D found it in four. A, with no mission, never found it.&lt;/p&gt;

&lt;p&gt;A mission can also hide things. B's charters pointed it at views and lifecycles, so B found a bug that let a user remove an item from someone else's claim in only one run of five. A and D found it every time.&lt;/p&gt;

&lt;p&gt;The misses followed patterns, and each one became a rule in the skill:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Agents test one path and generalise.&lt;/strong&gt; On Restful-booker, they saw an id ignored when creating a booking and assumed it was ignored everywhere. In an update, it silently moves the booking to another id. Only D, told to send fields like ids on every write path, found that.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;They check one side of a boundary.&lt;/strong&gt; One agent saw a date included at one edge and called an inverted filter correct.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;They trust the docs.&lt;/strong&gt; When the docs and the API agreed on a wrong status code, most agents filed it as a question.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;They stop early.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What stays human
&lt;/h2&gt;

&lt;p&gt;Two jobs stay with you. The first is deciding what counts as a bug. Agents lean on the docs and park real problems as questions. Someone has to decide whether a 200 should be a 201, or whether a case-sensitive search is intended.&lt;/p&gt;

&lt;p&gt;The second is the sandbox. An exploring agent reads whatever the system sends back, and a page or a response can carry instructions aimed at it. Anthropic calls &lt;a href="https://anthropic.com/research/prompt-injection-defenses" rel="noopener noreferrer"&gt;prompt injection&lt;/a&gt; one of the biggest security challenges for browser agents.&lt;/p&gt;

&lt;p&gt;Within those limits, the split works well. The agent brings stamina, evidence and repeatability. You bring judgement about what matters. Exploring stops being a phase someone schedules and becomes the step after every change.&lt;/p&gt;

&lt;p&gt;Before you trust the green checkmarks, ask your agent to explore.&lt;/p&gt;

</description>
      <category>testing</category>
      <category>softwaredevelopment</category>
      <category>ai</category>
      <category>agenticai</category>
    </item>
    <item>
      <title>Agentic Engineering Manifesto — Practical Principles for AI-Driven Development</title>
      <dc:creator>Hamidreza Mahdavipanah</dc:creator>
      <pubDate>Sat, 23 May 2026 19:05:31 +0000</pubDate>
      <link>https://dev.to/mahdavipanah/agentic-engineering-manifesto-pf</link>
      <guid>https://dev.to/mahdavipanah/agentic-engineering-manifesto-pf</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;The full manifesto is available here: &lt;a href="https://mahdavipanah.github.io/agentic-engineering-manifesto/" rel="noopener noreferrer"&gt;Agentic Engineering Manifesto&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;AI agents are changing how we build software, but I do not think the important change is only that code gets written faster.&lt;/p&gt;

&lt;p&gt;The bigger change is where engineering judgment moves.&lt;/p&gt;

&lt;p&gt;When working with agents, the human role becomes less about typing every line of code and more about shaping the problem, giving useful context, choosing the direction, reviewing the approach, and making sure there is a good feedback loop.&lt;/p&gt;

&lt;p&gt;That is why I wrote the Agentic Engineering Manifesto.&lt;/p&gt;

&lt;p&gt;The main idea is simple: agents are useful, but they need to be guided in the right way. A good prompt is not just a task description. It should tell the agent where to look, what matters, how to think about the change, and how to verify the result.&lt;/p&gt;

&lt;p&gt;I also think codebases now need to be easier for agents to navigate. Names, structure, examples, tests, and discoverable patterns matter more when another participant in the development process is searching and reasoning through the codebase.&lt;/p&gt;

&lt;p&gt;Another important shift is refactoring. Since agents can make broad changes more cheaply, we should be more willing to ask whether a feature is hard because the feature is actually hard, or because the current architecture is resisting it.&lt;/p&gt;

&lt;p&gt;The manifesto is my attempt to write down these ideas in a small and clear form.&lt;/p&gt;

&lt;p&gt;It is not about trusting AI blindly. It is almost the opposite. Agentic engineering needs planning, verification, tests, build output, logs, and review. The better the feedback loop, the more useful the agent becomes.&lt;/p&gt;

&lt;p&gt;I see this as an early version of how software teams will work with agents: humans leading design and direction, agents taking more ownership during implementation, and both depending on codebases that are easy to understand, change, and verify.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>agents</category>
      <category>programming</category>
      <category>software</category>
    </item>
    <item>
      <title>A Practical Specification for Roles, Permissions, and Policy Decisions</title>
      <dc:creator>Hamidreza Mahdavipanah</dc:creator>
      <pubDate>Sun, 17 May 2026 12:33:19 +0000</pubDate>
      <link>https://dev.to/mahdavipanah/a-practical-specification-for-roles-permissions-and-policy-decisions-2g6n</link>
      <guid>https://dev.to/mahdavipanah/a-practical-specification-for-roles-permissions-and-policy-decisions-2g6n</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;The full specification is available here: &lt;a href="https://mahdavipanah.github.io/authorization-model/" rel="noopener noreferrer"&gt;Authorization Model Specification&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Authorization logic often starts simple.&lt;/p&gt;

&lt;p&gt;At first, a role like &lt;code&gt;admin&lt;/code&gt;, &lt;code&gt;editor&lt;/code&gt;, or &lt;code&gt;viewer&lt;/code&gt; is enough. Then the product grows. You add organizations, projects, machine clients, service accounts, field-level rules, exceptions, audit logs, and suddenly the question "can this principal do this action?" is answered differently in different parts of the codebase.&lt;/p&gt;

&lt;p&gt;This specification is my attempt to make that question explicit, portable, and easy to evaluate.&lt;/p&gt;

&lt;p&gt;The goal is not to define authentication, token issuance, identity provisioning, or transport security. Those are separate concerns. This spec focuses only on authorization decisions: how roles, permissions, scopes, and policy evaluation should work together.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Core Idea
&lt;/h2&gt;

&lt;p&gt;The model is built around a small chain:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Principal -&amp;gt; Role -&amp;gt; Permission -&amp;gt; allow | deny
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A principal can be a human user, a service account, or a machine-to-machine client. A role is a named bundle of permission statements, such as &lt;code&gt;auditor&lt;/code&gt;, &lt;code&gt;editor&lt;/code&gt;, or &lt;code&gt;billing-admin&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Roles are not magical. Their names do not grant authority by themselves. A role only matters because of the permission statements it contains, and a principal only receives authority through an explicit binding:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(principal, role, scope)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is important because it keeps the model auditable. If someone has access, there should be a concrete binding and a concrete permission statement that explains why.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Design Principles
&lt;/h2&gt;

&lt;p&gt;The specification is based on five principles:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Default-deny&lt;/strong&gt;: if nothing explicitly allows an action, the answer is deny.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Deny-overrides&lt;/strong&gt;: if both an allow and a deny match the same request, the deny wins.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explicit over implicit&lt;/strong&gt;: authority is never inferred from role names, group names, or conventions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Decision and enforcement separation&lt;/strong&gt;: application code should ask a Policy Decision Point (PDP) for a decision. It should not spread authorization rules across controllers, services, and handlers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Auditability&lt;/strong&gt;: decisions should be loggable with enough context to reconstruct what happened.&lt;/p&gt;

&lt;p&gt;These principles make the model predictable. There is no hidden privilege, no role-name guessing, and no ambiguous conflict resolution.&lt;/p&gt;




&lt;h2&gt;
  
  
  Permission Strings
&lt;/h2&gt;

&lt;p&gt;Permission statements are serialized as compact strings:&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;organization&amp;gt;:&amp;lt;service&amp;gt;/&amp;lt;resource&amp;gt;[:&amp;lt;field&amp;gt;[:&amp;lt;resource_id&amp;gt;]]/&amp;lt;effect&amp;gt;/&amp;lt;action&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;acme:api/suppliers/allow/update
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means: in the &lt;code&gt;acme&lt;/code&gt; organization, for the &lt;code&gt;api&lt;/code&gt; service, allow updating &lt;code&gt;suppliers&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The format also supports field-level and instance-level rules:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;acme:api/contacts:email/allow/read
acme:api/suppliers:*:12345/deny/read
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first statement allows reading only the &lt;code&gt;email&lt;/code&gt; field of contacts. The second denies reading supplier &lt;code&gt;12345&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Wildcards are supported too:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;acme:api/suppliers/allow/*
acme:api/suppliers/deny/delete
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Together, these allow every action on suppliers except deletion.&lt;/p&gt;

&lt;p&gt;The string format is intentionally compact enough to be transported in places like JWT claims, while still being readable for operators and auditors.&lt;/p&gt;




&lt;h2&gt;
  
  
  How Evaluation Works
&lt;/h2&gt;

&lt;p&gt;The evaluator receives a request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(principal, action, resource_uri)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then it evaluates the principal's effective permission set in three steps.&lt;/p&gt;

&lt;p&gt;First, it keeps only the permission statements that match the request. A segment matches if it is exactly equal to the request segment or if it is &lt;code&gt;*&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Second, if any matching statement has &lt;code&gt;effect = deny&lt;/code&gt;, the result is deny.&lt;/p&gt;

&lt;p&gt;Third, if at least one matching statement has &lt;code&gt;effect = allow&lt;/code&gt; and no deny matched, the result is allow. Otherwise, the result is deny.&lt;/p&gt;

&lt;p&gt;In pseudocode:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;evaluate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;permissions&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;applicable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;permissions&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;matches&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;any&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;effect&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;deny&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;applicable&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;Decision&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DENY&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;any&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;effect&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;allow&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;applicable&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;Decision&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ALLOW&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Decision&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DENY&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One important detail: the evaluator is specificity-agnostic. A specific deny does not beat a wildcard allow because it is more specific. It wins because all denies override all allows.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;acme:api/suppliers/allow/read
acme:api/suppliers:*:12345/deny/read
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Reading suppliers is generally allowed, but reading supplier &lt;code&gt;12345&lt;/code&gt; is denied.&lt;/p&gt;




&lt;h2&gt;
  
  
  What This Spec Is For
&lt;/h2&gt;

&lt;p&gt;This specification is useful for teams building a Policy Decision Point, integrating a Policy Enforcement Point, or simply trying to make authorization rules easier to audit.&lt;/p&gt;

&lt;p&gt;It gives you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;a portable permission string format&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;clear role and binding semantics&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;default-deny behavior&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;deny-overrides conflict handling&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;a small evaluation algorithm&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;decision logging requirements&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The main benefit is consistency. Instead of each service inventing its own interpretation of roles and permissions, services can ask the same kind of question and receive the same kind of answer.&lt;/p&gt;

&lt;p&gt;Authorization should be boring, predictable, and explainable. That is what this spec is designed to support.&lt;/p&gt;

&lt;p&gt;You can read the complete spec, including the grammar, validation rules, examples, and versioning notes, at &lt;a href="https://mahdavipanah.github.io/authorization-model/" rel="noopener noreferrer"&gt;mahdavipanah.github.io/authorization-model&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>auth</category>
      <category>rbac</category>
      <category>permission</category>
      <category>authentication</category>
    </item>
    <item>
      <title>The Book “Atomic Habits” - My Insights and Key Takeaways</title>
      <dc:creator>Hamidreza Mahdavipanah</dc:creator>
      <pubDate>Thu, 20 Nov 2025 11:20:19 +0000</pubDate>
      <link>https://dev.to/mahdavipanah/the-book-atomic-habits-my-insights-and-key-takeaways-5f8b</link>
      <guid>https://dev.to/mahdavipanah/the-book-atomic-habits-my-insights-and-key-takeaways-5f8b</guid>
      <description>&lt;h2&gt;
  
  
  The Power of Small, Consistent Steps
&lt;/h2&gt;

&lt;p&gt;If there’s one lesson that hit me the hardest from James Clear, it’s this:&lt;br&gt;
Big changes don’t come from big actions. They come from tiny ones done consistently.&lt;/p&gt;

&lt;p&gt;People obsess over massive goals, perfect plans, and sudden motivation. But in reality, life rewards the person who quietly improves a little bit every single day. One percent. That’s it. Repeat long enough, and the compound effect takes over.&lt;/p&gt;

&lt;p&gt;Your current reality — your fitness, skills, career, relationships — is basically the accumulated result of your daily habits. When the inputs are solid, the outputs eventually align.&lt;/p&gt;

&lt;p&gt;And the real question isn’t:&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;/p&gt;




&lt;p&gt;Read the complete post on my Substack: &lt;a href="https://hamidrezam.substack.com/p/atomic-habits" rel="noopener noreferrer"&gt;https://hamidrezam.substack.com/p/atomic-habits&lt;/a&gt;&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>personaldevelopment</category>
      <category>motivation</category>
      <category>lifestyle</category>
    </item>
    <item>
      <title>Javascript's Tricky "This" Keyword and Why Arrow Functions Matter</title>
      <dc:creator>Hamidreza Mahdavipanah</dc:creator>
      <pubDate>Tue, 18 Mar 2025 14:28:41 +0000</pubDate>
      <link>https://dev.to/mahdavipanah/javascripts-tricky-this-keyword-and-why-arrow-functions-matter-5fmh</link>
      <guid>https://dev.to/mahdavipanah/javascripts-tricky-this-keyword-and-why-arrow-functions-matter-5fmh</guid>
      <description>&lt;p&gt;JavaScript's flexibility and dynamic nature make it a powerful language, but certain features—like the &lt;code&gt;this&lt;/code&gt; keyword—can lead to confusion, especially within nested functions. Understanding how &lt;code&gt;this&lt;/code&gt; behaves is crucial for writing predictable and maintainable code.&lt;/p&gt;

&lt;h3&gt;
  
  
  The &lt;code&gt;this&lt;/code&gt; Keyword in JavaScript
&lt;/h3&gt;

&lt;p&gt;In JavaScript, the value of &lt;code&gt;this&lt;/code&gt; is determined by how a function is invoked, not where it's defined. When a regular function is called as a method of an object (&lt;code&gt;obj.method()&lt;/code&gt;), &lt;code&gt;this&lt;/code&gt; refers to that object. However, when a function is invoked standalone (&lt;code&gt;func()&lt;/code&gt;), &lt;code&gt;this&lt;/code&gt; typically refers to the global object (in non-strict mode) or is &lt;code&gt;undefined&lt;/code&gt; (in strict mode) (&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this" rel="noopener noreferrer"&gt;MDN documentation&lt;/a&gt;).&lt;/p&gt;

&lt;h3&gt;
  
  
  Common Pitfall: Losing the Context
&lt;/h3&gt;

&lt;p&gt;Consider the following example:&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;class&lt;/span&gt; &lt;span class="nc"&gt;MyClass&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;factor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;methodA&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;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;factor&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;methodB&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="mi"&gt;1&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="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&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="nf"&gt;function &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;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;methodA&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newValue&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// TypeError: Cannot read properties of undefined (reading 'methodA')&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;p&gt;In this example, the callback passed to &lt;code&gt;map()&lt;/code&gt; is a regular function, causing &lt;code&gt;this&lt;/code&gt; to be &lt;code&gt;undefined&lt;/code&gt; inside it. This results in a runtime error.&lt;/p&gt;

&lt;h3&gt;
  
  
  Arrow Functions: A Concise Solution
&lt;/h3&gt;

&lt;p&gt;Arrow functions provide a concise syntax and lexically bind &lt;code&gt;this&lt;/code&gt;, meaning they inherit &lt;code&gt;this&lt;/code&gt; from their surrounding scope (&lt;a href="https://google.github.io/styleguide/jsguide.html#features-functions-arrow-functions" rel="noopener noreferrer"&gt;Google JS Style Guide&lt;/a&gt;):&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;class&lt;/span&gt; &lt;span class="nc"&gt;MyClass&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;factor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;methodA&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;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;factor&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;methodB&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="mi"&gt;1&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="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&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;value&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;methodA&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newValue&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;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With arrow functions, the context of &lt;code&gt;this&lt;/code&gt; remains bound to the instance of &lt;code&gt;MyClass&lt;/code&gt;, preventing the earlier error.&lt;/p&gt;

&lt;h3&gt;
  
  
  Best Practices
&lt;/h3&gt;

&lt;p&gt;To avoid confusion and potential errors with &lt;code&gt;this&lt;/code&gt;, consider these guidelines:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Prefer Arrow Functions for Nested Functions:&lt;/strong&gt; Arrow functions simplify the scoping of &lt;code&gt;this&lt;/code&gt; in nested functions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Avoid Function Expressions for Callbacks:&lt;/strong&gt; Regular functions can lead to unexpected bindings. Use arrow functions for callbacks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Explicit Binding with&lt;/strong&gt; &lt;code&gt;bind&lt;/code&gt;: If using regular functions, explicitly bind &lt;code&gt;this&lt;/code&gt; using &lt;code&gt;.bind(this)&lt;/code&gt; (&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind" rel="noopener noreferrer"&gt;MDN bind&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By understanding the behavior of &lt;code&gt;this&lt;/code&gt; and utilizing arrow functions appropriately, you can write more predictable and maintainable JavaScript code.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>typescript</category>
      <category>programmingtips</category>
      <category>node</category>
    </item>
    <item>
      <title>Introducing keyv-upstash: Seamless Key-Value Storage for Serverless Redis</title>
      <dc:creator>Hamidreza Mahdavipanah</dc:creator>
      <pubDate>Tue, 10 Dec 2024 17:27:11 +0000</pubDate>
      <link>https://dev.to/mahdavipanah/introducing-keyv-upstash-seamless-key-value-storage-for-serverless-redis-3f9d</link>
      <guid>https://dev.to/mahdavipanah/introducing-keyv-upstash-seamless-key-value-storage-for-serverless-redis-3f9d</guid>
      <description>&lt;p&gt;Github: &lt;a href="https://github.com/mahdavipanah/keyv-upstash" rel="noopener noreferrer"&gt;https://github.com/mahdavipanah/keyv-upstash&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;keyv-upstash&lt;/code&gt; is a storage adapter for &lt;a href="https://github.com/jaredwray/keyv" rel="noopener noreferrer"&gt;Keyv&lt;/a&gt; that connects it to &lt;a href="https://upstash.com/" rel="noopener noreferrer"&gt;Upstash Redis&lt;/a&gt;, a serverless Redis platform. With this adapter, you get a simple, efficient, and flexible solution for key-value storage in serverless applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Keyv?
&lt;/h2&gt;

&lt;p&gt;Keyv is a versatile key-value storage library that supports multiple backends through adapters. It provides:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;TTL-based Expiry&lt;/strong&gt;: Ideal for caching or persistent storage.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Namespace Support&lt;/strong&gt;: Avoids key collisions in shared environments.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Extensibility&lt;/strong&gt;: Easy to build custom modules or add features like compression.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Keyv works with many adapters, such as Redis, SQLite, MongoDB, and now, &lt;code&gt;keyv-upstash&lt;/code&gt; for Upstash Redis.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why &lt;code&gt;keyv-upstash&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;keyv-upstash&lt;/code&gt; extends Keyv's capabilities by integrating it with Upstash Redis, offering:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Serverless Compatibility&lt;/strong&gt;: Upstash Redis works without managing connections, scaling automatically, perfect for serverless apps.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Flexible&lt;/strong&gt;: Compatible with Keyv’s ecosystem and supports third-party extensions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Cache Layering&lt;/strong&gt;: Combine with &lt;a href="https://www.npmjs.com/package/cacheable" rel="noopener noreferrer"&gt;Cacheable&lt;/a&gt; for multi-layered caching.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;No Vendor Lock-In:&lt;/strong&gt; Is fully compatible with &lt;a href="https://github.com/hiett/serverless-redis-http" rel="noopener noreferrer"&gt;serverless-redis-http&lt;/a&gt; so you can setup your own serverless Redis and use this adapter with it.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;Follow these steps to integrate &lt;code&gt;keyv-upstash&lt;/code&gt;:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Install Keyv and &lt;code&gt;keyv-upstash&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Install Keyv and the Upstash adapter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;keyv keyv-upstash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Optional: Install Cacheable for layered caching:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;cacheable
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  2. Set Up &lt;code&gt;keyv-upstash&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Make sure you have a Redis database created in &lt;a href="https://upstash.com/" rel="noopener noreferrer"&gt;Upstash&lt;/a&gt;. Here’s how to use &lt;code&gt;keyv-upstash&lt;/code&gt; in your project:&lt;/p&gt;

&lt;h4&gt;
  
  
  Basic Usage
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Keyv&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;keyv&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;KeyvUpstash&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;keyv-upstash&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;keyv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Keyv&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;store&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;KeyvUpstash&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;your-upstash-redis-url&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;token&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;your-upstash-redis-token&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="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// Set a key-value pair&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;keyv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bar&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Retrieve the value&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;keyv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&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="c1"&gt;// 'bar'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Using Namespaces
&lt;/h4&gt;

&lt;p&gt;Namespaces prevent key collisions and allow scoped clearing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;keyv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Keyv&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;store&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;KeyvUpstash&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;your-upstash-redis-url&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;token&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;your-upstash-redis-token&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;namespace&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my-namespace&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="p"&gt;});&lt;/span&gt;

&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;keyv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bar&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Stored as 'my-namespace::foo'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Cache Layering with Cacheable
&lt;/h4&gt;

&lt;p&gt;Combine &lt;code&gt;keyv-upstash&lt;/code&gt; with Cacheable for multi-layer caching:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Cacheable&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cacheable&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;redisStore&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;KeyvUpstash&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;your-upstash-redis-url&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;token&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;your-upstash-redis-token&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Cacheable&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;primary&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="c1"&gt;// Fast in-memory caching&lt;/span&gt;
  &lt;span class="na"&gt;secondary&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;redisStore&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// Persistent Redis caching&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bar&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="na"&gt;ttl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt; &lt;span class="c1"&gt;// Stores in both layers&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Fast lookup from memory or Redis&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&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="c1"&gt;// 'bar'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Advanced Features
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Batch Operations
&lt;/h3&gt;

&lt;p&gt;Improve performance with &lt;code&gt;setMany&lt;/code&gt; and &lt;code&gt;getMany&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;keyv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setMany&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;key1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;value1&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="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;key2&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;value2&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;values&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;keyv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getMany&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;key1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;key2&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;values&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ['value1', 'value2']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Custom Configuration
&lt;/h3&gt;

&lt;p&gt;Customize your setup with options like &lt;code&gt;defaultTtl&lt;/code&gt;, &lt;code&gt;keyPrefixSeparator&lt;/code&gt;, and &lt;code&gt;clearBatchSize&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>redis</category>
      <category>database</category>
      <category>serverless</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Fast and Simple NestJS App Deployment on Vercel</title>
      <dc:creator>Hamidreza Mahdavipanah</dc:creator>
      <pubDate>Tue, 26 Nov 2024 19:10:38 +0000</pubDate>
      <link>https://dev.to/mahdavipanah/fast-and-simple-nestjs-app-deployment-on-vercel-1lo3</link>
      <guid>https://dev.to/mahdavipanah/fast-and-simple-nestjs-app-deployment-on-vercel-1lo3</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;This article is also available on my blog: &lt;a href="https://hamidreza.tech/nestjs-on-vercel" rel="noopener noreferrer"&gt;https://hamidreza.tech/nestjs-on-vercel&lt;/a&gt;&lt;/strong&gt;. &lt;/p&gt;
&lt;/blockquote&gt;



&lt;blockquote&gt;
&lt;p&gt;This guide is beneficial if you're using &lt;a href="https://expressjs.com/" rel="noopener noreferrer"&gt;Express&lt;/a&gt; adapter. For NestJS applications utilizing the &lt;a href="https://fastify.dev/" rel="noopener noreferrer"&gt;Fastify&lt;/a&gt; adapter, these links may be helpful:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://fastify.dev/docs/latest/Guides/Serverless/#vercel" rel="noopener noreferrer"&gt;https://fastify.dev/docs/latest/Guides/Serverless/#vercel&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/vercel/examples/tree/main/starter/fastify" rel="noopener noreferrer"&gt;https://github.com/vercel/examples/tree/main/starter/fastify&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;🚀 You can access the complete source code discussed in this article at this GitHub repository: &lt;a href="https://github.com/mahdavipanah/nestjs-on-vercel" rel="noopener noreferrer"&gt;https://github.com/mahdavipanah/nestjs-on-vercel&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Vercel's Support for Express Apps
&lt;/h2&gt;

&lt;p&gt;Vercel offers a convenient feature for deploying your Express app by:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Exposing the Express app object in an API.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Defining a rewrite rule that directs all incoming traffic to this single API.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I followed &lt;a href="https://vercel.com/guides/using-express-with-vercel" rel="noopener noreferrer"&gt;Vercel’s official guide for deploying Express&lt;/a&gt; to deploy NestJS by similarly exposing NestJS’s underlying Express app object.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1 - create a NestJS app
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Skip this step if you already have a NestJS app set up.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://docs.nestjs.com/first-steps" rel="noopener noreferrer"&gt;Install NestJS&lt;/a&gt; and create a new app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nest new my-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Step 2 - Install Necessary NPM Packages&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;express @nestjs/platform-express
npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-D&lt;/span&gt; @types/express
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3 - Create &lt;code&gt;src/AppFactory.ts&lt;/code&gt; file
&lt;/h2&gt;

&lt;p&gt;This file serves as a single module that manages all necessary NestJS app bootstrapping and exports both the NestJS app and its underlying Express app object.&lt;/p&gt;

&lt;p&gt;Create a file named &lt;code&gt;AppFactory.ts&lt;/code&gt; inside the &lt;code&gt;src&lt;/code&gt; directory in your project’s root:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ExpressAdapter&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/platform-express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;NestFactory&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/core&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Express&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;INestApplication&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AppModule&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./app.module.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppFactory&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;appPromise&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;INestApplication&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;expressApp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Express&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;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;expressApp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;adapter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ExpressAdapter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;expressApp&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;appPromise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;NestFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;AppModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;adapter&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nx"&gt;appPromise&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;app&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="c1"&gt;// You can add all required app configurations here&lt;/span&gt;

        &lt;span class="cm"&gt;/**
         * Enable cross-origin resource sharing (CORS) to allow resources to be requested from another domain.
         * @see {@link https://docs.nestjs.com/security/cors}
         */&lt;/span&gt;
        &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;enableCors&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
          &lt;span class="na"&gt;exposedHeaders&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;*&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;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;init&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;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;err&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="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="c1"&gt;// IMPORTANT This express application-level middleware makes sure the NestJS app is fully initialized&lt;/span&gt;
    &lt;span class="nx"&gt;expressApp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="na"&gt;req&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;res&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&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;appPromise&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&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="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
          &lt;span class="nf"&gt;next&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;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&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="nx"&gt;appPromise&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;expressApp&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;h2&gt;
  
  
  &lt;strong&gt;Step 4 - Modify&lt;/strong&gt; &lt;code&gt;src/main.ts&lt;/code&gt; File
&lt;/h2&gt;

&lt;p&gt;By default, NestJS has a &lt;code&gt;src/main.ts&lt;/code&gt; file that serves as the entry point of the application, including all configuration and bootstrapping. Modify this file to move everything to the &lt;code&gt;AppFactory.ts&lt;/code&gt; file, keeping only the invocation of the &lt;code&gt;listen&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AppFactory&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./AppFactory.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;bootstrap&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;appPromise&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;AppFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;appPromise&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;bootstrap&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Step 5 - Add&lt;/strong&gt; &lt;code&gt;api/index.ts&lt;/code&gt; File
&lt;/h2&gt;

&lt;p&gt;By default, the Vercel runtime builds and serves any function created within the &lt;code&gt;/api&lt;/code&gt; directory of a project to Vercel (&lt;a href="https://vercel.com/docs/functions/runtimes/node-js" rel="noopener noreferrer"&gt;doc&lt;/a&gt;). Since Vercel understands and handles the Express app object, create a function inside this directory that exports the Express app object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * This file exports Express instance for specifically for the deployment of the app on Vercel.
 */&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AppFactory&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../src/AppFactory.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;AppFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;expressApp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Step 6 - Add&lt;/strong&gt; &lt;code&gt;vercel.json&lt;/code&gt; File
&lt;/h2&gt;

&lt;p&gt;Create a file named &lt;code&gt;vercel.json&lt;/code&gt; in the project’s root directory to configure Vercel. Here, define a rewrite rule for Vercel to use the Express app to serve all incoming traffic (&lt;a href="https://vercel.com/docs/projects/project-configuration" rel="noopener noreferrer"&gt;doc&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;You can also use a &lt;code&gt;tsconfig.json&lt;/code&gt; file at the &lt;code&gt;api&lt;/code&gt; directory to configure the Vercel’s TypeScript compiler. Most options are supported aside from &lt;a href="https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping" rel="noopener noreferrer"&gt;&lt;strong&gt;"Path Mappings"&lt;/strong&gt;&lt;/a&gt; and &lt;a href="https://www.typescriptlang.org/docs/handbook/project-references.html" rel="noopener noreferrer"&gt;&lt;strong&gt;"Pr&lt;/strong&gt;&lt;/a&gt;&lt;a href="https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping" rel="noopener noreferrer"&gt;&lt;strong&gt;oject Reference&lt;/strong&gt;&lt;/a&gt;&lt;a href="https://www.typescriptlang.org/docs/handbook/project-references.html" rel="noopener noreferrer"&gt;&lt;strong&gt;s"&lt;/strong&gt;&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;version&lt;/span&gt;&lt;span class="dl"&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;buildCommand&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;npm run build&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;outputDirectory&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;rewrites&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="p"&gt;{&lt;/span&gt;
      &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;source&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/(.*)&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;destination&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/api&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Step 7 - Create a Project on Vercel&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Congratulations 🎉! We are almost done. Now, create a git repository and push your source code to it. Then, go to your Vercel account, create a new project, and import the git repository. You can also use &lt;a href="https://github.com/mahdavipanah/nestjs-on-vercel" rel="noopener noreferrer"&gt;this article’s example GitHub repository&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fllpglls42lo18a2qpvn8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fllpglls42lo18a2qpvn8.png" alt="Vercel's dashboard" width="800" height="284"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>nestjs</category>
      <category>vercel</category>
      <category>node</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Pitfalls of URL and URLSearchParams in JavaScript</title>
      <dc:creator>Hamidreza Mahdavipanah</dc:creator>
      <pubDate>Fri, 22 Nov 2024 16:59:48 +0000</pubDate>
      <link>https://dev.to/mahdavipanah/pitfalls-of-url-and-urlsearchparams-in-javascript-4ef8</link>
      <guid>https://dev.to/mahdavipanah/pitfalls-of-url-and-urlsearchparams-in-javascript-4ef8</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;This article is also available on my blog: &lt;a href="https://hamidreza.tech/pitfalls-of-url-and-urlsearchparams-in-nodejs" rel="noopener noreferrer"&gt;https://hamidreza.tech/pitfalls-of-url-and-urlsearchparams-in-nodejs&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  It All Started With a Bug
&lt;/h1&gt;

&lt;p&gt;Working with URLs in JavaScript and Node.js should be straightforward, but a recent bug in our project led me down a rabbit hole of subtle quirks in the &lt;a href="https://nodejs.org/api/url.html" rel="noopener noreferrer"&gt;&lt;code&gt;URL&lt;/code&gt; and &lt;code&gt;URLSearchParams&lt;/code&gt; APIs&lt;/a&gt;. This post will explore these quirks, how they can cause problems in your code, and what you can do to avoid them.&lt;/p&gt;




&lt;h1&gt;
  
  
  The Problem: URL Handling with Axios
&lt;/h1&gt;

&lt;p&gt;We encountered this issue while generating URLs and adding hash signatures to them. The query parameters weren’t consistently percent-encoded, leading to unexpected behavior and wrong hash signatures.&lt;/p&gt;

&lt;p&gt;It became clear that the interaction between &lt;code&gt;URL&lt;/code&gt; and &lt;code&gt;URLSearchParams&lt;/code&gt; objects required extra care.&lt;/p&gt;




&lt;h2&gt;
  
  
  Pitfall #1: &lt;code&gt;URL.search&lt;/code&gt; vs. &lt;code&gt;URLSearchParams.toString()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The first surprise was the difference between &lt;code&gt;URL.search&lt;/code&gt; and &lt;code&gt;URLSearchParams.toString()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Use care when using &lt;code&gt;.searchParams&lt;/code&gt; to modify the &lt;code&gt;URL&lt;/code&gt; because, per the &lt;a href="https://url.spec.whatwg.org/" rel="noopener noreferrer"&gt;WHATWG specification&lt;/a&gt;, the &lt;code&gt;URLSearchParams&lt;/code&gt; object uses different rules to determine which characters to percent-encode. For instance, the &lt;code&gt;URL&lt;/code&gt; object will not percent-encode the ASCII tilde (&lt;code&gt;~&lt;/code&gt;) character, while &lt;code&gt;URLSearchParams&lt;/code&gt; will always encode it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Example 1&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://example.com?param=foo bar&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// prints param=foo%20bar&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;searchParams&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// prints ?param=foo+bar&lt;/span&gt;

&lt;span class="c1"&gt;// Example 2&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myURL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://example.org/abc?foo=~bar&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myURL&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// prints ?foo=~bar&lt;/span&gt;
&lt;span class="c1"&gt;// Modify the URL via searchParams...&lt;/span&gt;
&lt;span class="nx"&gt;myURL&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;searchParams&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myURL&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// prints ?foo=%7Ebar&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our project, we needed to explicitly reassign &lt;code&gt;url.search = url.searchParams.toString()&lt;/code&gt; to ensure the query string was encoded consistently.&lt;/p&gt;




&lt;h2&gt;
  
  
  Pitfall #2: The Plus Sign Dilemma
&lt;/h2&gt;

&lt;p&gt;Another gotcha is how &lt;code&gt;URLSearchParams&lt;/code&gt; handles &lt;code&gt;+&lt;/code&gt; characters. By default, &lt;code&gt;URLSearchParams&lt;/code&gt; interprets &lt;code&gt;+&lt;/code&gt; as a space, which may lead to data corruption when encoding binary data or Base64 strings.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;URLSearchParams&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bin=E+AXQB+A&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// "E AXQB A"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One solution is to use &lt;code&gt;encodeURIComponent&lt;/code&gt; before appending values to &lt;code&gt;URLSearchParams&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;encodeURIComponent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;E+AXQB+A&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;More details are available in the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams#preserving_plus_signs" rel="noopener noreferrer"&gt;MDN documentation&lt;/a&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Pitfall #3: &lt;code&gt;URLSearchParams.get&lt;/code&gt; vs. &lt;code&gt;URLSearchParams.toString()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Another subtlety arises when comparing the outputs of &lt;code&gt;URLSearchParams.get&lt;/code&gt; and &lt;code&gt;URLSearchParams.toString&lt;/code&gt;. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;URLSearchParams&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;?key=value&amp;amp;key=other&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;key&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// "value" (first occurrence)&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// "key=value&amp;amp;key=other" (all occurrences serialized)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In multi-valued scenarios, &lt;code&gt;get&lt;/code&gt; returns only the first value, while &lt;code&gt;toString&lt;/code&gt; serializes all.&lt;/p&gt;




&lt;h1&gt;
  
  
  The Fix in Our Codebase
&lt;/h1&gt;

&lt;p&gt;In our project, we resolved the issue by explicitly reassigning the &lt;code&gt;search&lt;/code&gt; property:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;search&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;searchParams&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;searchParams&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hash&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;cryptography&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createSha256HmacBase64UrlSafe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;href&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;SECRET_KEY&lt;/span&gt; &lt;span class="o"&gt;??&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;This ensured that all query parameters were properly encoded before adding the &lt;code&gt;hash&lt;/code&gt; value.&lt;/p&gt;




&lt;h1&gt;
  
  
  Node.js &lt;code&gt;querystring&lt;/code&gt; module
&lt;/h1&gt;

&lt;p&gt;The WHATWG &lt;code&gt;URLSearchParams&lt;/code&gt; interface and the &lt;a href="https://nodejs.org/api/querystring.html" rel="noopener noreferrer"&gt;&lt;code&gt;querystring&lt;/code&gt; module&lt;/a&gt; have a similar purpose, but the purpose of the &lt;code&gt;querystring&lt;/code&gt; module is more general, as it allows the customization of delimiter characters (&lt;code&gt;&amp;amp;&lt;/code&gt; and &lt;code&gt;=&lt;/code&gt;). On the other hand, &lt;code&gt;URLSearchParams&lt;/code&gt; API is designed purely for URL query strings.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;querystring&lt;/code&gt; is more performant than &lt;code&gt;URLSearchParams&lt;/code&gt; but is not a standardized API. Use &lt;code&gt;URLSearchParams&lt;/code&gt; when performance is not critical or when compatibility with browser code is desirable.&lt;/p&gt;

&lt;p&gt;When using &lt;code&gt;URLSearchParams&lt;/code&gt; unlike &lt;code&gt;querystring&lt;/code&gt; module, duplicate keys in the form of array values are not allowed. Arrays are stringified using &lt;code&gt;array.toString()&lt;/code&gt;, which simply joins all array elements with commas.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;URLSearchParams&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;abc&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;first&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;second&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;query&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="c1"&gt;// Prints [ 'first,second' ]&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="c1"&gt;// Prints 'user=abc&amp;amp;query=first%2Csecond'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With &lt;code&gt;querystring&lt;/code&gt; module, the query string &lt;code&gt;'foo=bar&amp;amp;abc=xyz&amp;amp;abc=123'&lt;/code&gt; is parsed into:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bar&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;abc&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;xyz&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;123&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;h1&gt;
  
  
  Takeaways
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Be &lt;strong&gt;cautious of how&lt;/strong&gt; &lt;code&gt;URLSearchParams&lt;/code&gt; handles special characters (e.g. &lt;code&gt;~&lt;/code&gt;) and spaces. Use &lt;code&gt;encodeURIComponent&lt;/code&gt; when necessary.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Understand the difference between&lt;/strong&gt; &lt;code&gt;URL.search&lt;/code&gt;, &lt;code&gt;URLSearchParams.get&lt;/code&gt;, and &lt;code&gt;URLSearchParams.toString&lt;/code&gt; to avoid unexpected behavior.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;In Node.js use&lt;/strong&gt; &lt;code&gt;querystring&lt;/code&gt; &lt;strong&gt;module if&lt;/strong&gt; you want to parse duplicate query parameter keys as an array.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>api</category>
    </item>
    <item>
      <title>Resolving MongoDB Error When Starting with Homebrew on macOS</title>
      <dc:creator>Hamidreza Mahdavipanah</dc:creator>
      <pubDate>Thu, 21 Nov 2024 11:20:15 +0000</pubDate>
      <link>https://dev.to/mahdavipanah/resolving-mongodb-error-when-starting-with-homebrew-on-macos-52j3</link>
      <guid>https://dev.to/mahdavipanah/resolving-mongodb-error-when-starting-with-homebrew-on-macos-52j3</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;This article is also available on my blog: &lt;a href="https://hamidreza.tech/mongodb-error-with-homebrew-on-macos" rel="noopener noreferrer"&gt;https://hamidreza.tech/mongodb-error-with-homebrew-on-macos&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;While installing MongoDB Community Edition on my MacBook using Homebrew (&lt;a href="https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-os-x/" rel="noopener noreferrer"&gt;official MongoDB doc&lt;/a&gt;), I encountered an issue: the &lt;code&gt;mongodb-community&lt;/code&gt; service seemed to start successfully but was listed as &lt;strong&gt;error&lt;/strong&gt; when I checked its status. If you're facing the same problem, here's a quick guide to help you fix it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Issue
&lt;/h2&gt;

&lt;p&gt;After running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;brew services start mongodb-community
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I received a confirmation message. However, checking the service status with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;brew services list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;showed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Name              Status  User Plist
mongodb-community error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The MongoDB log (&lt;code&gt;/usr/local/var/log/mongodb/mongo.log&lt;/code&gt;) contained warnings:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Running as root user&lt;/strong&gt;: &lt;code&gt;"You are running this process as the root user, which is not recommended"&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Termination signal received&lt;/strong&gt;: &lt;code&gt;"Received signal","attr":{"signal":15,"error":"Terminated: 15"}}&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These indicated that MongoDB was running as the root user, which can cause permission issues and lead to the service being stopped.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Solution
&lt;/h2&gt;

&lt;p&gt;The main issue was incorrect ownership of the MongoDB data and log directories. Here's how to fix it:&lt;/p&gt;

&lt;h3&gt;
  
  
  Steps:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Stop the MongoDB Service&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;brew services stop mongodb-community
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Terminate Any Running MongoDB Processes&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pkill &lt;span class="nt"&gt;-f&lt;/span&gt; mongod
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Change Ownership of MongoDB Directories&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo chown&lt;/span&gt; &lt;span class="nt"&gt;-R&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;whoami&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt; /usr/local/var/mongodb
&lt;span class="nb"&gt;sudo chown&lt;/span&gt; &lt;span class="nt"&gt;-R&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;whoami&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt; /usr/local/var/log/mongodb
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Verify Directory Ownership&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-ld&lt;/span&gt; /usr/local/var/mongodb
&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-ld&lt;/span&gt; /usr/local/var/log/mongodb
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;Ensure your username is listed as the owner.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Start the MongoDB Service as Your User&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;brew services start mongodb-community
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Check the Service Status&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;brew services list
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;The service should now show as &lt;strong&gt;started&lt;/strong&gt; under your username.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Test the MongoDB Connection&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;mongosh
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;You should successfully connect to the MongoDB shell.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;By correcting the directory permissions and ensuring MongoDB runs under your user account (not as root), you can resolve the error when starting the &lt;code&gt;mongodb-community&lt;/code&gt; service with Homebrew on macOS. Remember to avoid using &lt;code&gt;sudo&lt;/code&gt; with &lt;code&gt;brew&lt;/code&gt; commands to prevent permission conflicts.&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>macos</category>
      <category>homebrew</category>
      <category>database</category>
    </item>
    <item>
      <title>Using @ and */ symbols inside JS multiline comments</title>
      <dc:creator>Hamidreza Mahdavipanah</dc:creator>
      <pubDate>Fri, 12 Apr 2024 12:55:37 +0000</pubDate>
      <link>https://dev.to/mahdavipanah/using-and-symbols-inside-js-multiline-comments-5gkl</link>
      <guid>https://dev.to/mahdavipanah/using-and-symbols-inside-js-multiline-comments-5gkl</guid>
      <description>&lt;p&gt;While documenting JavaScript code using JSDoc, I stumbled upon an annoying issue. My attempts to include the &lt;code&gt;/*&lt;/code&gt; symbol in the example code within a multiline comment caused the entire comment block to break. This happens because &lt;code&gt;*/&lt;/code&gt; is recognized as the ending tag for a multiline comment in JavaScript.&lt;/p&gt;

&lt;p&gt;You can see the problem clearly in the blow code block!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * Checks whether two permission strings are semantically equal or not.
 *
 * @example
 * // returns true
 * equals('a/b/c/d/allow', 'a/b/c/*⁣/*/&lt;/span&gt;&lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;allow&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;);
 *
 * @returns {boolean} True in case of equality and false otherwise.
*/
const equals = (first: string, second: string) =&amp;gt; {
    // function&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt; &lt;span class="nx"&gt;logic&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// or false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The solution is surprisingly simple: insert a Unicode invisible separator character between &lt;code&gt;*&lt;/code&gt; and &lt;code&gt;/&lt;/code&gt;. This character acts as a stealthy spacer, preventing the JavaScript interpreter from recognizing the sequence as the end of a comment. You can find and copy this invisible character &lt;a href="https://unicode-explorer.com/c/2063" rel="noopener noreferrer"&gt;&lt;strong&gt;here&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The same problem happens if you use the &lt;code&gt;@&lt;/code&gt; symbol to put a JSDoc code example of a decorator. Because the &lt;code&gt;@&lt;/code&gt; symbol has a special meaning for JSDoc, it leads to unintended changes in the rendered documentation. Consider this snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * A NestJS handler decorator that defines an access permission constraint and enforces it.
 *
 * @example
 * `⁣`⁣`ts
 * // the request only needs to be authenticated and doesn't need any specific permissions
 * @RequiresAccess()
 * Class MyController {}
 * `⁣`⁣`
*/&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;RequiresAccess&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Reflector&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createDecorator&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;
  &lt;span class="nx"&gt;PermissionPathGen&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;PermissionPathGen&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="nf"&gt;transform&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;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;MUST_BE_AUTHENTICATED&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;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Solving this is the same as the previous problem, you need to put an &lt;a href="https://unicode-explorer.com/c/2063" rel="noopener noreferrer"&gt;invisible separator&lt;/a&gt; before the &lt;code&gt;@&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>typescript</category>
      <category>jsdoc</category>
    </item>
  </channel>
</rss>
