<?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: badalkhatri</title>
    <description>The latest articles on DEV Community by badalkhatri (@badalkhatri).</description>
    <link>https://dev.to/badalkhatri</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%2F387848%2Fb45bade8-c075-4c86-9e42-63e58140aa0d.jpeg</url>
      <title>DEV Community: badalkhatri</title>
      <link>https://dev.to/badalkhatri</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/badalkhatri"/>
    <language>en</language>
    <item>
      <title>Deno + JWT</title>
      <dc:creator>badalkhatri</dc:creator>
      <pubDate>Sun, 14 Jun 2020 09:31:24 +0000</pubDate>
      <link>https://dev.to/badalkhatri/deno-jwt-f6p</link>
      <guid>https://dev.to/badalkhatri/deno-jwt-f6p</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ycLDJFXV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/0d37sy4h4xevshv4esz3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ycLDJFXV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/0d37sy4h4xevshv4esz3.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this application, I am going to create simple JWT encode and decode functions.&lt;/p&gt;

&lt;p&gt;First of all, we need to import two libraries or JWT from Deno's 3rd party packages&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;validateJwt — it will be used to validate our JWT token&lt;/li&gt;
&lt;li&gt;makeJwt— it will convert our payload into JWT token&lt;/li&gt;
&lt;li&gt;Jose — it indicates algorithm and type&lt;/li&gt;
&lt;li&gt;Payload— it is interface for our Payload or Data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Encode&lt;/strong&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;First of all we will put our data which into payload by using Payload interface (it is mandatory).&lt;/li&gt;
&lt;li&gt;Then we will define our header which will contains algorithm and type.&lt;/li&gt;
&lt;li&gt;Now we will pass header, payload and key (for simplicity I have put this key in same file but it should be placed in Environment Variable)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This function will return Promise that will contains our JWT token, we can also put expiration time in our payload.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Decode&lt;/strong&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In this function we will simply take the JWT string and pass it into validateJwt function with the key.&lt;/p&gt;

&lt;p&gt;We will extract our payload from the return data.&lt;/p&gt;

&lt;p&gt;Using this functions I have made simple REST API /encode and /decode.&lt;/p&gt;

&lt;p&gt;You can checkout the code for more from my repository here &lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vJ70wriM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/github-logo-ba8488d21cd8ee1fee097b8410db9deaa41d0ca30b004c0c63de0a479114156f.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/badal-khatri-blogs"&gt;
        badal-khatri-blogs
      &lt;/a&gt; / &lt;a href="https://github.com/badal-khatri-blogs/deno-jwt"&gt;
        deno-jwt
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;Happy coding :)&lt;/p&gt;

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

</description>
      <category>deno</category>
      <category>jwt</category>
      <category>rest</category>
    </item>
    <item>
      <title>How to generate dynamic ENUM type in GraphQL from DataBase using NestJS, MongoDB</title>
      <dc:creator>badalkhatri</dc:creator>
      <pubDate>Sun, 24 May 2020 10:54:42 +0000</pubDate>
      <link>https://dev.to/badalkhatri/how-to-generate-dynamic-enum-type-in-graphql-from-database-using-nestjs-mongodb-463n</link>
      <guid>https://dev.to/badalkhatri/how-to-generate-dynamic-enum-type-in-graphql-from-database-using-nestjs-mongodb-463n</guid>
      <description>&lt;p&gt;In this article I am gonna show how to generate &lt;strong&gt;Enum&lt;/strong&gt; type of &lt;strong&gt;GraphQL&lt;/strong&gt; by fetching data from Database in &lt;strong&gt;NestJS&lt;/strong&gt;, I am using &lt;strong&gt;MongoDB&lt;/strong&gt; as my Database with Mongoose, you can use any database.&lt;/p&gt;

&lt;p&gt;I am taking example of User, in which I am storing user’s firstName, lastName and role, for role I want to make an Enum which will be dynamically generated from the roles available in the role table in Database. Also, I am dynamically resolving the selected role to the corresponding id of that role, so that way I can store the id of role in my user record.&lt;/p&gt;

