<?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: Marcus</title>
    <description>The latest articles on DEV Community by Marcus (@marcus_castanho).</description>
    <link>https://dev.to/marcus_castanho</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%2F818186%2Fdf334088-2148-4ef2-a59d-63c0676b5376.jpg</url>
      <title>DEV Community: Marcus</title>
      <link>https://dev.to/marcus_castanho</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/marcus_castanho"/>
    <language>en</language>
    <item>
      <title>Spotify OAuth2 Authentication in a NestJS Application</title>
      <dc:creator>Marcus</dc:creator>
      <pubDate>Sun, 20 Mar 2022 15:46:25 +0000</pubDate>
      <link>https://dev.to/marcus_castanho/spotify-oauth2-authentication-in-a-nestjs-application-25eb</link>
      <guid>https://dev.to/marcus_castanho/spotify-oauth2-authentication-in-a-nestjs-application-25eb</guid>
      <description>&lt;h2&gt;
  
  
  Integrate an OAuth2 authorization code flow strategy for the Spotify Web API in a NodeJS with TypeScript and NestJS back end application
&lt;/h2&gt;

&lt;p&gt;When building an API, one of the most important parts of the application is the security and authentication of its users. Most frameworks provide some guidelines on how to implement different authentication strategies. NestJS, for instance, presents, in its &lt;a href="https://docs.nestjs.com/security/authentication"&gt;official documentation&lt;/a&gt;, the JWT strategy.&lt;/p&gt;

&lt;p&gt;However, one widely spread authentication strategy is the OAuth2 approach, usually used with 3rd party services such as Facebook, Google and Spotify accounts which provides a way to use an existing account in these services to authenticate the user and even interact with these services on behalf of the authenticated user.&lt;/p&gt;

&lt;p&gt;As there is no official documentation for integrating this type of authentication with NestJS and development articles usually focus on Google and Facebook integration, this article presents an alternative to integrate the &lt;a href="https://developer.spotify.com/documentation/general/guides/authorization/code-flow/"&gt;Spotify Authorization Code Flow&lt;/a&gt; with &lt;a href="https://nestjs.com/"&gt;NestJS&lt;/a&gt; using the &lt;a href="https://github.com/jaredhanson/passport"&gt;Passport&lt;/a&gt; authentication middleware along with the &lt;a href="https://github.com/JMPerez/passport-spotify"&gt;passport-spotify&lt;/a&gt; strategy.&lt;/p&gt;

&lt;h2&gt;
  
  
  Requirements
&lt;/h2&gt;

&lt;p&gt;This article focuses on the process of using the OAuth2 strategy for Spotify integrated with a NestJS application, therefore, the following requirements are considered to be fulfilled before the process described in this article:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A NestJS application bootsraped with its basic structure. For this part, it is sufficient to follow the quick setup guide in the &lt;a href="https://docs.nestjs.com/first-steps#setup"&gt;NestJS documentation&lt;/a&gt;;&lt;/li&gt;
&lt;li&gt;A Spotify account with access to the Spotify Developer Dashboard and an app registered with its CLIENT ID and CLIENT SECRET credentials. Following the step-by-step &lt;a href="https://developer.spotify.com/documentation/web-api/quick-start/"&gt;official documentation&lt;/a&gt; on how to use the Spotify API is sufficient for this article.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you are not familiar with the OAuth2 Authorization Code Flow, please check the guide provided by the &lt;a href="https://developer.spotify.com/documentation/general/guides/authorization/code-flow/"&gt;Spotify Web API documentation&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The auth folder
&lt;/h2&gt;

