<?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: Tabrez Ahmed</title>
    <description>The latest articles on DEV Community by Tabrez Ahmed (@tabrez).</description>
    <link>https://dev.to/tabrez</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F273942%2F0a597c3c-6d43-4483-8711-c34e9d855f0c.jpeg</url>
      <title>DEV Community: Tabrez Ahmed</title>
      <link>https://dev.to/tabrez</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tabrez"/>
    <language>en</language>
    <item>
      <title>How to handle 4xx and 5xx errors in Javascript fetch()</title>
      <dc:creator>Tabrez Ahmed</dc:creator>
      <pubDate>Sat, 27 Mar 2021 12:27:04 +0000</pubDate>
      <link>https://dev.to/tabrez/how-to-handle-4xx-and-5xx-errors-in-javascript-fetch-14cf</link>
      <guid>https://dev.to/tabrez/how-to-handle-4xx-and-5xx-errors-in-javascript-fetch-14cf</guid>
      <description>&lt;p&gt;Many modern javascript applications make use of the in-built &lt;a href="https://github.com/github/fetch" rel="noopener noreferrer"&gt;&lt;code&gt;fetch()&lt;/code&gt;&lt;/a&gt; API. This API takes care of most of the HTTP stuff and hence reduces the overhead of an external library like Axios or jQuery in many cases.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;fetch&lt;/code&gt; makes use of &lt;code&gt;Promise&lt;/code&gt; instead of callbacks to make things easier for the developers. For the sake of explanation, Let's fetch my github profile as an example and log to console. Typically, our &lt;code&gt;fetch&lt;/code&gt; usage would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fetchProfile(profileURL) {
    fetch(profileURL)
        .then(res =&amp;gt; res.json())
        .then(userData =&amp;gt; console.log('Response: ', userData))
}

fetchProfile('https://api.github.com/users/ahmedtabrez')
// This logs my profile in JSON format
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This should work for a happy flow. Let's do some error handling now.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fetchProfile(profileURL) {
    fetch(profileURL)
        .then(res =&amp;gt; res.json())
        .then(userData =&amp;gt; console.log('Success: ', userData))
        .catch(error =&amp;gt; console.error('Error: ', error))
}

fetchProfile('https://non.sense.url/')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And this is what gets logged to my console:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1616838030326%2FDhvq2rKBQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1616838030326%2FDhvq2rKBQ.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Fair enough so far. Now let's try fetching my profile with a deliberate typo (let's add a hyphen at the end)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fetchProfile('https://api.github.com/users/ahmedtabrez-')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And here's what is logged:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1616838809813%2Fug-tXIt8Y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1616838809813%2Fug-tXIt8Y.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Wait a minute, why did it log &lt;code&gt;Success: &amp;lt;object&amp;gt;&lt;/code&gt;? Wasn't it supposed to log &lt;code&gt;Error: &amp;lt;object&amp;gt;&lt;/code&gt; according to our code just like it did for &lt;code&gt;https://non.sense.url/&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;Well, that's the caveat that makes using &lt;code&gt;fetch&lt;/code&gt; slightly difficult. &lt;/p&gt;

&lt;p&gt;According to the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch" rel="noopener noreferrer"&gt;docs&lt;/a&gt;, &lt;code&gt;fetch&lt;/code&gt; actually throws an exception only if there is a network failure or if something prevented the request from completing. &lt;strong&gt;It doesn't throw an exception for 4xx or 5xx responses&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So in our case, &lt;code&gt;fetchProfile('https://non.sense.url/')&lt;/code&gt; entered the &lt;code&gt;catch&lt;/code&gt; block as expected because there was a network failure. There was actually no host at &lt;code&gt;https://non.sense.url/&lt;/code&gt;. But when we did &lt;code&gt;fetchProfile('https://api.github.com/users/ahmedtabrez-')&lt;/code&gt;, there was no network error. The URL we called took the request and responded with a response code. The request was complete and hence &lt;code&gt;fetch&lt;/code&gt; did not consider that as an error.&lt;/p&gt;

