Skip to content

Commit b965ee9

Browse files
authored
fix: use named wildcards in middleware route in nest v11 (#210)
Closes #205
1 parent 98e228e commit b965ee9

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/middleware.helper.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
import type { MikroOrmMiddlewareModuleOptions, NestMiddlewareConsumer } from './typings';
2-
import type { MiddlewareConsumer } from '@nestjs/common';
2+
import { type MiddlewareConsumer, HttpStatus } from '@nestjs/common';
33

44
export function forRoutesPath(options: MikroOrmMiddlewareModuleOptions, consumer: MiddlewareConsumer) {
5-
const isNestMiddleware = (consumer: MiddlewareConsumer): consumer is NestMiddlewareConsumer => {
6-
return typeof (consumer as any).httpAdapter === 'object';
7-
};
5+
if (options.forRoutesPath) {
6+
return options.forRoutesPath;
7+
}
8+
9+
// detect nest v11 based on a newly added enum value
10+
if (HttpStatus.MULTI_STATUS) {
11+
return '{*all}';
12+
}
13+
14+
const isFastify = (consumer: MiddlewareConsumer) => {
15+
if (typeof (consumer as any).httpAdapter !== 'object') {
16+
return false;
17+
}
818

9-
const usingFastify = (consumer: NestMiddlewareConsumer) => {
10-
return consumer.httpAdapter.constructor.name.toLowerCase().startsWith('fastify');
19+
return (consumer as NestMiddlewareConsumer).httpAdapter.constructor.name.toLowerCase().startsWith('fastify');
1120
};
1221

13-
return options.forRoutesPath ??
14-
(isNestMiddleware(consumer) && usingFastify(consumer) ? '(.*)' : '*');
22+
return isFastify(consumer) ? '(.*)' : '*';
1523
}

0 commit comments

Comments
 (0)