&lt;p&gt;With a NestJS application ready, an &lt;code&gt;auth&lt;/code&gt; resource must be created using the following command - considering that the &lt;a href="https://docs.nestjs.com/cli/overview"&gt;Nest CLI&lt;/a&gt; is installed in the machine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nest g mo auth
nest g s auth --no-spec
nest g co auth --no-spec
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Those commands create an auth folder with basic module, service, and controller files without any .spec files. With all in place, the folder structure should look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0tl4xo9yk199imyxckcg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0tl4xo9yk199imyxckcg.png" alt="Beginning folder tree" width="388" height="568"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, the following dependencies must be installed:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install @nestjs/passport @nestjs/jwt passport passport-jwt passport-spotify
npm install -D @types/passport-jwt @types/passport-spotify
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;From now on, there are 3 functionalities that must be available in the application in terms of authentication:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Login of users using the Spotify OAuth2 authorization code flow;&lt;/li&gt;
&lt;li&gt;Retrieving the user's info from Spotify and generate a JWT;&lt;/li&gt;
&lt;li&gt;Using the JWT strategy so that there is no need for connecting with the Spotify OAuth2 server every time there's a need for user authentication in during a session.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  The routes
&lt;/h2&gt;

&lt;p&gt;For the first and second functionalities described earlier, there needs to be a controller with the routes '/login' and '/redirect':&lt;/p&gt;


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



&lt;p&gt;The above code comprehends the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Both routes, '/login' and '/redirect' are guarded with the &lt;code&gt;SpotifyOauthGuard&lt;/code&gt; &lt;a href="https://docs.nestjs.com/guards"&gt;custom guard&lt;/a&gt; which implements the &lt;code&gt;passport-spotify&lt;/code&gt; strategy that will be described later;&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;login&lt;/code&gt; method/route is the endpoint that users will access to initiate the authentication;&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;spotifyAuthRedirect&lt;/code&gt; method ('/redirect' route) is the URL to which the Spotify OAuth2 service will be redirected once the user successfully logs in;&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;spotifyAuthRedirect&lt;/code&gt; method: retrieves the user's information coming from Spotify located the &lt;code&gt;req.user&lt;/code&gt; property - if there is no info, meaning the authentication wasn't performed or failed, the method redirects the request to the login route again - sets the &lt;code&gt;user&lt;/code&gt; req property to undefined (as it is will be defined as the JWT payload further), generates a JWT with it, and returns the user's info and Spotify tokens that can be used by the application to access routes in the Spotify Web API using the user's info depending on the &lt;a href="https://developer.spotify.com/documentation/general/guides/authorization/scopes/"&gt;scopes defined&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Spotify OAuth2 strategy
&lt;/h2&gt;

&lt;p&gt;When using a built-in passport strategy, a custom guard mustbe created and also its corresponding strategy. The &lt;code&gt;SpotifyOauthGuard&lt;/code&gt; is simply a class that extends the &lt;code&gt;AuthGuard&lt;/code&gt; class, so, after creating a /guards folder, inside it, the &lt;code&gt;SpotifyOauthGuard&lt;/code&gt; should look like this:&lt;/p&gt;


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


&lt;p&gt;Also, the named &lt;code&gt;spotify&lt;/code&gt; strategy must be created inside a /strategies folder:&lt;/p&gt;


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


