Hi everyone!
In this video I will show you the one more techniques in NestJS using the versioning.
Okay, now what exactly?
The versioning.
Actually, if you already know and you already code some backend framework like expessjs or something
like that, you can see the prefix for example api/v1 okay.
and for example /posts
That is right.
So I will it will have a "v1"
This is the version of project and this will be the versioning okay.
So that's it.
Versioning application.
And let me tell you one more reason we need use the version. Actually what will be right.
Right now we build something called API right.
And what is the API?
The API is just the software is being used by another software.
Right?
So this is just the third party application.
And because the third party application we should use the versioning.
Now for example, if I have a some product for example right here we have a version one with the methods
get methods okay.
GET methods with the for the post okay.
And with this here we have a return for example like "id" okay.
But maybe you change my mind and you will have a version two of this thing and we will return.
For example we will replace using the _id okay.
We want.
This is the newer version and this is the old version.
But.
Because right now the API is already used by, for example, 1000 people.
For that reason we cannot remove okay.
We cannot remove the v1.
We want what we want right now.
We want the two version running in the application at the same time.
The reason because the API right here we have a 1000 person.
It means.
It means if you remove entire version one it will cause the breaking of the any customer.
Okay.
Let us.
So for that reason we want to keep two versions running at the same time.
Okay.
And actually we can use something like this.
api/v1 like this.
Or we can use a different method.
For example, right here will be also api/v1.
/v1/user but in NESTJS we will not use that.
We will do something called versioning technique right here.
Now if you scroll down we have four types of versioning in NestJS right now like URLs, header media
type and custom versioning.
And actually in most of the cases we will care about URI versioning right here.
Let me show you what is the URI versioning.
We will use something like this in a main.ts.
you will see this code right.
We will enable the versioning with a type something like that.
And I will explain for you.
Don't worry about that. Right now.
I will remove the API right here.
Okay.
We don't need that remove now.
Let's close everything and I will open the main.ts.
And right here I will paste the API versioning.
And I will import VersioningType from nestjs/common right here.
And let's see it change.
Or not. If I open the API I will go to here and create a post.
For example, if I get all posts right here.
Now if I /v1, let's see it's work or not, send. Now it say we
Sorry, sorry.
We must use
v1 before the boss.
Okay, now if I send, now you can see it have a 404 it means the URL is not found in the system.
Right.
So how we can do that?
Right now.
Let me come back to the documentation.
And, you can see. If I scroll a little bit.
You can see right here we have something called controller and version.
Like that.
So let me use inside a post controller.
Inside a controller right here I will provide the version.
Right.
Let me use the second version.
No.
So here I will pass the version using one and one.
more thing.
Using the path
using the users
Okay.
This is just a normal thing.
Oh, sorry.
This is the posts
Now, if I get all, you can see it works right now, right?
So that is.
But I don't want you something like this.
Okay, I will come back to the previous one.
The reason I don't do that is because.
What about if I have a 1000 controller?
Okay.
I don't want to go back and add them one by one.
Right?
So for that reason, we can use.
Let me show you.
We can use the, the version one.
And lastly, we can use the version in the method level.
Okay, now you can see it has a same method, right?
It is same method, same controller, but it's just different version, right?
And, we also not you that you can test this for that reason.
And we also can use multiple version.
Let me show you how we can set it global.
Okay.
Global.
Default version in here. You can use that, for example if I copy the default version one right
here inside the main.ts
Like this okay
And now if I send okay it works.
Right.
But if I go back to the previous one, if I say now, it's not working right now because we already
add a version and you might know what is the V prefix comes from because right here we only specify
the number.
Right?
We don't specify the "v".
Actually this comes from here.
The type version in a URL like this okay.
If you scroll back a little bit now you can see the version URL automatically prefixed with
the v modifier.
Okay.
But technically you can change that using the prefix.
Okay I will show you prefix.
For example, I want it to have an api/v1 okay.
So it will add the one more thing right here with prefix.
Now if I send with the v1 okay it's not work.
But if I pass the /api
/v1, so it's work
What we expected
Right.
We can change.
About that if you want.
Okay now let me show you.
We can use a different version right here.
Instead of using the one we can use the one and two and three if you want.
Let me show.
And actually now you can see it will change the the terminal a little bit.
You can see. It have a version one and two right here okay.
Now if I send this work right.
And if I send a version 2, it's also work, if I send a version 3, it clearly not work.
Right.
And so I think that's it. But I'll leave with default 1
version 1 right here
And let me show you one more example with right here.
I will duplicate this and I will get one version two.
Okay.
I will use difference like this I will change the structure for example like version two.
And I will use the @Version
2 right if I use the get with the version one, let's see it works.
If I go with version two.
Now let me say it will turn on my version 2 right here.
Right.
So that's it. That's how we can cofnig the version in NestJS, right?
So I hope you understand this video.
I will see you in the next one.