WEBVTT

1
00:00.590 --> 00:01.880
Hi everyone!

2
00:01.910 --> 00:09.470
In this video I will show you the one more techniques in NestJS using the versioning.

3
00:09.740 --> 00:11.750
Okay, now what exactly?

4
00:11.750 --> 00:12.710
The versioning.

5
00:12.770 --> 00:21.260
Actually, if you already know and you already code some backend framework like expessjs or something

6
00:21.260 --> 00:28.310
like that, you can see the prefix for example api/v1 okay.

7
00:28.340 --> 00:30.110
and for example /posts

8
00:30.920 --> 00:31.970
That is right.

9
00:32.690 --> 00:35.150
So I will it will have a "v1"

10
00:35.180 --> 00:40.970
This is the version of project and this will be the versioning okay.

11
00:41.510 --> 00:43.190
So that's it.

12
00:43.190 --> 00:46.460
Versioning application.

13
00:48.020 --> 00:56.840
And let me tell you one more reason we need use the version. Actually what will be right.

14
00:56.840 --> 01:00.200
Right now we build something called API right.

15
01:00.200 --> 01:02.090
And what is the API?

16
01:02.180 --> 01:09.740
The API is just the software is being used by another software.

17
01:09.770 --> 01:10.400
Right?

18
01:10.940 --> 01:14.240
So this is just the third party application.

19
01:14.360 --> 01:20.300
And because the third party application we should use the versioning.

20
01:21.470 --> 01:31.820
Now for example, if I have a some product for example right here we have a version one with the methods

21
01:32.270 --> 01:33.740
get methods okay.

22
01:33.770 --> 01:38.750
GET methods with the for the post okay.

23
01:39.560 --> 01:45.830
And with this here we have a return for example like "id" okay.

24
01:45.860 --> 01:57.440
But maybe you change my mind and you will have a version two of this thing and we will return.

25
01:57.440 --> 02:02.450
For example we will replace using the _id okay.

26
02:03.060 --> 02:04.710
We want.

27
02:04.800 --> 02:08.100
This is the newer version and this is the old version.

28
02:08.100 --> 02:08.970
But.

29
02:11.430 --> 02:20.670
Because right now the API is already used by, for example, 1000 people.

30
02:20.670 --> 02:24.660
For that reason we cannot remove okay.

31
02:25.560 --> 02:27.060
We cannot remove the v1.

32
02:27.060 --> 02:29.550
We want what we want right now.

33
02:29.550 --> 02:35.010
We want the two version running in the application at the same time.

34
02:35.670 --> 02:43.350
The reason because the API right here we have a 1000 person.

35
02:43.440 --> 02:44.280
It means.

36
02:46.740 --> 02:58.470
It means if you remove entire version one it will cause the breaking of the any customer.

37
02:58.500 --> 02:59.040
Okay.

38
03:01.410 --> 03:02.850
Let us.

39
03:02.850 --> 03:07.830
So for that reason we want to keep two versions running at the same time.

40
03:08.460 --> 03:08.820
Okay.

41
03:09.390 --> 03:12.930
And actually we can use something like this.

42
03:12.960 --> 03:16.020
api/v1 like this.

43
03:16.740 --> 03:19.500
Or we can use a different method.

44
03:19.530 --> 03:23.130
For example, right here will be also api/v1.

45
03:23.340 --> 03:30.030
/v1/user but in NESTJS we will not use that.

46
03:30.060 --> 03:32.880
We will do something called versioning technique right here.

47
03:32.910 --> 03:43.080
Now if you scroll down we have four types of versioning in NestJS right now like URLs, header media

48
03:43.080 --> 03:44.610
type and custom versioning.

49
03:45.390 --> 03:52.170
And actually in most of the cases we will care about URI versioning right here.

50
03:52.740 --> 03:55.290
Let me show you what is the URI versioning.

51
03:55.560 --> 03:58.560
We will use something like this in a main.ts.