&lt;p&gt;The above code is responsible for connecting with the Spotify OAuth2 service and managing the redirection to the application. The process is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;SpotifyOauthStrategy&lt;/code&gt; class extends the &lt;code&gt;PassportStrategy&lt;/code&gt; using the strategy provided by the &lt;a href="https://github.com/JMPerez/passport-spotify"&gt;passport-spotify&lt;/a&gt; lib and name it 'spotify' so the &lt;code&gt;SpotifyOauthGuard&lt;/code&gt; can identify it;&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;constructor&lt;/code&gt; method calls the &lt;a href="https://github.com/JMPerez/passport-spotify"&gt;passport-spotify&lt;/a&gt; &lt;code&gt;Strategy&lt;/code&gt; constructor method using the &lt;code&gt;super&lt;/code&gt; method, passing the Spotify app credentials &lt;code&gt;CLIENT_ID&lt;/code&gt; and &lt;code&gt;CLIENT_SECRET&lt;/code&gt;(saved in .env vars as they must not be exposed publicly), better described &lt;a href="https://developer.spotify.com/documentation/web-api/quick-start/"&gt;here&lt;/a&gt;, a callback URL which is the same route defined in the auth.controller.ts, '/redirect', and the scopes that the app needs for interacting with the user's information;&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;super&lt;/code&gt; method also has a callback function, that will be called as soon as the user's login process succeeds and before it is redirected to the application. This function adds to the request that will be made to the '/redirect' route the following properties: user (containing the user's profile info) and authInfo (containing the &lt;code&gt;refreshToken&lt;/code&gt;, &lt;code&gt;accessToken&lt;/code&gt; and &lt;code&gt;expires_in&lt;/code&gt; info).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The redirection and JWT generation
&lt;/h2&gt;

&lt;p&gt;Once the strategy is implemented, the user will be redirected to the /redirect URL, and, in &lt;code&gt;auth.controller.ts&lt;/code&gt; (presented earlier), the &lt;code&gt;spotifyAuthRedirect&lt;/code&gt; method will intercept the &lt;code&gt;req&lt;/code&gt; object and extract the &lt;code&gt;user&lt;/code&gt; and &lt;code&gt;authInfo&lt;/code&gt; properties and pass the user to the authService. With the user's info, the &lt;code&gt;login&lt;/code&gt; method in the &lt;code&gt;AuthService&lt;/code&gt; class is responsible for generating the JWT. The auth.service.ts should look like:&lt;/p&gt;


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


&lt;p&gt;Finally, in &lt;code&gt;auth.service.ts&lt;/code&gt;, the '/redirect' route returns an object containing the authInfo and &lt;code&gt;user&lt;/code&gt; properties as well as a header Authentication set to 'Bearer ' concatenated with the JWT.&lt;/p&gt;

&lt;h2&gt;
  
  
  The JWT strategy
&lt;/h2&gt;

&lt;p&gt;This part of the authentication is basically as described in the &lt;a href="https://docs.nestjs.com/security/authentication#jwt-functionality"&gt;official NestJS documentation&lt;/a&gt;. For this part, there needs to be defined in your &lt;code&gt;.env&lt;/code&gt; vars a &lt;code&gt;JWT_SECRET&lt;/code&gt;, which is a string that is used to generate and encrypt/decrypt the JWT's that the application generates and must not be exposed publicly. Similar to the Spotify strategy, it's necessary to create a &lt;code&gt;JwtAuthGuard&lt;/code&gt; class that extends the built-in passport AuthGuard along with a corresponding, named 'jwt'. Inside the /guards folder, create the jwt-auth.guard.ts file as the following:&lt;/p&gt;


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


&lt;p&gt;And the corresponding strategy, inside the /strategies folder, should look like:&lt;/p&gt;


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


&lt;p&gt;The above code is executed when a route is decorated with the &lt;code&gt;JwtAuthGuard&lt;/code&gt;. The &lt;code&gt;super&lt;/code&gt; method extracts the JWT provided by the request to a guarded route, decrypts it with the &lt;code&gt;JWT_SECRET&lt;/code&gt; provided and inserts a &lt;code&gt;user&lt;/code&gt; property into the &lt;code&gt;req&lt;/code&gt; object containing the info that was previously inserted into the payload of the JWT.&lt;/p&gt;

&lt;p&gt;It's important to highlight that the inserted &lt;code&gt;user&lt;/code&gt; property is not the same that the &lt;code&gt;spotify-strategy&lt;/code&gt; inserts to the &lt;code&gt;req&lt;/code&gt; object, and this is the reason why in the &lt;code&gt;spotifyAuthRedirect&lt;/code&gt; method, the &lt;code&gt;req.user&lt;/code&gt; property is set to undefined before the sign in with the jwt strategy.&lt;/p&gt;

&lt;p&gt;Now any authenticate route can be decorated with the &lt;code&gt;JwtAuthGuard&lt;/code&gt; as the following:&lt;/p&gt;


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


&lt;h2&gt;
  
  
  The AuthModule and AppModule Configs
&lt;/h2&gt;

&lt;p&gt;With everything in place, it's time to configure the instantiation of all the modules. The &lt;code&gt;AuthModule&lt;/code&gt; class should look like this:&lt;/p&gt;


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


&lt;p&gt;The &lt;code&gt;auth.module.ts&lt;/code&gt; file defines all the providers of the auth resource and registers the &lt;code&gt;JWT_SECRET&lt;/code&gt; during the instantiation of the &lt;code&gt;JwtModule&lt;/code&gt; as well as the expiration time for it, here defined as 3600s (1 hour).&lt;/p&gt;

&lt;p&gt;Also, the AppModule should look like:&lt;/p&gt;


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


&lt;p&gt;The &lt;code&gt;app.module.ts&lt;/code&gt; instantiates all the modules of the application including the &lt;code&gt;ConfigModule&lt;/code&gt;, which is necessary for using all the env vars described in the process.&lt;/p&gt;

&lt;p&gt;The final state of the folders and files of the application should look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8pzbhjhojoy5gjaqlk5m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8pzbhjhojoy5gjaqlk5m.png" alt="Final folder tree" width="397" height="735"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;The OAuth2 is an interesting way of integrating an application with external apps such as widespread social media services, taking advantage of an easy way to log users as well as providing functionalities to the users related to these apps.&lt;/p&gt;

&lt;p&gt;Even though NestJS does not provide an official way of making this kind of integration, there are many open source projects that aims to make it easier this authentication method such as the ones described and used in this article.&lt;/p&gt;

&lt;h2&gt;
  
  
  Credits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Cover image by &lt;a href="https://unsplash.com/@alexbemore?utm_source=medium&amp;amp;utm_medium=referral"&gt;Alexander Shatov on Unsplash&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>nestjs</category>
      <category>spotify</category>
      <category>typescript</category>
      <category>node</category>
    </item>
    <item>
      <title>Integrate Node.js Client for Google Maps Services With a NestJS Application</title>
      <dc:creator>Marcus</dc:creator>
      <pubDate>Sat, 19 Mar 2022 00:15:28 +0000</pubDate>
      <link>https://dev.to/marcus_castanho/integrate-nodejs-client-for-google-maps-services-with-a-nestjs-application-46b0</link>
      <guid>https://dev.to/marcus_castanho/integrate-nodejs-client-for-google-maps-services-with-a-nestjs-application-46b0</guid>
      <description>&lt;h2&gt;
  
  
  Wrapping the NodeJS client for Google Maps Services in a NestJS app
&lt;/h2&gt;

&lt;p&gt;A common doubt that comes to mind when building a NestJS application is the proper way to implement some functionality respecting the framework architecture to take full advantage of the scalability it provides.&lt;/p&gt;

&lt;p&gt;This article's main goal is to propose a way of wrapping the &lt;a href="https://github.com/googlemaps/google-maps-services-js"&gt;NodeJS client for Google Maps Services&lt;/a&gt; in a NestJS module and use dependency injection to make it easy to reuse across the application. Although this article uses specifically the &lt;code&gt;@googlemaps/google-maps-service-js&lt;/code&gt; npm package, it may be used as inspiration for other client based external services.&lt;/p&gt;

&lt;h2&gt;
  
  
  NestJS framework
&lt;/h2&gt;

&lt;p&gt;NestJS is a framework for creating scalable and loosely coupled server-side NodeJS applications. One of its main characteristics is the implemented architecture and directory structure, which reinforces the modularized nature of the application. On top of that, NestJS fully supports TypeScript and also works with &lt;a href="https://expressjs.com/"&gt;Express&lt;/a&gt; and &lt;a href="https://www.fastify.io/"&gt;Fastify&lt;/a&gt; under the hood, which makes it an up-to-date and viable alternative framework for building a reliable web server with well-known tooling.&lt;/p&gt;

&lt;h2&gt;
  
  
  The framework architecture
&lt;/h2&gt;

&lt;p&gt;As stated in the &lt;a href="https://docs.nestjs.com/#philosophy"&gt;NestJS documentation&lt;/a&gt;, its architecture is heavily inspired by Angular's architecture. The folder structure is basically organized in modules that represent some entity that binds a specific context of the application. Each module is commonly composed by the files &lt;code&gt;app.module.ts&lt;/code&gt;, &lt;code&gt;app.controller.ts&lt;/code&gt;, and &lt;code&gt;app.service.ts&lt;/code&gt;. The common folder structure for a module looks like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;google-maps&lt;br&gt;
| - google-maps.controller.ts&lt;br&gt;
| - google-maps.module.ts&lt;br&gt;
| - google-maps.service.ts&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Node.js Client for Google Maps Services
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://github.com/googlemaps/google-maps-services-js"&gt;client for google maps service&lt;/a&gt; is a library created to interact with the &lt;a href="https://developers.google.com/maps/apis-by-platform#web_service_apis"&gt;Web API provided by Google&lt;/a&gt; directly from a given project's programming language, in this case, TypeScript with NodeJS.&lt;/p&gt;

&lt;p&gt;Given that the &lt;code&gt;@googlemaps/google-maps-service-js&lt;/code&gt; npm package provides many recurrently used methods and classes, one way to use dependency injection with this service is to wrap the google maps service client inside a getter method in a module class along with the most used methods to better serve the application.&lt;/p&gt;
&lt;h2&gt;
  
  
  Wrapping the library in a module
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;google-maps&lt;/code&gt; module folder is composed simply by a &lt;code&gt;module.ts&lt;/code&gt; file and a &lt;code&gt;service.ts&lt;/code&gt; file, which contains a client getter and other most used methods. The service would then look like this:&lt;/p&gt;


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


&lt;p&gt;The above code is composed by the following functionalities, using an &lt;a href="https://en.wikipedia.org/wiki/Object-oriented_programming"&gt;OOP&lt;/a&gt; approach:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;GoogleMapsService&lt;/code&gt; class extends the default &lt;code&gt;Client&lt;/code&gt; class and calls the super() method to instantiate it in order to use all its methods and properties;&lt;/li&gt;
&lt;li&gt;A private propertie is declared to store the access Key that is required to access the google api service;&lt;/li&gt;
&lt;li&gt;The accessKey is retrieved from an environment variable for security reasons, using the built-in NestJS service &lt;code&gt;ConfigService&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;In this example, an async method called &lt;code&gt;getCoordinates&lt;/code&gt; is defined to get latitude and longitude values from a given address using the .geocode method which communicates with its &lt;a href="https://developers.google.com/maps/documentation/geocoding/overview"&gt;web service API relative&lt;/a&gt;;&lt;/li&gt;
&lt;li&gt;Given that the application is using TypeScript, the package also provides custom types for the library so it is possible to define the type of the returned value for the method created as LatLngLiteral.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The whole structure of the service above is well described in its &lt;a href="https://docs.nestjs.com/providers#services"&gt;dedicated section in the NestJS documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Since the application described in this article does not provide any external route to interact with the service presented above, there is no controller.ts file to handle external requests.&lt;/p&gt;

&lt;p&gt;As for the module file, it is responsible for binding all files related to a given context and also to be injected in other modules. More on this section can be found in the &lt;a href="https://docs.nestjs.com/modules"&gt;NestJS documentation&lt;/a&gt;. The google-maps.module.ts looks something like the following:&lt;/p&gt;


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


&lt;p&gt;The above code defines the service created as a provider and exports it so that it can be reused in other modules.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dependency injection
&lt;/h2&gt;

&lt;p&gt;Lastly, it is required that the google-maps module created is declared inside the module that uses the google-maps service and then uses dependency injection on the class that uses the google-maps service methods. Other modules would look like this:&lt;/p&gt;


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


&lt;p&gt;And then, for the DI at service level:&lt;/p&gt;


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


&lt;p&gt;The above code uses the NestJS dependency injection format to use the google-maps service created and also the google-maps types to define the returned value of the &lt;code&gt;getAddressCoords&lt;/code&gt; method.&lt;/p&gt;

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

&lt;p&gt;As discussed before, it is interesting to write code in a modularized way in scalable applications with multiple entity scopes.&lt;/p&gt;

&lt;p&gt;Considering the described scenario, the application presented in this article shows a way of modularizing external services, making the code more reusable, and concentrating context-related methods and values in the same module.&lt;/p&gt;

&lt;h2&gt;
  
  
  Credits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Cover image by &lt;a href="https://unsplash.com/@ilyapavlov?utm_source=medium&amp;amp;utm_medium=referral"&gt;Ilya Pavlov on Unsplash&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>nestjs</category>
      <category>node</category>
      <category>typescript</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
