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
Copy file name to clipboardExpand all lines: content/techniques/versioning.md
+47-1Lines changed: 47 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@
4
4
5
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.
6
6
7
-
There are 3 types of versioning that are supported:
7
+
There are 4 types of versioning that are supported:
8
8
9
9
<table>
10
10
<tr>
@@ -19,6 +19,10 @@ There are 3 types of versioning that are supported:
19
19
<td><a href='techniques/versioning#media-type-versioning-type'><code>Media Type Versioning</code></a></td>
20
20
<td>The <code>Accept</code> header of the request will specify the version</td>
<td>Any aspect of the request may be used to specify the version(s). A custom function is provided to extract said version(s).</td>
25
+
</tr>
22
26
</table>
23
27
24
28
#### URI Versioning Type
@@ -87,6 +91,48 @@ The `key` property should be the key and separator of the key-value pair that co
87
91
88
92
> info **Hint** The `VersioningType` enum is available to use for the `type` property and is imported from the `@nestjs/common` package.
89
93
94
+
#### Custom Versioning Type
95
+
96
+
Custom Versioning uses any aspect of the request to specify the version (or versions). The incoming request is analyzed
97
+
using an `extractor` function that returns a string or array of strings.
98
+
99
+
If multiple versions are provided by the requester, the extractor function can return an array of strings, sorted in
100
+
order of greatest/highest version to smallest/lowest version. Versions are matched to routes in order from highest to
101
+
lowest.
102
+
103
+
If an empty string or array is returned from the `extractor`, no routes are matched and a 404 is returned.
104
+
105
+
For example, if an incoming request specifies it supports versions `1`, `2`, and `3`, the `extractor`**MUST** return `[3, 2, 1]`. This ensures that the highest possible route version is selected first.
106
+
107
+
If versions `[3, 2, 1]` are extracted, but routes only exist for version `2` and `1`, the route that matches version `2`
108
+
is selected (version `3` is automatically ignored).
109
+
110
+
> warning **Notice** Selecting the highest matching version based on the array returned from `extractor` > **does not reliably work** with the Express adapter due to design limitations. A single version (either a string or
111
+
> array of 1 element) works just fine in Express. Fastify correctly supports both highest matching version
112
+
> selection and single version selection.
113
+
114
+
To enable **Custom Versioning** for your application, create an `extractor` function and pass it into your application
115
+
like so:
116
+
117
+
```typescript
118
+
@@filename(main)
119
+
// Example extractor that pulls out a list of versions from a custom header and turns it into a sorted array.
120
+
// This example uses Fastify, but Express requests can be processed in a similar way.
Versioning allows you to version controllers, individual routes, and also provides a way for certain resources to opt-out of versioning. The usage of versioning is the same regardless of the Versioning Type your application uses.
0 commit comments