WEBVTT

1
00:01.100 --> 00:02.870
Hi everyone!

2
00:04.370 --> 00:14.510
We already discussed more and more concepts inside a NestJS Framework and this is the last one will

3
00:14.510 --> 00:16.280
be the custom decorator.

4
00:16.970 --> 00:31.760
Well, actually the decorator is a lot of common use programming languages like Java, ASP.Net.

5
00:32.510 --> 00:37.790
Okay, but in the JavaScript is pretty new, right?

6
00:38.690 --> 00:45.470
We can read more about this to understand what is the decorator and how it work behind the scenes,

7
00:45.470 --> 00:50.330
but I will not do that in this video.

8
00:51.620 --> 00:56.450
And with NestJS, it actually have some.

9
00:57.260 --> 01:05.800
Decorators, for example, if we want to access it to the request object, we can use the decorator @Request

10
01:05.830 --> 01:06.430
right here.

11
01:06.520 --> 01:14.560
Now for example, inside the post controller inside here, if I want to access it inside the post right.

12
01:14.560 --> 01:19.960
If I want to access it into the request object I can use the @Request.

13
01:21.250 --> 01:28.330
And any, okay I will console.log request.

14
01:31.630 --> 01:32.950
And go here.

15
01:36.490 --> 01:41.350
And I will send, and open the terminal.

16
01:41.380 --> 01:44.650
You can see, it will console.log for me the request right.

17
01:45.130 --> 01:51.700
And also we can play around with that response, next session.

18
01:52.870 --> 01:53.410
Okay.

19
01:53.440 --> 01:56.950
This is the something equivalent.

20
01:59.140 --> 02:06.790
A similar, instead of using directly using the request.session.

21
02:07.210 --> 02:07.570
Okay.

22
02:07.600 --> 02:10.810
I can easily create a session right here.

23
02:11.590 --> 02:13.030
You can see session.

24
02:13.810 --> 02:14.440
@Session.

25
02:14.560 --> 02:15.160
Okay.

26
02:15.460 --> 02:16.420
It will be the same.

27
02:19.030 --> 02:19.840
Let me remove.

28
02:23.170 --> 02:25.840
We have a @Param.

29
02:26.500 --> 02:27.040
Right?

30
02:27.070 --> 02:29.320
We already used @Param right here.

31
02:29.710 --> 02:34.330
And in order to send the body we use the @Body parameter.

32
02:34.630 --> 02:42.790
I will show you how to do that in a later where we work with the API and the project.

33
02:43.630 --> 02:48.430
It will similar with this thing inside the express.js.

34
02:48.430 --> 02:48.940
Right.

35
02:49.900 --> 02:56.880
And with the query is similar with the query right here actually, because I already showed you the

36
02:56.880 --> 02:57.450
@Param.

37
02:57.480 --> 03:00.360
I think I will show what is @Query right now.

38
03:01.380 --> 03:02.880
Inside here, I can you.

39
03:02.910 --> 03:05.070
I can pass right here or right here.

40
03:05.190 --> 03:10.020
I can mark the @Query and the query.

41
03:12.630 --> 03:20.700
It will take, for example, if I use the page: number

42
03:23.190 --> 03:25.590
Limit and limit

43
03:30.180 --> 03:30.870
will be number

44
03:31.740 --> 03:33.450
And if I console.

45
03:36.240 --> 03:37.350
Check page

46
03:39.780 --> 03:40.530
And limit.

47
03:43.560 --> 03:55.260
Now in order to use @Query, we must use the question mark and make sure the name is correct with the name

48
03:55.290 --> 03:59.280
of parameter page=1, limit=

49
04:00.330 --> 04:00.930
2

50
04:03.600 --> 04:04.020
I send.

51
04:06.030 --> 04:07.440
You can see, it have a page

52
04:07.740 --> 04:08.640
limit, right

53
04:13.620 --> 04:15.030
Oh, actually.

54
04:15.690 --> 04:16.440
My bad.

55
04:17.580 --> 04:18.690
We only need.

56
04:20.760 --> 04:21.780
One more thing.

57
04:24.360 --> 04:27.750
If I send, you see, it have a page and limit

58
04:33.390 --> 04:36.990
But if I specify here will be page

59
04:37.530 --> 04:38.310
Let's see.

60
04:40.500 --> 04:46.530
If I send here, you can see you have one.

61
04:46.710 --> 04:49.560
And if I duplicate and I use the limit.

62
04:51.540 --> 04:55.850
And we must, We must do something like this.

63
04:57.320 --> 04:57.710
Okay.

64
04:58.070 --> 04:59.780
With the limit.

65
05:04.670 --> 05:09.380
I send okay, it already console.log for me

66
05:09.560 --> 05:15.560
And actually, this is the string, right?

67
05:15.560 --> 05:20.300
So for that reason, I can use the ParseIntPipe right here also.

68
05:25.370 --> 05:28.760
So and again and here is clearly a number.

69
05:28.790 --> 05:29.360
Right.

70
05:30.590 --> 05:32.630
And that's the query.

71
05:32.930 --> 05:40.880
And I can access it through the @Header or the @IP @HostParam okay.

72
05:40.910 --> 05:45.590
We can custom the decorator, for example.

73
05:45.620 --> 05:50.450
Later we can return the current user.

74
05:51.140 --> 05:52.490
We can use something like this.

75
05:54.500 --> 06:00.020
But right now, we don't have a tool for you.

76
06:00.050 --> 06:00.980
That's right.

77
06:02.780 --> 06:04.760
But anyway, let me show you.

78
06:05.390 --> 06:11.270
I will copy the decorator and this just a syntax we don't need to memorize.

79
06:12.020 --> 06:25.400
I will create the folder called decorators and I will use the user.decorator.ts

80
06:25.400 --> 06:27.050
And this will be the user.

81
06:27.080 --> 06:27.710
Okay.

82
06:28.220 --> 06:29.120
This is the user.

83
06:29.150 --> 06:35.690
So for that reason, later I will use the @User decorator like this because this is the custom decorator.

84
06:36.470 --> 06:43.010
And in here instead of return something I will return an object with username.

85
06:45.170 --> 06:45.500
And.

86
06:47.570 --> 06:47.990
email

87
06:51.200 --> 06:58.040
Okay, I will hard code like this because we don't have a login user right now.

88
06:58.700 --> 07:05.900
And in order to do that we can do something like here, @User, okay

89
07:05.960 --> 07:08.420
The name right here and the name right here will be the same.

90
07:09.710 --> 07:13.520
And use the @User and I will use the any type

91
07:16.010 --> 07:19.340
And let me console log the user.

92
07:21.950 --> 07:28.520
Over the terminal run the project and you can see, it will console log for me.

93
07:28.550 --> 07:29.360
Right.

94
07:29.360 --> 07:34.160
And this is the idea of the custom decorator.

95
07:35.390 --> 07:36.920
Hopefully it makes sense right.

96
07:37.160 --> 07:39.380
You can see it's work similar.

97
07:40.880 --> 07:44.360
And that's it, that for the video.

98
07:45.650 --> 07:47.930
Okay I will see you in the next one.