File tree Expand file tree Collapse file tree 5 files changed +45
-2
lines changed
packages/platform-fastify Expand file tree Collapse file tree 5 files changed +45
-2
lines changed Original file line number Diff line number Diff line change @@ -53,6 +53,7 @@ import { parse as querystringParse } from 'fast-querystring';
53
53
import {
54
54
FASTIFY_ROUTE_CONFIG_METADATA ,
55
55
FASTIFY_ROUTE_CONSTRAINTS_METADATA ,
56
+ FASTIFY_ROUTE_SCHEMA_METADATA ,
56
57
} from '../constants' ;
57
58
import { NestFastifyBodyParserOptions } from '../interfaces' ;
58
59
import {
@@ -752,17 +753,22 @@ export class FastifyAdapter<
752
753
handlerRef ,
753
754
) ;
754
755
756
+ const routeSchema = Reflect . getMetadata (
757
+ FASTIFY_ROUTE_SCHEMA_METADATA ,
758
+ handlerRef ,
759
+ ) ;
760
+
755
761
const hasConfig = ! isUndefined ( routeConfig ) ;
756
762
const hasConstraints = ! isUndefined ( routeConstraints ) ;
757
-
763
+ const hasSchema = ! isUndefined ( routeSchema ) ;
758
764
const routeToInject : RouteOptions < TServer , TRawRequest , TRawResponse > &
759
765
RouteShorthandOptions = {
760
766
method : routerMethodKey ,
761
767
url : args [ 0 ] ,
762
768
handler : handlerRef ,
763
769
} ;
764
770
765
- if ( isVersioned || hasConstraints || hasConfig ) {
771
+ if ( isVersioned || hasConstraints || hasConfig || hasSchema ) {
766
772
const isPathAndRouteTuple = args . length === 2 ;
767
773
if ( isPathAndRouteTuple ) {
768
774
const constraints = {
@@ -779,6 +785,9 @@ export class FastifyAdapter<
779
785
...routeConfig ,
780
786
} ,
781
787
} ) ,
788
+ ...( hasSchema && {
789
+ schema : routeSchema ,
790
+ } ) ,
782
791
} ;
783
792
784
793
const routeToInjectWithOptions = { ...routeToInject , ...options } ;
Original file line number Diff line number Diff line change 1
1
export const FASTIFY_ROUTE_CONFIG_METADATA = '__fastify_route_config__' ;
2
2
export const FASTIFY_ROUTE_CONSTRAINTS_METADATA =
3
3
'__fastify_route_constraints__' ;
4
+ export const FASTIFY_ROUTE_SCHEMA_METADATA = '__fastify_route_schema__' ;
Original file line number Diff line number Diff line change 1
1
export * from './route-config.decorator' ;
2
2
export * from './route-constraints.decorator' ;
3
+ export * from './route-schema.decorator' ;
Original file line number Diff line number Diff line change
1
+ import { SetMetadata } from '@nestjs/common' ;
2
+ import { FASTIFY_ROUTE_SCHEMA_METADATA } from '../constants' ;
3
+ import { FastifySchema } from 'fastify' ;
4
+
5
+ /**
6
+ * @publicApi
7
+ * Allows setting the schema for the route. Schema is an object that can contain the following properties:
8
+ * - body: JsonSchema
9
+ * - querystring or query: JsonSchema
10
+ * - params: JsonSchema
11
+ * - response: Record<HttpStatusCode, JsonSchema>
12
+ * @param schema See {@link https://fastify.dev/docs/latest/Reference/Routes/#routes-options}
13
+ */
14
+ export const RouteSchema = ( schema : FastifySchema ) =>
15
+ SetMetadata ( FASTIFY_ROUTE_SCHEMA_METADATA , schema ) ;
Original file line number Diff line number Diff line change
1
+ import { expect } from 'chai' ;
2
+ import { FASTIFY_ROUTE_SCHEMA_METADATA } from '../../constants' ;
3
+ import { RouteSchema } from '../../decorators/route-schema.decorator' ;
4
+
5
+ describe ( '@RouteSchema' , ( ) => {
6
+ const routeSchema = { body : 'testValue' } ;
7
+ class Test {
8
+ config ;
9
+ @RouteSchema ( routeSchema )
10
+ public static test ( ) { }
11
+ }
12
+
13
+ it ( 'should enhance method with expected fastify route schema' , ( ) => {
14
+ const path = Reflect . getMetadata ( FASTIFY_ROUTE_SCHEMA_METADATA , Test . test ) ;
15
+ expect ( path ) . to . be . eql ( routeSchema ) ;
16
+ } ) ;
17
+ } ) ;
You can’t perform that action at this time.
0 commit comments