@@ -11,39 +11,50 @@ export class LegacyRouteConverter {
11
11
* path-to-regexp used by Express>=v5 and @fastify/middie>=v9 no longer support unnamed wildcards.
12
12
* This method attempts to convert the old syntax to the new one, and logs an error if it fails.
13
13
* @param route The route to convert.
14
+ * @param options Options object.
14
15
* @returns The converted route, or the original route if it cannot be converted.
15
16
*/
16
- static tryConvert ( route : string ) : string {
17
+ static tryConvert (
18
+ route : string ,
19
+ options ?: {
20
+ logs ?: boolean ;
21
+ } ,
22
+ ) : string {
17
23
// Normalize path to eliminate additional if statements.
18
24
const routeWithLeadingSlash = route . startsWith ( '/' ) ? route : `/${ route } ` ;
19
25
const normalizedRoute = route . endsWith ( '/' )
20
26
? routeWithLeadingSlash
21
27
: `${ routeWithLeadingSlash } /` ;
22
28
29
+ const loggingEnabled = options ?. logs ?? true ;
30
+ const printWarning = loggingEnabled
31
+ ? this . printWarning . bind ( this )
32
+ : ( ) => { } ;
33
+
23
34
if ( normalizedRoute . endsWith ( '/(.*)/' ) ) {
24
35
// Skip printing warning for the "all" wildcard.
25
36
if ( normalizedRoute !== '/(.*)/' ) {
26
- this . printWarning ( route ) ;
37
+ printWarning ( route ) ;
27
38
}
28
39
return route . replace ( '(.*)' , '{*path}' ) ;
29
40
}
30
41
31
42
if ( normalizedRoute . endsWith ( '/*/' ) ) {
32
43
// Skip printing warning for the "all" wildcard.
33
44
if ( normalizedRoute !== '/*/' ) {
34
- this . printWarning ( route ) ;
45
+ printWarning ( route ) ;
35
46
}
36
47
return route . replace ( '*' , '{*path}' ) ;
37
48
}
38
49
39
50
if ( normalizedRoute . endsWith ( '/+/' ) ) {
40
- this . printWarning ( route ) ;
51
+ printWarning ( route ) ;
41
52
return route . replace ( '/+' , '/*path' ) ;
42
53
}
43
54
44
55
// When route includes any wildcard segments in the middle.
45
56
if ( normalizedRoute . includes ( '/*/' ) ) {
46
- this . printWarning ( route ) ;
57
+ printWarning ( route ) ;
47
58
// Replace each /*/ segment with a named parameter using different name for each segment.
48
59
return route . replaceAll ( '/*/' , ( match , offset ) => {
49
60
return `/*path${ offset } /` ;
0 commit comments