-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fastify): add fastify route constraints
- Loading branch information
Showing
8 changed files
with
93 additions
and
748 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1 +1,3 @@ | ||
export const FASTIFY_ROUTE_CONFIG_METADATA = '__fastify_route_config__'; | ||
export const FASTIFY_ROUTE_CONSTRAINTS_METADATA = | ||
'__fastify_route_constraints__'; |
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 +1,2 @@ | ||
export * from './route-config.decorator'; | ||
export * from './route-constraints.decorator'; |
11 changes: 11 additions & 0 deletions
11
packages/platform-fastify/decorators/route-constraints.decorator.ts
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,11 @@ | ||
import { SetMetadata } from '@nestjs/common'; | ||
import { FASTIFY_ROUTE_CONSTRAINTS_METADATA } from '../constants'; | ||
import { RouteShorthandOptions } from 'fastify'; | ||
|
||
/** | ||
* @publicApi | ||
* | ||
* @param config See {@link https://fastify.dev/docs/latest/Reference/Routes/#constraints} | ||
*/ | ||
export const RouteConstraints = (config: RouteShorthandOptions['config']) => | ||
SetMetadata(FASTIFY_ROUTE_CONSTRAINTS_METADATA, config); |
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
39 changes: 39 additions & 0 deletions
39
packages/platform-fastify/test/decorators/router-constraints.decorator.spec.ts
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,39 @@ | ||
import { expect } from 'chai'; | ||
import { FASTIFY_ROUTE_CONSTRAINTS_METADATA } from '../../constants'; | ||
import { RouteConstraints } from '../../decorators/route-constraints.decorator'; | ||
|
||
describe('@RouteConstraints', () => { | ||
describe('has version constraints', () => { | ||
const routeConstraints = { version: '1.2.x' }; | ||
class TestVersionConstraints { | ||
config; | ||
@RouteConstraints(routeConstraints) | ||
public static test() {} | ||
} | ||
|
||
it('should have a version constraint', () => { | ||
const path = Reflect.getMetadata( | ||
FASTIFY_ROUTE_CONSTRAINTS_METADATA, | ||
TestVersionConstraints.test, | ||
); | ||
expect(path).to.be.eql(routeConstraints); | ||
}); | ||
}); | ||
|
||
describe('has custom constraints', () => { | ||
const customRouteConstraints = { something: 'foo' }; | ||
class TestConstraints { | ||
config; | ||
@RouteConstraints(customRouteConstraints) | ||
public static test() {} | ||
} | ||
|
||
it('should set a custom constraint', () => { | ||
const path = Reflect.getMetadata( | ||
FASTIFY_ROUTE_CONSTRAINTS_METADATA, | ||
TestConstraints.test, | ||
); | ||
expect(path).to.be.eql(customRouteConstraints); | ||
}); | ||
}); | ||
}); |