&lt;p&gt;First of all I’ll &lt;strong&gt;fetch&lt;/strong&gt; all the record of role table&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Then I’ll extract all the name field of role in roleNames variable,&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In this &lt;strong&gt;filter&lt;/strong&gt; and &lt;strong&gt;map&lt;/strong&gt; function tweaks I have returned an array containing string of all roles.&lt;/p&gt;

&lt;p&gt;As you can see I have discarded ‘ADMIN’ as I don’t want that user can select ‘ADMIN’ role by his own, to add admin user we might want to use seed data.&lt;/p&gt;

&lt;p&gt;Now I will generate Enum string,&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;By joining all the roleNames with &lt;strong&gt;‘ ’(single space)&lt;/strong&gt; we will have Enum type for UserRole ready, but its just &lt;strong&gt;string&lt;/strong&gt; we didn’t put it in actual &lt;strong&gt;GraphQL file&lt;/strong&gt;, will do it in a short time, before that let us resolve that roles to its corresponding id so that I can easily store it in user table.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;As you can see, I am making an object of UserRole which has &lt;strong&gt;property&lt;/strong&gt; as role name and &lt;strong&gt;value&lt;/strong&gt; as role id,&lt;/p&gt;

&lt;p&gt;&lt;em&gt;So, every role will have its corresponding role id.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now we have generated Enum and its resolver, we have to put it in GraphQL, for that we have to put both of them in &lt;strong&gt;GraphQL options&lt;/strong&gt;, which is executed every time you run the project.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;So that’s it you have successfully generated Enum type in GraphQL which is dynamically generated based on data in database.&lt;/p&gt;

&lt;p&gt;Let us have a look at what GraphQL playground looks like,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wpCidPiL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/yt0yqfm4x8k49ki1ovjm.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wpCidPiL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/yt0yqfm4x8k49ki1ovjm.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As I have only 2 roles ‘STUDENT’ and ‘TEACHER’ in my DB, GraphQL shows me those 2 as suggestion in role field.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aHcnguPL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4trsmseebyn2trjhwdnd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aHcnguPL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4trsmseebyn2trjhwdnd.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can go to my &lt;a href="https://github.com/badal-khatri-blogs/nestjs-dynamic-enum"&gt;GitHub&lt;/a&gt; to get whole code for this NestJS project.&lt;/p&gt;

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

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

</description>
      <category>nestjs</category>
      <category>graphql</category>
      <category>enum</category>
      <category>mongodb</category>
    </item>
    <item>
      <title>A very simple REST API built-in Deno using oak 3rd party module</title>
      <dc:creator>badalkhatri</dc:creator>
      <pubDate>Sun, 17 May 2020 10:24:26 +0000</pubDate>
      <link>https://dev.to/badalkhatri/a-very-simple-rest-api-built-in-deno-using-oak-3rd-party-module-2l2c</link>
      <guid>https://dev.to/badalkhatri/a-very-simple-rest-api-built-in-deno-using-oak-3rd-party-module-2l2c</guid>
      <description>&lt;p&gt;In this post I am going to show simple REST API using Deno (an alternative of Node.js)&lt;/p&gt;

&lt;p&gt;I am going to make 2 routes, one for &lt;strong&gt;GET&lt;/strong&gt; request and other is &lt;strong&gt;POST&lt;/strong&gt; request, I have taken an example to get and add users which are stored in an array, we can also store into the database but I want to make this app simple.&lt;/p&gt;

&lt;p&gt;For simplicity I have put the whole code in a single file, you can use a modular approach too.&lt;/p&gt;

&lt;p&gt;As I am using &lt;strong&gt;‘oak’&lt;/strong&gt; 3rd party library we will need to import it first into our file&lt;/p&gt;

