You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Versioning allows you to have different versions of your controllers or individual routes running within the same application. Applications change very often and it is not unusual that there are breaking changes that you need to make while still needing to support the previous version of the application.
3
+
> info **Hint** This chapter is only relevant to HTTP-based applications.
4
+
5
+
Versioning allows you to have **different versions** of your controllers or individual routes running within the same application. Applications change very often and it is not unusual that there are breaking changes that you need to make while still needing to support the previous version of the application.
4
6
5
7
There are 3 types of versioning that are supported:
@@ -28,27 +27,16 @@ URI Versioning uses the version passed within the URI of the request, such as `h
28
27
29
28
> warning **Notice** With URI Versioning the version will be automatically added to the URI after the <ahref="faq/global-prefix">global path prefix</a> (if one exists), and before any controller or route paths.
30
29
31
-
Example HTTP Requests for URI Versioning:
32
-
```http
33
-
GET https://example.com/v1/route HTTP/1.1
34
-
35
-
GET https://example.com/v2/route HTTP/1.1
36
-
```
37
-
38
30
To enable URI Versioning for your application, do the following:
39
31
40
32
```typescript
41
33
@@filename(main)
42
-
import { VersioningType } from'@nestjs/common';
43
-
44
-
asyncfunction bootstrap() {
45
-
const app =awaitNestFactory.create(AppModule);
46
-
app.enableVersioning({
47
-
type: VersioningType.URI,
48
-
});
49
-
awaitapp.listen(3000);
50
-
}
51
-
bootstrap();
34
+
const app =awaitNestFactory.create(AppModule);
35
+
// or "app.enableVersioning()"
36
+
app.enableVersioning({
37
+
type: VersioningType.URI,
38
+
});
39
+
awaitapp.listen(3000);
52
40
```
53
41
54
42
> warning **Notice** The version in the URI will be automatically prefixed with `v` by default, however the prefix value can be configured by setting the `prefix` key to your desired prefix or `false` if you wish to disable it.
@@ -60,29 +48,17 @@ bootstrap();
60
48
Header Versioning uses a custom, user specified, request header to specify the version where the value of the header will be the version to use for the request.
61
49
62
50
Example HTTP Requests for Header Versioning:
63
-
```http
64
-
GET https://example.com/route HTTP/1.1
65
-
Custom-Version-Header: 1
66
51
67
-
GET https://example.com/route HTTP/1.1
68
-
Custom-Version-Header: 2
69
-
```
70
-
71
-
To enable Header Versioning for your application, do the following:
52
+
To enable **Header Versioning** for your application, do the following:
72
53
73
54
```typescript
74
55
@@filename(main)
75
-
import { VersioningType } from'@nestjs/common';
76
-
77
-
asyncfunction bootstrap() {
78
-
const app =awaitNestFactory.create(AppModule);
79
-
app.enableVersioning({
80
-
type: VersioningType.HEADER,
81
-
header: 'Custom-Header',
82
-
});
83
-
awaitapp.listen(3000);
84
-
}
85
-
bootstrap();
56
+
const app =awaitNestFactory.create(AppModule);
57
+
app.enableVersioning({
58
+
type: VersioningType.HEADER,
59
+
header: 'Custom-Header',
60
+
});
61
+
awaitapp.listen(3000);
86
62
```
87
63
88
64
The `header` property should be the name of the header that will contain the version of the request.
@@ -95,30 +71,16 @@ Media Type Versioning uses the `Accept` header of the request to specify the ver
95
71
96
72
Within the `Accept` header, the version will be separated from the media type with a semi-colon, `;`. It should then contain a key-value pair that represents the version to use for the request, such as `Accept: application/json;v=2`. They key is treated more as a prefix when determining the version will to be configured to include the key and separator.
97
73
98
-
Example HTTP Requests for Media Type Versioning:
99
-
```http
100
-
GET https://example.com/route HTTP/1.1
101
-
Accept: application/json;v=1
102
-
103
-
GET https://example.com/route HTTP/1.1
104
-
Accept: application/json;v=2
105
-
```
106
-
107
-
To enable Media Type Versioning for your application, do the following:
74
+
To enable **Media Type Versioning** for your application, do the following:
108
75
109
76
```typescript
110
77
@@filename(main)
111
-
import { VersioningType } from'@nestjs/common';
112
-
113
-
asyncfunction bootstrap() {
114
-
const app =awaitNestFactory.create(AppModule);
115
-
app.enableVersioning({
116
-
type: VersioningType.MEDIA_TYPE,
117
-
key: 'v=',
118
-
});
119
-
awaitapp.listen(3000);
120
-
}
121
-
bootstrap();
78
+
const app =awaitNestFactory.create(AppModule);
79
+
app.enableVersioning({
80
+
type: VersioningType.MEDIA_TYPE,
81
+
key: 'v=',
82
+
});
83
+
awaitapp.listen(3000);
122
84
```
123
85
124
86
The `key` property should be the key and separator of the key-value pair that contains the version. For the example `Accept: application/json;v=2`, the `key` property would be set to `v=`.
@@ -131,16 +93,14 @@ Versioning allows you to version controllers, individual routes, and also provid
131
93
132
94
> warning **Notice** If versioning is enabled for the application but the controller or route does not specify the version, any requests to that controller/route will be returned a `404` response status. Similarly, if a request is received containing a version that does not have a corresponding controller or route, it will also be returned a `404` response status.
133
95
134
-
#### Controller Versions
96
+
#### Controller versions
135
97
136
98
A version can be applied to a controller, setting the version for all routes within the controller.
137
99
138
100
To add a version to a controller do the following:
139
101
140
102
```typescript
141
103
@@filename(cats.controller)
142
-
import { Controller, Get } from'@nestjs/common';
143
-
144
104
@Controller({
145
105
version: '1',
146
106
})
@@ -150,19 +110,7 @@ export class CatsControllerV1 {
150
110
return'This action returns all cats for version 1';
151
111
}
152
112
}
153
-
154
-
@Controller({
155
-
version: '2',
156
-
})
157
-
exportclassCatsControllerV2 {
158
-
@Get('cats')
159
-
findAll():string {
160
-
return'This action returns all cats for version 2';
161
-
}
162
-
}
163
113
@@switch
164
-
import { Controller, Get } from'@nestjs/common';
165
-
166
114
@Controller({
167
115
version: '1',
168
116
})
@@ -172,19 +120,9 @@ export class CatsControllerV1 {
172
120
return'This action returns all cats for version 1';
173
121
}
174
122
}
175
-
176
-
@Controller({
177
-
version: '2',
178
-
})
179
-
exportclassCatsControllerV2 {
180
-
@Get('cats')
181
-
findAll() {
182
-
return'This action returns all cats for version 2';
183
-
}
184
-
}
185
123
```
186
124
187
-
#### Route Versions
125
+
#### Route versions
188
126
189
127
A version can be applied to an individual route. This version will override any other version that would effect the route, such as the Controller Version.
190
128
@@ -227,18 +165,14 @@ export class CatsController {
227
165
}
228
166
```
229
167
230
-
> info **Hint** The `@Version()` decorator is imported from `@nestjs/common` package.
231
-
232
-
#### Multiple Versions
168
+
#### Multiple versions
233
169
234
170
Multiple versions can be applied to a controller or route. To use multiple versions, you would set the version to be an Array.
235
171
236
172
To add multiple versions do the following:
237
173
238
174
```typescript
239
175
@@filename(cats.controller)
240
-
import { Controller, Get } from'@nestjs/common';
241
-
242
176
@Controller({
243
177
version: ['1', '2'],
244
178
})
@@ -249,8 +183,6 @@ export class CatsController {
249
183
}
250
184
}
251
185
@@switch
252
-
import { Controller, Get } from'@nestjs/common';
253
-
254
186
@Controller({
255
187
version: ['1', '2'],
256
188
})
@@ -262,8 +194,7 @@ export class CatsController {
262
194
}
263
195
```
264
196
265
-
266
-
#### Version Neutral
197
+
#### Version "Neutral"
267
198
268
199
Some controllers or routes may not care about the version and would have the same functionality regardless of the version. To accommodate this, the version can be set to `VERSION_NEUTRAL` symbol.
269
200
@@ -284,7 +215,7 @@ export class CatsController {
284
215
@Get('cats')
285
216
findAll():string {
286
217
return'This action returns all cats regardless of version';
0 commit comments