Skip to content

Commit 681c65d

Browse files
committed
Configure fastify route injection ourselves
This is prep for our own expansion
1 parent a9ba9be commit 681c65d

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

src/core/http/http.adapter.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
import compression from '@fastify/compress';
22
import cookieParser from '@fastify/cookie';
33
import cors from '@fastify/cors';
4+
import {
5+
VERSION_NEUTRAL,
6+
type VersionValue,
7+
} from '@nestjs/common/interfaces/version-options.interface.js';
48
// eslint-disable-next-line @seedcompany/no-restricted-imports
59
import { HttpAdapterHost as HttpAdapterHostImpl } from '@nestjs/core';
610
import {
711
FastifyAdapter,
812
NestFastifyApplication,
913
} from '@nestjs/platform-fastify';
14+
import type { FastifyInstance, HTTPMethods, RouteOptions } from 'fastify';
1015
import * as zlib from 'node:zlib';
1116
import { ConfigService } from '~/core/config/config.service';
17+
import { RouteConfig, RouteConstraints } from './decorators';
1218
import type { CookieOptions, CorsOptions, IResponse } from './types';
1319

1420
export type NestHttpApplication = NestFastifyApplication & {
@@ -20,7 +26,18 @@ export type NestHttpApplication = NestFastifyApplication & {
2026

2127
export class HttpAdapterHost extends HttpAdapterHostImpl<HttpAdapter> {}
2228

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 {
2441
async configure(app: NestFastifyApplication, config: ConfigService) {
2542
await app.register(compression, {
2643
brotliOptions: {
@@ -42,6 +59,36 @@ export class HttpAdapter extends FastifyAdapter {
4259
config.applyTimeouts(app.getHttpServer(), config.httpTimeouts);
4360
}
4461

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+
4592
setCookie(
4693
response: IResponse,
4794
name: string,

0 commit comments

Comments
 (0)