Skip to content

fix: use named wildcards in middleware route in nest v11 #210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions src/middleware.helper.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import type { MikroOrmMiddlewareModuleOptions, NestMiddlewareConsumer } from './typings';
import type { MiddlewareConsumer } from '@nestjs/common';
import { type MiddlewareConsumer, HttpStatus } from '@nestjs/common';

export function forRoutesPath(options: MikroOrmMiddlewareModuleOptions, consumer: MiddlewareConsumer) {
const isNestMiddleware = (consumer: MiddlewareConsumer): consumer is NestMiddlewareConsumer => {
return typeof (consumer as any).httpAdapter === 'object';
};
if (options.forRoutesPath) {
return options.forRoutesPath;
}

// detect nest v11 based on a newly added enum value
if (HttpStatus.MULTI_STATUS) {
return '{*all}';
}

const isFastify = (consumer: MiddlewareConsumer) => {
if (typeof (consumer as any).httpAdapter !== 'object') {
return false;
}

const usingFastify = (consumer: NestMiddlewareConsumer) => {
return consumer.httpAdapter.constructor.name.toLowerCase().startsWith('fastify');
return (consumer as NestMiddlewareConsumer).httpAdapter.constructor.name.toLowerCase().startsWith('fastify');
};

return options.forRoutesPath ??
(isNestMiddleware(consumer) && usingFastify(consumer) ? '(.*)' : '*');
return isFastify(consumer) ? '(.*)' : '*';
}