&lt;p&gt;If you have been using APIs like &lt;code&gt;jQuery.ajax&lt;/code&gt;, you might expect the 4xx and 5xx errors in the &lt;code&gt;catch&lt;/code&gt; block too. So let's write a wrapper function as a workaround for this.&lt;/p&gt;

&lt;p&gt;To begin with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function myFetch(...args) {
    return fetch(...args)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To handle the 4xx and 5xx responses, the &lt;code&gt;fetch&lt;/code&gt; API, fortunately, provides us with a flag &lt;code&gt;ok&lt;/code&gt; in the response object. We can make use of this flag to our advantage.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function myFetch(...args) {
    return fetch(...args)
        .then(res =&amp;gt; {
            if (res.ok) {
                return res
            } else {
                throw new Error('An error occurred')
            }
        })
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That would be enough. Now let's update our &lt;code&gt;fetchProfile&lt;/code&gt; function and try again.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fetchProfile(profileURL) {
    myFetch(profileURL)
        .then(res =&amp;gt; res.json())
        .then(userData =&amp;gt; console.log('Success: ', userData))
        .catch(error =&amp;gt; console.error('Error: ', error))
}

fetchProfile('https://api.github.com/users/ahmedtabrez-')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And this is what the console looks like now:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1616843574446%2FIRjkhdr0o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1616843574446%2FIRjkhdr0o.png" alt="image.png"&gt;&lt;/a&gt;&lt;br&gt;
As expected, the 404 response causes our &lt;code&gt;fetchProfile&lt;/code&gt; to enter into the catch block.&lt;/p&gt;
&lt;h3&gt;
  
  
  A quick enhancement
&lt;/h3&gt;

&lt;p&gt;In the network log in developer tools, The response body looks like &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1616843805089%2FrdjYOvXOe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1616843805089%2FrdjYOvXOe.png" alt="image.png"&gt;&lt;/a&gt;&lt;br&gt;
Let's say we want the server error response body within the catch block for handling the user experience for example by showing an error popup. But our implementation so far is only capable of giving us a generic error message &lt;code&gt;An error occurred&lt;/code&gt;. To make this happen, we will make use of &lt;code&gt;Promise.reject&lt;/code&gt; in place of &lt;code&gt;throw&lt;/code&gt; like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function myFetch(...args) {
    return fetch(...args)        
        .then(res =&amp;gt; {
            if (res.ok) {
                return res
            } else {
                // Assume our 4xx/5xx responses are always JSON objects for the sake of simplicity
                return res.json().then(json =&amp;gt; Promise.reject(json))
            }
        })
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's see what &lt;code&gt;fetchProfile('https://api.github.com/users/ahmedtabrez-')&lt;/code&gt; logs in console.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1616844709680%2Fb8GVrxYG3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1616844709680%2Fb8GVrxYG3.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we have the error response body available at our catch block and we can make use of it as per our requirements.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;There are tons of other implementations available over the internet and they could be more robust than this implementation. But as far as I was able to search, I could not find any implementation which gives us the response body in the &lt;code&gt;catch&lt;/code&gt; block. That's what motivated me to write this post. &lt;/p&gt;

&lt;p&gt;Let me know in the comments below if you feel like giving me any feedback. Also, let me know if you want me to write on a topic. I would love to explore.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>angular</category>
      <category>fetch</category>
    </item>
    <item>
      <title>Recursive routes in Angular</title>
      <dc:creator>Tabrez Ahmed</dc:creator>
      <pubDate>Sun, 24 Nov 2019 10:49:40 +0000</pubDate>
      <link>https://dev.to/tabrez/recursive-routes-in-angular-58la</link>
      <guid>https://dev.to/tabrez/recursive-routes-in-angular-58la</guid>
      <description>&lt;h3&gt;
  
  
  Prologue
&lt;/h3&gt;

&lt;p&gt;You own a retail chain that has a lot of branches. You grow your business and soon realize that you won't be able to concentrate on growing business while parallelly managing all the branches yourself. &lt;/p&gt;

&lt;p&gt;You select some prominent branches in each region (Let's call them L-0 branches) and ask that branch to manage a few branches in their region (Let's call them L-1 branches). Your chain of stores thrives under the new business model. Now all your L-0 branches have too many L-1 to manage just like you had in the beginning.&lt;/p&gt;

&lt;p&gt;You re-organize a little more by selecting a few prominent L-1 branches and add a few L-2 branches under each of them. You keep reorganizing your chain this way up to the L-N level as the need be.&lt;/p&gt;

&lt;h3&gt;
  
  
  What now?
&lt;/h3&gt;

&lt;p&gt;You have a portal built in Angular which has a page to show you the list of branches at &lt;code&gt;example.com/branches&lt;/code&gt;. You find it very cumbersome to have a list of all the branches in the initial view most of which you don't manage directly. You want to have L-0 branches in the initial view and then after choosing to view the sub-branches of one of them, you want to open a list of respective L-1 branches. Similarly, you want to see a list of respective L-2 branches on choosing to view sub-branches of an L-1 branch. and so on.&lt;/p&gt;

&lt;h3&gt;
  
  
  What you need?
&lt;/h3&gt;

&lt;p&gt;Your site so far has the following component structure.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;|-app.component.ts
|-app.component.html
|-app.module.ts
|-app-routing.module.ts
|-branches
    |-branches.module.ts
    |-branches-routing.module.ts
    |-branches.component.ts
    |-branches.component.html
    |-branch
        |-branch.module.ts
        |-branch-routing.module.ts
        |-branch.component.ts
        |-branch.component.html
|-core
    |-services
        |-branch.service.ts
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;branches&lt;/code&gt; component in the &lt;code&gt;branches&lt;/code&gt; module lists all the branches.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;branch&lt;/code&gt; component in the &lt;code&gt;branch&lt;/code&gt; module displays all the details of a particular branch.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;/branches&lt;/code&gt; and &lt;code&gt;/branches/&amp;lt;branchCode&amp;gt;&lt;/code&gt; are lazyloaded routes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The only thing you need to do beforehand is to modify your branches API so it can accept a &lt;code&gt;parentBranchCode&lt;/code&gt; and give you sub-branches only of that branch. Like so:&lt;/p&gt;

&lt;h6&gt;
  
  
  Request:
&lt;/h6&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GET https://example-api.com/branches?parentBranchCode=bengaluru
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h6&gt;
  
  
  Response:
&lt;/h6&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
    {
        "id": 123, 
        "branchName": "Tumakuru",
        "branchCode": "tumakuru",
        "parentBranchCode": "bengaluru"
    },
    {
        "id": 124,
        "branchName": "Mysuru",
        "branchCode": "mysuru",
        "parentBranchCode": "bengaluru"
    },
    {
        "id": 125,
        "branchName": "Mandya",
        "branchCode": "mandya",
        "parentBranchCode": "bengaluru"
    }
]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; In the absence of the &lt;code&gt;parentBranchCode&lt;/code&gt;, the API should give a response containing an array of all L-0 branches.&lt;/p&gt;

