1
1
import compression from '@fastify/compress' ;
2
2
import cookieParser from '@fastify/cookie' ;
3
3
import cors from '@fastify/cors' ;
4
+ import {
5
+ VERSION_NEUTRAL ,
6
+ type VersionValue ,
7
+ } from '@nestjs/common/interfaces/version-options.interface.js' ;
4
8
// eslint-disable-next-line @seedcompany/no-restricted-imports
5
9
import { HttpAdapterHost as HttpAdapterHostImpl } from '@nestjs/core' ;
6
10
import {
7
11
FastifyAdapter ,
8
12
NestFastifyApplication ,
9
13
} from '@nestjs/platform-fastify' ;
14
+ import type { FastifyInstance , HTTPMethods , RouteOptions } from 'fastify' ;
10
15
import * as zlib from 'node:zlib' ;
11
16
import { ConfigService } from '~/core/config/config.service' ;
17
+ import { RouteConfig , RouteConstraints } from './decorators' ;
12
18
import type { CookieOptions , CorsOptions , IResponse } from './types' ;
13
19
14
20
export type NestHttpApplication = NestFastifyApplication & {
@@ -20,7 +26,18 @@ export type NestHttpApplication = NestFastifyApplication & {
20
26
21
27
export class HttpAdapterHost extends HttpAdapterHostImpl < HttpAdapter > { }
22
28
23
- export class HttpAdapter extends FastifyAdapter {
29
+ // @ts -expect-error Convert private methods to protected
30
+ class PatchedFastifyAdapter extends FastifyAdapter {
31
+ protected injectRouteOptions (
32
+ routerMethodKey : Uppercase < HTTPMethods > ,
33
+ ...args : any [ ]
34
+ ) : FastifyInstance {
35
+ // @ts -expect-error work around being marked as private
36
+ return super . injectRouteOptions ( routerMethodKey , ...args ) ;
37
+ }
38
+ }
39
+
40
+ export class HttpAdapter extends PatchedFastifyAdapter {
24
41
async configure ( app : NestFastifyApplication , config : ConfigService ) {
25
42
await app . register ( compression , {
26
43
brotliOptions : {
@@ -42,6 +59,36 @@ export class HttpAdapter extends FastifyAdapter {
42
59
config . applyTimeouts ( app . getHttpServer ( ) , config . httpTimeouts ) ;
43
60
}
44
61
62
+ protected injectRouteOptions (
63
+ method : Uppercase < HTTPMethods > ,
64
+ urlOrHandler : string | RouteOptions [ 'handler' ] ,
65
+ maybeHandler ?: RouteOptions [ 'handler' ] ,
66
+ ) {
67
+ // I don't know why NestJS allows url/path parameter to be omitted.
68
+ const url = typeof urlOrHandler === 'function' ? '' : urlOrHandler ;
69
+ const handler =
70
+ typeof urlOrHandler === 'function' ? urlOrHandler : maybeHandler ! ;
71
+
72
+ const config = RouteConfig . get ( handler ) ?? { } ;
73
+ const constraints = RouteConstraints . get ( handler ) ?? { } ;
74
+
75
+ let version : VersionValue | undefined = ( handler as any ) . version ;
76
+ version = version === VERSION_NEUTRAL ? undefined : version ;
77
+ if ( version ) {
78
+ // @ts -expect-error this is what upstream does
79
+ constraints . version = version ;
80
+ }
81
+
82
+ const route : RouteOptions = {
83
+ method,
84
+ url,
85
+ handler,
86
+ ...( Object . keys ( constraints ) . length > 0 ? { constraints } : { } ) ,
87
+ ...( Object . keys ( config ) . length > 0 ? { config } : { } ) ,
88
+ } ;
89
+ return this . instance . route ( route ) ;
90
+ }
91
+
45
92
setCookie (
46
93
response : IResponse ,
47
94
name : string ,
0 commit comments