52
03:58.560 --> 04:01.200
you will see this code right.

53
04:01.560 --> 04:05.010
We will enable the versioning with a type something like that.

54
04:05.070 --> 04:06.840
And I will explain for you.

55
04:06.840 --> 04:08.910
Don't worry about that. Right now.

56
04:09.180 --> 04:11.430
I will remove the API right here.

57
04:12.090 --> 04:12.390
Okay.

58
04:12.390 --> 04:16.650
We don't need that remove now.

59
04:16.710 --> 04:22.770
Let's close everything and I will open the main.ts.

60
04:23.370 --> 04:27.930
And right here I will paste the API versioning.

61
04:27.960 --> 04:32.220
And I will import VersioningType from nestjs/common right here.

62
04:32.880 --> 04:34.830
And let's see it change.

63
04:34.860 --> 04:44.370
Or not. If I open the API I will go to here and create a post.

64
04:44.550 --> 04:47.340
For example, if I get all posts right here.

65
04:47.670 --> 04:58.860
Now if I /v1, let's see it's work or not, send. Now it say we

66
05:00.900 --> 05:02.010
Sorry, sorry.

67
05:02.070 --> 05:02.760
We must use

68
05:02.760 --> 05:04.380
v1 before the boss.

69
05:05.760 --> 05:15.030
Okay, now if I send, now you can see it have a 404 it means the URL is not found in the system.

70
05:15.030 --> 05:15.510
Right.

71
05:15.630 --> 05:17.970
So how we can do that?

72
05:20.160 --> 05:20.640
Right now.

73
05:20.670 --> 05:23.070
Let me come back to the documentation.

74
05:23.760 --> 05:30.360
And, you can see. If I scroll a little bit.

75
05:33.720 --> 05:38.160
You can see right here we have something called controller and version.

76
05:38.760 --> 05:39.360
Like that.

77
05:40.980 --> 05:46.140
So let me use inside a post controller.

78
05:47.010 --> 05:52.950
Inside a controller right here I will provide the version.

79
05:53.640 --> 05:54.210
Right.

80
05:54.900 --> 05:56.940
Let me use the second version.

81
05:56.970 --> 05:57.450
No.

82
06:01.950 --> 06:07.560
So here I will pass the version using one and one.

83
06:07.840 --> 06:08.350
more thing.

84
06:08.530 --> 06:10.480
Using the path

85
06:10.510 --> 06:11.500
using the users

86
06:12.070 --> 06:12.340
Okay.

87
06:12.370 --> 06:13.930
This is just a normal thing.

88
06:16.240 --> 06:16.660
Oh, sorry.

89
06:16.690 --> 06:17.530
This is the posts

90
06:20.200 --> 06:26.980
Now, if I get all, you can see it works right now, right?

91
06:28.000 --> 06:30.850
So that is.

92
06:30.880 --> 06:33.790
But I don't want you something like this.

93
06:34.420 --> 06:36.820
Okay, I will come back to the previous one.

94
06:37.180 --> 06:39.880
The reason I don't do that is because.

95
06:40.090 --> 06:43.840
What about if I have a 1000 controller?

96
06:44.440 --> 06:44.860
Okay.

97
06:45.340 --> 06:50.230
I don't want to go back and add them one by one.

98
06:50.260 --> 06:50.680
Right?

99
06:51.160 --> 06:56.890
So for that reason, we can use.

100
06:57.880 --> 06:59.020
Let me show you.

101
07:01.180 --> 07:03.340
We can use the, the version one.

102
07:03.340 --> 07:08.110
And lastly, we can use the version in the method level.

103
07:08.470 --> 07:11.140
Okay, now you can see it has a same method, right?

104
07:11.170 --> 07:16.930
It is same method, same controller, but it's just different version, right?

105
07:19.150 --> 07:26.950
And, we also not you that you can test this for that reason.