&lt;h3&gt;
  
  
  Okay, now let's go ...
&lt;/h3&gt;

&lt;p&gt;This is a breeze. You just got three things to modify in your existing code.&lt;/p&gt;

&lt;h5&gt;
  
  
  1. &lt;code&gt;branch-routing.module.ts&lt;/code&gt;
&lt;/h5&gt;

&lt;h6&gt;
  
  
  Before:
&lt;/h6&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;NgModule&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="nx"&gt;RouterModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forRoot&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;component&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;BranchComponent&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="na"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;RouterModule&lt;/span&gt;&lt;span class="p"&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="nx"&gt;BranchRoutingModule&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;h6&gt;
  
  
  After:
&lt;/h6&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;NgModule&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="nx"&gt;RouterModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forRoot&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;component&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;BranchComponent&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sub-branches&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;loadChildren&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../branches.module#BranchesModule&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="na"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;RouterModule&lt;/span&gt;&lt;span class="p"&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="nx"&gt;BranchRoutingModule&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 is actually the trick. This will make the routes &lt;code&gt;/branches&lt;/code&gt;, &lt;code&gt;/branches/bengaluru/sub-branches&lt;/code&gt; and &lt;code&gt;/branches/bengaluru/sub-branches/mysuru/sub-branches&lt;/code&gt; render the same component ie. &lt;code&gt;BranchesComponent&lt;/code&gt;.&lt;/p&gt;

