Skip to content

Commit 1e412cb

Browse files
Merge pull request #14802 from nestjs/feat/options-for-legacy-route-converter
feat(core): add options to the legacy route converter
2 parents 3703a03 + cb66a47 commit 1e412cb

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

packages/core/router/legacy-route-converter.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,50 @@ export class LegacyRouteConverter {
1111
* path-to-regexp used by Express>=v5 and @fastify/middie>=v9 no longer support unnamed wildcards.
1212
* This method attempts to convert the old syntax to the new one, and logs an error if it fails.
1313
* @param route The route to convert.
14+
* @param options Options object.
1415
* @returns The converted route, or the original route if it cannot be converted.
1516
*/
16-
static tryConvert(route: string): string {
17+
static tryConvert(
18+
route: string,
19+
options?: {
20+
logs?: boolean;
21+
},
22+
): string {
1723
// Normalize path to eliminate additional if statements.
1824
const routeWithLeadingSlash = route.startsWith('/') ? route : `/${route}`;
1925
const normalizedRoute = route.endsWith('/')
2026
? routeWithLeadingSlash
2127
: `${routeWithLeadingSlash}/`;
2228

29+
const loggingEnabled = options?.logs ?? true;
30+
const printWarning = loggingEnabled
31+
? this.printWarning.bind(this)
32+
: () => {};
33+
2334
if (normalizedRoute.endsWith('/(.*)/')) {
2435
// Skip printing warning for the "all" wildcard.
2536
if (normalizedRoute !== '/(.*)/') {
26-
this.printWarning(route);
37+
printWarning(route);
2738
}
2839
return route.replace('(.*)', '{*path}');
2940
}
3041

3142
if (normalizedRoute.endsWith('/*/')) {
3243
// Skip printing warning for the "all" wildcard.
3344
if (normalizedRoute !== '/*/') {
34-
this.printWarning(route);
45+
printWarning(route);
3546
}
3647
return route.replace('*', '{*path}');
3748
}
3849

3950
if (normalizedRoute.endsWith('/+/')) {
40-
this.printWarning(route);
51+
printWarning(route);
4152
return route.replace('/+', '/*path');
4253
}
4354

4455
// When route includes any wildcard segments in the middle.
4556
if (normalizedRoute.includes('/*/')) {
46-
this.printWarning(route);
57+
printWarning(route);
4758
// Replace each /*/ segment with a named parameter using different name for each segment.
4859
return route.replaceAll('/*/', (match, offset) => {
4960
return `/*path${offset}/`;

0 commit comments

Comments
 (0)