@@ -12,65 +12,45 @@ type WrapResult = {
1212} ;
1313
1414/**
15- * Wraps global middleware arrays (requestMiddleware, functionMiddleware) in createStart() files .
15+ * Core function that wraps middleware arrays matching the given regex .
1616 */
17- export function wrapGlobalMiddleware ( code : string , id : string , debug : boolean ) : WrapResult {
17+ function wrapMiddlewareArrays ( code : string , id : string , debug : boolean , regex : RegExp ) : WrapResult {
1818 const skipped : string [ ] = [ ] ;
1919 let didWrap = false ;
2020
21- const transformed = code . replace (
22- / ( r e q u e s t M i d d l e w a r e | f u n c t i o n M i d d l e w a r e ) \s * : \s * \[ ( [ ^ \] ] * ) \] / g,
23- ( match : string , key : string , contents : string ) => {
24- const objContents = arrayToObjectShorthand ( contents ) ;
25- if ( objContents ) {
26- didWrap = true ;
27- if ( debug ) {
28- // eslint-disable-next-line no-console
29- console . log ( `[Sentry] Auto-wrapping ${ key } in ${ id } ` ) ;
30- }
31- return `${ key } : wrapMiddlewaresWithSentry(${ objContents } )` ;
32- }
33- // Track middlewares that couldn't be auto-wrapped
34- // Skip if we matched whitespace only
35- if ( contents . trim ( ) ) {
36- skipped . push ( key ) ;
21+ const transformed = code . replace ( regex , ( match : string , key : string , contents : string ) => {
22+ const objContents = arrayToObjectShorthand ( contents ) ;
23+ if ( objContents ) {
24+ didWrap = true ;
25+ if ( debug ) {
26+ // eslint-disable-next-line no-console
27+ console . log ( `[Sentry] Auto-wrapping ${ key } in ${ id } ` ) ;
3728 }
38- return match ;
39- } ,
40- ) ;
29+ return `${ key } : wrapMiddlewaresWithSentry(${ objContents } )` ;
30+ }
31+ // Track middlewares that couldn't be auto-wrapped
32+ // Skip if we matched whitespace only
33+ if ( contents . trim ( ) ) {
34+ skipped . push ( key ) ;
35+ }
36+ return match ;
37+ } ) ;
4138
4239 return { code : transformed , didWrap, skipped } ;
4340}
4441
42+ /**
43+ * Wraps global middleware arrays (requestMiddleware, functionMiddleware) in createStart() files.
44+ */
45+ export function wrapGlobalMiddleware ( code : string , id : string , debug : boolean ) : WrapResult {
46+ return wrapMiddlewareArrays ( code , id , debug , / ( r e q u e s t M i d d l e w a r e | f u n c t i o n M i d d l e w a r e ) \s * : \s * \[ ( [ ^ \] ] * ) \] / g) ;
47+ }
48+
4549/**
4650 * Wraps route middleware arrays in createFileRoute() files.
4751 */
4852export function wrapRouteMiddleware ( code : string , id : string , debug : boolean ) : WrapResult {
49- const skipped : string [ ] = [ ] ;
50- let didWrap = false ;
51-
52- const transformed = code . replace (
53- / ( \s + ) ( m i d d l e w a r e ) \s * : \s * \[ ( [ ^ \] ] * ) \] / g,
54- ( match : string , whitespace : string , key : string , contents : string ) => {
55- const objContents = arrayToObjectShorthand ( contents ) ;
56- if ( objContents ) {
57- didWrap = true ;
58- if ( debug ) {
59- // eslint-disable-next-line no-console
60- console . log ( `[Sentry] Auto-wrapping route ${ key } in ${ id } ` ) ;
61- }
62- return `${ whitespace } ${ key } : wrapMiddlewaresWithSentry(${ objContents } )` ;
63- }
64- // Track middlewares that couldn't be auto-wrapped
65- // Skip if we matched whitespace only
66- if ( contents . trim ( ) ) {
67- skipped . push ( `route ${ key } ` ) ;
68- }
69- return match ;
70- } ,
71- ) ;
72-
73- return { code : transformed , didWrap, skipped } ;
53+ return wrapMiddlewareArrays ( code , id , debug , / ( m i d d l e w a r e ) \s * : \s * \[ ( [ ^ \] ] * ) \] / g) ;
7454}
7555
7656/**
0 commit comments