&lt;h6&gt;
  
  
  But &lt;code&gt;branchCode&lt;/code&gt; from where?
&lt;/h6&gt;

&lt;p&gt;Just to clarify, since you already have a page &lt;code&gt;/branches/&amp;lt;branchCode&amp;gt;&lt;/code&gt;, there must be a route already in your &lt;code&gt;branches-routing.module.ts&lt;/code&gt; like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="nl"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;:branchCode&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="nx"&gt;loadChildren&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./branch/branch.module#BranchModule&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;h6&gt;
  
  
  A question you may have
&lt;/h6&gt;

&lt;p&gt;In a route like &lt;code&gt;/branches/bengaluru/sub-branches/mysuru/sub-branches&lt;/code&gt;, we have two sub-branches, there could be any number in other routes. How do we deal with this?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Short answer:&lt;/strong&gt; You don't have to.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Long answer:&lt;/strong&gt; If the route params of the same name appear more than once, the one appearing later will always overwrite the one preceding it. So rest assured, we are covered here. The &lt;code&gt;branchCode&lt;/code&gt; we finally get when we subscribe to the route params will be the last one that appears in the URL (in this case, &lt;code&gt;mysuru&lt;/code&gt;) which is what we desire.&lt;/p&gt;

&lt;h5&gt;
  
  
  2. &lt;code&gt;branches.component.ts&lt;/code&gt;
&lt;/h5&gt;

&lt;p&gt;You have got the &lt;code&gt;branches&lt;/code&gt; API modified beforehand so as to accept &lt;code&gt;parentBranchCode&lt;/code&gt; and give us back the branches managed by it (or all the L-0 branches if &lt;code&gt;parentBranchCode&lt;/code&gt; is empty). Let' start sending this in query params.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;ngOnInit&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;parentBranchCode&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;route&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;snapshot&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="nx"&gt;branchCode&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;branchService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getBranches&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;parentBranchCode&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;branches&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;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;branches&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;branches&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;h5&gt;
  
  
  3. &lt;code&gt;branch.service.ts&lt;/code&gt;
&lt;/h5&gt;

&lt;p&gt;Very obviously, after the previous step, we need to pass on the query params to the API request.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;getBranches&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;queryParams&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt; &lt;span class="o"&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;return&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;httpService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&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;https://example-api.com/branches&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;queryParams&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;h5&gt;
  
  
  An after step
&lt;/h5&gt;

&lt;p&gt;It makes sense to add a link to sub-branches in the branch component like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;a routerLink="sub-branches"&amp;gt;Sub-branches&amp;lt;/a&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now go to &lt;code&gt;/branches&lt;/code&gt;, you should see all your L-0 branches. Choose to view the details of that branch. Find the button you just added and click on it. You'll see a list of its sub-branches. Now choose to view any branch, find the button again, click on it. You'll get another level of branches. No end to it. &lt;/p&gt;

&lt;h3&gt;
  
  
  Done?
&lt;/h3&gt;

&lt;p&gt;Yeah, did you expect more? &lt;/p&gt;

&lt;p&gt;Hope you like this post. Scroll down to the comments section below and leave me your feedback or any questions you may have.&lt;/p&gt;

&lt;p&gt;I really wish you the very best for your retail chain. 😉😛&lt;/p&gt;

</description>
      <category>angular</category>
      <category>typescript</category>
      <category>spa</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
