Hi everyone.
In this video let me show you how we can not write this code and also achieve the goal of return the
response DTO
Okay, so if you remember we have a concept of the interceptor.
Now let me reminder for you what is interceptor now?
The first way is before the request, right before the request.
And here will be before the response, okay.
Before the controller return the result into the client, this will be the interceptor.
And you can see it's very similar with my DTO.
Right?
I want it is before it returns the client.
It will convert the DTO right here inside the interceptor.
Right here.
So that's what we want right now.
And let me show you how we can do that.
So let me copy the code from interceptor right here because I never remember.
That code
Okay.
I will create the in the interceptors
And I will name.
Sorry, interceptors
And I will use the name.
Will be.
ConvertDTO
post
Or transform-dto
.interceptor.ts okay, paste it right here and save that to format the code, and change the name into the
TransformInterceptor
And I don't want to write the code "before".
Okay.
I only care about the after.
Okay.
In this case, we care about here.
Okay.
That's what we care.
So I will remove the before, remove the now, I don't need it and I will remove here.
So the tap right here is also from the rxjs
And, but anyway let me show show you how we can do that.
Firstly I will change them.
I will not do the tap because I will give you the map right now.
I will use the map method.
I'm sorry.
Change the map.
So
The map right here is similar with the map from the.
From the JavaScript inside the array.
So we will.
We will convert the some input inside the output and the the purpose of the map.
And it will expect the argument.
The first argument will be the data.
And the second argument I will return.
Let's stay stune with me.
I will return something call.
plainToInstance
This plainToInstance right here from the class transformer.
And now let's make it work.
And I will explain for you what actually happened right now.
I will typo the ResponsePostDTO
And the second argument will be the data.
Okay.
That's the firstly I will show you.
And inside the post controller if I use the.
UseInterceptor and I will use the Transform
TransformDTO
Interceptor or actually TransformDTOInterceptor?
Okay.
How will you use that?
And let's see inside the get all
I will remove everything here.
I will remove and I only return the post
And I will not use the promise right now, because the converts into the "Response" only take care by the
interceptor.
And that's for the job of the interceptor right here, from the controller.
For that reason, we will not use the promise anymore.
Now let me see how I can get posts.
If I say you can say it returned for me something like this.
And that's not what we want.
In order to make this work, we use the something inside the Serializer.
We already have a @Exclude and we also have a property called @Expose.
It's right here.
So I will use @Expose inside the
Response Post
I will expose everything.
This is the opposite with the @Exclude, right.
So we want all of that can be exposed
So I will pass @Expose right here.
And the third argument I will use.
true, exclude will be true
To make the @Expose right here work
And now let's see.
If I return now.
Very cool.
Right.
We already have data like this.
And the what we want.
So let me explain what actually happened.
Right now you can see, firstly, I have the interceptor, right.
And I have a map right here.
Now let me discuss about what is inside the pipe over here.
The pipe right here is will return after the controller return.
So it will return before the controller return.
A result like this, right?
It means it will convert to DTO right here for me.
Okay.
This only take care after the pipe, and inside the pipe method, I will use the map and the map.
It will convert the some input to some output.
Okay, this is the input.
Sorry, that is the output.
Okay.
My my bad.
Now it will convert.
Now instead the pipe we have a map
And inside the map we have a data.
So what is the data right now?
Let me console.log.what is the data
Now if I send and you will send it, have I have everything right here, it's mean.
It mean when we use here.
this?
Here will be the data, right?
All of that will be the "data" that is already been passed with the some item.
Right?
You can see all of that will be the data.
And we will convert the data into the DTO.
That's what we want.
So in order to convert that, we use the some class transformer plainToInstance right here to convert
the instance.
Okay.
To convert this data right here into "DTO".
And make sure we pass the option to make sure.
The response right here work with @Expose
And that's the whole idea.
So we can easily remove that.
And right here we can easily remove it also.
Work, if I return the post and I will not use the promise right now anymore?
So let's see if I create a post.
You can see it only the id
title and description, right?
Very cool.
And that's it.
How we can customize the response.
Okay, but, we have one more downside.
It's mean the ResponsePostDTO right here, because later I don't have a post anymore.
I have a user.
Right.
So for that reason I will customize a little bit.
So I will see you in the next one.