106
07:27.640 --> 07:30.340
And we also can use multiple version.

107
07:30.370 --> 07:33.580
Let me show you how we can set it global.

108
07:33.910 --> 07:34.420
Okay.

109
07:34.450 --> 07:35.260
Global.

110
07:35.260 --> 07:43.240
Default version in here. You can use that, for example if I copy the default version one right

111
07:43.240 --> 07:45.910
here inside the main.ts

112
07:48.550 --> 07:50.410
Like this okay

113
07:52.510 --> 07:56.620
And now if I send okay it works.

114
07:56.620 --> 07:57.100
Right.

115
07:57.100 --> 08:04.360
But if I go back to the previous one, if I say now, it's not working right now because we already

116
08:04.360 --> 08:12.070
add a version and you might know what is the V prefix comes from because right here we only specify

117
08:12.070 --> 08:12.700
the number.

118
08:12.910 --> 08:13.660
Right?

119
08:13.690 --> 08:15.280
We don't specify the "v".

120
08:15.580 --> 08:18.100
Actually this comes from here.

121
08:18.280 --> 08:23.260
The type version in a URL like this okay.

122
08:23.680 --> 08:34.930
If you scroll back a little bit now you can see the version URL automatically prefixed with

123
08:34.930 --> 08:37.660
the v modifier.

124
08:37.690 --> 08:38.350
Okay.

125
08:38.380 --> 08:44.590
But technically you can change that using the prefix.

126
08:45.760 --> 08:47.440
Okay I will show you prefix.

127
08:49.120 --> 08:55.960
For example, I want it to have an api/v1 okay.

128
08:56.470 --> 09:02.110
So it will add the one more thing right here with prefix.

129
09:02.620 --> 09:08.020
Now if I send with the v1 okay it's not work.

130
09:08.080 --> 09:10.060
But if I pass the /api

131
09:12.370 --> 09:16.780
/v1, so it's work

132
09:16.780 --> 09:19.270
What we expected

133
09:19.270 --> 09:19.570
Right.

134
09:19.600 --> 09:20.770
We can change.

135
09:20.800 --> 09:22.240
About that if you want.

136
09:22.930 --> 09:25.270
Okay now let me show you.

137
09:25.300 --> 09:27.550
We can use a different version right here.

138
09:28.000 --> 09:33.760
Instead of using the one we can use the one and two and three if you want.

139
09:36.520 --> 09:37.480
Let me show.

140
09:38.770 --> 09:46.270
And actually now you can see it will change the the terminal a little bit.

141
09:46.300 --> 09:51.550
You can see. It have a version one and two right here okay.

142
09:53.110 --> 09:56.890
Now if I send this work right.

143
09:57.430 --> 10:05.500
And if I send a version 2, it's also work, if I send a version 3, it clearly not work.

144
10:05.530 --> 10:06.100
Right.

145
10:07.540 --> 10:14.570
And so I think that's it. But I'll leave with default 1

146
10:14.570 --> 10:17.600
version 1 right here

147
10:17.780 --> 10:22.160
And let me show you one more example with right here.

148
10:22.190 --> 10:27.200
I will duplicate this and I will get one version two.

149
10:27.860 --> 10:28.250
Okay.

150
10:28.280 --> 10:35.420
I will use difference like this I will change the structure for example like version two.

151
10:36.050 --> 10:41.990
And I will use the @Version

152
10:43.340 --> 10:52.370
2 right if I use the get with the version one, let's see it works.

153
10:52.940 --> 10:55.520
If I go with version two.

154
10:55.550 --> 10:58.730
Now let me say it will turn on my version 2 right here.

155
10:58.910 --> 10:59.480
Right.

156
10:59.510 --> 11:05.690
So that's it. That's how we can cofnig the version in NestJS, right?

157
11:05.750 --> 11:08.450
So I hope you understand this video.

158
11:11.150 --> 11:12.830
I will see you in the next one.