Hi everyone.
Let's understand the pipes.
Into the core concept of Nestjs.
Okay.
The pipe is a class within the injectable.
It means it's a dependency injection.
Okay.
It implement from PipeTransform interface
but ignore that.
Let me show you the idea of the pipes.
The pipe, it's have two use cases
You can see, the first one will be transformation.
It can be transform the input into the some output.
For example, from string to integer.
It has some validation rule.
Okay, now let me show you something like this.
And notice about built-in pipe, in NestJS.
It has a nine.
9 pipes
Something like validation.
Okay, but before understand pipes, let me show you.
What is the @Get(':id')
right here and @Param right here
What is mean?
Okay.
After we understand that, we will understand the pipes.
Let me show you.
For example, inside the post controller.
Inside the post controller.
Okay.
Let me collapse the code right here, inside the post controller.
For example, I will create one more method I will call method will be the getPost, and with the
id okay, it means I want to get the post.
...
For example, if I go to the postman and "/1" right here, I want it will pass the "1" inside
the controller and find that for me, how we can do that.
Firstly, in order to use that, to access the "id" parameter, we must.
We must use the @Param id: number, for example like this.
Now we can see, I will use the @Param.
Right.
It will be the "id" and make sure here and here will be the same.
And "id" okay, we can pass this something like this if you want, but I will say the id: number
and you will getOne.
And now let me console.log.
What is inside the "id" for you?
check post
id
If I send and the check post id will be 1 because I passed the 1 right here.
Two.
Okay.
It will be the post.
Two.
Okay.
That's the param
And.
For now, let me write the code for post service, if I go to post service right here and I will.
I will remove the throw.
I will pass the get one and I will accept within the "id".
I will take the "id", right?
It will be the number.
And I will use the data.
.find will be "post"
equal post
dot id
The "id" from parameter.
Okay.
After we find us, we will return.
We will return the data.
This is clearly the business logic.
Actually I must do that in the repository.
But don't worry it for now, I will use get one.
Get one and I will pass the "id"
Now let's see.
If I send.
The word two actually is not work.
Let me see.
Now, the reason it not work because.
if I change the "===" into "=="
Okay.
If I send, sorry.
If I send it works, why?
Why's that? It's a little bit weird
Right?
You can see this is the number.
This is the number.
But why it not work?
Let me tell you the reason why it not work, firstly
I will console.log typeof "id", can you guess it is string or number?
now it's, I guess.
It's clearly a string.
You can see this is clearly a string.
And the typescript right here.
It doesn't check.
Okay.
It doesn't check.
It leaves the developer okay.
So it doesn't check.
And because the string is not compare with the "===" like this.
Right?
And for that reason, we must convert it into the number.
Right.
And for that reason I can convert it using the "+"
"+" before "id", if I send okay, it's already worked.
But.
With.
NestJS we can use a pipe, right here.
We can convert this using the ParseIntPipe, right
So if I use the second parameter "ParseIntPipe"
If I console.log typeof
id
Now you can see it already worked right.
And the type will be the number.
And that's the reason we use pipe.
Okay.
So now let's go to the use case.
You can see, the pipe it will transform the string into the integer right.
This is the first use case
And let's see what is the difference.
The second use case.
If I scroll down a little bit and because we use the ParseIntPipe, it already validation the param
for me.
If I pass something here.
ABCXYZ?
And now you can see, it showed a message validation failed numeric
Okay.
String is expect.
For that reason, we will use the numeric string like: 2,3,4
Right.
Very cool.
And I hope you understand what is the "pipe" right now.
And we can use the some different "pipe" like ParseFloatPipe, or something like this to validation
okay.
To validation the param
Let me see, we have something like this or not okay.
Anyway, this is the pipe
I hope you understand this video.
I will see you in the next one.