&lt;p&gt;You can find it on — &lt;strong&gt;&lt;a href="https://deno.land/x/oak"&gt;https://deno.land/x/oak&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3HSPRGVn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ge5t3k3m0g943sp4ax8m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3HSPRGVn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ge5t3k3m0g943sp4ax8m.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Application&lt;/strong&gt; — The Application class wraps the serve() function from the http package.&lt;br&gt;
&lt;strong&gt;Router&lt;/strong&gt; — The Router class produces middleware that can be used with an Application to enable routing based on thepathname of the request.&lt;/p&gt;

&lt;p&gt;Let us make an interface for User, as Deno out of the box supports TypeScript&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mzv09WFI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fv57g6z7qx7alue4zmwo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mzv09WFI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fv57g6z7qx7alue4zmwo.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let us store dummy data of the user in an array&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OhXPmVLX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bdiv4knk5tk5tv21gl6t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OhXPmVLX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bdiv4knk5tk5tv21gl6t.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This can also be fetched from database but that would make this post bit longer&lt;/p&gt;

&lt;p&gt;Now as I mentioned above I want to make 2 routes for GET and POST, for that I am going to make 2 functions.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;To get all the user&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YhN9jHzr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/80j62189qfgnhcebt1pb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YhN9jHzr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/80j62189qfgnhcebt1pb.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You might wondering what is context, it just hold &lt;strong&gt;request&lt;/strong&gt;, &lt;strong&gt;response&lt;/strong&gt; related values, as you can see i am sending response using &lt;strong&gt;response.body&lt;/strong&gt; of context&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;To Add new users&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wrSInIId--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/clqioufdb0vfjgdini62.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wrSInIId--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/clqioufdb0vfjgdini62.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this function I am taking value passed in the body and simply pushing it into an existing array, I am not validating any values as of now but for the real application we should validate.&lt;/p&gt;

&lt;p&gt;And after pushing it to the array I am simply sending a response with the updated values of an array.&lt;/p&gt;

&lt;p&gt;So we are done with the logic of getter and setter methods, but still how to define routes?&lt;/p&gt;

&lt;p&gt;For that we have to use &lt;strong&gt;Router&lt;/strong&gt; class that we have imported at the very beginning of the post.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PRYaoXmW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9ojltuu5fnt21qwxfwr2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PRYaoXmW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9ojltuu5fnt21qwxfwr2.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Using router I have defined get and post request with the name of routes(paths) and corresponding functions to be called when that route hits&lt;/p&gt;

&lt;p&gt;Now to bind our router with our application we need to instantiate Application first and using &lt;strong&gt;.use&lt;/strong&gt; method to set the middleware for router&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rMF5X-kN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ee5dmvf1ws1i8m6kamgi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rMF5X-kN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ee5dmvf1ws1i8m6kamgi.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The final step is to start the application with the &lt;strong&gt;listen&lt;/strong&gt; method of the &lt;strong&gt;Application&lt;/strong&gt; class&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DyzLh94X--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mans4fjjra5xi4wn0gty.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DyzLh94X--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mans4fjjra5xi4wn0gty.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To run this code we need to fire following command&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4f00HkxY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/pp5pocc74c2n9pxyn55a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4f00HkxY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/pp5pocc74c2n9pxyn55a.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: As you can see i have put &lt;strong&gt;--allow-net&lt;/strong&gt; which is mandatory in Deno.&lt;/p&gt;

&lt;p&gt;Now we can test our API in &lt;a href="https://www.postman.com/"&gt;Postman&lt;/a&gt; or &lt;a href="https://insomnia.rest/"&gt;Insomnia&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;GET Request&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yUrEbgiJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zrw4erddassm0memnh45.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yUrEbgiJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zrw4erddassm0memnh45.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;POST request&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KGUYxGDi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/kzgy2b2fmghry98ji7vl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KGUYxGDi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/kzgy2b2fmghry98ji7vl.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That’s the end of this simple Deno app!&lt;/p&gt;

&lt;p&gt;Happy Coding!!!&lt;/p&gt;

&lt;p&gt;If you have not installed Deno yet you can find the guide &lt;a href="https://deno.land/#installation"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>node</category>
      <category>deno</category>
      <category>javascript</category>
      <category>rest</category>
    </item>
  </channel>
</rss>
