This post has some notes and the code I did while studying nestjs. I started those notes in my first post about nestjs with simple use of the components. Now, I am going a step forward to use some components in a different way and use other resources available in nestjs framework. My references are the udemy course NestJS: The Complete Developer's Guide and the nest page. The code used in this post you can find inside the messages-level-3 project.
This first topic was already shown in the last post. It shows the basic way to use the providers. However, there are other ways to set them.
Standard / useClass
Use the class name inside the provider is the short way make the Nest IoC container handler the instance.
It is using 'useClass' attribute. It's possible to decide between different classes to be instantiate.
useValue
It can be used when you want give the instance. In this, the instance was created.
Non-class-based provider tokens
In this case, the 'provide' attribute can be used with a string. To use it by IoC is necessary use the @Inject inside the constructor and do the reference.
useFactory
It can be used to create providers dynamically. The function that define the useFactory can be an asynchronous function.
exports
The export attribute is used when you want the resource from that module be available by other module. It can be a service or controller (e.g) from the current module, or even some resource (service, module, etc) you import to be used by the current module but you want it be visible for who import the current module.
So, considering we have the first module 'messages' and the second module 'auth'. Messages module will have access to the auth resource when the service is declared in the @Module decorator in AuthModule inside the export and in import attribute inside the messages module.
Interceptors
In the last post, the example shows how to use interceptors. It used the @UseInterceptors decorator from '@nestjs/common'. Let's go forward and create our interceptor.
The example below create the @Serialize interceptor decorator. Pay attention that the class doesn't have the @Inject but a function. The attribute 'excludeExtraneousValues' print the value in the browser if false and return an empty value if true.
If you need to have an interceptor with a global scope you can declare it inside the module. However, you can choose to use the middleware instead of that because of the order of execution. Middleware executes before the interceptors and Interceptors run after guards. If you need run before the guards you need to create middleware.
Session
You can use the session object to storage information across multiple requests. In a simple example is necessary to have the express-session installed and the main.js configurated.
To test the session you can test in the browser the ''/chats/:chat' endpoint, which will storage the value in the session, and after that use the endpoint '/chats'. The last endpoint will print for you what you sent previously.
Cookie
As far as session storage data across requests, cookie storage data in the user's browser.
Following the nestjs documentation, we will use cookie-parser.
An alternative is to use cookie-session. In this case, you cannot use the import in the main.js file. You have to use a variable because of the incompatibility with nestjs.
Dynamic Module
Until now, we have seen the modules been declared statically. Just add the service name, for instance, inside the providers. However, there are some scenarios where is necessary to decide between different services, as by environment.
Here is an example of how to choose between two services and use some attributes injected to do the processing. Here is using config example by environment.
The ConfigureModule is added inside the module will use it. In the example will be MessagesModule. The static method 'register' is created inside the ConfigureModule and pass a parameter to be used in the decisions.
Inside the ConfigModule is done the decision about which class will be used. Also, it's create the provide 'fileProvide' be used for some class. In this example, production class will use it.
Conlusion
Here we saw more about the components. However, it is an overview yet. The difficult part is using all of this in a real project. But don't worry. Baby steps.