<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>TypeScript Blog &#8212; onthecode</title>
	<atom:link href="https://onthecode.co.uk/blog/category/typescript/feed" rel="self" type="application/rss+xml" />
	<link>https://onthecode.co.uk/blog/category/typescript</link>
	<description>onthecode blog</description>
	<lastBuildDate>Mon, 31 Jan 2022 14:15:33 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.3</generator>

<image>
	<url>https://onthecode.co.uk/wp-content/uploads/2019/02/onthecode-icon-1-100x100.png</url>
	<title>TypeScript Blog &#8212; onthecode</title>
	<link>https://onthecode.co.uk/blog/category/typescript</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to Fix VS Code Rename Symbols Feature for TypeScript</title>
		<link>https://onthecode.co.uk/blog/how-to-fix-vs-code-rename-symbols-feature-for-typescript</link>
					<comments>https://onthecode.co.uk/blog/how-to-fix-vs-code-rename-symbols-feature-for-typescript#comments</comments>
		
		<dc:creator><![CDATA[Umut Esen]]></dc:creator>
		<pubDate>Thu, 20 Jun 2019 19:57:40 +0000</pubDate>
				<category><![CDATA[TypeScript]]></category>
		<category><![CDATA[typescript]]></category>
		<category><![CDATA[visual studio code]]></category>
		<guid isPermaLink="false">https://onthecode.co.uk/?p=2076</guid>

					<description><![CDATA[<p>Visual Studio Code is an open-source, lightweight IDE from Microsoft. I enjoy using it on a day-to-day basis, generally for front-end projects like Angular web applications. VS Code has many handy features, one of my favourite is the rename symbols feature. The rename feature allows you to easily change the name of a variable, function and even a class name. VS Code&#8217;s rename symbols feature saves a lot of time during refactoring sessions as it finds all usages of the symbols and applies the changes to all files in the open folder. The rename feature suddenly stopped working while I was working on a TypeScript-based Angular project. I was getting the following error when I hit F12 on a function: Typescript Server Error (3.5.1) entry.node.getSourceFile is not a function TypeError: entry.node.getSourceFile is not a function at entryToDocumentSpan (tsserver.js:1222213:41) ... Here is a screenshot of the error: This issue appears to occur when you have multiple TypeScript versions installed. When you install Visual Studio Code, it generally installs TypeScript for you. In my case, the project I was working on had TypeScript installed through npmjs. The solution for this problem is to select TypeScript version in Visual Studio Code. You can do this by opening up the command palette (Ctrl + Shift + P) and type Select TypeScript Version&#8230; From the available options, select the local TypeScript version. In my case, this is version 2.9.2. Changing the TypeScript version causes TypeScript server to restart and fixes the rename symbols problem in Visual Studio Code.</p>
<p>The post <a href="https://onthecode.co.uk/blog/how-to-fix-vs-code-rename-symbols-feature-for-typescript">How to Fix VS Code Rename Symbols Feature for TypeScript</a> appeared first on <a href="https://onthecode.co.uk">onthecode</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Visual Studio Code is an open-source, lightweight IDE from Microsoft. I enjoy using it on a day-to-day basis, generally for front-end projects like Angular web applications. VS Code has many handy features, one of my favourite is the rename symbols feature.</p>



<p>The rename feature allows you to easily change the name of a variable, function and even a class name. VS Code&#8217;s rename symbols feature saves a lot of time during refactoring sessions as it finds all usages of the symbols and applies the changes to all files in the open folder. </p>



<p>The rename feature suddenly stopped working while I was working on a TypeScript-based Angular project. I was getting the following error when I hit F12 on a function:</p>



<pre class="wp-block-preformatted">Typescript Server Error (3.5.1) entry.node.getSourceFile is not a function TypeError: entry.node.getSourceFile is not a function at entryToDocumentSpan (tsserver.js:1222213:41) ...</pre>



<p>Here is a screenshot of the error:</p>



<figure class="wp-block-image"><img fetchpriority="high" decoding="async" width="1024" height="155" src="https://onthecode.co.uk/wp-content/uploads/2019/06/VS-Code-Rename-Error-1024x155.png" alt="Visual Studio Code Unable to Rename Typescript  Symbols" class="wp-image-2080" srcset="https://onthecode.co.uk/wp-content/uploads/2019/06/VS-Code-Rename-Error-1024x155.png 1024w, https://onthecode.co.uk/wp-content/uploads/2019/06/VS-Code-Rename-Error-300x46.png 300w, https://onthecode.co.uk/wp-content/uploads/2019/06/VS-Code-Rename-Error-768x117.png 768w, https://onthecode.co.uk/wp-content/uploads/2019/06/VS-Code-Rename-Error-700x106.png 700w, https://onthecode.co.uk/wp-content/uploads/2019/06/VS-Code-Rename-Error.png 1536w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption>Visual Studio Code Unable to Rename TypeScript  Symbols</figcaption></figure>



<p>This issue appears to occur when you have multiple TypeScript versions installed. </p>



<p>When you install Visual Studio Code, it generally installs TypeScript for you. In my case, the project I was working on had TypeScript installed through <code>npmjs</code>.</p>



<p>The solution for this problem is to select TypeScript version in Visual Studio Code. </p>



<p>You can do this by opening up the command palette (Ctrl + Shift + P) and type <strong>Select TypeScript Version&#8230;</strong> </p>



<div class="wp-block-image"><figure class="aligncenter is-resized"><img decoding="async" src="https://onthecode.co.uk/wp-content/uploads/2019/06/VS-Code-Rename-Error-Change-Typescript-Version.png" alt="" class="wp-image-2079" width="450" height="140" srcset="https://onthecode.co.uk/wp-content/uploads/2019/06/VS-Code-Rename-Error-Change-Typescript-Version.png 692w, https://onthecode.co.uk/wp-content/uploads/2019/06/VS-Code-Rename-Error-Change-Typescript-Version-300x94.png 300w, https://onthecode.co.uk/wp-content/uploads/2019/06/VS-Code-Rename-Error-Change-Typescript-Version-600x187.png 600w" sizes="(max-width: 450px) 100vw, 450px" /><figcaption>Change VS Code Typescript Version</figcaption></figure></div>



<p>From the available options, select the local TypeScript version. In my case, this is version 2.9.2.</p>



<div class="wp-block-image"><figure class="aligncenter is-resized"><img decoding="async" src="https://onthecode.co.uk/wp-content/uploads/2019/06/VS-Code-Rename-Error-Change-Typescript-Version-2.png" alt="" class="wp-image-2078" width="436" height="119" srcset="https://onthecode.co.uk/wp-content/uploads/2019/06/VS-Code-Rename-Error-Change-Typescript-Version-2.png 679w, https://onthecode.co.uk/wp-content/uploads/2019/06/VS-Code-Rename-Error-Change-Typescript-Version-2-300x82.png 300w, https://onthecode.co.uk/wp-content/uploads/2019/06/VS-Code-Rename-Error-Change-Typescript-Version-2-600x163.png 600w" sizes="(max-width: 436px) 100vw, 436px" /><figcaption>Select Correct Typescript Version</figcaption></figure></div>



<p>Changing the TypeScript version causes TypeScript server to restart and fixes the rename symbols problem in Visual Studio Code.</p>
<p>The post <a href="https://onthecode.co.uk/blog/how-to-fix-vs-code-rename-symbols-feature-for-typescript">How to Fix VS Code Rename Symbols Feature for TypeScript</a> appeared first on <a href="https://onthecode.co.uk">onthecode</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://onthecode.co.uk/blog/how-to-fix-vs-code-rename-symbols-feature-for-typescript/feed</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Easily Decode JSON Web Token (JWT) in Your SPA</title>
		<link>https://onthecode.co.uk/blog/decode-json-web-tokens-jwt-angular</link>
					<comments>https://onthecode.co.uk/blog/decode-json-web-tokens-jwt-angular#comments</comments>
		
		<dc:creator><![CDATA[Umut Esen]]></dc:creator>
		<pubDate>Sun, 03 Feb 2019 13:36:03 +0000</pubDate>
				<category><![CDATA[TypeScript]]></category>
		<category><![CDATA[angular best practices]]></category>
		<category><![CDATA[decode jwt angular]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[json web token]]></category>
		<category><![CDATA[jwt]]></category>
		<category><![CDATA[jwt-decode]]></category>
		<category><![CDATA[npm]]></category>
		<category><![CDATA[typescript]]></category>
		<guid isPermaLink="false">https://onthecode.co.uk/?p=1386</guid>

					<description><![CDATA[<p>JSON Web Tokens (JWT) are commonly used in single page application frameworks like Angular for authentication and authorisation. In situations where you want grab the details of the user from the token, you need a way to decode it yourself. In this post, I will be using jwt-decode, which is an open-source library to decode JWT tokens. What&#8217;s a JSON Web Token A JWT is simply a long string value, which is represents a&#160;JSON object&#160;with all the information about a user. Often, JWTs are initially generated by APIs and passed to the client to help authenticate subsequent requests.&#160; In a typical client-server architecture, the client gets the token from the server and keeps it locally. The token would be embedded it in each request that requires authentication.&#160;A JWT token consists of three parts: header, payload and signature. They are small in size for transmission and are also secure due to the algorithms (HMAC, RSA) used to sign them. These reasons make JWT tokens a very attractive option for modern web &#38; mobile applications. eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1laWQiOiJmNjIxM2JjMC03OThlLTRmNWEtOGRiYy03ZWY2NmVkOTAzODQiLCJzdWIiOiJqb2guZG9lQG9udGhlY29kZS5jby51ayIsImp0aSI6IjdjZWI2Y2M2LTcxZDEtNGE1NS1hMDg1LTQ5ZWIwMjM5YjM0MyIsInVuaXF1ZV9uYW1lIjoiam9oLmRvZUBvbnRoZWNvZGUuY28udWsiLCJmYW1pbHlfbmFtZSI6IkRvZSIsImdpdmVuX25hbWUiOiJKb2huIiwiaHR0cDovL3NjaGVtYXMubWljcm9zb2Z0LmNvbS93cy8yMDA4LzA2L2lkZW50aXR5L2NsYWltcy9yb2xlIjoiQWRtaW4iLCJleHAiOjE1NDkyMjQ2NDYsImlzcyI6IlRPRE9BUFAuQVBJLkxpdmUiLCJhdWQiOiJUT0RPQVBQLkNsaWVudC5MaXZlIn0.IvDbQGRwsBiJfo76aRKzBRz1bujEO_k_W8VzlfMSL5E As we can see above, each part of the token is separated with a dot. Header contains the algorithm and token type of the token. Payload contains all details about the user. Signature is for verifying the identity of the sender and ensure the message was not modified during transmission. Stick the example token above into the online converter and we get the following: {&#160;&#8220;alg&#8221;: &#8220;HS256&#8221;,&#8220;typ&#8221;: &#8220;JWT&#8221;},{&#160;&#8220;nameid&#8221;: &#8220;f6213bc0-798e-4f5a-8dbc-7ef66ed90384&#8221;,&#160; &#160; &#8220;sub&#8221;: &#8220;joh.doe@onthecode.co.uk&#8221;,&#160; &#160; &#8220;jti&#8221;: &#8220;7ceb6cc6-71d1-4a55-a085-49eb0239b343&#8221;,&#160; &#160; &#8220;unique_name&#8221;: &#8220;joh.doe@onthecode.co.uk&#8221;,&#160; &#160; &#8220;family_name&#8221;: &#8220;Doe&#8221;,&#160; &#160; &#8220;given_name&#8221;: &#8220;John&#8221;,&#160; &#160; &#8220;http://schemas.microsoft.com/ws/2008/06/identity/claims/role&#8221;: &#8220;Admin&#8221;,&#160; &#160; &#8220;exp&#8221;: 1549224646,&#160; &#160; &#8220;iss&#8221;: &#8220;TODOAPP.API.Live&#8221;,&#160; &#160; &#8220;aud&#8221;: &#8220;TODOAPP.Client.Live&#8221;}HMACSHA256(base64UrlEncode(header) + &#8220;.&#8221; + &#160; base64UrlEncode(payload), secret-key) How to decode JWT We can easily decode a JWT using the jwt-decode library. First, install the package via NPM: Simply import the dependency in your service: You can now decode the token like so: The output is a JSON object, containing the full details about the user. The decoded value is persisted on the client application, to be sent in the Authorization header in subsequent requests to the API. For example, we can choose to store it in localStorage so that it is accessible across different browser tabs: So there you have it! We just used the jwt-decode library to decode a JSON Web Token. This allows us to extract user details, which can be useful for identifying the specific information such as role details.</p>
<p>The post <a href="https://onthecode.co.uk/blog/decode-json-web-tokens-jwt-angular">Easily Decode JSON Web Token (JWT) in Your SPA</a> appeared first on <a href="https://onthecode.co.uk">onthecode</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>JSON Web Tokens (JWT) are commonly used in single page application frameworks like <a href="https://angular.io">Angular</a> for authentication and authorisation. In situations where you want grab the details of the user from the token, you need a way to decode it yourself.</p>



<p>In this post, I will be using <a href="https://www.npmjs.com/package/jwt-decode" target="_blank" rel="noreferrer noopener">jwt-decode</a>, which is an open-source library to decode JWT tokens.</p>



<h2 class="wp-block-heading" id="what-s-a-json-web-token">What&#8217;s a JSON Web Token</h2>



<p>A JWT is simply a long string value, which is represents a&nbsp;<a href="http://www.w3schools.com/json/" target="_blank" rel="noreferrer noopener">JSON object</a>&nbsp;with all the information about a user. Often, JWTs are initially generated by APIs and passed to the client to help authenticate subsequent requests.&nbsp;</p>



<p>In a typical client-server architecture, the client gets the token from the server and keeps it locally. The token would be embedded it in each request that requires authentication.&nbsp;A JWT token consists of three parts: <strong>header</strong>, <strong>payload</strong> and <strong>signature</strong>.</p>



<p>They are small in size for transmission and are also secure due to the algorithms (HMAC, RSA) used to sign them. These reasons make JWT tokens a very attractive option for modern web &amp; mobile applications.</p>


<p><span style="color: #ff0000;">eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9</span><strong>.</strong><span style="color: #008000;">eyJuYW1laWQiOiJmNjIxM2JjMC03OThlLTRmNWEtOGRiYy03ZWY2NmVkOTAzODQiLCJzdWIiOiJqb2guZG9lQG9udGhlY29kZS5jby51ayIsImp0aSI6IjdjZWI2Y2M2LTcxZDEtNGE1NS1hMDg1LTQ5ZWIwMjM5YjM0MyIsInVuaXF1ZV9uYW1lIjoiam9oLmRvZUBvbnRoZWNvZGUuY28udWsiLCJmYW1pbHlfbmFtZSI6IkRvZSIsImdpdmVuX25hbWUiOiJKb2huIiwiaHR0cDovL3NjaGVtYXMubWljcm9zb2Z0LmNvbS93cy8yMDA4LzA2L2lkZW50aXR5L2NsYWltcy9yb2xlIjoiQWRtaW4iLCJleHAiOjE1NDkyMjQ2NDYsImlzcyI6IlRPRE9BUFAuQVBJLkxpdmUiLCJhdWQiOiJUT0RPQVBQLkNsaWVudC5MaXZlIn0</span><strong>.</strong><span style="color: #0000ff;">IvDbQGRwsBiJfo76aRKzBRz1bujEO_k_W8VzlfMSL5E</span></p>
<p>As we can see above, each part of the token is separated with a dot.</p>
<ul>
<li><span style="color: #ff0000;">Header</span> contains the algorithm and token type of the token.</li>
<li><span style="color: #008000;">Payload</span> contains all details about the user.</li>
<li><span style="color: #0000ff;">Signature</span> is for verifying the identity of the sender and ensure the message was not modified during transmission.</li>
</ul>


<p>Stick the example token above into <a href="https://jwt.io" target="_blank" rel="noreferrer noopener nofollow">the online converter</a> and we get the following:</p>


<p style="padding-left: 30px; font-size: 13px;"><span style="color: #ff0000;">{</span><span style="color: #ff0000;">&nbsp;&#8220;alg&#8221;: &#8220;HS256&#8221;,</span><span style="color: #ff0000;">&#8220;typ&#8221;: &#8220;JWT&#8221;</span><span style="color: #ff0000;">},</span><br><span style="color: #008000;">{&nbsp;&#8220;nameid&#8221;: &#8220;f6213bc0-798e-4f5a-8dbc-7ef66ed90384&#8221;,<br>&nbsp; &nbsp; &#8220;sub&#8221;: &#8220;joh.doe@onthecode.co.uk&#8221;,<br>&nbsp; &nbsp; &#8220;jti&#8221;: &#8220;7ceb6cc6-71d1-4a55-a085-49eb0239b343&#8221;,<br>&nbsp; &nbsp; &#8220;unique_name&#8221;: &#8220;joh.doe@onthecode.co.uk&#8221;,<br>&nbsp; &nbsp; &#8220;family_name&#8221;: &#8220;Doe&#8221;,<br>&nbsp; &nbsp; &#8220;given_name&#8221;: &#8220;John&#8221;,<br>&nbsp; &nbsp; &#8220;http://schemas.microsoft.com/ws/2008/06/identity/claims/role&#8221;: &#8220;Admin&#8221;,<br>&nbsp; &nbsp; &#8220;exp&#8221;: 1549224646,<br>&nbsp; &nbsp; &#8220;iss&#8221;: &#8220;TODOAPP.API.Live&#8221;,<br>&nbsp; &nbsp; &#8220;aud&#8221;: &#8220;TODOAPP.Client.Live&#8221;<br></span><span style="color: #008000;">}</span><br><span style="color: #0000ff;">HMACSHA256(base64UrlEncode(header) + &#8220;.&#8221; + <span class="Apple-converted-space">&nbsp; </span>base64UrlEncode(payload), secret-key)</span></p>


<h2 class="wp-block-heading" id="how-to-decode-jwt">How to decode JWT</h2>



<p>We can easily decode a JWT using the <a href="https://github.com/auth0/jwt-decode#readme">jwt-decode</a> library. </p>



<p>First, install the package via NPM:</p>


<pre class="wp-block-code"><span><code class="hljs language-bash">npm install jwt-decode --save</code></span></pre>


<p>Simply import the dependency in your service:</p>


<pre class="wp-block-code"><span><code class="hljs language-typescript"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> jwt_decode <span class="hljs-keyword">from</span> <span class="hljs-string">'jwt-decode'</span>;</code></span></pre>


<p>You can now decode the token like so:</p>


<pre class="wp-block-code"><span><code class="hljs language-typescript"><span class="hljs-keyword">var</span> token = <span class="hljs-string">'eyJ0eXAiO... /// jwt token'</span>;
<span class="hljs-keyword">var</span> decoded = jwt_decode(token); 
<span class="hljs-built_in">console</span>.log(decoded);   


<span class="hljs-comment">// Output:</span>
{
  <span class="hljs-string">"nameid"</span>: <span class="hljs-string">"5293518a-7940-47af-a807-5459dbb0ee21"</span>,
  <span class="hljs-string">"sub"</span>: <span class="hljs-string">"johndoe@gmail.com"</span>,
  <span class="hljs-string">"jti"</span>: <span class="hljs-string">"dec3da1a-0510-4b21-99a6-fc82abb4debc"</span>,
  <span class="hljs-string">"unique_name"</span>: <span class="hljs-string">"johndoe@gmail.com"</span>,
  <span class="hljs-string">"family_name"</span>: <span class="hljs-string">"Doe"</span>,
  <span class="hljs-string">"given_name"</span>: <span class="hljs-string">"John"</span>,
  <span class="hljs-string">"company_id"</span>: <span class="hljs-string">"7236"</span>,
  <span class="hljs-string">"exp"</span>: <span class="hljs-number">1642370653</span>,
  <span class="hljs-string">"iss"</span>: <span class="hljs-string">"API.Live"</span>,
  <span class="hljs-string">"aud"</span>: <span class="hljs-string">"Client.Live"</span>
}</code></span></pre>


<p>The output is a JSON object, containing the full details about the user.</p>


<pre class="wp-block-code"><span><code class="hljs language-typescript"><span class="hljs-keyword">var</span> givenName = decodedToken&#91;<span class="hljs-string">'given_name'</span>];</code></span></pre>


<p>The decoded value is persisted on the client application, to be sent in the <code>Authorization</code> header in subsequent requests to the API.</p>



<p>For example, we can choose to store it in <code>localStorage</code> so that it is accessible across different browser tabs:</p>


<pre class="wp-block-code"><span><code class="hljs language-typescript"><span class="hljs-keyword">var</span> currentUser = <span class="hljs-built_in">JSON</span>.stringify({ 
    isAdmin: <span class="hljs-string">'Admin'</span> === decodedToken&#91;<span class="hljs-string">'http://schemas.microsoft.com/ws/2008/06/identity/claims/role'</span>],
    id: decodedToken&#91;<span class="hljs-string">'nameid'</span>],
    fullName: <span class="hljs-string">`<span class="hljs-subst">${decodedToken&#91;<span class="hljs-string">'given_name'</span>]}</span> <span class="hljs-subst">${decodedToken&#91;<span class="hljs-string">'family_name'</span>]}</span>`</span>});

localStorage.setItem(<span class="hljs-string">'currentUser'</span>,currentUser);</code></span></pre>


<p>So there you have it! We just used the <code>jwt-decode</code> library to decode a JSON Web Token. This allows us to extract user details, which can be useful for identifying the specific information such as role details. </p>
<p>The post <a href="https://onthecode.co.uk/blog/decode-json-web-tokens-jwt-angular">Easily Decode JSON Web Token (JWT) in Your SPA</a> appeared first on <a href="https://onthecode.co.uk">onthecode</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://onthecode.co.uk/blog/decode-json-web-tokens-jwt-angular/feed</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
	</channel>
</rss>
