-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding the ability to have different versions of routes to support changing applications that still need to support legacy consumers closes #5065
- Loading branch information
1 parent
d5c51c1
commit ba60e4a
Showing
39 changed files
with
10,346 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { VERSION_METADATA } from '../../constants'; | ||
import { VersionValue } from '../../interfaces/version-options.interface'; | ||
|
||
/** | ||
* Sets the version of the endpoint to the passed version | ||
* | ||
* @publicApi | ||
*/ | ||
export function Version(version: VersionValue): MethodDecorator { | ||
return ( | ||
target: any, | ||
key: string | symbol, | ||
descriptor: TypedPropertyDescriptor<any>, | ||
) => { | ||
Reflect.defineMetadata(VERSION_METADATA, version, descriptor.value); | ||
return descriptor; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './request-method.enum'; | ||
export * from './http-status.enum'; | ||
export * from './shutdown-signal.enum'; | ||
export * from './version-type.enum'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* @publicApi | ||
*/ | ||
export enum VersioningType { | ||
URI, | ||
HEADER, | ||
MEDIA_TYPE, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { VersioningType } from '../enums/version-type.enum'; | ||
|
||
/** | ||
* Indicates that this will work for any version passed in the request, or no version. | ||
* | ||
* @publicApi | ||
*/ | ||
export const VERSION_NEUTRAL = Symbol('VERSION_NEUTRAL'); | ||
|
||
export type VersionValue = string | string[] | typeof VERSION_NEUTRAL; | ||
|
||
/** | ||
* @publicApi | ||
*/ | ||
export interface VersionOptions { | ||
/** | ||
* Specifies an optional API Version. When configured, methods | ||
* withing the controller will only be routed if the request version | ||
* matches the specified value. | ||
* | ||
* @see [Versioning](https://docs.nestjs.com/techniques/versioning) | ||
*/ | ||
version?: VersionValue; | ||
} | ||
|
||
export interface HeaderVersioningOptions { | ||
type: VersioningType.HEADER; | ||
/** | ||
* The name of the Request Header that contains the version. | ||
*/ | ||
header: string; | ||
} | ||
|
||
export interface UriVersioningOptions { | ||
type: VersioningType.URI; | ||
/** | ||
* Optional prefix that will prepend the version within the URI. | ||
* | ||
* Defaults to `v`. | ||
* | ||
* Ex. Assuming a version of `1`, for `/api/v1/route`, `v` is the prefix. | ||
*/ | ||
prefix?: string | false; | ||
} | ||
|
||
export interface MediaTypeVersioningOptions { | ||
type: VersioningType.MEDIA_TYPE; | ||
/** | ||
* The key within the Media Type Header to determine the version from. | ||
* | ||
* Ex. For `application/json;v=1`, the key is `v=`. | ||
*/ | ||
key: string; | ||
} | ||
|
||
/** | ||
* @publicApi | ||
*/ | ||
export type VersioningOptions = | ||
| HeaderVersioningOptions | ||
| UriVersioningOptions | ||
| MediaTypeVersioningOptions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { expect } from 'chai'; | ||
import { VERSION_METADATA } from '../../constants'; | ||
import { Version } from '../../decorators/core/version.decorator'; | ||
|
||
describe('@Version', () => { | ||
const version = '1'; | ||
|
||
class Test { | ||
@Version(version) | ||
public static test() {} | ||
} | ||
|
||
it('should enhance method with expected version string', () => { | ||
const metadata = Reflect.getMetadata(VERSION_METADATA, Test.test); | ||
expect(metadata).to.be.eql(version); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.