Hi everyone!
We already discussed more and more concepts inside a NestJS Framework and this is the last one will
be the custom decorator.
Well, actually the decorator is a lot of common use programming languages like Java, ASP.Net.
Okay, but in the JavaScript is pretty new, right?
We can read more about this to understand what is the decorator and how it work behind the scenes,
but I will not do that in this video.
And with NestJS, it actually have some.
Decorators, for example, if we want to access it to the request object, we can use the decorator @Request
right here.
Now for example, inside the post controller inside here, if I want to access it inside the post right.
If I want to access it into the request object I can use the @Request.
And any, okay I will console.log request.
And go here.
And I will send, and open the terminal.
You can see, it will console.log for me the request right.
And also we can play around with that response, next session.
Okay.
This is the something equivalent.
A similar, instead of using directly using the request.session.
Okay.
I can easily create a session right here.
You can see session.
@Session.
Okay.
It will be the same.
Let me remove.
We have a @Param.
Right?
We already used @Param right here.
And in order to send the body we use the @Body parameter.
I will show you how to do that in a later where we work with the API and the project.
It will similar with this thing inside the express.js.
Right.
And with the query is similar with the query right here actually, because I already showed you the
@Param.
I think I will show what is @Query right now.
Inside here, I can you.
I can pass right here or right here.
I can mark the @Query and the query.
It will take, for example, if I use the page: number
Limit and limit
will be number
And if I console.
Check page
And limit.
Now in order to use @Query, we must use the question mark and make sure the name is correct with the name
of parameter page=1, limit=
2
I send.
You can see, it have a page
limit, right
Oh, actually.
My bad.
We only need.
One more thing.
If I send, you see, it have a page and limit
But if I specify here will be page
Let's see.
If I send here, you can see you have one.
And if I duplicate and I use the limit.
And we must, We must do something like this.
Okay.
With the limit.
I send okay, it already console.log for me
And actually, this is the string, right?
So for that reason, I can use the ParseIntPipe right here also.
So and again and here is clearly a number.
Right.
And that's the query.
And I can access it through the @Header or the @IP @HostParam okay.
We can custom the decorator, for example.
Later we can return the current user.
We can use something like this.
But right now, we don't have a tool for you.
That's right.
But anyway, let me show you.
I will copy the decorator and this just a syntax we don't need to memorize.
I will create the folder called decorators and I will use the user.decorator.ts
And this will be the user.
Okay.
This is the user.
So for that reason, later I will use the @User decorator like this because this is the custom decorator.
And in here instead of return something I will return an object with username.
And.
email
Okay, I will hard code like this because we don't have a login user right now.
And in order to do that we can do something like here, @User, okay
The name right here and the name right here will be the same.
And use the @User and I will use the any type
And let me console log the user.
Over the terminal run the project and you can see, it will console log for me.
Right.
And this is the idea of the custom decorator.
Hopefully it makes sense right.
You can see it's work similar.
And that's it, that for the video.
Okay I will see you in the next one.